【问题标题】:Using XSL to show inner XML pretty printed使用 XSL 显示漂亮的内部 XML
【发布时间】:2018-05-17 23:45:06
【问题描述】:

我有一个 XML 文件,其中一个元素中有一个 XML 字符串:

<Container>
<Object>
    <Metadata>
        <Fields>
            <Field Label="Contents">
                <Value>&lt;Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:cur="urn:oasis:names:tc:ubl:codelist:CurrencyCode:1:0" xmlns:sdt="urn:oasis:names:tc:ubl:SpecializedDatatypes:1:0" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"&gt;   &lt;cbc:UBLVersionID xmlns=""&gt;2.1&lt;/cbc:UBLVersionID&gt;   &lt;cbc:CustomizationID xmlns=""&gt;urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.difi.no:ehf:faktura:ver2.0&lt;/cbc:CustomizationID&gt;   &lt;cbc:ProfileID xmlns=""&gt;urn:www.cenbii.eu:profile:bii04:ver2.0&lt;/cbc:ProfileID&gt;   &lt;cbc:ID xmlns=""&gt;821576&lt;/cbc:ID&gt;   &lt;cbc:IssueDate xmlns=""&gt;2018-04-30&lt;/cbc:IssueDate&gt;   &lt;cbc:InvoiceTypeCode xmlns="" listAgencyID="6" listID="UNCL1001"&gt;380&lt;/cbc:InvoiceTypeCode&gt;   &lt;cbc:TaxPointDate xmlns=""&gt;2018-04-30&lt;/cbc:TaxPointDate&gt;   &lt;cbc:DocumentCurrencyCode xmlns="" listAgencyID="6" listID="ISO4217"&gt;NOK&lt;/cbc:DocumentCurrencyCode&gt;   &lt;cbc:AccountingCost xmlns=""&gt;2007&lt;/cbc:AccountingCost&gt;   &lt;cac:ContractDocumentReference xmlns=""&gt;     &lt;cbc:ID&gt;2007&lt;/cbc:ID&gt;   &lt;/cac:ContractDocumentReference&gt;   &lt;/Invoice&gt;</Value>
            </Field>
        </Fields>
    </Metadata>
</Object>

我使用 XSLT 从该字符串生成有效的 XML:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" encoding="utf-8"/>
<xsl:template match="/">
    <xsl:value-of select="/Container/Object/Metadata/Fields/Field[@Label='Contents']/Value" disable-output-escaping="yes" />
</xsl:template>

<?xml version="1.0" encoding="utf-8"?><Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2" xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2" xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2" xmlns:ccts="urn:un:unece:uncefact:documentation:2" xmlns:cur="urn:oasis:names:tc:ubl:codelist:CurrencyCode:1:0" xmlns:sdt="urn:oasis:names:tc:ubl:SpecializedDatatypes:1:0" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2" xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">   <cbc:UBLVersionID xmlns="">2.1</cbc:UBLVersionID>   <cbc:CustomizationID xmlns="">urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.difi.no:ehf:faktura:ver2.0</cbc:CustomizationID>   <cbc:ProfileID xmlns="">urn:www.cenbii.eu:profile:bii04:ver2.0</cbc:ProfileID>   <cbc:ID xmlns="">821576</cbc:ID>   <cbc:IssueDate xmlns="">2018-04-30</cbc:IssueDate>   <cbc:InvoiceTypeCode xmlns="" listAgencyID="6" listID="UNCL1001">380</cbc:InvoiceTypeCode>   <cbc:TaxPointDate xmlns="">2018-04-30</cbc:TaxPointDate>   <cbc:DocumentCurrencyCode xmlns="" listAgencyID="6" listID="ISO4217">NOK</cbc:DocumentCurrencyCode>   <cbc:AccountingCost xmlns="">2007</cbc:AccountingCost>   <cac:ContractDocumentReference xmlns="">     <cbc:ID>2007</cbc:ID>   </cac:ContractDocumentReference>   </Invoice>

