使用xslt排序节点不起作用

时间:2014-06-16 17:27:03

标签: xml xslt saxon

我有以下xml和xsl文件,我正在尝试对CARTONDETAIL节点进行排序,但保留文档的其余部分作为原始文件。

到目前为止,我的文档的顶部被复制了,CARTONDETAIL之前的所有内容都被复制了。之后我得到了很多空的<CARTONDETAIL>标签,就像我创建一个无限循环一样,因为文档从不将结束括号放在末尾,也不放置另一个HEADER实例。在输入文件中,我有HEADER树的两个实例,但我在输出文件中看不到第二个的任何痕迹。

我正在使用撒克逊语来解析文档。

非常感谢任何帮助..谢谢大家!

XML:

<?xml version="1.0" encoding="UTF-8"?>
<TRUCKSHIPUPLOAD>
    <HEADER>
        <WH>WH1</WH>
        <OWN>FakeOwner</OWN>
        <ORD>1035710</ORD>
        <RELEASE_NUM>1</RELEASE_NUM>
        <ORDER_LINE/>
        <TRUCK>0222001406</TRUCK>
        <ORDERING>1</ORDERING>
        <TRAILER_ID/>
        <WAREHOUSE_ID>UDS01</WAREHOUSE_ID>
        <OWNER_ID>CAYRE</OWNER_ID>
        <IF_SEQ_NUM>222001406</IF_SEQ_NUM>
        <IF_STATUS/>
        <IF_ERROR_MSG/>
        <ORDER_ID>1035710</ORDER_ID>
        <STAT_DATE/>
        .........
        <CARTON>
            <WH>WH1</WH>
            <OWN>FakeOwner</OWN>
            <ORD>1035710</ORD>
            <RELEASE_NUM>1</RELEASE_NUM>
            <ORDER_LINE/>
            <TRUCK>0222001406</TRUCK>
            <ORDERING>4</ORDERING>
            <TRAILER_ID/>
            ....
            <CARTONDETAIL>
                <WH>WH1</WH>
                <OWN>FakeOwner</OWN>
                <ORD>1035710</ORD>
                <RELEASE_NUM>1</RELEASE_NUM>
                <ORDER_LINE>4</ORDER_LINE>
                <TRUCK>0222001406</TRUCK>
                <TRAILER_ID/>
                <WAREHOUSE_ID>WH1</WAREHOUSE_ID>
                <OWNER_ID>FakeOwner</OWNER_ID>
                <IF_SEQ_NUM/>
                <IF_STATUS/>
                <IF_ERROR_MSG/>
                <ORDER_ID>1035710</ORDER_ID>
                <STAT_DATE/>
                ...
            </CARTONDETAIL>

            <CARTONDETAIL>
                <WH>WH1</WH>
                <OWN>FakeOwner</OWN>
                <ORD>1035710</ORD>
                <RELEASE_NUM>1</RELEASE_NUM>
                <ORDER_LINE>8</ORDER_LINE>
                <TRUCK>0222001406</TRUCK>
                <TRAILER_ID/>
                <WAREHOUSE_ID>WH1</WAREHOUSE_ID>
                <OWNER_ID>FakeOwner</OWNER_ID>
                <IF_SEQ_NUM/>
                <IF_STATUS/>
                <IF_ERROR_MSG/>
                <ORDER_ID>1035710</ORDER_ID>
                <STAT_DATE/>
                ...
            </CARTONDETAIL>
            <CARTONDETAIL>
                <WH>WH1</WH>
                <OWN>FakeOwner</OWN>
                <ORD>1035710</ORD>
                <RELEASE_NUM>1</RELEASE_NUM>
                <ORDER_LINE>2</ORDER_LINE>
                <TRUCK>0222001406</TRUCK>
                <TRAILER_ID/>
                <WAREHOUSE_ID>WH1</WAREHOUSE_ID>
                <OWNER_ID>FakeOwner</OWNER_ID>
                <IF_SEQ_NUM/>
                <IF_STATUS/>
                <IF_ERROR_MSG/>
                <ORDER_ID>1035710</ORDER_ID>
                <STAT_DATE/>
                ...
            </CARTONDETAIL>

        </CARTON>
    </HEADER>
</TRUCKSHIPUPLOAD>

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="xml" indent="yes"/>
            <xsl:template match="CARTONDETAIL">
                <xsl:copy>
                    <xsl:apply-templates select="//CARTONDETAIL">
                        <xsl:sort select="ORDER_LINE" data-type="number"/>
                    </xsl:apply-templates>
                </xsl:copy> 
            </xsl:template>
            <xsl:template match="node()|@*">
                <xsl:copy>
                    <xsl:apply-templates select="node()|@*"/>
                </xsl:copy>
            </xsl:template>
        </xsl:stylesheet>

3 个答案:

答案 0 :(得分:3)

我认为不是

        <xsl:template match="CARTONDETAIL">
            <xsl:copy>
                <xsl:apply-templates select="//CARTONDETAIL">
                    <xsl:sort select="ORDER_LINE" data-type="number"/>
                </xsl:apply-templates>
            </xsl:copy> 
        </xsl:template>

你想要

        <xsl:template match="CARTON">
            <xsl:copy>
              <xsl:for-each-group select="*" group-adjacent="boolean(self::CARTONDETAIL)">
               <xsl:choose>
                <xsl:when test="current-grouping-key()">
                 <xsl:apply-templates select="current-group()">
                    <xsl:sort select="ORDER_LINE" data-type="number"/>
                 </xsl:apply-templates>
                </xsl:when>
                <xsl:otherwise>
                   <xsl:apply-templates select="current-group()"/>
                </xsl:otherwise>
            </xsl:copy> 
        </xsl:template>

答案 1 :(得分:0)

首先:你在这里有一个无限循环:

<xsl:template match="CARTONDETAIL">
    <xsl:copy>
        <xsl:apply-templates select="//CARTONDETAIL">
     ...

现在,假设您要对其父CARTONDETAIL中的CARTON个节点进行排序,请尝试:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="CARTON">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()">
            <xsl:sort select="ORDER_LINE" data-type="number"/>
        </xsl:apply-templates>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

答案 2 :(得分:0)

复制所有节点并根据ORDER_LINE对CARTONDETAIL进行排序,希望这是你想要的。     

       <xsl:output method="xml" indent="yes"/>
        <xsl:template match="CARTON">
            <xsl:copy>
                <xsl:apply-templates select="CARTONDETAIL">
                    <xsl:sort select="ORDER_LINE" data-type="number"/>
                </xsl:apply-templates>
            </xsl:copy> 
        </xsl:template>
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    </xsl:stylesheet>
相关问题