0

我需要根据里面的标签拆分一个 XML 文件。这是输入文件的样子。spli 应该基于 Order 标签发生

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

我可能在“兄弟姐妹”方面做得不好。请审查和帮助!

4

1 回答 1

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>
于 2019-05-09T17:58:16.287 回答