使用 XSLT 获取 XML 中的标记名称/属性名称 [英] Get tag name/attribute name in XML using XSLT

查看:40
本文介绍了使用 XSLT 获取 XML 中的标记名称/属性名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 XML 中获取标签名称和属性名称的方法是什么?

What's the way to get a tag name and attribute name in XML?

如果我有这样的 XML 文件:

If I have a XML file like this:

<a>
<apple color="red"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</a>

我的 XSLT 文件的一部分如下:

And part of my XSLT file is as below:

<xsl:element name="AAA">
<???>
</xsl:element>

那么我应该在 ??? 部分写什么,以便我可以得到这样的输出:

So what should I write in the ??? part so I can get the output like this:

对于标签名称:

<AAA>apple</AAA>
<AAA>banana</AAA>
<AAA>sugar</AAA>
<AAA>cat</AAA>

对于属性名称:

<AAA>color</AAA>
<AAA>color</AAA>
<AAA>taste</AAA>
<AAA>size</AAA>

推荐答案

标签名称:

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

第一个 (!) 属性的属性名称.如果你有更多的属性,你就必须选择不同的方法

Attribute name of the first (!) attribute. If you have more attributes, you'd have to choose a different approach

<xsl:value-of select="name(@*[1])"/>

然后,这两个表达式都将在与您的输入元素匹配的模板中使用.例如

Both expressions would then be used in a template matching your input elements. e.g.

<xsl:template match="*">
  <xsl:element name="AAA">
    <!-- ... -->
  </xsl:element>
</xsl:template>

这篇关于使用 XSLT 获取 XML 中的标记名称/属性名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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