XPath will get unique item names

I want to use XPath to get a list of the names of all the elements that appear in an XML file. However, I do not want the names to be repeated, so the element with the same name as the previous element should not match. So far I have:

*[not(local-name() = local-name(preceding::*))]

This works well, but it spits out duplicates. Why does he spit out duplicates and how can I eliminate them? (I am using the Firefox XPath engine.)

+5
source share
3 answers

You get duplicates because your filter does not evaluate how you think.

The local-name () function returns the local name of the first node in the set of nodes.

, , - , .

, , , XPATH 1.0. XPATH 2.0, Firefox.

XSLT meunchien, , .

. - XML, (,//* ):

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"><xsl:output method="xml"/>
<xsl:key name="names" match="//*" use="local-name(.)"/>
<xsl:template match="/">
    <xsl:for-each select="//*[generate-id(.) = generate-id(key('names', local-name(.)))]">
        <!--Do something with the unique list of elements-->
    </xsl:for-each>
</xsl:template>
</xsl:stylesheet>
+5

XPath 2.0:

distinct-values(//*/name())
+6

, .

, :

var allElements = doc.select("//node()");
var distinctElementTypes = new object();
foreach (var elem in allElements) {
    distinctElementTypes[elem.name] = elem.name;
}

distinctElementTypes .

+1

All Articles