I am writing an XPath expression to count unique child attribues. Using the following xPath expression I could fetch all the child attributes along with which are not unique:
//*[count(*)=0]
I need an XPath expression to return me all the unique attributes and the count of number of unique attributes
Eg: XML file
<details>
<Employee>
<EmpNo>10</EmpNo>
<EmpName>TestName</EmpName>
<Address>
<Address1>market</Address1>
<Address2>motel</Address2>
<Street/>
</Address>
</Employee>
<Employee>
<EmpNo>20</EmpNo>
<EmpName>TestName2</EmpName>
<Address>
<Address1>school</Address1>
<Address2>playground</Address2>
<Street>
<StreetName>TestStreet2</StreetName>
<StreetCode>200</StreetCode>
</Street>
</Address>
</Employee>
Expected output:
<!-- Unique element's count -->
<data>6</data>
<!-- Unique Element Names -->
<data>EmpNo</data>
<data>EmpName</data>
<data>Address1</data>
<data>Address2</data>
<data>StreetName</data>
<data>StreetCode</data>
<!-- Unique Element values -->
<!-- Data Set 1 -->
<data>10</data>
<data>TestName</data>
<data>market</data>
<data>motel</data>
<data>null</data>
<data>null</data>
<!-- Data Set 2 -->
<data>20</data>
<data>TestName2</data>
<data>school</data>
<data>playground</data>
<data>TestStreet2</data>
<data>200</data>
Thanks.
This XSLT 1.0 stylesheet
produces (line-breaks may reproduce differently for you):
Notes:
//*[count(*)=0]and//*[not(*)]are equal. The latter is nicer.<xsl:key>and Muenchian Grouping to figure out the unique element names among the descendants of<Employee>$fields, does two things:name().not( key('kFields', name())/* ). Otherwise<data>Street</data>would show up in the output.