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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:35:12+00:00 2026-05-23T09:35:12+00:00

Say I had some XML that I wanted to convert to HTML. The XML

  • 0

Say I had some XML that I wanted to convert to HTML. The XML is divided into ordered sections:

<?xml version="1.0" encoding="utf-8"?>
<root>
  <section attr="someCriteria">
    <h1>Title 1</h1>
    <p>paragraph 1-1</p>
    <p>paragraph 1-2</p>
  </section>
  <section attr="someOtherCriteria">
    <h3>Subtitle 2</h3>
    <ul>
      <li>list item 2-1</li>
      <li>list item 2-2</li>
      <li>list item 2-3</li>
      <li>list item 2-4</li>
    </ul>
  </section>
  <section attr="anotherSetOfCriteria">
    <warning>
      Warning: This product could kill you
    </warning>
  </section>
  <section attr="evenMoreCriteria">
    <disclaimer>
      You were warned
    </disclaimer>
  </section>
  <section attr="criteriaSupreme">
    <p>Copyright 1999-2011</p>
  </section>
</root>

I have several of these XML documents.
I need to group and transform these sections based on criteria. There will be two different kinds of buckets.

  • So the first section will go in a
    bucket (e.g.<div
    class="FormatOne"></div>
    )
  • If the
    second section meets the criteria to
    qualify for the “FormatOne” bucket it
    will also go in this bucket
  • If the
    third section requires a different
    bucket (e.g.<div
    class="FormatTwo"></div>
    ) then a new
    bucket is created and section
    contents are placed in this bucket
  • If the bucket for the fourth section requires “FormatOne” (which is different than the previous format) then a new
    bucket is created again and section
    contents are placed in this bucket
  • etc. Each section would go into the same bucket as the previous section if they are the same format. If not, a new bucket is created.

So for each document, depending on the logic for separating buckets, the document may end up like this:

<body>
  <div class="FormatOne">
    <h1>Title 1</h1>
    <p>paragraph 1-1</p>
    <p>paragraph 1-2</p>
    <h3>Subtitle 2</h3>
    <ul>
      <li>list item 2-1</li>
      <li>list item 2-2</li>
      <li>list item 2-3</li>
      <li>list item 2-4</li>
    </ul>
  </div>
  <div class="FormatTwo">
    <span class="warningText">
      Warning: This product could kill you
    </span>
  </div>
  <div class="FormatOne">
    <span class="disclaimerText"> You were warned</span>
    <p class="copyright">Copyright 1999-2011</p>
  </div>
</body>

this:

<body>
  <div class="FormatOne">
    <h1>Title 1</h1>
    <p>paragraph 1-1</p>
    <p>paragraph 1-2</p>
    <h3>Subtitle 2</h3>
  </div>
  <div class="FormatTwo">
    <ul>
      <li>list item 2-1</li>
      <li>list item 2-2</li>
      <li>list item 2-3</li>
      <li>list item 2-4</li>
    </ul>
  </div>
  <div class="FormatOne">
    <span class="warningText">
      Warning: This product could kill you
    </span>
    <span class="disclaimerText"> You were warned</span>
    <p class="copyright">Copyright 1999-2011</p>
  </div>
</body>

or even this:

<body>
  <div class="FormatOne">
    <h1>Title 1</h1>
    <p>paragraph 1-1</p>
    <p>paragraph 1-2</p>
    <h3>Subtitle 2</h3>
    <ul>
      <li>list item 2-1</li>
      <li>list item 2-2</li>
      <li>list item 2-3</li>
      <li>list item 2-4</li>
    </ul>
    <span class="warningText">
      Warning: This product could kill you
    </span>
    <span class="disclaimerText"> You were warned</span>
    <p class="copyright">Copyright 1999-2011</p>
  </div>
</body>

depending on how the sections are defined.

Is there a way to use an XSLT to perform this type of grouping magic?

