RE: [xsl] Modifying select attribute values in XHTML

Subject: RE: [xsl] Modifying select attribute values in XHTML
From: "Brook Ellingwood" <belling@xxxxxxx>
Date: Thu, 17 Aug 2006 11:58:47 -0700
Hi Mukul,

I think this may be identical to what I had earlier when I began working
on this. I know I just tried it again at got the same results: it
outputs the value of the attributes as text node children of the
elements, and fails to go deeper than one level down the descendant
axis.

If I could get it to follow the tree down the descendant axis, the
formatting of the output should be fairly straight forward.

-- Brook

-----Original Message-----
From: Mukul Gandhi [mailto:gandhi.mukul@xxxxxxxxx]
Sent: Thursday, August 17, 2006 11:00 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] Modifying select attribute values in XHTML

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:output method="xml" indent="yes" />

 <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>

</xsl:stylesheet>


On 8/17/06, Brook Ellingwood <belling@xxxxxxx> wrote:
> 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

http://gandhimukul.tripod.com

Current Thread