使用XSL显示内部XML相当印刷
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用XSL显示内部XML相当印刷相关的知识,希望对你有一定的参考价值。
我有一个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解决方案。
答案
如果你使用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>
所以输出是缩进的。
以上是关于使用XSL显示内部XML相当印刷的主要内容,如果未能解决你的问题,请参考以下文章