RE: [xsl] XSLT - update attribute with new value

Subject: RE: [xsl] XSLT - update attribute with new value
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Tue, 9 Nov 2004 14:16:40 -0000
> Hi David and Michael,

It's best not to address questions to specific people. Sometimes in an
initial response I try to point people in the right direction, but that
doesn't necessarily mean I'm prepared to hold their hands all the way down
the road - whereas someone else might well be happy to do so.
> 
> Now I have one final question. I want to add an attribute/value to the
> xml tree only if that attribute does not exist anywhere in 
> the document
> source tree.
> 
> Is there a way to do that in XSLT?

You can test whether the attribute is absent from the tree with 

<xsl:if test="not(//@x)">

But remember that your stylesheet code must be structured to mirror the
result tree structure, not the source tree structure. So you have to put
this code in the template rule for the element that is to carry the new
attribute. If this template rule is evaluated repeatedly, it will be more
efficient to put the condition

<xsl:variable name="x-is-absent" select="not(//@x)"/>

in a global variable.

Michael Kay
http://www.saxonica.com/

Current Thread