Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

copy all attributes of an attribute except one

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
hi. I have the following problem with xslt when doing xml to xml transformations. In some cases I want to copy an element and it's attributes exept one -which should get a fixed value- to the output tree. What I do to achieve this, you can see in this example code snippet:

<xsl:template name=&quot;tname&quot;>
<element_x att1=&quot;{@att1}&quot; att2=&quot;{@att2}&quot; att3=&quot;fixed&quot;/>
</xsl:template>

It works fine. But my question is, whether it is possible somehow, to say in xsl: copy the element and all atrributes to the output, except the attribute, of name att3 and give a fixed value to it. (This is because not all of my &quot;element_x&quot; elements have the same attributes, and because everytime i add a new feature and therefore add a new attribute to element_x i have to search and replace in all my xsl which works with element_x)

(sorry for my bad english and thanks in advance)

 
<xsl:for-each select=&quot;@*&quot;>
<xsl:copy/>
</xsl:for-each>

this would copy all the attributes from the current node. you might be able to use

<xsl:for-each select=&quot;@*[local-name()!=@att3]>

but i'm guessing here.
 
Thanks for Your answer,

This worked:

<xsl:template match=&quot;element_x&quot;>
<element_x>
<xsl:for-each select=&quot;@*&quot;>
<xsl:choose>
<xsl:when test=&quot;not(local-name()='att3')&quot;>
<xsl:copy/>
</xsl:when>
<xsl:eek:therwise>
<xsl:attribute name=&quot;{name()}&quot;>fixed</xsl:attribute>
</xsl:eek:therwise>
</xsl:choose>
</xsl:for-each>
</element_x>
<xsl:apply-templates/>
</xsl:template>

I only hope that i won't receive those &quot;cannot set attribute after child has been added&quot;-errors later on...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top