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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:55:29+00:00 2026-05-27T16:55:29+00:00

I have an HTML file like this: <root> <br> <h3>The first H3 text</h3><br> <p>para1

  • 0

I have an HTML file like this:

<root>
    <br>
    <h3>The first H3 text</h3><br>
    <p>para1 content in first H3</p><br>
    <p>para2 content in first H3</p><br>
    <h3>The second H3 text</h3><br>
    <p>para1 content in second H3</p><br>
    <p>para2 content in second H3</p><br>
    <p>para3 content in second H3</p><br>
    <p>para4 content in second H3</p><br>
</root>

I want to write an XSL which gives output like this one:

<sec>
    <br />
    <secHead>The first H3 text</secHead><br />
    <para>para1 content in first H3</para><br />
    <para>para2 content in first H3</para><br />
</sec>
<br />
<sec>
    <br/>
    <secHead>The second H3 text</secHead><br/>
    <para>para1 content in second H3</para><br/>
    <para>para2 content in second H3</para><br/>
    <para>para3 content in second H3</para><br/>
    <para>para4 content in second H3</para><br/>
</sec>

How can we get this output writing XSL?

  • 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-27T16:55:30+00:00Added an answer on May 27, 2026 at 4:55 pm

    Good question, +1.

    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="*[not(self::h3)]"
      use="generate-id(preceding-sibling::h3[1])"/>
    
     <xsl:template match="/*">
      <xsl:apply-templates select="h3"/>
     </xsl:template>
    
     <xsl:template match="h3">
        <xsl:if test="position() > 1">
         <br />
        </xsl:if>
         <sec>
          <br />
          <secHead><xsl:value-of select="."/></secHead>
          <xsl:copy-of select="key('kFollowing', generate-id())"/>
         </sec>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided source (the html must be converted to wellformed XML!!!):

    <root>
        <br/>
        <h3>The first H3 text</h3><br/>
        <p>para1 content in first H3</p><br/>
        <p>para2 content in first H3</p><br/>
        <h3>The second H3 text</h3><br/>
        <p>para1 content in second H3</p><br/>
        <p>para2 content in second H3</p><br/>
        <p>para3 content in second H3</p><br/>
        <p>para4 content in second H3</p><br/>
    </root>
    

    produces the wanted, correct result:

    <sec>
       <br/>
       <secHead>The first H3 text</secHead>
       <br/>
       <p>para1 content in first H3</p>
       <br/>
       <p>para2 content in first H3</p>
       <br/>
    </sec>
    <br/>
    <sec>
       <br/>
       <secHead>The second H3 text</secHead>
       <br/>
       <p>para1 content in second H3</p>
       <br/>
       <p>para2 content in second H3</p>
       <br/>
       <p>para3 content in second H3</p>
       <br/>
       <p>para4 content in second H3</p>
       <br/>
    </sec>
    

    Explanation:

    The key point in this solution is that we define an xsl:key that captures the relation between an h3 element and all its immediate following siblings elements (such that they aren’t h3 themselves and this h3 is their first h3 preceding sibling).

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

Sidebar

Related Questions

I have nav code like this in my HTML file: <div id="center_links"> <ul> <li><a
Absolute beginner question: I have a template file index.html that looks like this: ...
I have html-file. I have to replace all text between this: [%anytext%]. As I
I have a basic xml file that looks like this. <root> <item> <title><p>some title</p></title>
I have a html file that has invoice details I would like to know
I have written xml file which contains html tags as element like <component> <input
I have an HTML file and I want to use javascript to call a
I have a html file that I want to trim. I want to remove
Currently I have a url like this http://<client_name>.website.com/index.php?function_name&cat=32 I want to set things up
I have a .htaccess file that looks like this: SetEnv APPLICATION_ENV development RewriteEngine On

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.