使用字符串的前三位数来循环并对输出中的元素进行分组

时间:2016-11-09 14:05:50

标签: loops xslt select xslt-grouping

在下面的xml中," number"的前三位数字可用于分组"数字"结果输出中的元素。但是我不知道怎么样? 请参阅输入XML和我想要的输出

输入 [参见输入和所需的输出] [1]

<data>
<InputElemenmts>
    <Element11>
        <key>73004</key>
        <number>001001</number>
    </Element11>
    <Element11>
        <key>7300465</key>
        <number>002001</number>
    </Element11>
    <Element11>
        <key>730046</key>
        <number>003001</number>
    </Element11>
    <Element11>
        <key>730046</key>
        <number>003002</number>
    </Element11>
    <Element11>
        <key>730046</key>
        <number>003003</number>
    </Element11>
    <Element11>
        <key>730049</key>
        <number>004001</number>
    </Element11>
    <Element11>
        <key>730046</key>
        <number>005001</number>
    </Element11>
    <Element11>
        <key>730040</key>
        <number>006001</number>
    </Element11>
    <Element11>
        <key>730040</key>
        <number>006002</number>
    </Element11>
    <Element11>
        <key>730041</key>
        <number>007001</number>
    </Element11>
    <Element11>
        <key>730042</key>
        <number>008001</number>
    </Element11>
</InputElemenmts>
<OutputElemenmts>
    <result>
        <key>7300462</key>
        <number>001001</number>
    </result>
    <result>
        <key>7300465</key>
        <number>002001</number>
    </result>
    <result>
        <key>73004</key>
        <Element>
            <number>003001</number>
            <number>003002</number>
            <number>003003</number>
        </Element>
    </result>
    <result>
        <key>730049</key>
        <number>004001</number>
    </result>
    <result>
        <key>730046</key>
        <number>005001</number>
    </result>
    <result>
        <key>730040</key>
        <Element>
            <number>006001</number>
            <number>006002</number>
        </Element>
    </result>
    <result>
        <key>730041</key>
        <number>007001</number>
    </result>
    <result>
        <key>730042</key>
        <number>008001</number>
    </result>
</OutputElemenmts>

1 个答案:

答案 0 :(得分:-1)

这是我所理解的,但可能不是你想要的?

<!-- find the last of Element11 with any key -->
    <xsl:template match="Element11[not(following::Element11[key = current()/key])]">
       <result>
        <key><xsl:value-of select="key"/></key>
        <xsl:choose>
         <!-- are there more than one with this key? -->
         <xsl:when test="preceding-sibling::Element11[key = current()/key]">
           <!-- if yes it seems you want an Element wrapper -->
           <Element>
             <xsl:for-each select="preceding-sibling::Element11[key = current()/key]/number">
                <number><xsl:value-of select="."/></number>
             </xsl:for-each>
             <!-- the current one -->
             <number><xsl:value-of select="number"/></number>
           </Element>
         </xsl:when>
         <xsl:otherwise>
             <!-- only the current one -->
             <number><xsl:value-of select="number"/></number>
         </xsl:otherwise>
        </xsl:choose>
       </result>
     </xsl:template>

     <xsl:template match="Element11"/>