unr
unr

Reputation: 13

count all distinct attributes with xslt

I have an xsl document with some attributes and i have to count the number of unique attributes in this document by their name.

for example xml:

<Collection>
  <item>
    <Id attr1="value1">123</Id>
    <property>u1</property>
  </item>  
  <item>
    <Id>1234</Id>
    <property>u2</property>
  </item>
  <item attr1="value11">
    <Id>12345</Id>
    <property>u3</property>
  </item>
  <item attr2="value2">
    <Id>123456</Id>
    <property attr3="value3">u4</property>
  </item>
</Collection>

There are 4 attributes, but attr1 is duplicated, so the answers is 3. I'm interested to do it with xslt 1.0.

Now i have something like this, but a can't make a counter

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:key name="node" match="@*" use="local-name()"/>

<xsl:template match="/">
<xsl:for-each select="//@*"> 
    <xsl:variable name="name" select="local-name()"/>
    <xsl:if test="generate-id(.) = generate-id(key('node', $name))">
       <!-- increment counter? -->
    </xsl:if>
</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

thanks

Upvotes: 1

Views: 935

Answers (1)

David Carlisle
David Carlisle

Reputation: 5652

   <xsl:value-of select="count(//@*[generate-id(.) = generate-id(key('node', local-name()))])"/>

Upvotes: 2

Related Questions