【发布时间】:2018-05-17 23:45:06
【问题描述】:
我有一个 XML 文件,其中一个元素中有一个 XML 字符串:
<Container>
<Object>
<Metadata>
<Fields>
<Field Label="Contents">
<Value><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></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