如何在具有XSLT的XML文档中获取根元素的标签名称?

前端之家收集整理的这篇文章主要介绍了如何在具有XSLT的XML文档中获取根元素的标签名称?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有兴趣将xml文档中的根元素的标签名称分配给xslt变量。例如,如果文档看起来像(减去DTD):
  1. <foo xmlns="http://.....">
  2. <bar>1</bar>
  3. </foo>

我想将字符串’foo’分配给一个xslt变量。有没有办法引用?

谢谢,马特

我想你想要检索最外层XML元素的名称。这可以像下面的XSL示例一样完成:
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.  
  4. <xsl:variable name="outermostElementName" select="name(/*)" />
  5.  
  6. <xsl:template match="/">
  7. <xsl:value-of select="$outermostElementName"/>
  8. </xsl:template>
  9. </xsl:stylesheet>

请注意,XPath术语略有不同:

The top of the tree is a root node
(1.0 terminology) or document node
(2.0). This is what “/” refers to.
It’s not an element: it’s the parent
of the outermost element (and any
comments and processing instructions
that precede or follow the outermost
element). The root node has no name.

http://www.dpawson.co.uk/xsl/sect2/root.html#d9799e301

猜你在找的XML相关文章