xml – XSLT:如何获取所有使用的命名空间的列表

前端之家收集整理的这篇文章主要介绍了xml – XSLT:如何获取所有使用的命名空间的列表前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在编写一个XSLT 1.0样式表,将多命名空间 XML文档转换为HTML.在结果HTML的某个地方,我想列出文档中出现的所有命名空间.

这可能吗

我想到了像

  1. <xsl:for-each select="//*|//@*">
  2. <xsl:value-of select="namespace-uri(.)" />
  3. </xsl:for-each>

但是我当然会收到重复的数据.所以我必须过滤,我已经打印了.

递归调用模板可以工作,但是我不能围绕如何覆盖所有元素.

访问// @ xmlns:*直接不起作用,因为无法通过XPath访问(不允许将任何前缀绑定到xmlns:namespace).

另一个没有扩展功能
  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:template match="*">
  3. <xsl:param name="pNamespaces" select="'&#xA;'"/>
  4. <xsl:variable name="vNamespaces">
  5. <xsl:variable name="vMyNamespaces">
  6. <xsl:value-of select="$pNamespaces"/>
  7. <xsl:for-each select="namespace::*
  8. [not(contains(
  9. $pNamespaces,concat('&#xA;',.,'&#xA;')))]">
  10. <xsl:value-of select="concat(.,'&#xA;')"/>
  11. </xsl:for-each>
  12. </xsl:variable>
  13. <xsl:variable name="vChildsNamespaces">
  14. <xsl:apply-templates select="*[1]">
  15. <xsl:with-param name="pNamespaces"
  16. select="$vMyNamespaces"/>
  17. </xsl:apply-templates>
  18. </xsl:variable>
  19. <xsl:value-of select="concat(substring($vMyNamespaces,1 div not(*)),substring($vChildsNamespaces,1 div boolean(*)))"/>
  20. </xsl:variable>
  21. <xsl:variable name="vFollowNamespaces">
  22. <xsl:apply-templates select="following-sibling::*[1]">
  23. <xsl:with-param name="pNamespaces" select="$vNamespaces"/>
  24. </xsl:apply-templates>
  25. </xsl:variable>
  26. <xsl:value-of
  27. select="concat(substring($vNamespaces,1 div not(following-sibling::*)),substring($vFollowNamespaces,1 div boolean(following-sibling::*)))"/>
  28. </xsl:template>
  29. </xsl:stylesheet>

输出(使用Dimitre的输入样本):

  1. http://www.w3.org/XML/1998/namespace
  2. mynamespace
  3. mynamespace2
  4. mynamespace3

编辑:此XPath表达式:

  1. //*/namespace::*[not(. = ../../namespace::*|preceding::*/namespace::*)]

作为证明,此样式表:

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="text"/>
  3. <xsl:template match="/">
  4. <xsl:for-each select="//*/namespace::*
  5. [not(. = ../../namespace::*|
  6. preceding::*/namespace::*)]">
  7. <xsl:value-of select="concat(.,'&#xA;')"/>
  8. </xsl:for-each>
  9. </xsl:template>
  10. </xsl:stylesheet>

输出

  1. http://www.w3.org/XML/1998/namespace
  2. mynamespace
  3. mynamespace2
  4. mynamespace3

编辑4:与双程转换相同的效率.

这个样式表:

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="text"/>
  3. <xsl:key name="kElemByNSURI"
  4. match="*[namespace::*[not(. = ../../namespace::*)]]"
  5. use="namespace::*[not(. = ../../namespace::*)]"/>
  6. <xsl:template match="/">
  7. <xsl:for-each select=
  8. "//namespace::*[not(. = ../../namespace::*)]
  9. [count(..|key('kElemByNSURI',.)[1])=1]">
  10. <xsl:value-of select="concat(.,'&#xA;')"/>
  11. </xsl:for-each>
  12. </xsl:template>
  13. </xsl:stylesheet>

输出

  1. http://www.w3.org/XML/1998/namespace
  2. mynamespace
  3. mynamespace2
  4. mynamespace3

编辑5:当您处理没有命名空间ax实现的XSLT处理器(像TransforMiix)时,您只能提取与此样式表实际使用的命名空间:

  1. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  2. <xsl:output method="text"/>
  3. <xsl:key name="kElemByNSURI" match="*|@*" use="namespace-uri()"/>
  4. <xsl:template match="/">
  5. <xsl:for-each select=
  6. "(//*|//@*)[namespace-uri()!='']
  7. [count(.|key('kElemByNSURI',namespace-uri())[1])=1]">
  8. <xsl:value-of select="concat(namespace-uri(),'&#xA;')"/>
  9. </xsl:for-each>
  10. </xsl:template>
  11. </xsl:stylesheet>

TransforMix输出

  1. mynamespace2

猜你在找的XML相关文章