我试图将模式位置和前缀'ns1‘添加到XML中的所有元素中。XSLT可以很好地插入模式位置,但无法为每个元素分配前缀。样本输入
**<?xml version="1.0" encoding="UTF-8"?>
<ns1:StandardBusinessDocument xmlns:ns1="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
<ns1:StandardBusinessDocumentHeader>
<HeaderVersion>1.0</HeaderVersion>
<Sender>
<ns1:Identifier Authority="EAN.UCC">1234567890</ns1:Identifier>
</Sender>
<Receiver>
<ns1:Identifier Authority="EAN.UCC">BUILDLINK</ns1:Identifier>
</Receiver>
<DocumentIdentification>
<ns1:Standard>EAN.UCC</ns1:Standard>
<ns1:TypeVersion>2.4</ns1:TypeVersion>
<ns1:InstanceIdentifier>0448431457</ns1:InstanceIdentifier>
<ns1:Type>Invoice</ns1:Type>
<ns1:CreationDateAndTime>2012-10-18</ns1:CreationDateAndTime>
</DocumentIdentification>
</ns1:StandardBusinessDocumentHeader>**成为
**<?xml version="1.0" encoding="UTF-8"?>
<ns1:StandardBusinessDocument xmlns:ns1="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
xmlns:eanucc="urn:ean.ucc:2"
xmlns:pay="urn:ean.ucc:pay:2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader ../Schemas/sbdh/StandardBusinessDocumentHeader.xsd urn:ean.ucc:2 ../Schemas/InvoiceProxy.xsd">
<ns1:StandardBusinessDocumentHeader>
<ns1:HeaderVersion>1.0</ns1:HeaderVersion>
<ns1:Sender>
<ns1:Identifier Authority="EAN.UCC">1234567890</ns1:Identifier>
</ns1:Sender>
<ns1:Receiver>
<ns1:Identifier Authority="EAN.UCC">BUILDLINK</ns1:Identifier>
</ns1:Receiver>
<ns1:DocumentIdentification>
<ns1:Standard>EAN.UCC</ns1:Standard>
<ns1:TypeVersion>2.4</ns1:TypeVersion>
<ns1:InstanceIdentifier>0448431457</ns1:InstanceIdentifier>
<ns1:Type>Invoice</ns1:Type>
<ns1:CreationDateAndTime>2022-07-21T12:00:00.000+08:00</ns1:CreationDateAndTime>
</ns1:DocumentIdentification>
</ns1:StandardBusinessDocumentHeader>**XSLT -
**<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/">
<ns1:StandardBusinessDocument xmlns:ns1="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader"
xmlns:eanucc="urn:ean.ucc:2"
xmlns:pay="urn:ean.ucc:pay:2"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader ../Schemas/sbdh/StandardBusinessDocumentHeader.xsd urn:ean.ucc:2 ../Schemas/InvoiceProxy.xsd">
<xsl:copy-of select="//ns1:StandardBusinessDocument"/>
</ns1:StandardBusinessDocument>
</xsl:template>
</xsl:stylesheet>**你能指点我吗。谢谢
发布于 2022-08-18 08:37:24
首先,不是“添加前缀”,而是将元素名称更改为不同的名称空间。
第二,xsl:copy-of不作修改地复制元素(除了有一个选项可以删除未使用的命名空间)。如果要重命名元素,则需要在遇到每个元素时对每个元素进行完全递归下降遍历。
<xsl:template match="*">
<xsl:element name="ns1:{local-name()}" namespace="http://www.unece.org/cefact/namespaces/StandardBusinessDocumentHeader">
<xsl:copy-of select="@*"/>
<xsl:apply-templates/>
</xsl:element>
</xsl:template>https://stackoverflow.com/questions/73398589
复制相似问题