开发者

Alphanumeric sort on mixed string values: alpha numeric alpha

Given XML where FORMNUMBER can be numeric only; begin with numeric and then include alpha; or follow a numeric alpha numeric pattern:

<FORMSLIST>
    <FORMS>
        <FORMNUMBER>3 ABC</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>12 ABC 45</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>3 XYZ</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>18</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>326</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>21 B</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>12 EFG 79</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>3 ABCD</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
</FORMSLIST>

This is my stylesheet (variation on Alphanumeric sort on mixed string value revisited):

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <xsl:variable name="vAlpha" select="' ABCDEFGHIJKLMNOPQRSTUVWXYZ'"/>
        <xsl:for-each-group select="FORMSLIST/FORMS" group-by="FORMSEQ">
            <xsl:sort select="FORMSEQ" data-type="number"/>
            <!-- NEED HELP WITH THE FOLLOWING SORT -->
            <xsl:for-each select="current-group()[(floor(number(FORMNUMBER)) = f开发者_运维百科loor(number(FORMNUMBER))) or (substring-before(FORMNUMBER,' ') and translate(substring-before(FORMNUMBER,' '),' 0123456789','') = '')]">
                <xsl:sort select="translate(FORMNUMBER, $vAlpha, '')" data-type="number"/>
                <xsl:sort select="substring-after(FORMNUMBER,' ')"/>
                <xsl:element name="{local-name()}">
                    <xsl:copy-of select="*"/>
                </xsl:element>
            </xsl:for-each>
            <!-- THIS SORT WORKS AND I DON'T HAVE QUESTIONS ON THIS: ALPHA ONLY FORMS; BEGIN WITH ALPHA AND THEN GO TO NUMERIC...MAY ALSO GO BACK TO ALPHA -->
            <xsl:for-each select="current-group()[contains($vAlpha,substring(FORMNUMBER,1,1)) or (translate(FORMNUMBER, $vAlpha, '') = '')]">
                <xsl:sort select="translate(FORMNUMBER, ' 0123456789', '')"/>
                <xsl:sort select="translate(FORMNUMBER, $vAlpha, '')" data-type="number"/>
                <xsl:element name="{local-name()}">
                    <xsl:copy-of select="*"/>
                </xsl:element>
            </xsl:for-each>
        </xsl:for-each-group>
    </xsl:template>
</xsl:stylesheet>

The resulting transform is:

<FORMSLIST>
    <FORMS>
        <FORMNUMBER>3 ABC</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>3 ABCD</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>3 XYZ</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>18</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>21 B</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>326</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>12 ABC 45</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>12 EFG 79</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
</FORMSLIST>

My desired result would be to have the '12 ABC 45' and '12 EFG 79' forms included in the sort like so (list is first sorted by beginning number):

<FORMSLIST>
    <FORMS>
        <FORMNUMBER>3 ABC</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>3 ABCD</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>3 XYZ</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>12 ABC 45</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>12 EFG 79</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>18</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>21 B</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
    <FORMS>
        <FORMNUMBER>326</FORMNUMBER>
        <FORMSEQ>99900</FORMSEQ>
    </FORMS>
</FORMSLIST>

I have tried numerous variations but can't seem to get it to work.


In XSLT 2.0, tokenize(FORMNUMBER, ' ') returns a sequence of the tokens that are separated by a space in FORMNUMBER. So you could use these <xsl:sort> elements:

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

Untested. But it looks like it would do exactly what you want.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