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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T22:23:42+00:00 2026-06-08T22:23:42+00:00

Here is my requirement. My sample input document is like below. (I have added

  • 0

Here is my requirement. My sample input document is like below. (I have added white lines to make it clear)

<body>
       <p name="h-title" other="main">Introduction</p>
       <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>


       <p name=""> XSLT is used to write stylesheets.</p>
    <p name="section-title" other=" other-section">XSLT</p>
    <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    <p name=""> Some text.</p>
    <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
    <p name="h1-title" other=" other-h1">XSLT</p>
    <p name=""> Some text.</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-title" other=" other-section">XSLT</p>
    <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
    <p name=""> Some text.</p>
    <p name="">
          <p1 name="bold"> XQuery is used to query XML databases.</p1>
       </p>
    <p name="h1-title" other=" other-h1">XSLT</p>
    <p name=""> Some text.</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 ="summary-title">this is summary</p>
       <p name="summary-desc " other="other-summarydesc">the summary</p>
    </body>

Now my wanted output is this.

<body>
       <p name="h-title" other="main">Introduction</p>
       <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>


       <p name=""> XSLT is used to write stylesheets.</p>

    <body-contents>
        <p name="section-title" other=" other-section">XSLT</p>
        <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        <p name=""> Some text.</p>
        <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <h1>
        <p name="h1-title" other=" other-h1">XSLT</p>
        <p name=""> Some text.</p>
        <h2>
           <p name="h2-title " name="other-h2">XQuery</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </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>
    </body-contents>
    <body-contents>
        <p name="section-title" other=" other-section">XSLT</p>
        <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        <p name=""> Some text.</p>
        <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <h1>
        <p name="h1-title" other=" other-h1">XSLT</p>
        <p name=""> Some text.</p>
        <h2>
           <p name="h2-title " name="other-h2">XQuery</p>
           <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </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>

    </body-contents>
    <body-contents>
           <p name ="summary-title">this is summary</p>
           <p name="summary-desc " other="other-summarydesc">the summary</p>
    </body-contents>
    </body>

Please help me solve this problem.

{OPTIONAL
There are restrictions like:

  • h1, h2, h3 come sequentially(that means, h3 does not come between h1 and h2)
  • lines with name=”section-title” should come before name=”section-desc”
  • h1, h2, h3, etc. should come after section-desc.

I solved the problem of h1,h2,h3,etc here. I know this is very had. Any help is great.

The transformation should not happen if these rules violate.
}

  • 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-08T22:23:44+00:00Added an answer on June 8, 2026 at 10:23 pm

    Here is my adaption of the previously posted stylesheet, it simply does an additional group-starting-with before calling the recursive function grouping the levels. I realize that is kind of the same suggestion as in an earlier post you made but so far it is not clear to me why that suggestion does not work for you.

    So here is 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="xml" version="1.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-title' or @name = 'summary-title']">
          <xsl:choose>
            <xsl:when test="not(self::p[@name = 'section-title' or @name = 'summary-title'])">
              <xsl:apply-templates select="current-group()"/>
            </xsl:when>
            <xsl:otherwise>
              <body-contents>
                 <xsl:sequence select="mf:group(current-group(), 1)"/>
              </body-contents>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:for-each-group>
      </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    When I apply that stylesheet with Saxon 9.4 to the corrected input

    <body>
           <p name="h-title" other="main">Introduction</p>
           <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>
    
    
           <p name=""> XSLT is used to write stylesheets.</p>
        <p name="section-title" other=" other-section">XSLT</p>
        <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        <p name=""> Some text.</p>
        <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <p name="h1-title" other=" other-h1">XSLT</p>
        <p name=""> Some text.</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-title" other=" other-section">XSLT</p>
        <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
        <p name=""> Some text.</p>
        <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
        <p name="h1-title" other=" other-h1">XSLT</p>
        <p name=""> Some text.</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 ="summary-title">this is summary</p>
           <p name="summary-desc " other="other-summarydesc">the summary</p>
        </body>
    

    I get the output

    <body>
       <p name="h-title" other="main">Introduction</p>
       <p name="h-titledesc " other="other-desc">XSLT and XQuery</p>
       <p name=""> XSLT is used to write stylesheets.</p>
       <body-contents>
          <p name="section-title" other=" other-section">XSLT</p>
          <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
          <p name=""> Some text.</p>
          <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
          <h1>
             <p name="h1-title" other=" other-h1">XSLT</p>
             <p name=""> Some text.</p>
             <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>
       </body-contents>
       <body-contents>
          <p name="section-title" other=" other-section">XSLT</p>
          <p name="section-desc" other=" other-sectionsdesc">XSLT</p>
          <p name=""> Some text.</p>
          <p name="">
              <p1 name="bold"> XQuery is used to query XML databases.</p1>
           </p>
          <h1>
             <p name="h1-title" other=" other-h1">XSLT</p>
             <p name=""> Some text.</p>
             <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>
       </body-contents>
       <body-contents>
          <p name="summary-title">this is summary</p>
          <p name="summary-desc " other="other-summarydesc">the summary</p>
       </body-contents>
    </body>
    

    Please next time when you provide an input sample make sure it is well-formed, so far you have always posted stuff like <p name="h2-title " name="other-h2">XQuery</p>, and two attributes of the same name are not possible.

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

Sidebar

Related Questions

Here is simplified version of my requirement I have a java class say Processor
I have a jsfiddle here - http://jsfiddle.net/9kKXX/20/ My jQuery function $(function() { $('form input[type=text]').live('keyup',
I have a following requirement for a very complex UI. (Complex here means there
Slightly unusual requirement here, which unfortunately is down to a poor table design a
I am creating a installer using WIX, The requirement here is that when the
Here is my requirement. I need to unzip a zip file and store it
First question here so hello everyone. The requirement I'm working on is a small
Here are my requirements: I have a DIV which should display an image (JPEG).
Here I have 2 requirements: Need a batch file to start a process on
Here is a sample code showing the oddity: (code fixed based on comment) public

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.