XML and XSLT: need it to sort only certain child nodes

Posted by MT on Stack Overflow See other posts from Stack Overflow or by MT
Published on 2010-05-21T00:22:29Z Indexed on 2010/05/21 0:30 UTC
Read the original article Hit count: 322

Filed under:
|
|

Hello, I need to have my XSLT stylesheet sort my XML file's child nodes, but only certain ones. Here's an example of what the XML is like:

<?xml version="1.0"?>
<xmltop>
<child1 num="1">
<data>12345</data>
</child1>

<child1 num="2">
<data>12345</data>
</child1>

<child2 num="3">
<data>12345</data>
</child2>

<child2 num="2">
<data>12345</data>
</child2>

<child2 num="1">
<data>12345</data>
</child2>
</xmltop>

And this is the XSL file I'm using:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/xmltop">
 <xsl:copy>
  <xsl:apply-templates>
   <xsl:sort select="@num"/>
  </xsl:apply-templates>
 </xsl:copy>
</xsl:template>
<xsl:template match="child2">
 <xsl:copy-of select="."/>
</xsl:template>

</xsl:stylesheet>

This creates problems for me because the nodes are stripped of their tags, and their contents remain, making my XML invalid. I'm not really an expert at XSL so pardon me if this is a dumb question.

The <child2>'s are sorted properly.

Thank you.

© Stack Overflow or respective owner

Related posts about Xml

Related posts about xslt