[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: Adding a node to an existing node set

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

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.