Reputation: 263
I have to sort my XML output based on the the number of characters in the data. I haven't been able to figure it out so far in XSLT 1.0. Here is what I am working with:
Source XML File:
<ROOT>
<REPLIST>
<NAME>ABCDE.xml</NAME>
<NAME>ABCDEFGH.xml</NAME>
<NAME>ABC.xml</NAME>
<NAME>ABCDEFG.xml</NAME>
<NAME>ABCD.xml</NAME>
<NAME>ABCDEF.xml</NAME>
<NAME>JKLMNOPQRST.xml</NAME>
<NAME>JKLMNOPQRS.xml</NAME>
<NAME>JKLMNOPQ.xml</NAME>
<NAME>JKLMN.xml</NAME>
</REPLIST>
<DATALIST>
<ExtractedName>ABCDEFGH.xml</ExtractedName>
<ExtractedName>JKLMN.xml</ExtractedName>
<ExtractedName>ABCDEFG.xml</ExtractedName>
<ExtractedName>ABCD.xml</ExtractedName>
<ExtractedName>JKLMNOPQRST.xml</ExtractedName>
<ExtractedName>ABCDEFG.xml</ExtractedName>
</DATALIST>
</ROOT>
Output
<TestData>
<FormName>ABCDEFGH</FormName>
<FormName>ABCDEFG</FormName>
<FormName>ABCD</FormName>
<FormName>JKLMNOPQRST</FormName>
<FormName>JKLMN</FormName>
</TestData>
Desired Output:
<TestData>
<FormName>JKLMNOPQRST</FormName>
<FormName>ABCDEFGH</FormName>
<FormName>ABCDEFG</FormName>
<FormName>JKLMN</FormName>
<FormName>ABCD</FormName>
</TestData>
Stylesheet ( 1.0):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:output method="xml" omit-xml-declaration="yes" standalone="no"/>
<xsl:template match="/">
<TestData>
<xsl:apply-templates/>
</TestData>
</xsl:template>
<xsl:template match="/ROOT/REPLIST/NAME[.=/ROOT/DATALIST/ExtractedName]">
<FormName>
<xsl:value-of select="translate(.,'.xml','')"/>
</FormName>
</xsl:template>
<xsl:template match="text()">
</xsl:template>
Any help regarding this will be greatly appreciated.
Upvotes: 1
Views: 115
Reputation: 243549
A shorter, simpler, and more robust solution:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kNameByVal" match="NAME" use="."/>
<xsl:template match="REPLIST">
<TestData>
<xsl:apply-templates select=
"key('kNameByVal', /*/DATALIST/*)">
<xsl:sort select="-string-length()" data-type="number"/>
</xsl:apply-templates>
</TestData>
</xsl:template>
<xsl:template match="NAME">
<FormName><xsl:value-of select="substring(.,1, string-length()-4)"/></FormName>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
When this transformation is applied on the provided XML document:
<ROOT>
<REPLIST>
<NAME>ABCDE.xml</NAME>
<NAME>ABCDEFGH.xml</NAME>
<NAME>ABC.xml</NAME>
<NAME>ABCDEFG.xml</NAME>
<NAME>ABCD.xml</NAME>
<NAME>ABCDEF.xml</NAME>
<NAME>JKLMNOPQRST.xml</NAME>
<NAME>JKLMNOPQRS.xml</NAME>
<NAME>JKLMNOPQ.xml</NAME>
<NAME>JKLMN.xml</NAME>
</REPLIST>
<DATALIST>
<ExtractedName>ABCDEFGH.xml</ExtractedName>
<ExtractedName>JKLMN.xml</ExtractedName>
<ExtractedName>ABCDEFG.xml</ExtractedName>
<ExtractedName>ABCD.xml</ExtractedName>
<ExtractedName>JKLMNOPQRST.xml</ExtractedName>
<ExtractedName>ABCDEFG.xml</ExtractedName>
</DATALIST>
</ROOT>
the wanted, correct result is produced:
<TestData>
<FormName>JKLMNOPQRST</FormName>
<FormName>ABCDEFGH</FormName>
<FormName>ABCDEFG</FormName>
<FormName>JKLMN</FormName>
<FormName>ABCD</FormName>
</TestData>
Upvotes: 1
Reputation: 101748
Here's how you can do it:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:output method="xml" omit-xml-declaration="yes" standalone="no"/>
<xsl:key name="kExtract" match="ExtractedName" use="."/>
<xsl:template match="/ROOT">
<TestData>
<xsl:apply-templates select="REPLIST/NAME[key('kExtract', .)]">
<xsl:sort select="string-length(substring-before(., '.xml'))"
data-type="number" order="descending"/>
</xsl:apply-templates>
</TestData>
</xsl:template>
<xsl:template match="REPLIST/NAME">
<FormName>
<xsl:value-of select="substring-before(., '.xml')"/>
</FormName>
</xsl:template>
</xsl:stylesheet>
When run on your sample input, this produces:
<TestData>
<FormName>JKLMNOPQRST</FormName>
<FormName>ABCDEFGH</FormName>
<FormName>ABCDEFG</FormName>
<FormName>JKLMN</FormName>
<FormName>ABCD</FormName>
</TestData>
Upvotes: 1