如何使用xslt传输命名空间

时间:2018-04-13 07:01:45

标签: xslt

是否可以从xml更改名称空间,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<ns1:Message xmlns:ns1="http://www.rof.com/schemas/">
<Head Release="1.2.0" TransDate="2018-08-07T23:39:00" SplitLevel=""/>
<Data>
  <Business BusinessId="ROF">
  </Business>
</Data>
</ns1:Message>

以下使用XSLT?

<?xml version="1.0" encoding="ISO-8859-1"?>
<rof:Message xmlns:rof="http://www.rof.com/schemas/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.rof.com/schemas/
http://www.rof.com/schemas/4.9.5/2001/message.xsd">
<Head Release="1.2.0" TransDate="2018-08-07T23:39:00" SplitLevel=""/>
<Data>
  <Business BusinessId="ROF">
  </Business>
</Data>
</rof:Message>

1 个答案:

答案 0 :(得分:0)

您可以使用身份转换。

XSLT 2.0解决方案:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xmlns:rof="http://www.rof.com/schemas/"
    exclude-result-prefixes="xs"
    version="2.0">

    <xsl:template match="node()|@*">
        <xsl:copy copy-namespaces="no">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/*">
        <xsl:element name="rof:{local-name()}">
            <xsl:apply-templates select="node()|@*"/>
        </xsl:element>
    </xsl:template>
</xsl:stylesheet>

编辑:添加修复程序只是更改根节点的命名空间,删除了复制不必要的命名空间节点。