xslt 1.0 - Sorting from multiple nodes by alphanumeric format -
<?xml version="1.0" encoding="utf-8"?> <accountlist> <previousaccount> <account> <lastname>nash</lastname> <accountstatus>removed</accountstatus> <accno>8d</accno> </account> <account> <lastname>adoga</lastname> <accountstatus>removed</accountstatus> <accno>8a</accno> </account> <account> <lastname>lucas</lastname> <accountstatus>hold</accountstatus> <accno>9a</accno> </account> <account> <lastname>donald</lastname> <accountstatus>hold</accountstatus> <accno>10d</accno> </account> <account> <accountstatus>hold</accountstatus> <lastname>london</lastname> <accno>10b</accno> </account> </previousaccount> <account> <title>mr</title> <firstname>richard</firstname> <lastname>john</lastname> <city>london</city> <accno>5a</accno> </account> <account> <title>mr</title> <firstname>xxx</firstname> <lastname>john</lastname> <city>london</city> <accno>5d</accno> </account> <account> <title>mr</title> <firstname>hewit</firstname> <lastname>john</lastname> <city>london</city> <accno>20b</accno> </account> <account> <title>mr</title> <firstname>xxx</firstname> <lastname>john</lastname> <city>london</city> <accno>21d</accno> </account> <account> <title>mr</title> <firstname>kevin</firstname> <lastname>pete</lastname> <city>london</city> <accno>5f</accno> </account> </accountlist>
xslt code
<xsl:stylesheet version="2.0" xsi:schemalocation="http://www.w3.org/1999/xsl/transform file:/c:/users/n434947/desktop/workspace/sonicxslt/ba xslt page.xsd" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fn="http://www.w3.org/2005/xpath-functions" xmlns:xs="http://www.w3.org/2001/xmlschema" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance"> <xsl:output method="text"/> <xsl:variable name="newline"> <xsl:text> </xsl:text> </xsl:variable> <xsl:template match="accountlist"> <xsl:for-each-group select="descendant::account" group-starting-with="*[firstname != 'xxx' or lastname != preceding-sibling::*[1]/lastname]"> <xsl:sort select="accno" data-type="number"/> <xsl:value-of select="accno"/> <xsl:text> </xsl:text> <xsl:value-of select="accountstatus"/> <xsl:text> </xsl:text> <xsl:value-of select="lastname"/> <xsl:value-of select="$newline"/> </xsl:for-each-group> </xsl:template>
in requirement ,i got sort accno different nodes.i think i'll better explaining in code rather words, accountlist/previousaccount/account/accno accountlist/account/accno
i used descendant,which not working fine requirement.here have used 2 for-each 2 nodes , sorted separtedly.last not least got sort alphanumeric combinations.
actual output
8d removed nash 8a removed adoga 9a hold lucas 10d hold donald 10b hold london 5a john 20b john 5f pete
expecting output
5a john 5f pete 8a removed adoga 8d removed nash 9a hold lucas 10b hold london 10d hold donald 20b johnaccno in sorted manner.
the main problem unable sort alphanumeric combinations 2a,2b,3b,3g
well dirty solution... use function:
<xsl:function name="px:tointeger" as="xs:double"> <xsl:param name="value" as="xs:string"/> <xsl:variable name="letter" select="replace($value, '\d', '')"/> <xsl:variable name="digit" select="xs:integer(replace($value, '\d', ''))" as="xs:integer"/> <xsl:variable name="codepoints" select="xs:integer(string-to-codepoints($letter))" as="xs:integer"/> <xsl:value-of select="$digit + ($codepoints div 100000)"/> </xsl:function>
and call in xsl:sort:
<xsl:sort select="px:tointeger(accno)" data-type="number"/>
it works integer + 1 character samples.
Comments
Post a Comment