且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2021-12-24 07:17:18

我认为您想检索最外层 XML 元素的名称.这可以在以下 XSL 示例中完成:

I think you want to retrieve the name of the outermost XML element. This can be done like in the following XSL sample:

<?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 术语略有不同:

Please note that there is a slight difference in XPath terminology:

树的顶部是根节点(1.0 术语)或文档节点(2.0).这就是/"所指的.它不是一个元素:它是父元素最外层元素(以及任何评论和处理说明在最外层之前或之后元素).根节点没有名字.

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