So you want to sort the Program
elements by the Month
child, in XSLT 3 with support for the higher-order sort
function you can do that with
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
exclude-result-prefixes="xs math"
version="3.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="Root/Level1"/>
</xsl:template>
<xsl:template match="Level1">
<xsl:value-of select="EMPLID, sort(Program, (), function($p) { -$p/Month/xs:integer(.) })[1]/(Sales_Program/ID1, Start_Date, Month)" separator=","/>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
in XSLT 2 you need to implement the sorting in your own function with xsl:perform-sort
:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="xs mf"
version="2.0">
<xsl:output method="text"/>
<xsl:function name="mf:sort">
<xsl:param name="programs" as="element(Program)*"/>
<xsl:perform-sort select="$programs">
<xsl:sort select="xs:integer(Month)" order="descending"/>
</xsl:perform-sort>
</xsl:function>
<xsl:template match="/">
<xsl:apply-templates select="Root/Level1"/>
</xsl:template>
<xsl:template match="Level1">
<xsl:value-of select="EMPLID, mf:sort(Program)[1]/(Sales_Program/ID1, Start_Date, Month)" separator=","/>
<xsl:text> </xsl:text>
</xsl:template>
</xsl:stylesheet>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…