使用XSL显示内部XML相当印刷

问题描述 投票:0回答:1

我有一个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 xslt saxon pretty-print
1个回答
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>

所以输出是缩进的。

© www.soinside.com 2019 - 2024. All rights reserved.