Here is sample xml
<Data version="2.0">
<Group>
<Item>3</Item>
<Item>1</Item>
<Item>2</Item>
</Group>
<Group>
<Item>7</Item>
<Item>5</Item>
</Group>
</Data>
And for ordering nodes in Group by Item value I tried to use the following xsl:
<xsl:template match="/Data">
<xsl:apply-templates select="Group">
<xsl:sort select="Item" />
</xsl:apply-templates>
</xsl:template>
But get only values, even without sorting:
3
1
2
7
5
So the questions are: 1. why sorting not work 2. How to keep all nodes and keep structure of xml?
@Jim’s answer is basically correct.
However, applied to a slightly more realistic XML document, such as this:
the result produced is clearly not what you want (10 comes before 2 and 3):
Here is a correct solution (that is also slightly shorter):
when this transformation is applied on the same XML document (above), the wanted, correct result is produced:
Explanation: Use of the
data-typeattribute of<xsl:sort>to specify that the sort keys value should be treated as number, not as (the default) string.