我有兴趣将xml文档中的根元素的标签名称分配给xslt变量。例如,如果文档看起来像(减去DTD):
- <foo xmlns="http://.....">
- <bar>1</bar>
- </foo>
我想将字符串’foo’分配给一个xslt变量。有没有办法引用?
谢谢,马特
我想你想要检索最外层XML元素的名称。这可以像下面的XSL示例一样完成:
- <?xml version="1.0" encoding="UTF-8" ?>
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:variable name="outermostElementName" select="name(/*)" />
- <xsl:template match="/">
- <xsl:value-of select="$outermostElementName"/>
- </xsl:template>
- </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.