Subject: Change 1 attribute
Posted By: safin Post Date: 10/3/2005 11:01:59 AM
Hi,
how can i change only one attribute in XLST ?
Like if i have
<node a="test" b="something" c="hello"></node>

I want to change everything that has a hello, to bye, but mantain the other attributes untouched, so the output would be
 
<node a="test" b="something" c="bye"></node>

The problem is i don't know how many attributes will the node have, if it'll have only a,b and c, or a,b,c,d,e,f,g....
So i can't just "hardcode" the @a and @b attributes, and only change the c

thank you


Reply By: mhkay Reply Date: 10/3/2005 12:19:46 PM
Write a template rule for any attribute

<xsl:template match="*">
  <xsl:copy/>
</xsl:template>

and another to change the c attribute

<xsl:template match="c">
  <xsl:attribute name="c">bye</xsl:attribute>
</xsl:template>

and then apply-templates to all attributes:

<xsl:template match="node">
<xsl:copy>
  <xsl:apply-templates select="@*"/>
</xsl:copy>
</xsl:template>

Alternatively, you can exploit the fact that if you write the same attribute twice, the second one wins:

<xsl:copy-of select="@*"/>
<xsl:attribute name="c">bye</xsl:attribute>

But that doesn't work if you want to delete one attribute.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference

Go to topic 35343

Return to index page 461
Return to index page 460
Return to index page 459
Return to index page 458
Return to index page 457
Return to index page 456
Return to index page 455
Return to index page 454
Return to index page 453
Return to index page 452