Re: Creating elements in an arbitrary position in the
Thank you very much for your answer Ken. Your code will not solve my problem, because in the template that matches A I don't know the attribute names and values for attrC, attrD, value1, and value2.
Yes, please forgive me for missing that copying of attributes ... I thought you were injecting them arbitrarily and did not read ahead.
I will find that information in the subtree of B, and B can be anywhere in the source document.
This can be done.
I tried this but it didn't work:
You are missing some basic understanding of tree building ... an element is made at the point in time you are constructing the result. You have to have created the result <A> element before creating the <new_element> ... and in your earlier post you wanted it created as the first child of <A>, not as the last child before <B>.
The example below shows how you can add the element and its attributes by copying the attributes from C and D.
If you want to suppress the C and the D then you'll have to add empty templates for them.
When writing XSLT don't give up so soon! It is an extremely powerful and expressive language to get the results you need.
I hope this helps.
. . . . . . . . . . . Ken
t:\ftemp>type cristobal.xml <A> ... <- unknown/variable amount of nodes <B> <C attrC="value1"/> <D attrD="value2"/> </B> </A>
t:\ftemp>xslt cristobal.xml cristobal.xsl
<?xml version="1.0" encoding="utf-8"?><A><new_element attrC="value1" attrD="value2"/>
... <- unknown/variable amount of nodes
<?xml version="1.0" encoding="US-ASCII"?>
<xsl:template match="A"> <xsl:copy> <xsl:copy-of select="@*"/> <new_element> <xsl:copy-of select="//B/C/@*"/> <xsl:copy-of select="//B/D/@*"/> </new_element> <xsl:apply-templates select="node()"/> </xsl:copy> </xsl:template>
<xsl:template match="@*|node()"><!--identity for all other nodes--> <xsl:copy> <xsl:apply-templates select="@*|node()"/> </xsl:copy> </xsl:template>
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