Reputation: 606
Last time I had some problems with grouping on xslt 1.0. Now I've faced another problem with sorting. Here is some code.
XML
<Development>
<PlotTypes>
<Apartments>
<Building>
<PlotType Phase="1" />
<PlotType Phase="2" />
</Building>
</Apartments>
</PlotTypes>
<Phases>
<Phase ContentID="40514" PhaseCode="1" Title="Properties available to move in" SortOrder="2" />
<Phase ContentID="40515" PhaseCode="2" Title="test" SortOrder="1" />
</Phases>
</Development>
And here is XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:key name ="phase" match="//PlotType" use="@Phase"/>
<xsl:template match="Development">
<xsl:for-each select="//PlotType[generate-id(.)=generate-id(key('phase',@Phase)[1])]">
<xsl:sort select="//Phase[@PhaseCode=@Phase]/@SortOrder" order="ascending" data-type="number"/>
<xsl:variable select="@Phase" name="groupedPhase"/>
Phase - <xsl:value-of select="@Phase"/><br/>
Phase Sort order - <xsl:value-of select="//Phase[@PhaseCode=$groupedPhase]/@SortOrder"/><br/>
<xsl:value-of select="//Phase[@PhaseCode=$groupedPhase]/@Title"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
And I need to make this final result
<?xml version="1.0" encoding="UTF-8"?>
Phase - 2<br/>
Phase Sort order - 2<br/>
<h3>test</h3>
Phase - 1<br/>
Phase Sort order - 1<br/>
<h3>Properties available to move in</h3>
But when I use such sorting <xsl:sort select="//Phase[@PhaseCode=@Phase]/@SortOrder"...
it doesn't work.
Upvotes: 0
Views: 100
Reputation: 167446
Define a second key
<xsl:key name="phase-by-code" match="Phases/Phase" use="@PhaseCode"/>
then use
<xsl:sort select="key('phase-by-code', @Phase)/@SortOrder" order="ascending" data-type="number"/>
for your sort.
Upvotes: 1