如何为格式不正确的 XML 结构添加额外的根节点? [英] How to add extra root nodes for not well formed XML structure?
问题描述
我们有来自源系统的以下格式不正确的输入 XML.
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>
如果我可以将下面的标签添加到上面的输入中,我可以使上面的 xml 格式良好.
I can make the above xml as well formed, if I can add the below tags to the above input.
开始标签:
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
对应的结束标签:
</ns0:Message1>
</ns0:Messages>
如何编写 XSLT 逻辑,以便生成以下输出?
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>
我尝试了很多方法,但由于输入的 XML 格式不正确,我在生成上述输出时遇到了问题.
I have tried many ways but as the input is not well formed XML, I am having issues generating the above output.
推荐答案
您可以使用 XPath 3 parse-xml-fragment 在 Saxon 9.8 或 9.9 或 Altova 2017/2018/2019 支持的 XSLT 3 中执行此操作
函数,例如,您可以将您拥有的片段作为字符串参数传入:
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
当然,在 XSLT 2/3 中,您也可以使用 unparsed-text
加载带有片段的其他文档,例如
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)"/>
示例在 https://xsltfiddle.liberty-development.net/bFDb2Dd/1一>.
在支持外部实体的 XML 级别上,您可以使用带有所需包装元素的文档,其中包含您的片段作为外部实体,例如 http://xsltransform.hikmatu.com/jyH9rLR 或 https://xsltfiddle.liberty-development.net/eiZQaGf,您可以将包装文档声明为
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>
这篇关于如何为格式不正确的 XML 结构添加额外的根节点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!