元素的命名空间,我想使这个更改使用XSLT
<hello>
<one>
<paragraph>p1</paragraph>
</one>
</hello>
这个
<x:hello y:an_attribute="a value for an_attribute" xmlns:x="some_new_namespace" xmlns:y="other_ns">
<one>
<paragraph>p1</paragraph>
</one>
</x:hello>
这是我想出了样式表:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="element_localname" select="'hello'"/>
<xsl:template match="node()">
<xsl:choose>
<xsl:when test="local-name() = $element_localname">
<xsl:element name="{$element_localname}" namespace="some_new_namespace">
<xsl:attribute name="an_attribute" namespace="other_ns">a value for an_attribute</xsl:attribute>
<xsl:apply-templates select="node()"/>
</xsl:element>
</xsl:when>
<!-- copy the rest as is -->
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates select="node()" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
但出于一些奇怪的原因,我添加到元素的属性与根元素本身具有相同的名称空间?为什么?
<ns0:hello xmlns:ns0="other_ns" ns0:an_attribute="a value for an_attribute">
<one>
<paragraph>p1</paragraph>
</one>
</ns0:hello>
谢谢您的阅读。
通过在xsltproc你的转换运行源给我'<你好的xmlns = “some_new_namespace” 的xmlns:ns_1 = “other_ns” ns_1:an_attribute =“an_attribute的值”>''hello'元素,这似乎是你想要的。不知道这是XSL 1.0还是XSL 2.0? – 2010-11-05 14:06:16
谢谢你的尝试。我正在使用xslt处理器中的eclipse wtp版本。我不知道使用什么实现。 – Luca 2010-11-05 15:02:08
好问题,+1。看到我的答案比现在接受的更简单和更短的解决方案。 :) – 2010-11-05 16:02:04