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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T19:20:14+00:00 2026-06-06T19:20:14+00:00

My XHTML input: <h1 class=section>Text</h1> <h2 class=section>More text</h2> Desired XHTML output: <div class=section> <h1

  • 0

My XHTML input:

<h1 class="section">Text</h1>
<h2 class="section">More text</h2>

Desired XHTML output:

<div class="section">
<h1 class="section">Text</h1>
<h2 class="section">More text</h2>
</div>

Therefore, the group-adjacent approach seems suitable. The code below will do the right thing with the h1 and h2 nodes, but it will erase everything else in the <body> node including the <body> tags themselves.

Clearly I’m making an error, but I’m not enough of an expert with for-each-group to understand where it is.

Thanks.

<xsl:template match="xhtml:body"> 
    <xsl:for-each-group select="xhtml:h1|xhtml:h2" group-adjacent="@class"> 
        <xsl:choose>
            <xsl:when test="current-grouping-key()='section'">
                <xsl:element name="div">
                    <xsl:attribute name="class">
                        <xsl:value-of select="current-grouping-key()"/>
                    </xsl:attribute>
                    <xsl:apply-templates select="current-group()"/> 
                </xsl:element>
            </xsl:when>
            <xsl:otherwise>
                <xsl:copy-of select="." />    
            </xsl:otherwise>
        </xsl:choose>            
    </xsl:for-each-group>  
</xsl:template>

Update: What I didn’t understand is that for-each-group essentially acts as a filter on whatever node you point it at. Therefore, if you want to preserve every subnode, the original command has to include select="*". Furthermore, the grouping rule has to ensure that every subnode will end up in a group. This means group-adjacent is not right tool for the job; group-starting-with is.

The template below separates the whole <body> of the XHTML file into groups that start with h1. (Be warned: This grouping rule relies on the assumption that an h1 is always the first subnode in the <body> of the XHTML.) Then I loop over the groups, using a conditional to look at the first two nodes in each group to see if they match my criteria. If so, I wrap them in my <div>.

I’d still be interested whether there’s a more idiomatic XSLT solution, as what I’ve done is basically write Python within XSLT.

<xsl:template match="xhtml:body"> 
    <xsl:copy>            
    <!--Divide file into groups of nodes starting with h1 tags-->
    <xsl:for-each-group select="*" group-starting-with="xhtml:h1"> 
        <xsl:choose>
            <!-- if the group starts with h1.section + h2.section -->
            <xsl:when test="current-group()[1][@class='section'] and current-group()[2][name() = 'h2'] and current-group()[2][@class = 'section']">
                <!--wrap in a div tag-->
                <div class="section">
                    <xsl:apply-templates select="current-group()[1]"/>
                    <xsl:apply-templates select="current-group()[2]"/>
                </div>                    
                <!--then process the rest of the nodes in this group normally-->
                <xsl:apply-templates select="current-group()[position()>=3]"/> 
            </xsl:when>
            <xsl:otherwise>
                <!--process normally-->
                <xsl:apply-templates select="current-group()"/> 
            </xsl:otherwise>
        </xsl:choose>
    </xsl:for-each-group> 
    </xsl:copy>  
</xsl:template>
  • 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-06T19:20:15+00:00Added an answer on June 6, 2026 at 7:20 pm

    In your update to the question, you’ve identified part of the answer. However, it’s possible to solve this kind of problem with group-adjacent. The usual pattern is to compute a grouping key of “true” for the elements to be wrapped as a group, and “false” for the others. So the coding pattern is:

    <xsl:for-each-group select="*" group-adjacent="self::h1 or self::h2">
      <xsl:choose>
        <xsl:when test="current-grouping-key()">
          <div>
            <xsl:copy-of select="current-group()"/>
          </div>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="current-group()"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:for-each-group>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I got 2 errors when validating my XHTML strict input type below: <form action=
I have the following code : <html xmlns=http://www.w3.org/1999/xhtml> <body style=height: 1336px> <input type=hidden id=loadingtime/>
I have the JQuery AutoCompletion plug installed and working on 4 input text boxes,
Guys, I need to develop a tool which would meet following requirements: Input: XHTML
I have a text input with a search buton absolute positioned over it... to
I'm developing a class for a content management system. The input content is supplied
I am using STAX parser to process every text node in a xhtml. The
I have a sample.xhtml file which has few lines of javascript. This javascript will
so i heard that XHTML is not supported by IE and so it is
Will this validate in XHTML? <span>hello<span>world</span></span>

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.