xml – 向元素添加命名空间

前端之家收集整理的这篇文章主要介绍了xml – 向元素添加命名空间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个带有未命名空间的元素的 XML文档,我想使用XSLT来添加命名空间.大多数元素将在命名空间A中;几个将在命名空间B.我该如何做?
用foo.xml
  1. <foo x="1">
  2. <bar y="2">
  3. <baz z="3"/>
  4. </bar>
  5. <a-special-element n="8"/>
  6. </foo>

和foo.xsl

  1. <xsl:template match="*">
  2. <xsl:element name="{local-name()}" namespace="A" >
  3. <xsl:copy-of select="attribute::*"/>
  4. <xsl:apply-templates />
  5. </xsl:element>
  6. </xsl:template>
  7.  
  8. <xsl:template match="a-special-element">
  9. <B:a-special-element xmlns:B="B">
  10. <xsl:apply-templates match="children()"/>
  11. </B:a-special-element>
  12. </xsl:template>
  13.  
  14. </xsl:transform>

我得到

  1. <foo xmlns="A" x="1">
  2. <bar y="2">
  3. <baz z="3"/>
  4. </bar>
  5. <B:a-special-element xmlns:B="B"/>
  6. </foo>

这是你要找的?

猜你在找的XML相关文章