Re: replace attribute of specific elements

M

Marrow

Hi Markus,

Try something like...

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:changes="urn:my-changes">
<!-- just used to store the node-set -->
<changes:nodeset>
<item name="change" attr="text" value="new_value"/>
<item name="change2" attr="label" value="new_value2"/>
</changes:nodeset>
<!-- store changes in a global variable -->
<xsl:variable name="changes" select="document('')/*/changes:nodeset/*"/>

<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>

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

<xsl:template match="@*">
<!-- get the change node - if present -->
<xsl:variable name="this-change" select="$changes[@name =
current()/parent::*/@name and @attr = name(current())]"/>
<!-- see if this attribute needs changing -->
<xsl:choose>
<xsl:when test="$this-change">
<xsl:attribute name="{name()}" namespace="{namespace-uri()}">
<xsl:value-of select="$this-change/@value"/>
</xsl:attribute>
</xsl:when>
<xsl:eek:therwise>
<xsl:copy/>
</xsl:eek:therwise>
</xsl:choose>
</xsl:template>

<xsl:template match="text() | comment() | processing-instruction()">
<xsl:copy/>
</xsl:template>
</xsl:stylesheet>

Hope this helps
Marrow
http://www.marrowsoft.com - home of Xselerator (XSLT IDE and debugger)
http://www.topxml.com/Xselerator
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top