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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:24:56+00:00 2026-05-11T17:24:56+00:00

Given the following xml: <container> <val>2</val> <id>1</id> </container> <container> <val>2</val> <id>2</id> </container> <container> <val>2</val>

  • 0

Given the following xml:

<container>
    <val>2</val>
    <id>1</id>
</container>
<container>
    <val>2</val>
    <id>2</id>
</container>
<container>
    <val>2</val>
    <id>3</id>
</container>
<container>
    <val>4</val>
    <id>1</id>
</container>
<container>
    <val>4</val>
    <id>2</id>
</container>
<container>
    <val>4</val>
    <id>3</id>
</container>

I’d like to return something like

2 - 1
2 - 3
4 - 1
4 - 3

Using a nodeset I’ve been able to get the last occurrence via:

exsl:node-set($list)/container[not(val = following::val)]

but I can’t figure out how to get the first one.

  • 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-11T17:24:57+00:00Added an answer on May 11, 2026 at 5:24 pm

    To get the first and the last occurrence (document order) in each “<val>” group, you can use an <xsl:key> like this:

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
      <xsl:output method="text" />
    
      <xsl:key name="ContainerGroupByVal" match="container" use="val" />
    
      <xsl:variable name="ContainerGroupFirstLast" select="//container[
        generate-id() = generate-id(key('ContainerGroupByVal', val)[1])
        or
        generate-id() = generate-id(key('ContainerGroupByVal', val)[last()])
      ]" />
    
      <xsl:template match="/">
        <xsl:for-each select="$ContainerGroupFirstLast">
          <xsl:value-of select="val" />
          <xsl:text> - </xsl:text>
          <xsl:value-of select="id" />
          <xsl:value-of select="'&#10;'" /><!-- LF -->
        </xsl:for-each>
      </xsl:template>
    
    </xsl:stylesheet>
    

    EDIT #1: A bit of an explanation since this might not be obvious right away:

    • The <xsl:key> returns all <container> nodes having a given <val>. You use the key() function to query it.
    • The <xsl:variable> is where it all happens. It reads as:
      • for each of the <container> nodes in the document (“//container“) check…
      • …if it has the same unique id (generate-id()) as the first node returned by key() or the last node returned by key()
      • where key('ContainerGroupByVal', val) returns the set of <container> nodes matching the current <val>
      • if the unique ids match, include the node in the selection
    • the <xsl:for-each> does the output. It could just as well be a <xsl:apply-templates>.

    EDIT #2: As Dimitre Novatchev rightfully points out in the comments, you should be wary of using the “//” XPath shorthand. If you can avoid it, by all means, do so — partly because it potentially selects nodes you don’t want, and mainly because it is slower than a more specific XPath expression. For example, if your document looks like:

    <containers>
      <container><!-- ... --></container>
      <container><!-- ... --></container>
      <container><!-- ... --></container>
    </containers>
    

    then you should use “/containers/container” or “/*/container” instead of “//container“.


    EDIT #3: An alternative syntax of the above would be:

    <xsl:stylesheet 
      version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    >
      <xsl:output method="text" />
    
      <xsl:key name="ContainerGroupByVal" match="container" use="val" />
    
      <xsl:variable name="ContainerGroupFirstLast" select="//container[
        count(
            .
          | key('ContainerGroupByVal', val)[1]
          | key('ContainerGroupByVal', val)[last()]
        ) = 2
      ]" />
    
      <xsl:template match="/">
        <xsl:for-each select="$ContainerGroupFirstLast">
          <xsl:value-of select="val" />
          <xsl:text> - </xsl:text>
          <xsl:value-of select="id" />
          <xsl:value-of select="'&#10;'" /><!-- LF -->
        </xsl:for-each>
      </xsl:template>
    
    </xsl:stylesheet>
    

    Explanation: The XPath union operator “|” combines it’s arguments into a node-set. By definition, a node-set cannot contain duplicate nodes — for example: “. | . | .” will create a node-set containing exactly one node (the current node).

    This means, if we create a union node-set from the current node (“.”), the “key(…)[1]” node and the “key(…)[last()]” node, it’s node count will be 2 if (and only if) the current node equals one of the two other nodes, in all other cases the count will be 3.

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

Sidebar

Ask A Question

Stats

  • Questions 97k
  • Answers 97k
  • 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 If you're using the standard Twitter feed web code for… May 11, 2026 at 7:29 pm
  • Editorial Team
    Editorial Team added an answer Short Answer: You just need to move your dataaccess layer… May 11, 2026 at 7:29 pm
  • Editorial Team
    Editorial Team added an answer vdhant - That's not how containers are meant to be… May 11, 2026 at 7:29 pm

Related Questions

Given the following code: var people = new List<person>(){ new person { Name =
I'm setting up a User Control driven by a XML configuration. It is easier
Is there an xpath way to find a node that has a given attribute
I'm trying to use XPath to find all elements that have an element in
I have an interesting problem with XSL. We have an XML Input file that

Trending Tags

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

Top Members

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.