从格式不正确的XML输出中删除不必要的<ns0:messages>和<ns0:message1>标记

时间:2018-11-02 21:00:17

标签: xslt xslt-1.0 xslt-2.0

请提供XSLT逻辑(可以使用XSLT 1.0或XSLT 2.0版本),以将下面格式不正确的xml输入转换为格式正确的XML数据。

输入:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
    <ns0:Message1>
        <ns0:invoiceOrder xmlns:ns0="http://cts.com"
                   type="ABCD"
                   businessDivision="X0"
                   salesOrganization="XYZ10"
                   Channel="Y0">
            <ns0:references>
                <ns0:key type="Source System">ABC</ns0:key>
                <ns0:relationship scheme="Customer" schemeAgency="APAC" role="Sell to">703760000</ns0:relationship>
                <ns0:document type="Purchase Order">AB123</ns0:document>
            </ns0:references>
            <ns0:notes>
                <ns0:note type="XY67" language="EN">package1</ns0:note>
            </ns0:notes>
            <ns0:orderLines>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">100411</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="credit Amount">274.50</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">140072</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="credit Amount">58.41</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
            </ns0:orderLines>
            <ns0:terms>
                <ns0:term type="Reason Code">D19</ns0:term>
                <ns0:term type="Payment Terms">Y000</ns0:term>
            </ns0:terms>
            <ns0:billingInfo>
                <ns0:paymentMethod/>
            </ns0:billingInfo>
            <ns0:flags>
                <ns0:flag type="1099 Payment Flag">Y</ns0:flag>
            </ns0:flags>
            <ns0:dates>
                <ns0:date type="Proposed Invoice Date">2018-11-02</ns0:date>
            </ns0:dates>
        </ns0:invoiceOrder>
    </ns0:Message1>
</ns0:Messages>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
    <ns0:Message1>
        <ns0:invoiceOrder xmlns:ns0="http://cts.com"
                   type="ABCD"
                   businessDivision="X0"
                   salesOrganization="ABC10"
                   Channel="Y0">
            <ns0:references>
                <ns0:key type="Source System">ABC</ns0:key>
                <ns0:relationship scheme="Customer" schemeAgency="APAC" role="Sell to">702390000</ns0:relationship>
                <ns0:document type="Purchase Order">AB123</ns0:document>
            </ns0:references>
            <ns0:notes>
                <ns0:note type="XY34" language="EN">package2</ns0:note>
            </ns0:notes>
            <ns0:orderLines>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">100411</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="Rebate Amount">76.25</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">140072</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="Rebate Amount">9.74</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
            </ns0:orderLines>
            <ns0:terms>
                <ns0:term type="Reason Code">Y19</ns0:term>
                <ns0:term type="Payment Terms">Y000</ns0:term>
            </ns0:terms>
            <ns0:billingInfo>
                <ns0:paymentMethod/>
            </ns0:billingInfo>
            <ns0:flags>
                <ns0:flag type="1099 Payment Flag">Y</ns0:flag>
            </ns0:flags>
            <ns0:dates>
                <ns0:date type="Proposed Invoice Date">2018-11-02</ns0:date>
            </ns0:dates>
        </ns0:invoiceOrder>
    </ns0:Message1>
</ns0:Messages>

预期输出:

<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
    <ns0:Message1>
        <ns0:invoiceOrder xmlns:ns0="http://cts.com"
                   type="ABCD"
                   businessDivision="X0"
                   salesOrganization="XYZ10"
                   Channel="Y0">
            <ns0:references>
                <ns0:key type="Source System">ABC</ns0:key>
                <ns0:relationship scheme="Customer" schemeAgency="APAC" role="Sell to">703760000</ns0:relationship>
                <ns0:document type="Purchase Order">AB123</ns0:document>
            </ns0:references>
            <ns0:notes>
                <ns0:note type="XY67" language="EN">package1</ns0:note>
            </ns0:notes>
            <ns0:orderLines>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">100411</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="credit Amount">274.50</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">140072</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="credit Amount">58.41</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
            </ns0:orderLines>
            <ns0:terms>
                <ns0:term type="Reason Code">D19</ns0:term>
                <ns0:term type="Payment Terms">Y000</ns0:term>
            </ns0:terms>
            <ns0:billingInfo>
                <ns0:paymentMethod/>
            </ns0:billingInfo>
            <ns0:flags>
                <ns0:flag type="1099 Payment Flag">Y</ns0:flag>
            </ns0:flags>
            <ns0:dates>
                <ns0:date type="Proposed Invoice Date">2018-11-02</ns0:date>
            </ns0:dates>
        </ns0:invoiceOrder>
        <ns0:invoiceOrder xmlns:ns0="http://cts.com"
                   type="ABCD"
                   businessDivision="X0"
                   salesOrganization="ABC10"
                   Channel="Y0">
            <ns0:references>
                <ns0:key type="Source System">ABC</ns0:key>
                <ns0:relationship scheme="Customer" schemeAgency="APAC" role="Sell to">702390000</ns0:relationship>
                <ns0:document type="Purchase Order">AB123</ns0:document>
            </ns0:references>
            <ns0:notes>
                <ns0:note type="XY34" language="EN">package2</ns0:note>
            </ns0:notes>
            <ns0:orderLines>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">100411</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="Rebate Amount">76.25</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
                <ns0:orderLine>
                    <ns0:identifiers>
                        <ns0:identifier scheme="Item" schemeAgency="ABC">140072</ns0:identifier>
                    </ns0:identifiers>
                    <ns0:amounts>
                        <ns0:amount type="Rebate Amount">9.74</ns0:amount>
                    </ns0:amounts>
                </ns0:orderLine>
            </ns0:orderLines>
            <ns0:terms>
                <ns0:term type="Reason Code">Y19</ns0:term>
                <ns0:term type="Payment Terms">Y000</ns0:term>
            </ns0:terms>
            <ns0:billingInfo>
                <ns0:paymentMethod/>
            </ns0:billingInfo>
            <ns0:flags>
                <ns0:flag type="1099 Payment Flag">Y</ns0:flag>
            </ns0:flags>
            <ns0:dates>
                <ns0:date type="Proposed Invoice Date">2018-11-02</ns0:date>
            </ns0:dates>
        </ns0:invoiceOrder>
    </ns0:Message1>
</ns0:Messages>

只希望在XML输出(如上所述)中有一次开始和结束标记,以便XML格式正确并符合我的要求

开始标签:

<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
    <ns0:Message1>

对应的结束标签:

</ns0:Message1>
</ns0:Messages>

任何帮助将不胜感激。

0 个答案:

没有答案