使用XSLT根据标签拆分XML文件

时间:2019-05-09 16:32:20

标签: split xslt-1.0 filesplitting

我需要根据内部标签拆分XML文件。这是输入文件的样子。拆分应该基于订单标签

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Root>
    <MultiApi>
        <API>
            <Input>
                <Order test="0001" >
                    <OrderLines>
                        <OrderLine test1="123" />
                    </OrderLines>
                </Order>
                <Order test="0002" >
                    <OrderLines>
                        <OrderLine test1="456" />
                    </OrderLines>
                </Order>
                <Order test="0003" >
                    <OrderLines>
                        <OrderLine test1="789" />
                    </OrderLines>
                </Order>
            </Input>
        </API>
    </MultiApi>
    <MultiApi>
        <EOF abc="test" MaxMsgPerFile="1" >
        </EOF>
    </MultiApi>
    </Root>

输出应如下所示:

<Root>
<MultiApi>
    <API>
        <Input>
            <Order test="0001" >
                <OrderLines>
                    <OrderLine test1="123" />
                </OrderLines>
            </Order>
        </Input>
    </API>
</MultiApi>
<MultiApi>
    <API>
        <Input>
            <Order test="0002" >
                <OrderLines>
                    <OrderLine test1="456" />
                </OrderLines>
            </Order>
        </Input>
    </API>
</MultiApi>
<MultiApi>
    <API>
        <Input>
            <Order test="0003" >
                <OrderLines>
                    <OrderLine test1="789" />
                </OrderLines>
            </Order>
        </Input>
    </API>
</MultiApi>
</Root>

我尝试了以下XSL-但返回的是null。任何帮助都会很棒:

<?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" version="1.0" encoding="UTF-8" indent="yes"/>

      <xsl:variable name="tag">
      <xsl:value-of select="/*/*/EOF/@MaxMsgPerFile"/>
   </xsl:variable>

<xsl:template match="/Root">
    <xsl:copy>


        <xsl:for-each select="MultiApi[not (EOF)]/API/Input/Order[position() mod $tag = 1]">
            <MultiApi>
                <xsl:copy-of select="| . | following-sibling::Order[position() &lt; $tag]"/>

            </MultiApi>
        </xsl:for-each>
    </xsl:copy>
</xsl:template> 

</xsl:stylesheet>

我可能在“跟随同胞”方面做得不好。请查看并提供帮助!

1 个答案:

答案 0 :(得分:2)

仅需要一个输出文件,即可使用以下样式表轻松实现此目的。不需要[not(EOF)]元素上的MultiApi谓词,因为另一个元素不包含此结构。

<?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" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:variable name="tag">
        <xsl:value-of select="/*/*/EOF/@MaxMsgPerFile"/>
    </xsl:variable>

    <xsl:template match="/Root">
        <xsl:copy>
            <xsl:for-each select="MultiApi/API/Input/Order">
                <MultiApi>
                    <API>
                        <Input>
                            <xsl:copy-of select="."/>
                        </Input>
                    </API>
                </MultiApi>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

其输出为:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
    <MultiApi>
        <API>
            <Input>
                <Order test="0001">
                    <OrderLines>
                        <OrderLine test1="123"/>
                    </OrderLines>
                </Order>
            </Input>
        </API>
    </MultiApi>
    <MultiApi>
        <API>
            <Input>
                <Order test="0002">
                    <OrderLines>
                        <OrderLine test1="456"/>
                    </OrderLines>
                </Order>
            </Input>
        </API>
    </MultiApi>
    <MultiApi>
        <API>
            <Input>
                <Order test="0003">
                    <OrderLines>
                        <OrderLine test1="789"/>
                    </OrderLines>
                </Order>
            </Input>
        </API>
    </MultiApi>
</Root>