RE: [xsl] Adding a node to an existing node set

Subject: RE: [xsl] Adding a node to an existing node set
From: Jarno.Elovirta@xxxxxxxxx
Date: Tue, 23 Jul 2002 07:53:49 +0300
Hi,

> Say the XPath expression ="/parent/child" returns all
> children, how can I add a new child element and have
> the resulting node set assigned to a variable.
> 
> I tried 
> <xsl:variable name="newTree">
>     <xsl:copy-of select="/parent/child"/>
>     <child age="1">foo</child>
> </xsl:variable>

That would work. Note, however, that here you're binding a RTF to the variable, and that is immutable.

> However there seems to be a difference between
> <xsl:variable name="newTree" select="/parent/child"/>
> 
> and 
> 
> <xsl:variable name="newTree">
>     <xsl:copy-of select="/parent/child"/>
> </xsl:variable>

Yes, the former binds a node-set to the variable, the latter a RTF. See http://www.w3.org/TR/xslt#section-Result-Tree-Fragments

> Amont other things, the attributes seem to have
> disappeared.

Which attributes? If all child elements have an age attribute, they will be preserved. Which XSLT processor are you using?

> Also when I try 
> <xsl:variable name="newTree">
>     <xsl:for-each select="/parent/child">
>        <xsl:copy-of select="."/>
>     </xsl:for-each>
> </xsl:variable>
> 
> Saxon complains saying : "Cannot write an attribute
> when there is no open start tag"

Err... it shouldn't, since the for-each selects a node-set of element nodes, and no attributes are copies directly. That would suggest that the problem is not in the code above, but rather somewhere else in you stylesheet.

Cheers,

Santtu

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Current Thread