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 921693
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T18:54:25+00:00 2026-05-15T18:54:25+00:00

I have XML like this: <assessment> <variables> <variable> <attributes> <variable_name value=FRED/> </attributes> </variable> </variables>

  • 0

I have XML like this:

<assessment>
 <variables>
  <variable>
   <attributes>
    <variable_name value="FRED"/>
   </attributes>
  </variable>
 </variables>
 <variables>
  <variable>
   <attributes>
    <variable_name value="MORTIMER"/>
   </attributes>
  </variable>
 </variables>
 <variables>
  <variable>
   <attributes>
    <variable_name value="FRED"/>
   </attributes>
  </variable>
 </variables>
</assessment>

I know that with this XSLT:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
 <xsl:output method="html"/>

 <xsl:template match="assessment">
  <xsl:for-each select=".//variables/variable/attributes/variable_name">
   <xsl:value-of select="@value"/>
   <br/>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

I can output the following:

FRED 
MORTIMER 
FRED

But what I really want to output is this:

FRED: 2 
MORTIMER: 1 

That is, I want to list the distinct elements and how many times each occurs. Note that I want the elements to appear in the order of their first appearance (which might rule out some solutions that use sorting).

How do I do this?

  • 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-05-15T18:54:26+00:00Added an answer on May 15, 2026 at 6:54 pm

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output method="text"/>
    
     <xsl:key name="kValueByVal" match="variable_name/@value"
      use="."/>
    
     <xsl:template match="/">
      <xsl:for-each select="
       /*/*/variable/attributes/variable_name/@value
                 [generate-id()
                 =
                  generate-id(key('kValueByVal', .)[1])
                 ]
       ">
         <xsl:value-of select=
         "concat(., ' ', count(key('kValueByVal', .)), '&#xA;')"/>
      </xsl:for-each>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document, produces the wanted, correct result:

    FRED 2
    MORTIMER 1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 488k
  • Answers 488k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Hook up to the DayRender event protected void SomeCalendar_DayRender(object sender,… May 16, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer The replyData argument contains the body of the response returned… May 16, 2026 at 8:49 am
  • Editorial Team
    Editorial Team added an answer I'm using your $content variable: $preg1 = preg_match_all('#<name>([^<]+)#', $content, $name_arr);… May 16, 2026 at 8:49 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have the following XML: <assessment> <section> <item> <attributes> <variables> <variable> <variable_name value=MORTIMER/> </variable>
I have XML like this: <exam> <section name=SampleSection1> <attributes> <variable_name data_type=String value=SCORE/> </attributes> <item
Suppose I have XML like this: <child_metadata> <metadata> <attributes> <metadata_key value=include/> <metadata_value value=value1/> </attributes>
I have XML like this: <assessment name=Assessment> <section name=Section1> <item name=Item1-1/> <item name=Item1-2/> <item
Suppose I have XML like this: <xsl:template match=assessment | section > . . .
I have xml like this: <configurationData> <path name='b'> <path name='a'> <setting name='s1'> ![CDATA[XXXX]] </setting>
I have xml like this <categories> <category> <Loc>India</Loc> <Loc>US</Loc> <Loc>Spain</Loc> <type>A</type> <type>B</type> <Cat>unknown</Cat> <SubCat>True</SubCat>
I have XML like this: <items> <item> <products> <product>laptop</product> <product>charger</product> </products> </item> <item> <products>
I have xml like this: <A><B>test</B><B><B>test2</B></B><B><B><B>test2</B></B></B></A> How can I get the level of each
If I have XML like this: <sample> <a:element1 xmlns:a=... /> <b:element2 xmlns:b=... /> </sample>

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.