【发布时间】:2014-11-26 10:43:50
【问题描述】:
我想使用 xslt“扫描”我的 xml 文件并获取不同元素名称的列表以及它们的属性名称。
我的 XML:
<?xml version="1.0" encoding="UTF-8"?>
<dictionary>
<entry>
<form type="hyperlemma" xml:lang="cu">
<note type="editor's comment">CHECK</note>
<orth>hlE1</orth>
</form>
<form type="lemma" xml:lang="cu">
<orth>lE1</orth>
</form>
<form type="variant" xml:lang="cu">
<orth>var5</orth>
</form>
</entry>
<entry>
<form type="hyperlemma" xml:lang="cu">
<orth>hlE2</orth>
</form>
<form type="lemma" xml:lang="cu">
<orth>lE2</orth>
</form>
</entry>
</dictionary>
How to list complete XML document using XSLT 中记录了获取不同元素名称列表的方法(请参阅 Dimitre Novatchev 的回答)。
使用这个样式表
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output method="text"/>
<xsl:strip-space elements="*" />
<xsl:key name="kElemByName" match="*" use="name(.)"/>
<xsl:template match="
*[generate-id()
=
generate-id(key('kElemByName', name(.))[1])
]">
<xsl:value-of select="concat(name(.), '
')"/>
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
(正确的)输出是
dictionary
entry
form
note
orth
是否也可以获取属性名称?我想要以下输出
dictionary
entry
form type="hyperlemma" xml:lang="cu"
form type="lemma" xml:lang="cu"
form type="variant" xml:lang="cu"
note type="editor's comment"
orth
我如何做到这一点?
【问题讨论】: