提问人:Kratos 提问时间:9/18/2023 最后编辑:Michael KayKratos 更新时间:9/18/2023 访问量:33
如何使用 XSLT 1.0 更改 xml 根命名空间
How to change xml root namespace with XSLT 1.0
问:
我是 XSLT 映射的新手,我有一个需要更改根命名空间的任务。
这是我的示例 XML
<ns1:ShipStatusResponse xmlns:ns1="http://test.com/pi/VN/ABC/Invoice/10">
<httpcode>200</httpcode>
<httpstatus>OK</httpstatus>
<httpresult>test</httpresult>
</ns1:ShipStatusResponse>
我预期的输出 xml 将像
<ns1:ShipStatusResponse xmlns:ns1="http://test.com/pi/ERP/FICO/Invoice/10">
<httpcode>200</httpcode>
<httpstatus>OK</httpstatus>
<httpresult>test</httpresult>
</ns1:ShipStatusResponse>
答:
0赞
Michael Kay
9/18/2023
#1
您基本上希望 (a) 标识模板默认复制内容不变,以及 (b) 规则,例如
<xsl:template match="ns1:ShipStatusResponse" xmlns:ns1="http://test.com/pi/VN/ABC/Invoice/10">
<xs:element name="ns1:{local-name()}" namespace="http://test.com/pi/ERP/FICO/Invoice/10">
<xsl:apply-templates/>
</xs:element>
</xsl:template>
您可能很难让它使用正确的命名空间前缀,因为 XSLT 1.0 不保证结果文档中使用的命名空间前缀(在以后的版本中会说更多)。
0赞
michael.hor257k
9/18/2023
#2
我建议:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="http://test.com/pi/VN/ABC/Invoice/10"
xmlns:ns1="http://test.com/pi/ERP/FICO/Invoice/10"
exclude-result-prefixes="old">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<!-- rename root element -->
<xsl:template match="/old:ShipStatusResponse">
<ns1:ShipStatusResponse>
<xsl:apply-templates/>
</ns1:ShipStatusResponse>
</xsl:template>
<!-- copy all other elements, without copying namespaces -->
<xsl:template match="*">
<xsl:element name="{name()}">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
请注意,这假定输入 XML 中没有属性。
评论