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

标签 xml xslt xpath

我对将 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.

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

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

相关文章:

xml - 对同一个文件应用两个 xslt 转换

.net - 计算具有 2 种子节点类型的父节点 - 特别是 1 "boy"节点和 1 "girl"节点

sql - 通过sql查询读取xml

python - 从python中的标签名称查找xml文本内容

c# - 使用 C# 在 XML 中获取具有特定名称的标签

json - 使用 XSLT 1.0 版从 JSON 文件中提取信息

selenium webdriver 中的 css 或 xpath 正则表达式

c# - 使用 JSON.Net 自定义 JSON 到 XML 的转换

用于解析 XSD 模式文件的 Java API

xml - XSLT 包含错误