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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:45:30+00:00 2026-05-20T10:45:30+00:00

I’m working with XSLT 1.0 from PHP and want to wrap all the sibling

  • 0

I’m working with XSLT 1.0 from PHP and want to wrap all the sibling elements after a heading (h2) into a div so I can toggle them.

The input would look like

...
<h2>Nth title</h2>
<first child>...</first child>
...
<last child>...</last child>
<h2>N+1st title</h2>
...

and the output should be

...
<h2>Nth title</h2>
<div>
  <first child>...</first child>
  ...
  <last child>...</last child>
</div>
<h2>N+1st title</h2>
...

Is there a way to do this in 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-20T10:45:31+00:00Added an answer on May 20, 2026 at 10:45 am

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kFollowing" match="node()[not(self::h2)]"
      use="generate-id(preceding-sibling::h2[1])"/>
    
     <xsl:template match="node()|@*" name="identity">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
     </xsl:template>
    
     <xsl:template match="h2">
      <xsl:call-template name="identity"/>
      <div>
       <xsl:apply-templates mode="copy"
           select="key('kFollowing', generate-id())"/>
      </div>
     </xsl:template>
    
     <xsl:template match="node()[not(self::h2)][preceding-sibling::h2]"/>
    
     <xsl:template match="node()" mode="copy">
      <xsl:call-template name="identity"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on this XML document:

    <html>
        <h2>Nth title</h2>
        <first-child>...</first-child> ... 
        <last-child>...</last-child>
        <h2>N+1st title</h2> ...
        <x/>
         <y/>
         <z/>
    </html>
    

    produces the wanted, correct result:

    <html>
       <h2>Nth title</h2>
       <div>
          <first-child>...</first-child> ... 
    
          <last-child>...</last-child>
       </div>
       <h2>N+1st title</h2>
       <div> ...
    
          <x></x>
          <y></y>
          <z></z>
       </div>
    </html>
    

    Explanation:

    1. The identity rule/template copies every node “as-is”.

    2. The identity rule is overriden for h2 elements. Here the action is to copy the h2 element and then to output a div and inside it to apply templates (in a special mode) to all nodes (that are not h2 themselves) for which this h2 element is the first preceding-sibling h2 element.

    3. The nodes to include in the previous step are conveniently defined as an <xsl:key> instruction.

    4. In order to stop the nodes that are wrapped in div to be output again by the identity rule, we provide a template matching such nodes, that simply ignores them.

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I have a text area in my form which accepts all possible characters from
Does anyone know how can I replace this 2 symbol below from the string
I am currently running into a problem where an element is coming back from
I would like to count the length of a string with PHP. The string

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.