Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8239269
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T20:05:22+00:00 2026-06-07T20:05:22+00:00

I am writing an XPath expression to count unique child attribues. Using the following

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T20:05:23+00:00Added an answer on June 7, 2026 at 8:05 pm

    This XSLT 1.0 stylesheet

    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
      <xsl:output indent="yes" omit-xml-declaration="yes" />
    
      <!-- index data fields by their element name -->
      <xsl:key 
        name  = "kFields" 
        match = "Employee//*"
        use   = "name()" 
      />
    
      <!-- store a unique list of elements (Muenchian Grouping) -->    
      <xsl:variable name="fields" select="
        /details/Employee//*[
          generate-id()
          =
          generate-id(key('kFields', name())[1])
        ][
          not(
            key('kFields', name())/*
          )
        ]
      " />
    
      <!-- main output ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <xsl:template match="/details">
        <xsl:comment> unique element count </xsl:comment>
        <data>
          <xsl:value-of select="count($fields)" />
        </data>
        <xsl:call-template name="newline" />
    
        <xsl:comment> unique element names </xsl:comment>
        <xsl:for-each select="$fields">
          <data>
            <xsl:value-of select="name()" />
          </data>
          <xsl:call-template name="newline" />
        </xsl:for-each>
    
        <xsl:comment> unique element values </xsl:comment>
        <xsl:apply-templates select="Employee" />
      </xsl:template>
    
      <!-- Employee output ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <xsl:template match="Employee">
        <xsl:variable name="this" select="." />
    
        <xsl:comment> data set <xsl:value-of select="position()" /> </xsl:comment>
        <xsl:for-each select="$fields">
          <xsl:variable 
            name="val" 
            select="$this//*[not(*) and name() = name(current())]" 
          />
          <data>
            <xsl:choose>
              <xsl:when test="normalize-space($val) != ''">
                <xsl:value-of select="$val" />
              </xsl:when>
              <xsl:otherwise>
                <xsl:text>null</xsl:text>
              </xsl:otherwise>
            </xsl:choose>
          </data>
          <xsl:call-template name="newline" />
        </xsl:for-each>
      </xsl:template>
    
      <!-- Helpers ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ -->
      <xsl:template name="newline">
        <xsl:value-of select="'&#xA;'" />
      </xsl:template>
    
    </xsl:stylesheet>
    

    produces (line-breaks may reproduce differently for you):

    <!-- unique element 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>
    

    Notes:

    • //*[count(*)=0] and //*[not(*)] are equal. The latter is nicer.
    • I’ve used an <xsl:key> and Muenchian Grouping to figure out the unique element names among the descendants of <Employee>
    • The XPath expression in the variable $fields, does two things:
      • First it uses Muenchian grouping of the elements to make them unique by name().
      • Then it checks the remaining elements. No element of the same name may have any children anywhere in the input (not( key('kFields', name())/* ). Otherwise <data>Street</data> would show up in the output.
    • Your output format is ambiguous. If there are elements that have equal names but different nesting positions, things will get messed up.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing an XPath expression in Java, and am trying to grab all elements
I was writing an XPath expression, and I had a strange error which I
Is there a way of writing an XPath expression to select the content of
I'm having trouble writing an XPath expression to select nodes that contain certain elements,
i.e. - i want to return a string yellow using something like xpath expression
We are using Soap-UI for writing some web-services tests. I put this XPath validation
So I am using writing this method, which work fine in term of looking
I have played for a while writing XPath but am unable to come up
I'm writing a template in xslt with xpath 1.0 and need to access the
While writing selenium testcases for a webapplication I'm having trouble with the xpath selector.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.