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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T06:48:16+00:00 2026-05-17T06:48:16+00:00

Using XSLT, how can I wrap siblings that share the same value for an

  • 0

Using XSLT, how can I wrap siblings that share the same value for an attribute.

Lets say I need to wrap a one or more <amendment/> with the <chapter/> they belong too.
From this:

<section>
      <heading>some heading text</heading>
      <amendment num='1' chapter='1'>
            <foo/>
      </amendment>
      <amendment num='2' chapter='1'>
            <bar/>
      </amendment>
      <amendment num='3' chapter='2'>
            <baz/>
      </amendment>
      <heading>some heading text</heading>
      <amendment num='4' chapter='3'>
            <baz/>
      </amendment>
</section>

into this:

<section>
      <heading>some heading text</heading>
      <chapter num="1">
            <amendment num='1'>
                  <foo/>
            </amendment>
            <amendment num='2'>
                  <bar/>
            </amendment>
      </chapter>
      <chapter num="2">
            <amendment num='3'>
                  <baz/>
            </amendment>
      </chapter>
      <heading>some heading text</heading>
      <chapter num="3">
            <amendment num='4'>
                  <baz/>
            </amendment>
      </chapter>
</section>

Note 1: Amendments are always listed sorted by chapter in the source XML.

Note 2: Im using PHP5 with XSLT 1.0

  • 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-17T06:48:17+00:00Added an answer on May 17, 2026 at 6:48 am

    This stylesheet:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:key name="kAmendementByChapter" match="amendment" use="@chapter"/>
        <xsl:template match="node()|@*" name="identity">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
        <xsl:template match="amendment[count(.|key('kAmendementByChapter',
                                                   @chapter)[1])=1]">
            <chapter num="{@chapter}">
                <xsl:apply-templates select="key('kAmendementByChapter',@chapter)"
                                     mode="copy"/>
            </chapter>
        </xsl:template>
        <xsl:template match="amendment"/>
        <xsl:template match="amendment" mode="copy">
            <xsl:call-template name="identity"/>
        </xsl:template>
        <xsl:template match="@chapter"/>
    </xsl:stylesheet>
    

    Output:

    <section>
        <heading>some heading text</heading>
        <chapter num="1">
            <amendment num="1">
                <foo></foo>
            </amendment>
            <amendment num="2">
                <bar></bar>
            </amendment>
        </chapter>
        <chapter num="2">
            <amendment num="3">
                <baz></baz>
            </amendment>
        </chapter>
        <heading>some heading text</heading>
        <chapter num="3">
            <amendment num="4">
                <baz></baz>
            </amendment>
        </chapter>
    </section>
    

    Note: Copy all (indentity rule), grouping on @chapter.

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

Sidebar

Related Questions

Question Using XSLT 1.0, given a string with arbitrary characters how can I get
I'd like to output html controls using xslt, but I need to be able
I'm using XSLT and need to generate the doctype dynamically in the transformed output,
I am parsing a document using XSLT. Within the XSLT I am loading a
Is it possible to merge elements using XSLT. If I have the following XML
.NET allows to extend XSLT by using the so called extension object. Very handy
I want to apply an XSLT Stylesheet to an XML Document using C# and
Using PyObjC , you can use Python to write Cocoa applications for OS X.
Using ASP.NET MVC there are situations (such as form submission) that may require a
Using XSLT I'm trying to work out a way to only emit a HTML

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.