如何使用 XSLT 在 XML 文档中获取根元素的标签名称? [英] How to get tag name of root element in an XML document w/ XSLT?

查看:31
本文介绍了如何使用 XSLT 在 XML 文档中获取根元素的标签名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有兴趣将 xml 文档中根元素的标记名称分配给 xslt 变量.例如,如果文档看起来像(减去 DTD):

I'm interested in assigning the tag name of the root element in an xml document to an xslt variable. For instance, if the document looked like (minus the DTD):

<foo xmlns="http://.....">
    <bar>1</bar>
</foo>

我想将字符串 'foo' 分配给一个 xslt 变量.有没有办法参考?

and I wanted to assign the string 'foo' to an xslt variable. Is there a way to reference that?

谢谢,马特

推荐答案

我认为您想检索最外层 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

这篇关于如何使用 XSLT 在 XML 文档中获取根元素的标签名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