我正在使用微软的XSLT处理器(仅限1.0)
XML开头行:
<?xml version="1.0" encoding="utf-8"?>
<Header xmlns="http:\\OldNameSpace.com">
<Detail>
Run Code Online (Sandbox Code Playgroud)
使用以下XSLT模板来获取<Header>文档的元素并更改其命名空间.
<xsl:template match="*">
<xsl:element name="{name()}" xmlns="http:\\NewNameSpace.com">
<xsl:copy-of select="@*"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
果然<Header xmlns="http:\\OldNameSpace.com">进入<Header xmlns="http:\\NewNameSpace.com">
但是我现在需要为此添加第二个命名空间,以便获得以下输出:
<Header xmlns="NewNameSpace.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
Run Code Online (Sandbox Code Playgroud)
我尝试过使用:
<xsl:template match="*">
<xsl:element name="{name()}" xmlns="NewNameSpace.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xsl:copy-of select="@*"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
Run Code Online (Sandbox Code Playgroud)
但是,我仍然只获得与原始XSLT模板相同的输出.
任何人都可以告诉我为什么这是?
这种转变:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:old="http:\\OldNameSpace.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
exclude-result-prefixes="old xsi">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="pNewNamespace" select="'http:\\NewNameSpace.com'"/>
<xsl:variable name="vXsi" select="document('')/*/namespace::*[name()='xsi']"/>
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="old:*">
<xsl:element name="{local-name()}" namespace="{$pNewNamespace}">
<xsl:copy-of select="$vXsi"/>
<xsl:copy-of select="@*"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
当应用于以下XML文档时:
<Header xmlns="http:\\OldNameSpace.com">
<Detail/>
</Header>
Run Code Online (Sandbox Code Playgroud)
产生(我猜的是)想要的,正确的结果:
<Header xmlns="http:\\NewNameSpace.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Detail/>
</Header>
Run Code Online (Sandbox Code Playgroud)