Reputation: 39
I am trying to sort the elements in XML based on custom sort requirement. First I need to sort based on 1st character in VerificationCode in the order of V,P,U,A,C,R. Then I need to sort based on numeric in the 2nd and 3rd characters.
The sample xml I am using is
<ns3:Response xmlns:ns3="http://www.example.org">
<ns3:Codes>
<ns3:Name>US</ns3:Name>
<ns3:VerificationCode>A42</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>CH</ns3:Name>
<ns3:VerificationCode>V54</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>IN</ns3:Name>
<ns3:VerificationCode>U14</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>MY</ns3:Name>
<ns3:VerificationCode>V84</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>MX</ns3:Name>
<ns3:VerificationCode>C34</ns3:VerificationCode>
</ns3:Codes>
</ns3:Response>
For the 1st level of sort I XSL I have is as below. But it is not doing anything.
<xsl:stylesheet version="1.0" xmlns:ns0="http://www.example.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="*">
<xsl:variable name="sortOrder1" select="'|V|P|U|A|C|R'" />
<xsl:apply-templates select="ns0:Codes">
<xsl:sort select="string-length(substring-before($sortOrder1, concat('|', substring(ns0:VerificationCode,1,1), '|')))" data-type="number"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="/">
<ns0:Response>
<xsl:copy-of select="/ns0:Response"/>
</ns0:Response>
</xsl:template>
</xsl:stylesheet>
Below is the expected output
<ns3:Response xmlns:ns3="http://www.example.org">
<ns3:Codes>
<ns3:Name>MY</ns3:Name>
<ns3:VerificationCode>V84</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>CH</ns3:Name>
<ns3:VerificationCode>V54</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>IN</ns3:Name>
<ns3:VerificationCode>U14</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>US</ns3:Name>
<ns3:VerificationCode>A42</ns3:VerificationCode>
</ns3:Codes>
<ns3:Codes>
<ns3:Name>MX</ns3:Name>
<ns3:VerificationCode>C34</ns3:VerificationCode>
</ns3:Codes>
</ns3:Response>
Upvotes: 1
Views: 52
Reputation: 7385
It took me a while to figure out what you want. You can make your life easier by using multiple sort statements, only one template and none of these namespaces.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/Response">
<xsl:variable name="sortOrder" select="'VPUACR'" />
<Response>
<xsl:for-each select="Codes">
<xsl:sort select="string-length(substring-before($sortOrder, substring(VerificationCode/text(), 1, 1)))"/>
<xsl:sort select="substring(VerificationCode/text(), 1)" data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</Response>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1
Reputation: 167446
You can't use copy-of
on the root node if you want to apply templates, also simply match on that particular Response
element:
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns0="http://www.example.org"
version="1.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="ns0:Response">
<xsl:copy>
<xsl:variable name="sortOrder1" select="'|V|P|U|A|C|R'" />
<xsl:apply-templates select="ns0:Codes">
<xsl:sort select="string-length(substring-before($sortOrder1, concat('|', substring(ns0:VerificationCode,1,1), '|')))" data-type="number"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/6rewNxR, however, has a slightly different order than the one you have shown as the V54 value in the input is before the V84 one.
Upvotes: 3