如何在XML文档w/ XSLT中获取根元素的标记名?

时间:2021-03-02 23:49:21

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):

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

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

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

我想把字符串“foo”赋给一个xslt变量。有什么方法可以参考吗?

Thanks, Matt

谢谢,马特

4 个解决方案

#1


23  

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

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

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

请注意,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.

树的顶部是根节点(1.0术语)或文档节点(2.0)。这就是"/"指的。它不是一个元素:它是最外层元素的父元素(以及在最外层元素之前或之后的任何注释和处理指令)。根节点没有名称。

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

看到http://www.dpawson.co.uk/xsl/sect2/root.html d9799e301

#2


16  

Use the XPath name() function.

使用XPath名称()函数。

One XPath expression to obtain the name of the top (not root!) element is:

获取top(而不是root!)元素名称的XPath表达式是:

       name(/*)

名称(/ *)

The name() function returns the fully-qualified name of the node, so for an element <bar:foo/> the string "bar:foo" will be returned.

函数的作用是:返回节点的全限定名,因此对于元素 ,将返回字符串“bar:foo”。

In case only the local part of the name is wanted (no prefix and ":"), then the XPath local-name() function should be used.

如果只需要名称的局部部分(没有前缀和“:”),则应该使用XPath local-name()函数。

#3


2  

Figured it out. The function name() given the parameter * will return foo.

算出来。给定参数*的函数名()将返回foo。

#4


0  

you want local-name()

你想要的本地名称()

#1


23  

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

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

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

请注意,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.

树的顶部是根节点(1.0术语)或文档节点(2.0)。这就是"/"指的。它不是一个元素:它是最外层元素的父元素(以及在最外层元素之前或之后的任何注释和处理指令)。根节点没有名称。

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

看到http://www.dpawson.co.uk/xsl/sect2/root.html d9799e301

#2


16  

Use the XPath name() function.

使用XPath名称()函数。

One XPath expression to obtain the name of the top (not root!) element is:

获取top(而不是root!)元素名称的XPath表达式是:

       name(/*)

名称(/ *)

The name() function returns the fully-qualified name of the node, so for an element <bar:foo/> the string "bar:foo" will be returned.

函数的作用是:返回节点的全限定名,因此对于元素 ,将返回字符串“bar:foo”。

In case only the local part of the name is wanted (no prefix and ":"), then the XPath local-name() function should be used.

如果只需要名称的局部部分(没有前缀和“:”),则应该使用XPath local-name()函数。

#3


2  

Figured it out. The function name() given the parameter * will return foo.

算出来。给定参数*的函数名()将返回foo。

#4


0  

you want local-name()

你想要的本地名称()