XSLT 1.0 sort on filtered XML data

Roaselofreert

New Member
Ok so based on this question XSLT 1.0 sort elements I cannot figure out why the following is not working:I have the following XML:<?xml version="1.0" encoding="UTF-8"?><viewentries> <viewentry> <entrydata name="Waste"> <text>Bric-a-Brac</text> </entrydata> <entrydata name="Disposal"> <text/> </entrydata> </viewentry> <viewentry> <entrydata name="Waste"> <textlist> <text>Paper</text> <text>Glass</text> </textlist> </entrydata> <entrydata name="Disposal"> <text/> </entrydata> </viewentry> <viewentry> <entrydata name="Waste"> <textlist> <text>Paper</text> <text>Cans</text> </textlist> </entrydata> <entrydata name="Disposal"> <text>Washing Machines</text> <text>Cars</text> </entrydata> </viewentry></viewentries>And the following XSLT:<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:key name="k1" match="entrydata[@name = 'Waste' or @name = 'Disposal']//text" use="concat(ancestor::entrydata/@name, '|', .)"/> <xsl:template match="viewentries"> <categories> <xsl:apply-templates/> </categories> </xsl:template> <xsl:template match="viewentry"> <xsl:apply-templates select="entrydata[@name = 'Waste' or @name = 'Disposal']//text [generate-id() = generate-id(key('k1', concat(ancestor::entrydata/@name, '|', .))[1])]"> <xsl:sort select="."/> </xsl:apply-templates> </xsl:template> <xsl:template match="text[normalize-space() != '']"> <category type="{ancestor::entrydata/@name}"> <xsl:apply-templates/> </category> </xsl:template></xsl:stylesheet>This gives the following output:<?xml version="1.0" encoding="UTF-8"?><categories> <category type="Waste">Bric-a-Brac</category> <category type="Waste">Glass</category> <category type="Waste">Paper</category> <category type="Waste">Cans</category> <category type="Disposal">Cars</category> <category type="Disposal">Washing Machines</category></categories>I need the output in sorted order:<?xml version="1.0" encoding="UTF-8"?><categories> <category type="Waste">Bric-a-Brac</category> <category type="Waste">Cans</category> <category type="Disposal">Cars</category> <category type="Waste">Glass</category> <category type="Waste">Paper</category> <category type="Disposal">Washing Machines</category></categories>What am I doing wrong ?EDIT:It seems to be sorting based on the first <text> value of <entrydata> only instead of all <text> values.However this stylesheet works fine:<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:key name="k1" match="entrydata[@name = 'Waste' or @name = 'Disposal']//text" use="concat(ancestor::entrydata/@name, '|', .)"/> <xsl:template match="viewentries"> <categories> <xsl:apply-templates select="viewentry/entrydata[@name = 'Waste' or @name = 'Disposal']//text [generate-id() = generate-id(key('k1', concat(ancestor::entrydata/@name, '|', .))[1])]"> <xsl:sort select="."/> </xsl:apply-templates> </categories> </xsl:template> <xsl:template match="text[normalize-space() != '']"> <category type="{ancestor::entrydata/@name}"> <xsl:value-of select="."/> </category> </xsl:template></xsl:stylesheet>Can someone explain why the first example doesn't work but the second example does.
 
Back
Top