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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:24:04+00:00 2026-06-09T21:24:04+00:00

In this question I asked if it is possible to prevent output of consecutive

  • 0

In this question I asked if it is possible to prevent output of consecutive identical values using XSLT. In this question I extend the input data so that the Muenchian Grouping method for XSLT 1.0 syntax will be clearer.

Is it possible to match only the first occurrence of a value in an XML doc using XSL transforms?

I’d like to print out every value inside f2,f3 but only the first instance of the contents of f1, retaining order.

XML Data

    <doc> 
    <datum>
        <f1>One</f1>
        <f2>Monday</f2>
        <f3>January</f3>
      </datum>
      <datum>
        <f1>One</f1>
        <f2>Tuesday</f2>
        <f3>February</f3>
      </datum>
      <datum>
        <f1>Two</f1>
        <f2>Wednesday</f2>
        <f3>March</f3>
    </datum>
    </doc>

Output

      One
      -Monday-January
      -Tuesday-February
      Two
      -Wednesday-March

This is the solution that was proposed for XSLT 1.0 by Dimitre Novatchev for an input data set containing the fields f1 and f2.

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

 <xsl:key name="kDatumByF1" match="datum" use="f1"/>

 <xsl:template match=
   "datum[generate-id() = generate-id(key('kDatumByF1', f1)[1])]">
   <xsl:value-of select="concat('&#xA;', f1)"/>
   <xsl:apply-templates select="key('kDatumByF1', f1)/f2" mode="inGroup"/>
 </xsl:template>

 <xsl:template match="f2" mode="inGroup">
   - <xsl:value-of select="."/>
 </xsl:template>
 <xsl:template match="text()"/>
</xsl:stylesheet>

It works quite well but I am unsure of the syntax to extend to include the field ‘f3’ in the output.

  • 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-09T21:24:06+00:00Added an answer on June 9, 2026 at 9:24 pm

    This transformation:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <xsl:output method="text"/>
         <xsl:strip-space elements="*"/>
         <xsl:key name="kDatumByF1" match="datum" use="f1"/>
    
         <xsl:template match=
           "datum[generate-id() = generate-id(key('kDatumByF1', f1)[1])]">
           <xsl:value-of select="concat(f1, '&#xA;')"/>
           <xsl:apply-templates mode="inGroup"
               select="key('kDatumByF1', f1)/*[self::f2 or self::f3]" />
         </xsl:template>
    
         <xsl:template match="f2|f3" mode="inGroup">
           <xsl:value-of select="concat('- ',., ' ')"/>
           <xsl:if test="not(following-sibling::*[self::f2 or self::f3])">
            <xsl:text>&#xA;</xsl:text>
           </xsl:if>
         </xsl:template>
         <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <doc>
        <datum>
            <f1>One</f1>
            <f2>Monday</f2>
            <f3>January</f3>
        </datum>
        <datum>
            <f1>One</f1>
            <f2>Tuesday</f2>
            <f3>February</f3>
        </datum>
        <datum>
            <f1>Two</f1>
            <f2>Wednesday</f2>
            <f3>March</f3>
        </datum>
    </doc>
    

    produces the wanted, correct result:

    One
    - Monday - January 
    - Tuesday - February 
    Two
    - Wednesday - March 
    

    Explanation:

    Proper use of the Muenchian grouping method.

    Do note:

    This solution produces the wanted result even if f2 and f3 can come in any order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

It's possible that this question has already been asked, but I can't seem to
This question was asked quite some time ago, and while it covers possible solutions
Edit (2012-04-12): Since this question was asked it is now possible (as of jQuery
Possible Duplicate: NoClassDefFoundError - Eclipse and Android I'm seeing this question is getting asked
Possible Duplicate: optimal algorithm for finding unique divisors I have asked this question before,
Either this this question isn't asked often (possible), everyone knows how to do it
Like this question already asked, I'd like to initialize a container using STL where
I re-asked this question here because the people at ServerFault deemed that this is
I keep getting this question asked in interviews: Write a program without using main()
Possible Duplicate: Hide facebook app from search Okay, I know that this question was

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.