Any help would be great.
Thanks!

  • 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-23T09:35:13+00:00Added an answer on May 23, 2026 at 9:35 am

    I came up with a solution that involves hitting each section sequentially. The processing of each section is broken into two parts: a “shell” and a “contents” portion. The “shell” is responsible for rendering the <div class="FormatOne">...</div> bits, and the “contents” is responsible for rendering the actual contents of the current section and all following sections until a non-matching section is found.

    When a non-matching section is found, control reverts to the “shell” template for that section.

    This gives an interesting bit of flexibility: the “shell” templates may be very aggressive in what they match, and the “contents” sections may be more discerning. Specifically, with your first example output, you need the warning element to appear as <span class="warningText">...</span>, and this is accomplished with a more closely matching template.

    All “content” templates, after rendering the contents of their current section, call a named template that looks for the “next” appropriate content section. This helps consolidate the rules for determining what qualifies as a “matching” section.

    You can see a working example here.

    Here is my code, built to replicate what you asked for in your first example:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" />
    
        <xsl:template match="/">
            <body>
                <xsl:apply-templates select="/root/section[1]" mode="shell" />
            </body>
        </xsl:template>
    
        <xsl:template match="section[
            @attr = 'someCriteria' or
            @attr = 'someOtherCriteria' or
            @attr = 'evenMoreCriteria' or
            @attr = 'criteriaSupreme']" mode="shell">
    
            <div class="FormatOne">
                <xsl:apply-templates select="." mode="contents" />
            </div>
    
            <xsl:apply-templates select="following-sibling::section[
                @attr != 'someCritera' and
                @attr != 'someOtherCriteria' and
                @attr != 'evenMoreCriteria' and
                @attr != 'criteriaSupreme'][1]" mode="shell" />
    
        </xsl:template>
    
        <xsl:template name="nextFormatOne">
            <xsl:variable name="next" select="following-sibling::section[1]" />
            <xsl:if test="$next[
                @attr = 'someCriteria' or
                @attr = 'someOtherCriteria' or
                @attr = 'evenMoreCriteria' or
                @attr = 'criteriaSupreme']">
                <xsl:apply-templates select="$next" mode="contents" />
            </xsl:if>
        </xsl:template>
    
        <xsl:template match="section[
            @attr = 'someCriteria' or
            @attr = 'someOtherCriteria']" mode="contents">
    
            <xsl:copy-of select="*" />
    
            <xsl:call-template name="nextFormatOne" />
        </xsl:template>
    
        <xsl:template match="section[@attr = 'evenMoreCriteria']" mode="contents">
            <span class="disclaimerText">
                <xsl:value-of select="disclaimer" />
            </span>
    
            <xsl:call-template name="nextFormatOne" />
        </xsl:template>
    
        <xsl:template match="section[@attr = 'criteriaSupreme']" mode="contents">
            <p class="copyright">
                <xsl:value-of select="p" />
            </p>
    
            <xsl:call-template name="nextFormatOne" />
        </xsl:template>
    
        <xsl:template match="section[@attr = 'anotherSetOfCriteria']" mode="shell">
            <div class="FormatTwo">
                <xsl:apply-templates select="." mode="contents" />
            </div>
            <xsl:apply-templates select="
                following-sibling::section[@attr != 'anotherSetOfCriteria'][1]"
                mode="shell" />
        </xsl:template>
    
        <xsl:template name="nextFormatTwo">
            <xsl:variable name="next" select="following-sibling::section[1]" />
            <xsl:if test="$next[@attr = 'anotherSetOfCriteria']">
                <xsl:apply-templates select="$next" mode="contents" />
            </xsl:if>
        </xsl:template>
    
        <xsl:template
            match="section[@attr = 'anotherSetOfCriteria']"
            mode="contents">
    
            <span class="warningText">
                <xsl:value-of select="warning" />
            </span>
    
            <xsl:call-template name="nextFormatTwo" />
        </xsl:template>
    
    </xsl:stylesheet>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had this doubt for sometime now, some people say that there's no such
Say I had the following HTML: <h2>Heading <span></span></h2> <ul> <li><a href=# title=I am wanting
Let's say I had a program in C# that did something computationally expensive, like
Say I had some data, for which I want to fit a parametrized model
So say i had some text like this: line num 1 line num 2
I just started writing Applets and I had some questions about the HTML applet
I have some xml-configuration files that we create in a Windows environment but is
Lets say you had some resource clean up like: This is C#. try{/*stuff*/} catch(Exception
Yesterday I had a team leader of another team say that they took a
Let's say I had an xml file like this: <items> <item> <id>1</id> <name>Item 1</name>

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.