Reputation: 15
I need the following xml to sort on the id field:
<top>
<repeat1>
<repeat2>
<contents>
<contentitem>
<id>9</id>
<moretags1/>
<moretags2/>
</contentitem>
</contents>
</repeat2>
</repeat1>
<repeat1>
<repeat2>
<contents>
<contentitem>
<id>6</id>
<moretags1/>
<moretags2/>
</contentitem>
</contents>
</repeat2>
</repeat1>
<repeat1>
<repeat2>
<contents>
<contentitem>
<id>3</id>
<moretags1/>
<moretags2/>
</contentitem>
</contents>
</repeat2>
</repeat1>
</top>
to so be sorted like this:
<top>
<repeat1>
<repeat2>
<contents>
<contentitem>
<id>3</id>
<moretags1/>
<moretags2/>
</contentitem>
</contents>
</repeat2>
</repeat1>
<repeat1>
<repeat2>
<contents>
<contentitem>
<id>6</id>
<moretags1/>
<moretags2/>
</contentitem>
</contents>
</repeat2>
</repeat1>
<repeat1>
<repeat2>
<contents>
<contentitem>
<id>9</id>
<moretags1/>
<moretags2/>
</contentitem>
</contents>
</repeat2>
</repeat1>
</top>
In other words, I want the contentitem grouping to remain intact but list out in the resulting xml in order by id. I'm trying to use the following xsl but not having any luck.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
<!-- -->
<xsl:strip-space elements="*"/>
<!-- -->
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<!-- -->
<xsl:template match="contents">
<xsl:copy>
<xsl:apply-templates select="contentitem">
<xsl:sort select="id" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 0
Views: 166
Reputation: 70598
It looks like you should be sorting the repeat1
elements, rather than the contents
elements here.
Try this XSLT instead:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes" method="xml"/>
<xsl:strip-space elements="*"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="top">
<xsl:copy>
<xsl:apply-templates select="repeat1">
<xsl:sort select="repeat2/contents/contentitem/id" data-type="number" order="ascending"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 3