xml - XSLT sort working with numbers but not text -
given following xml file:
<?xml version="1.0"?> <mappings> <mapping> <class-a>c</class-a> <class-b>a</class-b> </mapping> <mapping> <class-a>b</class-a> <class-b>b</class-b> </mapping> <mapping> <class-a>a</class-a> <class-b>c</class-b> </mapping> </mappings>
and following xslt file:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/xsl/transform"> <xsl:output method="xml" omit-xml-declaration="no" indent="yes" /> <xsl:strip-space elements="*"/> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <xsl:template match="mappings"> <xsl:copy> <xsl:apply-templates> <xsl:sort select="(name() = 'mapping')*class-a" data-type="text" order="ascending" /> </xsl:apply-templates> </xsl:copy> </xsl:template> </xsl:stylesheet>
produces bad xml result:
<?xml version="1.0" encoding="utf-8"?> <mappings> <mapping> <class-a>c</class-a> <class-b>a</class-b> </mapping> <mapping> <class-a>b</class-a> <class-b>b</class-b> </mapping> <mapping> <class-a>a</class-a> <class-b>c</class-b> </mapping> </mappings>
expected result is:
<?xml version="1.0" encoding="utf-8"?> <mappings> <mapping> <class-a>a</class-a> <class-b>c</class-b> </mapping> <mapping> <class-a>b</class-a> <class-b>b</class-b> </mapping> <mapping> <class-a>c</class-a> <class-b>a</class-b> </mapping> </mappings>
mappings should sorted alphabetically based on class-a
element values.
by using numbers in place of strings class-a
elements of input xml:
<?xml version="1.0"?> <mappings> <mapping> <class-a>3</class-a> <class-b>a</class-b> </mapping> <mapping> <class-a>2</class-a> <class-b>b</class-b> </mapping> <mapping> <class-a>1</class-a> <class-b>c</class-b> </mapping> </mappings>
with either data-type="text"
or data-type="number"
in xslt sort tag, getting same expected result :
<?xml version="1.0" encoding="utf-8"?> <mappings> <mapping> <class-a>1</class-a> <class-b>c</class-b> </mapping> <mapping> <class-a>2</class-a> <class-b>b</class-b> </mapping> <mapping> <class-a>3</class-a> <class-b>a</class-b> </mapping> </mappings>
does know how expected result text well?
you can try this:
<xsl:sort select="class-a" data-type="text" order="ascending" />
Comments
Post a Comment