How to add extra root nodes for not well formed XML structure?

我们两清 提交于 2019-12-13 03:48:19

问题


We have the below not well formed input XML which is coming from source system.

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

I can make the above xml as well formed, if I can add the below tags to the above input.

Start tags :

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

Corresponding end tags:

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

How can I write the XSLT logic, so that the below output is generated?

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

I have tried many ways but as the input is not well formed XML, I am having issues generating the above output.


回答1:


You can do that in XSLT 3 as supported by Saxon 9.8 or 9.9 or Altova 2017/2018/2019 using the XPath 3 parse-xml-fragment function, you could for instance pass in the fragment you have as a string parameter:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all"
    version="3.0">

    <xsl:param name="fragment-string" as="xs:string"><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<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>]]></xsl:param>

    <xsl:output indent="yes"/>

    <xsl:template match="/" name="xsl:initial-template">
        <ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
            <ns0:Message1>
                <xsl:sequence select="parse-xml-fragment($fragment-string)"/>
            </ns0:Message1>
        </ns0:Messages>
    </xsl:template>

</xsl:stylesheet>

https://xsltfiddle.liberty-development.net/bFDb2Dd

Of course in XSLT 2/3 you can also load the other document with the fragment using unparsed-text e.g.

<xsl:param name="fragment-uri" as="xs:string">fragment.xml</xsl:param>

<xsl:param name="fragment-string" as="xs:string" select="unparsed-text($fragment-uri)"/>

Example at https://xsltfiddle.liberty-development.net/bFDb2Dd/1.

On the XML level with external entities supported you could use a document with your wanted wrapper elements that include your fragment as an external entity, example at http://xsltransform.hikmatu.com/jyH9rLR or https://xsltfiddle.liberty-development.net/eiZQaGf, you would declare the wrapper document as

<!DOCTYPE ns0:Messages [
  <!ENTITY external-fragment SYSTEM "fragment.xml">
]>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
    <ns0:Message1>&external-fragment;</ns0:Message1>
</ns0:Messages>


来源:https://stackoverflow.com/questions/53124256/how-to-add-extra-root-nodes-for-not-well-formed-xml-structure

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!