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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:58:55+00:00 2026-05-15T20:58:55+00:00

I have the following XML: <assessment> <section> <item> <attributes> <variables> <variable> <variable_name value=MORTIMER/> </variable>

  • 0

I have the following XML:

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

I have the following XSLT to process that XML:

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

 <xsl:key name="kValueByVal" match="item//variables//variable_name" 
          use="@value"/>

 <xsl:template match="assessment">
     <xsl:for-each select="
      .//item//variables//variable_name/@value
      ">
        <xsl:value-of select=
        "concat(., ' ', count(key('kValueByVal', .)), '&#xA;')"/>
         <br/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

It outputs the following, which is almost what I want:

MORTIMER 2
FRED 1
MORTIMER 2

It lists each of the variable_names and how many times each occurs. The only problem is that it gives this count once for each time the variable_name occurs instead of only once.

This is what I want it to output:

MORTIMER 2
FRED 1

How do I modify the XSLT code to give me that? Note that we’re using XSLT 1.0.

The following solution, which seems like it should work, outputs nothing:

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

 <xsl:key name="kValueByVal" match="item//variables//variable_name"
          use="@value"/>

 <xsl:template match="assessment">
     <xsl:for-each select=".//item//variables//variable_name/@value[generate-id()
                                                                    =
                                                                    generate-id(key('kValueByVal',.)[1])]">
        <xsl:value-of select=
        "concat(., ' ', count(key('kValueByVal', .)), '&#xA;')"/>
         <br/>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>
  • 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-15T20:58:56+00:00Added an answer on May 15, 2026 at 8:58 pm

    You really need to understand how the Muenchian grouping works, otherwise you’d be asking variations of the same questions forever.

    Do read Jeni Tennison’s tutorial.

    Here is a solution for your latest question:

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

    When this transformation is performed on the provided XML document, the wanted result is produced:

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

Sidebar

Related Questions

I have XML like this: <assessment> <variables> <variable> <attributes> <variable_name value=FRED/> </attributes> </variable> </variables>
I have the following XML: <assessment> <section> <item name=Item1/> <item name=Item2/> <item name=Item3> <attributes>
So I have the following XML chain: $xml->assessment->outcomes_processing->outcomes->decvar->attributes()->cutvalue XML example In Some XML-files $xml->assessment->outcomes_processing->outcomes->...
I have following XML in this.treeSpec variable. <root> <item id=9PQ0 pathId=08Z specId=9PQ0 rel=MainRequest> <content>
I have following xml and I want to fetch the value of node which
I have the following XML :- <inventory> <item> <name_element> <!--some structure--> </name_element> <some_element1>val1</some_element1> <some_element2>val2</some_element2>
I have the following XML layout for an item that I put in my
I have following XML: <Library> <Item> <Field Name=Name>smooth</Field> <Field Name=File Type>mid</Field> <Field Name=File Size>60026</Field>
I have the following XML: <InterSection criteria=Microsoft-ASP>value</Intersection> <InterSection criteria=Microsoft-MVC>value</Intersection> <InterSection criteria=HP-MVC>value</Intersection> I need to
I have the following xml string: <report> <item id=4219 Co=6063 LastName=Doe FirstName=John/> <item id=2571

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.