我对将 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.
关于xml - 如何在带有 XSLT 的 XML 文档中获取根元素的标签名称?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/368668/