[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

[xsl] Sorting nested steps


Subject: [xsl] Sorting nested steps
From: "Rick Quatro" <rick@xxxxxxxxxxxxxx>
Date: Wed, 31 Jul 2013 08:30:10 -0400

Hi,

I have a series of nested steps that I want to reverse sort at each level.
Here is what I am starting with:

<?xml version="1.0"?>
<steps>
    <step>A
        <step>(1)</step>
        <step>(2)
            <step>(a)</step>
            <step>(b)</step>
            <step>(c)</step>
        </step>
        <step>(3)</step>
    </step>
    <step>B</step>
    <step>C</step>
</steps>

This is what I want to get:

<?xml version="1.0"?>
<steps>
    <step>C</step>
    <step>B</step>
    <step>A
        <step>(3)</step>
        <step>(2)
            <step>(c)</step>
            <step>(b)</step>
            <step>(a)</step>
        <step>(1)</step>
        </step>
    </step>
</steps>

My steps could be nested up to five levels, so I tried to call a template
recursively, but it is not giving me exactly what I want. Here is my
stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:output method="xml" indent="yes"/>
    
    <xsl:template match="/steps">
        <xsl:call-template name="sort-steps"/>
    </xsl:template>
    
    <xsl:template name="sort-steps">
        <xsl:for-each select="step">
            <xsl:sort order="descending"/>
            <xsl:copy><xsl:value-of select="."/></xsl:copy>
            <xsl:call-template name="sort-steps"/>
        </xsl:for-each>
    </xsl:template>
    
</xsl:stylesheet>

Thank you in advance.

Rick


Current Thread