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

  • Home
  • SEARCH
  • 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 228759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:42:33+00:00 2026-05-11T19:42:33+00:00

Apologies if this is a very simple question; I don’t use XSLT very much

  • 0

Apologies if this is a very simple question; I don’t use XSLT very much and I can’t find much advice on the web, as there is lots of pollution in search results!

I have an XML document in the following form. Its main purpose is to be reformatted in a few ways by XSLT for display in a couple of different formats.

<desk>
<drawer>
    <contents>pencils</contents>
    <quantity>2</quantity>
</drawer>
<drawer>
    <contents>pens</contents>
    <quantity>15</quantity>
</drawer>
<drawer>
    <contents>pencils</contents>
    <quantity>3</quantity>
</drawer>
<drawer>
    <contents>rulers</contents>
    <quantity>2</quantity>
</drawer>
</desk>

I’d like to extract from the xml two pieces of information: i) the average quantity; ii) the most frequently encountered content by number of appearances in the xml (i.e. “pencils” because it appears twice rather than “pens” because it has the largest quantity). The idea is that this can be piped into a very simple shell script. I therefore thought that the easiest way of getting this information would be to write couple of short xsl style-sheets and then use xsltproc to get the data.

The first piece of information seems straight-forward. The heart of the style-sheet would be this line:

<xsl:value-of select="(sum(drawer/quantity)) div (count(drawer))" />

but I’m a bit stuck by the second.

I think I can use something like this for loop through a list of each individual content:

<xsl:for-each select="drawer[not(contents = preceding-sibling::drawer/contents)]" />

but I’m not quite sure how then to count the number of elements which have $current_contents and the value of their content element. Nor can I see an easy way of then sorting by results so I can get the name of the most frequently encountered value of contents.

I have a feeling this is easier in XSLT 2.0 with its various group-by options, but unfortunately, xsltproc doesn’t seem to support that. Any help would be gratefully received.

Many thanks,

Jacob

  • 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-11T19:42:33+00:00Added an answer on May 11, 2026 at 7:42 pm

    As with a great many problems solved in XSLT, I think your answer here is muenchian grouping. Group by whatever data you’re interested in, a for-each against that will let you use xsl:sort and then do whatever you need to with the first result.

    Untested, top-of-head, might-be-a-cleaner-way code:

    <xsl:key name="average" match="desk/drawer/contents" use="text()"/>
    
    <xsl:template match="/">
        <xsl:for-each select="desk/drawer/contents[generate-id() = generate-id(key('average',text())[1])]">     
            <xsl:sort select="count(//desk/drawer/contents[text()=current()])"  order="descending"/>
            <xsl:if test="position()=1">
                Most common value: "<xsl:value-of select="current()"/>" (<xsl:value-of select="count(//desk/drawer/contents[text()=current()])"/>)
            </xsl:if>       
        </xsl:for-each>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.