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

Subject: RE: [xsl] Adding a node to an existing node set
From: "Michael Kay" <michael.h.kay@xxxxxxxxxxxx>
Date: Tue, 23 Jul 2002 08:46:29 +0100
> 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>
> 
> 
> 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>

The first returns a node-set: think of it as a list of pointers to
elements in the source tree, specifically the <child> element nodes that
are children of the <parent> element at the outermost level of the
document.

The second constructs a new tree, containing copies of the nodes
selected by this expression. These <child> nodes will all be children of
the root node of the new tree. The <parent> element is not copied.
> 
> Amont other things, the attributes seem to have
> disappeared.

In the first case, nothing has disappeared, the original tree is
unchanged. 

In the second case, the original tree is unchanged, the new tree
contains copies of the <child> elements and their attributes and
descendants, in fact it contains everything except the <parent> elemnts
and its attributes.

> Can someone explain the difference between the two?
> 
> 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"
> 
No, I think you must have written something different. Perhaps you wrote
parent/@child. You will get this error if you try to copy an attribute
node when there is no element for it to be attached to. That's not the
case with this example.

Michael Kay
Software AG
home: Michael.H.Kay@xxxxxxxxxxxx
work: Michael.Kay@xxxxxxxxxxxxxx 


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


Current Thread