当前位置: 动力学知识库 > 问答 > 编程问答 >

xml - xsl cannot sort added nodes

问题描述:

After having added some nodes in the xsl, I need to sort them with the xml document nodes to properly format the text output.

Please note that the output can be sorted in 4 different ways depending on the application reading the text document preferred item order.

Here is the XML:

<?xml version="1.0" encoding="UTF-8"?>

<Main>

<GroupA>

<a>A</a>

<b>B</b>

<Sub type="Z">

<bb name="100" dev="1" c="C"><P>N100</P></bb>

<bb name="50" dev="1" c="C"><P>N50</P></bb>

<bb name="20" dev="1" c="C"><P>N20</P></bb>

<bb name="10" dev="1" c="C"><P>N10</P></bb>

<bb name="5" dev="1" c="C"><P>N5</P></bb>

<bb name="200" dev="2" c="C"><P>C200</P></bb>

<bb name="100" dev="2" c="C"><P>C100</P></bb>

<bb name="50" dev="2" c="C"><P>C50</P></bb>

<bb name="25" dev="2" c="C"><P>C25</P></bb>

<bb name="10" dev="2" c="C"><P>C10</P></bb>

<bb name="5" dev="2" c="C"><P>C5</P></bb>

<bb name="1" dev="2" c="C"><P>C1</P></bb>

<bb name="100" dev="1" c="U"><P>N100</P></bb>

<bb name="50" dev="1" c="U"><P>N50</P></bb>

<bb name="20" dev="1" c="U"><P>N20</P></bb>

<bb name="10" dev="1" c="U"><P>N10</P></bb>

<bb name="5" dev="1" c="U"><P>N5</P></bb>

<bb name="2" dev="1" c="U"><P>N2</P></bb>

<bb name="1" dev="1" c="U"><P>N1</P></bb>

</Sub>

<Sub type="B">

<bb name="111" dev="1" c="C"><P>N100</P></bb>

<bb name="55" dev="1" c="C"><P>N50</P></bb>

<bb name="22" dev="1" c="C"><P>N20</P></bb>

<bb name="11" dev="1" c="C"><P>N10</P></bb>

<bb name="5" dev="1" c="C"><P>N5</P></bb>

<bb name="222" dev="2" c="C"><P>C200</P></bb>

<bb name="111" dev="2" c="C"><P>C100</P></bb>

<bb name="55" dev="2" c="C"><P>C50</P></bb>

<bb name="25" dev="2" c="C"><P>C25</P></bb>

<bb name="11" dev="2" c="C"><P>C10</P></bb>

<bb name="5" dev="2" c="C"><P>C5</P></bb>

<bb name="1" dev="2" c="C"><P>C1</P></bb>

</Sub>

</GroupA>

</Main>

Here is the XSL:

<?xml version="1.0" encoding="UTF-8"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="text"/>

<xsl:strip-space elements="*"/>

<xsl:template match="Sub[@type='Z']" name='ABC'>

<bb c="C" name="1000" dev="1"><P>N1000X</P></bb>

<bb c="C" name="2" dev="1"><P>N2X</P></bb>

<bb c="C" name="1" dev="1"><P>N1X</P></bb>

</xsl:template>

<xsl:template match="GroupA">

<xsl:value-of select="a" />

<xsl:text>,</xsl:text>

<xsl:value-of select="b" />

<xsl:text>,</xsl:text>

<xsl:call-template name="ABC"/>

<xsl:for-each select="Sub[@type='Z']/bb[@c='C']">

<xsl:sort select="@dev" data-type="number" order="ascending"/>

<xsl:sort select="@name" data-type="number" order="descending"/>

<xsl:value-of select="./P"/>

<xsl:if test="position()!=last()">

<xsl:text>,</xsl:text>

</xsl:if>

</xsl:for-each>

</xsl:template>

</xsl:stylesheet>

This is the current output:

A,B,N1000XN2XN1XN100,N50,N20,N10,N5,C200,C100,C50,C25,C10,C5,C1

This is my desired output:

A,B,N1000X,N100,N50,N20,N10,N5,N2X,N1X,C200,C100,C50,C25,C10,C5,C1

Thank for your help.

网友答案:

If (!) I understand correctly, you want to do:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:output method="text"/>
<xsl:strip-space elements="*"/>

<xsl:variable name="add">
    <bb c="C" name="1000" dev="1"><P>N1000X</P></bb>
    <bb c="C" name="2" dev="1"><P>N2X</P></bb>
    <bb c="C" name="1" dev="1"><P>N1X</P></bb>
</xsl:variable>

<xsl:template match="GroupA">
        <xsl:value-of select="a" />
        <xsl:text>,</xsl:text>
        <xsl:value-of select="b" />
        <xsl:text>,</xsl:text>
        <xsl:for-each select="Sub[@type='Z']/bb[@c='C'] | exsl:node-set($add)/bb">
            <xsl:sort select="@dev" data-type="number" order="ascending"/>
            <xsl:sort select="@name" data-type="number" order="descending"/>
            <xsl:value-of select="P"/>
            <xsl:if test="position()!=last()">
                <xsl:text>,</xsl:text>
            </xsl:if>
        </xsl:for-each>             
</xsl:template> 

</xsl:stylesheet>
分享给朋友:
您可能感兴趣的文章:
随机阅读: