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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T08:28:33+00:00 2026-06-08T08:28:33+00:00

I had a problem in xsl:for-each-group and it was very nicely solved in here

  • 0

I had a problem in xsl:for-each-group and it was very nicely solved in here. Now I have some other problem. I have like this as input.

<?xml version="1.0" encoding="UTF-8"?>

    <body>
       <p name ="section">this is section</p>
       <p name="h-title" other="main">Introduction</p>
       <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <p name="h2-title" other=" other-h2">XSLT</p>
       <p name="">
          <p1 name="bold"> XSLT is used to write stylesheets.</p1>
       </p>
       <p name="h2-title " name="other-h2">XQuery</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name="h3-title" name="other-h3">XQuery and stylesheets</p>
       <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
       <p name ="section">this is section</p>
       <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <p name="h2-title " other=" other-h2">XSLT</p>
       <p name ="section">this is section</section>
       <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <p name="h2-title " other=" other-h2">XSLT</p>
    </body>

Now my wanted output is this

<?xml version="1.0" encoding="UTF-8"?>
<body>
   <p name="h-title " other="main">Introduction</p>
   <section>
   <p name ="section">this is section</p>
   <h1>
      <p name="h1-title " other="other-h1"> XSLT and XQuery </p>
      <h2>
         <p name="h2-title " other="other-h2">XSLT</p>
         <p name="">
            <p1 name="bold">XSLT is used to write stylesheets.
            </p1>
         </p>
      </h2>
      <h2>
         <p name="h2-title " other="other-h2"> XQuery is used to query XMLdatabases    
         </p>
         <p name="">
            <p name="bold"> XQuery is used to query XML databases.</p>
         </p>
         <h3>
            <p name="h3-title " name="other-h3">XQuery and stylesheets</p>
            <p name="">
            <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        </h3>
      </h2>
</h1>
</section>
<section>
<p name ="section">this is section</p>
<h1>
            <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <h2>   
            <p name="h2"-title other=" other-h2">XSLT</p>
       </h2>
</h1>
</section>
<section>
<p name ="section">this is section</p>
<h1>
            <p name="h1-title " other="other-h1">XSLT and XQuery</p>
       <h2>   
            <p name="h2"-title other=" other-h2">XSLT</p>
       </h2>
</h1>
</section>
</body>

My used stylesheet(not working properly)

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:mf="http://example.com/mf"
      exclude-result-prefixes="xs mf">

    <xsl:param name="prefix" as="xs:string" select="'h'"/>
    <xsl:param name="suffix" as="xs:string" select="'-title'"/>

    <xsl:output method="html" version="4.0" indent="yes"/>

    <xsl:function name="mf:group" as="node()*">
      <xsl:param name="items" as="node()*"/>
      <xsl:param name="level" as="xs:integer"/>
      <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix,$level, $suffix)]">
        <xsl:choose>
          <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
            <xsl:apply-templates select="current-group()"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="h{$level}">
              <xsl:apply-templates select="."/>
              <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
            </xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:function>

    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:template>

    <xsl:template match="body" name ="myTemplate">
      <xsl:copy>
        <xsl:sequence select="mf:group(*, 1)"/>
      </xsl:copy>
    </xsl:template>

   <xsl:template match="body[@name='section']">
        <section>
            <xsl:call-template name="myTemplate"/>
        </section>
    </xsl:template>    
    </xsl:stylesheet>

But this is not correct. Things like <p name ="section">this is section</p> appear widely. not only that, there are few others like this. If someone please show me how to handle with section I will be able to do that for others too. Please tell me how to do this correctly.

ADDED

<body>
<intro>
<p></p>
<p></p>
</intro>

<section>
<para>
</para>
<h1></h2>
<h2></h2>
</section>

<section>
<para>
</para>
<h1></h2>
<h2></h2>
</section>

<sumary>
</summary>
</body>

what I did

<xsl:for-each-group select="*" group-starting-with="p[@name = 'intro']">
            <intro>
                    <xsl:apply-templates select="current()"/>
            </intro>
</xsl:for-each-group>
  • 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-08T08:28:34+00:00Added an answer on June 8, 2026 at 8:28 am

    I think you mainly want another grouping step; the stylesheet

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:mf="http://example.com/mf"
      exclude-result-prefixes="xs mf">
    
    <xsl:param name="prefix" as="xs:string" select="'h'"/>
    <xsl:param name="suffix" as="xs:string" select="'-title'"/>
    
    <xsl:output method="html" version="4.0" indent="yes"/>
    
    <xsl:function name="mf:group" as="node()*">
      <xsl:param name="items" as="node()*"/>
      <xsl:param name="level" as="xs:integer"/>
      <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix, $level, $suffix)]">
        <xsl:choose>
          <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
            <xsl:apply-templates select="current-group()"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="h{$level}">
              <xsl:apply-templates select="."/>
              <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
            </xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:function>
    
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="body">
      <xsl:copy>
        <xsl:for-each-group select="*" group-starting-with="p[@name = 'section']">
          <section>
            <xsl:sequence select="mf:group(current-group(), 1)"/>
          </section>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    transforms the corrected input

    <?xml version="1.0" encoding="UTF-8"?>
    
        <body>
           <p name ="section">this is section</p>
           <p name="h-title" other="main">Introduction</p>
           <p name="h1-title" other="other-h1">XSLT and XQuery</p>
           <p name="h2-title" other=" other-h2">XSLT</p>
           <p name="">
              <p1 name="bold"> XSLT is used to write stylesheets.</p1>
           </p>
           <p name="h2-title" other="other-h2">XQuery</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
           <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
           <p name ="section">this is section</p>
           <p name="h1-title" other="other-h1">XSLT and XQuery</p>
           <p name="h2-title" other=" other-h2">XSLT</p>
           <p name ="section">this is section</p>
           <p name="h1-title" other="other-h1">XSLT and XQuery</p>
           <p name="h2-title" other=" other-h2">XSLT</p>
        </body>
    

    into the result

    <body>
       <section>
          <p name="section">this is section</p>
          <p name="h-title" other="main">Introduction</p>
          <h1>
             <p name="h1-title" other="other-h1">XSLT and XQuery</p>
             <h2>
                <p name="h2-title" other=" other-h2">XSLT</p>
                <p name="">
    
                   <p1 name="bold"> XSLT is used to write stylesheets.</p1>
    
                </p>
             </h2>
             <h2>
                <p name="h2-title" other="other-h2">XQuery</p>
                <p name="">
    
                   <p1 name="bold"> XQuery is used to query XML databases.</p1>
    
                </p>
                <h3>
                   <p name="h3-title" other="other-h3">XQuery and stylesheets</p>
                   <p name="">
    
                      <p1 name="bold"> XQuery is used to query XML databases.</p1>
    
                   </p>
                </h3>
             </h2>
          </h1>
       </section>
       <section>
          <p name="section">this is section</p>
          <h1>
             <p name="h1-title" other="other-h1">XSLT and XQuery</p>
             <h2>
                <p name="h2-title" other=" other-h2">XSLT</p>
             </h2>
          </h1>
       </section>
       <section>
          <p name="section">this is section</p>
          <h1>
             <p name="h1-title" other="other-h1">XSLT and XQuery</p>
             <h2>
                <p name="h2-title" other=" other-h2">XSLT</p>
             </h2>
          </h1>
       </section>
    </body>
    

    That has the structure you posted I think, with the exception of the <p name="h-title" other="main">Introduction</p> element being inside a section while your posted example moved it to the top. I am not sure what are the rules for doing that so I have not tried to implement that. Please clarify whether you simply want to move that single element to the top and not apply the grouping to it or whether there are more complex rules to exempt certain elements.

    [edit]In case you simply want to move all p[@name = 't-title'] to the top and not group them then the following adaption of above stylesheet should do the job:

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:mf="http://example.com/mf"
      exclude-result-prefixes="xs mf">
    
    <xsl:param name="prefix" as="xs:string" select="'h'"/>
    <xsl:param name="suffix" as="xs:string" select="'-title'"/>
    
    <xsl:output method="html" version="4.0" indent="yes"/>
    
    <xsl:function name="mf:group" as="node()*">
      <xsl:param name="items" as="node()*"/>
      <xsl:param name="level" as="xs:integer"/>
      <xsl:for-each-group select="$items" group-starting-with="p[@name = concat($prefix, $level, $suffix)]">
        <xsl:choose>
          <xsl:when test="not(self::p[@name = concat($prefix, $level, $suffix)])">
            <xsl:apply-templates select="current-group()"/>
          </xsl:when>
          <xsl:otherwise>
            <xsl:element name="h{$level}">
              <xsl:apply-templates select="."/>
              <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
            </xsl:element>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:function>
    
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="body">
      <xsl:copy>
        <xsl:apply-templates select="p[@name = 'h-title']"/>
        <xsl:for-each-group select="* except p[@name = 'h-title']" group-starting-with="p[@name = 'section']">
          <section>
            <xsl:sequence select="mf:group(current-group(), 1)"/>
          </section>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Had a problem with the recursive conflictCheck() method. That seems fine now. I have
I had this problem some time ago and I gave up but lately it
I had some problem to load Class object with this code: <html> <head> <meta
I had a problem with some JavaScript functions that had me scratching my head
I had this problem before and can't for life of me remember how to
Something I have always had problem with is structuring my web applications. When I
i had a problem with a NSMutableURLRequest, i want to translate this curl -b
_ I had problem with installing the mod_wsgi.I have found the solution as below
I had problem in my other code and huge, but I had made one
I had problem even in choosing a title for this question. Please feel free

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.