我有一个未命名空间的元素的XML文档,我想使用XSLT命名空间添加到他们。 大多数元素将在命名空间A; 一些将在命名空间B.我如何做到这一点?
Answer 1:
随着foo.xml
<foo x="1">
<bar y="2">
<baz z="3"/>
</bar>
<a-special-element n="8"/>
</foo>
和foo.xsl
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="A" >
<xsl:copy-of select="attribute::*"/>
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="a-special-element">
<B:a-special-element xmlns:B="B">
<xsl:apply-templates match="children()"/>
</B:a-special-element>
</xsl:template>
</xsl:transform>
我得到
<foo xmlns="A" x="1">
<bar y="2">
<baz z="3"/>
</bar>
<B:a-special-element xmlns:B="B"/>
</foo>
这是你在找什么?
Answer 2:
你需要为这个食谱的两个主要成分。
酱油的股票将是恒等变换 ,主味将被赋予namespace
属性xsl:element
。
下面,未经测试的代码,如果添加http://example.com/命名空间的所有元素。
<xsl:template match="*">
<xsl:element name="xmpl:{local-name()}" namespace="http://example.com/">
<xsl:apply-templates select="@*|node()"/>
</xsl:element>
</xsl:template>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
个人信息:你好,杰尼·坦尼森。 我知道你在读这一点。
Answer 3:
这是我到目前为止有:
<xsl:template match="*">
<xsl:element name="{local-name()}" namespace="A" >
<xsl:apply-templates />
</xsl:element>
</xsl:template>
<xsl:template match="a-special-element">
<B:a-special-element xmlns:B="B">
<xsl:apply-templates />
</B:a-special-element>
</xsl:template>
这几乎工程; 问题是,它不是复制属性。 从我读thusfar,XSL:元素没有办法的所有属性从原样(使用属性集不会出现削减它)复制元素。
文章来源: Add a namespace to elements