但由于这实际上仍然只是一个字符串,因此结果打印得并不好。有什么方法可以让我在一次操作中获取内部 XML 并将其格式化为 XML 文件?我需要 Saxon 解析器的 XSLT 2.0 解决方案。

【问题讨论】:

  • 底部 XML 有效。根元素是 Invoice。

标签: xml xslt saxon pretty-print


【解决方案1】:

如果您使用 Saxon 9.8,则可以使用 XSLT 3 和 parse-xml 函数 https://www.w3.org/TR/xpath-functions/#func-parse-xml

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="3.0">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <xsl:sequence select="parse-xml(/Container/Object/Metadata/Fields/Field[@Label='Contents']/Value)"/>
  </xsl:template>

</xsl:stylesheet>

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

对于 Saxon 的早期版本,您需要像 PE 或 EE 这样的商业版本,并以类似的方式使用像 http://saxonica.com/html/documentation9.7/functions/saxon/parse.html 这样的扩展功能。

当我从命令行针对您的输入样本运行 Saxon 9.6.0.9 EE 时(添加了结束标记以使其格式良好),结果是

<?xml version="1.0" encoding="UTF-8"?>
<Invoice xmlns="urn:oasis:names:specification:ubl:schema:xsd:Invoice-2"
         xmlns:ext="urn:oasis:names:specification:ubl:schema:xsd:CommonExtensionComponents-2"
         xmlns:qdt="urn:oasis:names:specification:ubl:schema:xsd:QualifiedDatatypes-2"
         xmlns:ccts="urn:un:unece:uncefact:documentation:2"
         xmlns:cur="urn:oasis:names:tc:ubl:codelist:CurrencyCode:1:0"
         xmlns:sdt="urn:oasis:names:tc:ubl:SpecializedDatatypes:1:0"
         xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2"
         xmlns:udt="urn:un:unece:uncefact:data:specification:UnqualifiedDataTypesSchemaModule:2"
         xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2"
         xmlns:xsd="http://www.w3.org/2001/XMLSchema"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <cbc:UBLVersionID xmlns="">2.1</cbc:UBLVersionID>
   <cbc:CustomizationID xmlns="">urn:www.cenbii.eu:transaction:biitrns010:ver2.0:extended:urn:www.peppol.eu:bis:peppol4a:ver2.0:extended:urn:www.difi.no:ehf:faktura:ver2.0</cbc:CustomizationID>
   <cbc:ProfileID xmlns="">urn:www.cenbii.eu:profile:bii04:ver2.0</cbc:ProfileID>
   <cbc:ID xmlns="">821576</cbc:ID>
   <cbc:IssueDate xmlns="">2018-04-30</cbc:IssueDate>
   <cbc:InvoiceTypeCode xmlns="" listAgencyID="6" listID="UNCL1001">380</cbc:InvoiceTypeCode>
   <cbc:TaxPointDate xmlns="">2018-04-30</cbc:TaxPointDate>
   <cbc:DocumentCurrencyCode xmlns="" listAgencyID="6" listID="ISO4217">NOK</cbc:DocumentCurrencyCode>
   <cbc:AccountingCost xmlns="">2007</cbc:AccountingCost>
   <cac:ContractDocumentReference xmlns="">
      <cbc:ID>2007</cbc:ID>
   </cac:ContractDocumentReference>
</Invoice>

所以输出是缩进的。

【讨论】:

  • 不幸的是,我们使用的 Saxon 库是 v. 9.6.0.4。它将允许我使用您的 XSLT,但结果仍然只是一个长字符串。
  • 那就是 9.6.0.4 HE :(
  • 我希望 9.6 他会给出一个错误。除了升级到 9.8 HE 或使用 9.6 来运行两个转换,第一个输出和序列化元素,第二个然后消耗并输出结果缩进,我没有看到用 9.6 HE 解决这个问题的方法。跨度>
猜你喜欢
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多