Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
930 views
in Technique[技术] by (71.8m points)

xslt 1.0 - xsl:sort: sorting double digit by numeric value

UPDATE : example and add some information.

I have to sort out the number below in numerical order in XSLT 1.0. The "code" follow the pattern below :

for example,

<?xml version="1.0" encoding="ISO-8859-1"?>
<DATA>
    <CTX>
        <VA>
            <code>25896_1_1_1</code>
        </VA>   
        <VA>
            <code>25896_10_1_1</code>
        </VA>   
        <VA>
            <code>25896_2_1_2</code>
        </VA>   
        <VA>
            <code>25896_3_1_1</code>
        </VA>                       
        <VA>
            <code>25896_4_1_1</code>
        </VA>                           
        <VA>
            <code>25897_1_1_1</code>
        </VA>                           
        <VA>
            <code>25897_2_1_1</code>
        </VA>                           
        <VA>
            <code>25897_2_1_2</code>
        </VA>   
    </CTX>
</DATA>

    

when I just do <xsl:sort select="code" order="ascending" /> it displays above result.

However, I want that to be in order like this :

25896_1_1_1
25896_2_1_2
25896_3_1_1
25896_4_1_1
25896_10_1_1
25897_1_1_1
25897_2_1_1
25897_2_1_2

How do I do this?

JAXP XSLT APIs (javax.xml.transform) to transform xml file. With XSL Transform, the engine is Saxon 6.5.5 // https://xml.apache.org/xalan-j/trax.html

question from:https://stackoverflow.com/questions/65842147/xslsort-sorting-double-digit-by-numeric-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To do this in pure XSLT 1.0, you will need to do it the pedestrian way:

<xsl:sort select="substring-before(code, '_')" data-type="number"/>
<xsl:sort select="substring-before(substring-after(code, '_'), '_')" data-type="number"/>
<xsl:sort select="substring-before(substring-after(substring-after(code, '_'), '_'), '_')" data-type="number"/>
<xsl:sort select="substring-after(substring-after(substring-after(code, '_'), '_'), '_')" data-type="number"/>

If your processor happens to support the EXSLT str:tokenize() extension function, you could make it a bit more elegant:

<xsl:sort select="str:tokenize(code, '_')[1]" data-type="number"/>
<xsl:sort select="str:tokenize(code, '_')[2]" data-type="number"/>
<xsl:sort select="str:tokenize(code, '_')[3]" data-type="number"/>
<xsl:sort select="str:tokenize(code, '_')[4]" data-type="number"/>

Untested, because no code was provided.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...