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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T11:36:21+00:00 2026-06-13T11:36:21+00:00

XSLT XML Question. Looking into a simple transformation. I have simple index xml input.

  • 0

XSLT XML Question.

Looking into a simple transformation. I have simple index xml input. I have to output the the first and last element with a for each chapter. AS shown below. any help will be much appreciated.

Regards JJ

Input

<book>
  <page number="1">Chapter01</page> 
  <page number="2">Chapter01</page> 
  <page number="3">Chapter01</page> 
  <page number="4">Chapter01</page> 
  <page number="5">Chapter01</page> 
  <page number="6">Chapter01</page> 
  <page number="7">Chapter02</page> 
  <page number="8">Chapter02</page> 
  <page number="9">Chapter02</page> 
  <page number="10">Chapter02</page> 
  <page number="11">Chapter02</page> 
  <page number="12">Chapter02</page> 
  <page number="13">Chapter03</page> 
  <page number="14">Chapter03</page> 
  <page number="15">Chapter03</page> 
  <page number="16">Chapter03</page> 
  <page number="17">Chapter03</page> 
  <page number="18">Chapter03</page> 
 </book>

Output

<book>
  <page number="1">Chapter01</page>  
  <page number="6">Chapter01</page> 
  <page number="7">Chapter02</page> 
  <page number="12">Chapter02</page> 
  <page number="13">Chapter03</page> 
  <page number="18">Chapter03</page> 
</book>
  • 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-13T11:36:22+00:00Added an answer on June 13, 2026 at 11:36 am

    Updated to take advantage of a cleaner predicate – thanks to @SeanB.Durkin.

    I. XSLT 1.0 Solution

    When this XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
      <xsl:output omit-xml-declaration="no" indent="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:key name="kChapters" match="page" use="." />
    
      <xsl:template match="/*">
        <book>
          <xsl:apply-templates 
            select="page[generate-id() = generate-id(key('kChapters', .)[1])]" />
        </book>
      </xsl:template>
    
      <xsl:template match="page">
        <xsl:copy-of select=".|key('kChapters', .)[last()]" />
      </xsl:template>
    
    </xsl:stylesheet>
    

    …is applied to the original XML:

    <book>
      <page number="1">Chapter01</page>
      <page number="2">Chapter01</page>
      <page number="3">Chapter01</page>
      <page number="4">Chapter01</page>
      <page number="5">Chapter01</page>
      <page number="6">Chapter01</page>
      <page number="7">Chapter02</page>
      <page number="8">Chapter02</page>
      <page number="9">Chapter02</page>
      <page number="10">Chapter02</page>
      <page number="11">Chapter02</page>
      <page number="12">Chapter02</page>
      <page number="13">Chapter03</page>
      <page number="14">Chapter03</page>
      <page number="15">Chapter03</page>
      <page number="16">Chapter03</page>
      <page number="17">Chapter03</page>
      <page number="18">Chapter03</page>
    </book>
    

    …the desired result is produced:

    <?xml version="1.0"?>
    <book>
      <page number="1">Chapter01</page>
      <page number="6">Chapter01</page>
      <page number="7">Chapter02</page>
      <page number="12">Chapter02</page>
      <page number="13">Chapter03</page>
      <page number="18">Chapter03</page>
    </book>
    

    Explanation:

    • Note the proper use of Muenchian Grouping to determine the unique <page> elements by their value.
    • For each unique <page> element, two <page> elements are copied: the first and last from the group that contains the unique value.

    II. XSLT 2.0 Solution

    Note that in XSLT 2.0, the solution becomes even simpler.

    When this XSLT:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
      <xsl:output omit-xml-declaration="no" indent="yes" />
      <xsl:strip-space elements="*" />
    
      <xsl:template match="/*">
        <book>
          <xsl:for-each-group select="page" group-by=".">
            <xsl:copy-of select=".|current-group()[last()]" />
          </xsl:for-each-group>
        </book>
      </xsl:template>
    
    </xsl:stylesheet>
    

    …is applied to the original XML, the same desired result is produced:

    <?xml version="1.0"?>
    <book>
      <page number="1">Chapter01</page>
      <page number="6">Chapter01</page>
      <page number="7">Chapter02</page>
      <page number="12">Chapter02</page>
      <page number="13">Chapter03</page>
      <page number="18">Chapter03</page>
    </book>
    

    Explanation:

    • The same methodology is applied, but instead of Muenchian Grouping, XSLT 2.0’s for-each-group element and current-group() instruction are used.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im looking for a XSLT (1.0) stylesheet. I have input similar to this: <?xml
I have written a C# code for triggering an XML to XML (XSLT) transformation.
we have XSLT to transform XML into HTML in XHTML 1.0 Strict in XSLT
Next question: I have InputDoc xml document and xslt file in order to transform
XSLT XML Question. I have another variation of my previous question. I have to
XSLT XML Question. I have a variation of my previous question. I have to
I am currently doing XML and XSLT and I have a question I have
After an XSLT transformation I keep getting leading question marks that (seem to) have
I've got a question about copying some xml, with xslt. I have xml that
I have the following XSLT question: Suppose I have this XML <items> <item> <type>dog</type>

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.