Skip to content

Instantly share code, notes, and snippets.

@alicraigmile
Created July 23, 2013 14:02
Show Gist options
  • Save alicraigmile/6062574 to your computer and use it in GitHub Desktop.
Save alicraigmile/6062574 to your computer and use it in GitHub Desktop.
Get all unique elements in a document
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml"/>
<xsl:key name="elements" match="*" use="name()"/>
<xsl:template match="/">
<xsl:for-each select="//*[generate-id(.)=generate-id(key('elements',name())[1])]">
<xsl:sort select="name()"/>
<xsl:for-each select="key('elements', name())">
<xsl:if test="position()=1">
<xsl:element name="element">
<xsl:attribute name="name">
<xsl:value-of select="name()"/>
</xsl:attribute>
<xsl:attribute name="count">
<xsl:value-of select="count(//*[name()=name(current())])"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment