xslt – XSL – 复制元素,但删除未使用的命名空间

前端之家收集整理的这篇文章主要介绍了xslt – XSL – 复制元素,但删除未使用的命名空间前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一些XML声明一个仅用于属性的命名空间,如下所示:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <a xmlns:x="http://tempuri.com">
  3. <b>
  4. <c x:att="true"/>
  5. <d>hello</d>
  6. </b>
  7. </a>

我想使用XSL来创建所选节点及其值的副本 – 摆脱属性。所以我想要的输出是:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <b>
  3. <c />
  4. <d>hello</d>
  5. </b>

我有一些XSL几乎这样做,但我似乎不能阻止它将命名空间声明放在输出的顶级元素中。我的XSL是:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  3. <xsl:template match="/">
  4. <xsl:apply-templates select="/a/b"/>
  5. </xsl:template>
  6.  
  7. <xsl:template match="node()">
  8. <xsl:copy>
  9. <xsl:apply-templates select="node()"/>
  10. </xsl:copy>
  11. </xsl:template>
  12. </xsl:stylesheet>

输出的第一个元素是< b xmlns:x =“http://tempuri.com”>而不是< b>。我尝试在XSL中声明命名空间,并将前缀放在exclude-result-prefixes列表中,但这似乎没有任何效果。我究竟做错了什么?

更新:我发现通过在XSL中声明命名空间并使用extension-element-prefixes属性可以工作,但是这似乎不正确!我想我可以使用这个,但我想知道为什么排除结果前缀不起作用!

更新:实际上,这个extension-element-prefixes解决方案似乎只适用于XMLSpy的内置XSLT引擎,而不适用于MSXML。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
  3. xmlns:x="http://tempuri.com">
  4. <xsl:template match="/">
  5. <xsl:apply-templates select="/a/b"/>
  6. </xsl:template>
  7.  
  8. <xsl:template match="*">
  9. <xsl:element name="{local-name(.)}">
  10. <xsl:apply-templates/>
  11. </xsl:element>
  12. </xsl:template>
  13.  
  14. <xsl:template match="@*">
  15. <xsl:copy/>
  16. </xsl:template>
  17.  
  18. <!-- This empty template is not needed.
  19. Neither is the xmlns declaration above:
  20. <xsl:template match="@x:*"/> -->
  21. </xsl:stylesheet>

我发现了一个解释here

Michael Kay wrote: exclude-result-prefixes only affects the namespaces copied from the stylesheet by a literal result element,it doesn’t affect copying of namespaces from source documents.

猜你在找的XML相关文章