[xsl] Modifying select attribute values in XHTML

Subject: [xsl] Modifying select attribute values in XHTML
From: "Brook Ellingwood" <belling@xxxxxxx>
Date: Thu, 17 Aug 2006 10:20:49 -0700
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

Current Thread