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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:48:22+00:00 2026-06-08T03:48:22+00:00

I have an XML with records, some records are associated with each other and,

  • 0

I have an XML with records, some records are associated with each other and, as such, I want to group them together in the output.

XML:

<Records>
   <Record id="1" group="10" />
   <Record id="2" group="20" />
   <Record id="3" group="20" />
   <Record id="4" group="20" />
</Records>

Currently, I display

<span>1</span><span>2</span><span>3</span><span>4</span>

What I would like to display is (based on the records having the same group)

<span>1</span><span>2-4</span>

I have looked into using preceding-sibling::Record/@group to see if the grouping has changed between iterations of Records but am struggling to figure out how to achieve the 2-4 grouping I require.

Here is what I have so far, interspersed with some comments to illustrate what I am trying to do:

<xsl:for-each select="Records/Record">
   <xsl:if test="@group != preceding-sibling::Record/@group">
      <!-- obviously here we need 2-4...somehow? -->
      <span><xsl:value-of="@id" /></span>
   </xsl:if>
</xsl:for-each>
  • 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-08T03:48:26+00:00Added an answer on June 8, 2026 at 3:48 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kFollowing" match="Record"
      use="generate-id(preceding-sibling::*
                             [not(@group = current()/@group)
                              ][1])"/>
    
     <xsl:template match="/*">
           <xsl:apply-templates mode="makeGroup" select=
           "Record[not(@group = preceding-sibling::*[1]/@group)]"/>
     </xsl:template>
    
     <xsl:template match="Record" mode="makeGroup">
      <xsl:variable name="vGroup"
        select="key('kFollowing', generate-id(preceding-sibling::*[1]))"/>
      <span>
       <xsl:value-of select="$vGroup[1]/@id"/>
       <xsl:if test="$vGroup[2]">
        <xsl:value-of select="concat('-', $vGroup[last()]/@id)"/>
       </xsl:if>
      </span>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <Records>
        <Record id="1" group="10" />
        <Record id="2" group="20" />
        <Record id="3" group="20" />
        <Record id="4" group="20" />
    </Records>
    

    produces the wanted, correct result:

    <span>1</span><span>2-4</span>
    

    Explanation:

    1. This is positional grouping using a key to define all adjacent Record elements that comprise a group.

    2. This is an efficient (sublinear) algorithm because keys are used. Algorithms using a siblings axis are typically O(N^2) — quadratical in time complexity and can be too slow if the total number of siblings N is big.

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

Sidebar

Related Questions

I have some XML which contains records and sub records, like this: <data> <record
I have a short xml I want to get some attribs out of it,
I have a SQL Server column of type XML containing some records with more
I have a set of records, stored as XML files, where the XML files
I have an XML document which looks something like this: <meadinkent> <record> <comp_div>MENSWEAR</comp_div> <sty_ret_type>ACCESSORIES</sty_ret_type>
I have xml file with such structure: ... <outer> ... <inner/> ... </outer> ...
I have XML in the following form that I want to parse with PHP
I have some XML data retrieved from a web service that I use to
I have a dataset (called dataSet below) with a single table and some records
I have a table that contains some meta data in an XML field. For

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.