PDA

View Full Version : Copying and Sorting at the same time


XML_Neophyte
02-09-2005, 04:32 AM
Hi
I am new to XSL/XML and I just want to find out if it is possible to copy a node (and all child nodes and leaves) while at the same time sorting the grandchildren nodes from a specific offspring node i.e. suppose the following is the XML structure

...
<parentnode>
...
<childnode1>
</childnode1>
<childnode2>
<grandkidnode2>
</grandkidnode2>
<grandkidnode3>
</grandkidnode3>
<grandkidnode1>
</grandkidnode1>
</childnode2>
<childnode3>
</childnode3>
...
</parentnode>
...

The question then is, while I am copying the 'parentnode' (using XSLT) to an output file (XML) , can I copy and sort grandkidnodes in the correct order without breaking the order in which the 'childnodes' appear AND allow for possibility that at a future date other childrennodes can be addded to the original XML which would also need to be copied using the same XSLT file.

Thank you for your help.
-XML_Neophyte

XML_Neophyte
03-10-2005, 12:51 AM
Hello all,
Thanks to all who looked at the problem, but I think I have found the solution, which I am posting here. In general, the xsl code should like the following

_________________________________________________________________
<xsl:template match="@*|node()|comment()">
<xsl:copy>
<xsl:apply-templates select="@*|node()|comment()"/>
</xsl:copy>
<xsl:template>



<xsl:template match="//child">
<xsl:element name="child">
<xsl:copy-of select="@*"/>
<xsl:copy-of select="node()[name() != 'grandkid']"/>
<xsl:apply-templates select="//child/grandkid" mode="sortgrandkid">
<xsl:sort .../>
</xsl:apply-templates>
</xsl:element>
</xsl:template>



<xsl:template match="//child/grandkid" mode="sortgrandkid">
<xsl:element name="grandkid">
<xsl:copy-of select="@*"/>
<xsl:copy-of select="node()"/>
</xsl:element>
</xsl:template>
_________________________________________________________________

Hope this helps.
-xml_neophyte