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.
Here is your "second template":
Here you want to sort the children of the current node by their string value.
However, in the provided XML document any
text
element has a single text-node child -- therefore there isn't anything to sort!In this and in the previous question you commit the same error -- trying to sort the children of the elements that must be sorted -- you are sorting too-late.
Remember:
is an abbreviation for:
So, this means: apply templates to my children" -- not "apply templates to me".
Update: Here is a correct solution to the problem:
When applied on the provided XML document:
the wanted, correct result is produced:
Explanation:
This kind of grouping requires indexing based on a composite key that has two parts. Therefore the
use
attribute ofxsl:key
is specified as the concatenation of these two parts, joined together by a character that we know cannot be present in any of their values (to avoid false-positives when a value of the first key component is a prefix of a value of the second key component).