xml 是否有 XSLT name-of 元素?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/585261/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-06 12:23:07  来源:igfitidea点击:

Is there an XSLT name-of element?

xmlxsltxpath

提问by Robert Gould

In XSLT there is the

在 XSLT 中有

<xsl:value-of select="expression"/>

to get the value of an element, but is there something to select the tag-name of the element?

获取元素的值,但是有什么可以选择元素的标签名吗?

In a situation like this:

在这样的情况下:

<person>
  <!-- required stuff -->
  <name>Robert</name>
  <!-- optional stuff, free form for future extension. 
       Using XMLSchema's xsd:any -->
  <profession>programmer</profession>
  <hobby>photography</hobby>
</person>

<xsl:for-each select="person">
   <xsl:tag-of select="."/> : <xsl:value-of select="."/>
</xsl:for-each>

To get output like this:

要获得这样的输出:

name : Robert
profession : programmer
hobby : photography
name : Robert
profession : programmer
hobby : photography

Of course the above XSLT won't compile because

当然上面的 XSLT 不会编译,因为

 <xsl:tag-of select="expression"/>

doesn't exist. But how could this be done?

不存在。但这怎么可能呢?

回答by SO User

This will give you the current element name (tag name)

这将为您提供当前元素名称(标签名称)

<xsl:value-of select ="name(.)"/>

OP-Edit:This will also do the trick:

OP-Edit:这也能解决问题:

<xsl:value-of select ="local-name()"/>

回答by Dimitre Novatchev

Nobody did point the subtle difference in the semantics of the functions name()and local-name().

没有人指出函数name()local-name().

  • name(someNode)returns the full name of the node, and that includes the prefix and colon in case the node is an element or an attribute.
  • local-name(someNode)returns only the local name of the node, and that doesn't include the prefix and colon in case the node is an element or an attribute.
  • name(someNode)返回节点的全名,如果节点是元素或属性,则包括前缀和冒号。
  • local-name(someNode)仅返回节点的本地名称,如果节点是元素或属性,则不包括前缀和冒号。

Therefore, in situations where a name may belong to two different namespaces, one must use the name()function in order for these names to be still distinguished.

因此,在名称可能属于两个不同名称空间的情况下,必须使用该name()函数才能区分这些名称。

And, BTW, it is possible to specify both functions without any argument:

而且,顺便说一句,可以在没有任何参数的情况下指定两个函数

name()is an abbreviation for name(.)

name()是缩写 name(.)

local-name()is an abbreviation for local-name(.)

local-name()是缩写 local-name(.)

Finally, do remember that not only elements and attributes have names, these two functions can also be used on PIs and on these they are identical).

最后,请记住,不仅元素和属性有名称,这两个函数也可以用于 PI,并且它们是相同的)。

回答by Ray Lu

<xsl:for-each select="person">
  <xsl:for-each select="*">
    <xsl:value-of select="local-name()"/> : <xsl:value-of select="."/>
  </xsl:for-each>  
</xsl:for-each>

回答by Tim

For those interested, there is no:

对于那些有兴趣的人,没有:

<xsl:tag-of select="."/>

However you can re-create the tag/element by going:

但是,您可以通过以下方式重新创建标签/元素:

<xsl:element name="{local-name()}">
  <xsl:value-of select="substring(.,1,3)"/>
</xsl:element>

This is useful in an xslt template that for example handles formatting data values for lots of different elements. When you don't know the name of the element being worked on and you can still output the same element, and modify the value if need be.

这在 xslt 模板中很有用,例如处理许多不同元素的格式化数据值。当您不知道正在处理的元素的名称时,您仍然可以输出相同的元素,并在需要时修改值。

回答by Rowland Shaw

<xsl:value-of select="name(.)" /> : <xsl:value-of select="."/>