Subject: Re: [xsl] Modifying select attribute values in XHTML From: "Mukul Gandhi" <gandhi.mukul@xxxxxxxxx> Date: Thu, 17 Aug 2006 23:29:46 +0530 |
Hi Brook, I think we can write something like this (this is just a modified identity stylesheet):
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:variable name="nonSecureHost" select="'http//www.example.com'" /> <xsl:variable name="imagePath" select="'http//images.example.com'" />
<xsl:template match="node() | @*"> <xsl:copy> <xsl:apply-templates select="node() | @*" /> </xsl:copy> </xsl:template>
<xsl:template match="@href"> <xsl:attribute name="href"> <xsl:value-of select="$nonSecureHost"/> <xsl:value-of select="."/> </xsl:attribute> </xsl:template>
<xsl:template match="@src"> <xsl:attribute name="src"> <xsl:value-of select="$imagePath"/> <xsl:value-of select="."/> </xsl:attribute> </xsl:template>
Hi,
I have XML that contains small sections of XHTML, within an element named 'xhtml.' I need to take the XHTML and prepend the value of variables into the attribute values for any 'src' or 'href' attributes. Other than this, the XHTML needs to be passed transparently. I quickly came up with a series of templates that works properly two levels down from the 'xhtml' element, but I need this to work no matter how deeply nested the decendent nodes are. I'd like to avoid recursion if at all possible, and it seems to me that I should be able to do this with a simple template match, but I keep not quite getting it.
Sample XML: <xhtml> <div style="width:570px;text-align:center;"> <img src="/path/to/img.jpg" width="570" height="100" border="0" alt="Alt text"/> <p style="margin:10px 0 0 0;" class="class">Display copy<br/> <a href="/link">Linked text</a><br/> <form> <select style="margin-top:0px;" size="1" name="selectMenu"> <option value="#" selected="selected">Choose an option</option> <option value="one.html">One</option> <option value="two.html">Two</option> </form> </p> </div> <br/> </xhtml>
Desired sample output:
<div style="width:570px;text-align:center;"> <img src="http//images.example.com/path/to/img.jpg" width="570" height="100" border="0" alt="Alt text"/> <p style="margin:10px 0 0 0;" class="class">Display copy<br/> <a href="http//www.example.com/link">Linked text</a><br/> <form> <select style="margin-top:0px;" size="1" name="selectMenu"> <option value="#" selected="selected">Choose an option</option> <option value="one.html">One</option> <option value="two.html">Two</option> </form> </p> </div> <br/>
XSL that I've been working with:
<xsl:template match="xhtml/*"> <xsl:copy> <xsl:copy-of select="@*[not(name()='src' or name()='href')]"/> <xsl:apply-templates select="*"/> </xsl:copy> </xsl:template>
<xsl:template match="a"> <xsl:copy> <xsl:copy-of select="@*[not(name()='src' or name()='href')]"/> <xsl:apply-templates select="@href"/> </xsl:copy> </xsl:template>
<xsl:template match="@href"> <xsl:attribute name="href"> <xsl:value-of select="$nonSecureHost"/> <xsl:value-of select="."/> </xsl:attribute> </xsl:template>
<xsl:template match="img"> <xsl:copy> <xsl:copy-of select="@*[not(name()='src' or name()='href')]"/> <xsl:apply-templates select="@src"/> </xsl:copy> </xsl:template>
<xsl:template match="@src"> <xsl:attribute name="src"> <xsl:value-of select="$imagePath"/> <xsl:value-of select="."/> </xsl:attribute> </xsl:template>
Thanks, -- Brook
-- Regards, Mukul Gandhi
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Modifying select attribute va, Brook Ellingwood | Thread | RE: [xsl] Modifying select attribut, Brook Ellingwood |
Re: [xsl] How do I build a nodeset , Elliotte Harold | Date | RE: [xsl] Modifying select attribut, Brook Ellingwood |
Month |