1
        <?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 代码

<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>

在我的要求中,我必须对不同节点的 accNo 进行排序。我想我会用代码而不是文字来更好地解释这一点,accountList/previousAccount/account/accNo accountList/account/accNo

我使用了后代,这不能满足我的要求。这里我为两个节点使用了两个 for-each 并单独排序。最后但并非最不重要的是,我必须按字母数字组合进行排序。

实际输出

    8D REMOVED NASH
    8A REMOVED ADOGA
    9A HOLD LUCAS
    10D HOLD DONALD
    10B HOLD LONDON
    5A  JOHN
    20B  JOHN
    5F  PETE

期望输出

    5A  JOHN
    5F  PETE
    8A REMOVED ADOGA
    8D REMOVED NASH
    9A HOLD LUCAS
    10B HOLD LONDON
    10D HOLD DONALD
    20B  JOHNaccNo will be in sorted manner.

主要问题是无法按 2A、2B、3B、3G 等字母数字组合排序

4

1 回答 1

0

那么这是一个肮脏的解决方案......使用这个功能:

<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>

并在 xsl:sort 中调用它:

<xsl:sort select="px:toInteger(accNo)" data-type="number"/>

它只适用于像您的样本这样的整数 + 一个字符。

于 2013-08-13T12:08:11.347 回答