Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k 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 Reply

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...