使用 XSLT 获取 XML 中的标记名称/属性名称
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8522245/
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
Get tag name/attribute name in XML using XSLT
提问by ahliang1412
What's the way to get a tag name and attribute name in XML?
在 XML 中获取标签名称和属性名称的方法是什么?
If I have a XML file like this:
如果我有这样的 XML 文件:
<a>
<apple color="red"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</a>
And part of my XSLT file is as below:
我的 XSLT 文件的一部分如下:
<xsl:element name="AAA">
<???>
</xsl:element>
So what should I write in the ???part so I can get the output like this:
那么我应该在这???部分写些什么才能得到这样的输出:
For tag name:
对于标签名称:
<AAA>apple</AAA>
<AAA>banana</AAA>
<AAA>sugar</AAA>
<AAA>cat</AAA>
For attribute name:
对于属性名称:
<AAA>color</AAA>
<AAA>color</AAA>
<AAA>taste</AAA>
<AAA>size</AAA>
回答by Lukas Eder
Tag name:
标签名称:
<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>
回答by Wayne
Output the name of an element or attribute using one of name()or local-name():
使用name()或local-name()之一输出元素或属性的名称:
<xsl:value-of select="name()"/>
<xsl:value-of select="local-name()"/>
Assume this document:
假设这个文件:
<root>
<apple color="red"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</root>
Then this stylesheet:
然后这个样式表:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="/">
<root>
<xsl:apply-templates select="/*/*"/>
<xsl:apply-templates select="/*/*/@*"/>
</root>
</xsl:template>
<xsl:template match="*|@*">
<AAA><xsl:value-of select="local-name()"/></AAA>
</xsl:template>
</xsl:stylesheet>
Produces:
产生:
<root>
<AAA>apple</AAA>
<AAA>banana</AAA>
<AAA>sugar</AAA>
<AAA>cat</AAA>
<AAA>color</AAA>
<AAA>color</AAA>
<AAA>taste</AAA>
<AAA>size</AAA>
</root>
Notice that both elements and attributes are handled by the same template.
请注意,元素和属性都由同一个模板处理。
回答by Dimitre Novatchev
This is probably one of the shortest solutions:
这可能是最短的解决方案之一:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*/*|@*">
<AAA>
<xsl:value-of select="name()"/>
</AAA>
<xsl:apply-templates select="@*"/>
</xsl:template>
</xsl:stylesheet>
when this transformation is applied on the following XML document(your fragment wrapped into a top element):
当此转换应用于以下 XML 文档(您的片段包装到顶部元素中)时:
<things>
<apple color="red"/>
<banana color="yellow"/>
<sugar taste="sweet"/>
<cat size="small"/>
</things>
the wanted, correct result is produced:
产生了想要的、正确的结果:
<AAA>apple</AAA>
<AAA>color</AAA>
<AAA>banana</AAA>
<AAA>color</AAA>
<AAA>sugar</AAA>
<AAA>taste</AAA>
<AAA>cat</AAA>
<AAA>size</AAA>

