RE: Adding a node to an existing node set
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
PURCHASE STYLUS STUDIO ONLINE TODAY!
Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!
Download The World's Best XML IDE!
Accelerate XML development with our award-winning XML IDE - Download a free trial today!
Subscribe in XML format