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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T20:12:46+00:00 2026-05-20T20:12:46+00:00

I have the following XML from which I am trying to generate HTML files.

  • 0

I have the following XML from which I am trying to generate HTML files.

<?xml version="1.0" encoding="UTF-8"?>
<text>
    <body>
        <milestone unit="fragment"/>
            <div>

                <p>First line text in FIRST fragment</p>
                <p>Second line <seg>text in FIRST</seg> fragment</p>
                <p>Third line text in FIRST fragment</p>

                <milestone unit="fragment"/>
                <p>First line text in SECOND fragment</p>
                <p>Second line <seg>text in SECOND </seg> fragment</p>
                <p>Third line text in SECOND fragment</p>

                <milestone unit="fragment"/>
                <p>First line text in THIRD fragment</p>
                <p>Second line <seg>text in THIRD </seg> fragment</p>
                <p>Third line text in THIRD fragment</p>

            </div>
    </body>
</text>

Expected HTML Output:

HTML 1:
<p>First line text in FIRST fragment</p>
<p>Second line <span>text in FIRST</span>fragment</p>
<p>Third line text in FIRST fragment</p>


HTML 2:
<p>First line text in SECOND fragment</p>
<p>Second line <span>text in SECOND </span> fragment</p>
<p>Third line text in SECOND fragment</p>

HTML 3:
<p>First line text in THIRD fragment</p>
<p>Second line <span>text in THIRD </span> fragment</p>
<p>Third line text in THIRD fragment</p>

This is the XSLT where I am using for-each-group to group the nodes between “milestone” tags, but the grouping does not occur as expected.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs" version="2.0">
    <xsl:output method="html" name="html" encoding="UTF-8" indent="yes" omit-xml-declaration="yes" include-content-type="no"/>

    <xsl:template match="/">
        <xsl:apply-templates/>
    </xsl:template>

    <xsl:template match="body">
        <xsl:for-each-group select="descendant::*" group-starting-with="milestone[@unit='fragment']">

            <xsl:variable name="currFragNumber">
                    <xsl:number count="milestone[@unit='fragment']" level="any" from="text"/>
                </xsl:variable>
                                <xsl:result-document href="{$currFragNumber}.html" format="html">
                    <xsl:apply-templates select="current-group()" />
                </xsl:result-document>

        </xsl:for-each-group>
    </xsl:template>

    <xsl:template match="p">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template>

    <xsl:template match="seg">
        <span>
            <xsl:apply-templates/>
        </span>
    </xsl:template>
</xsl:stylesheet>

Below is the output after of the transform.

HTML 1:
<p>First line text in FIRST fragment</p>
<p>Second line <span>text in FIRST</span>fragment</p>
<p>Third line text in FIRST fragment</p>

<p>First line text in SECOND fragment</p>
<p>Second line <span>text in SECOND</span>fragment</p>
<p>Third line text in SECOND fragment</p>

<p>First line text in THIRD fragment</p>
<p>Second line <span>text in THIRD</span>fragment</p>
<p>Third line text in THIRD fragment</p>

<p>First line text in FIRST fragment</p>
<p>Second line <span>text in FIRST</span>fragment</p>
<span>text in FIRST</span>
<p>Third line text in FIRST fragment</p>


HTML 2:
<p>First line text in SECOND fragment</p>
<p>Second line <span>text in SECOND </span> fragment</p>
<span>text in SECOND </span>
<p>Third line text in SECOND fragment</p>

HTML 3:
<p>First line text in THIRD fragment</p>
<p>Second line <span>text in THIRD </span> fragment</p>
<span>text in THIRD </span>
<p>Third line text in THIRD fragment</p>

I am trying to understand why the first current-group()(i.e. first “milestone”) is matching the whole document rather than just the nodes between the “milestone” tags.

  • 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-20T20:12:46+00:00Added an answer on May 20, 2026 at 8:12 pm

    The problem is that you are also selecting the div, as I’ve suggested in comments.

    Use:

    <xsl:for-each-group select="div/*" 
                        group-starting-with="milestone[@unit='fragment']">
    

    It will correctly serialize three result documents. Do note that you could just forget the first milestone because according to http://www.w3.org/TR/xslt20/#xsl-for-each-group

    If the group-starting-with attribute
    is present, then its value must be a
    pattern. In this case, the items in
    the population must all be nodes.

    The nodes in the population are
    examined in population order. If a
    node matches the pattern, or is the
    first node in the population
    *, then a
    new group is created and the node
    becomes its first member. Otherwise,
    the node is assigned to the same group
    as its preceding node within the
    population.

    *: emphasis is mine.

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

Sidebar

Related Questions

I have the following xml, from which I am trying to remove all xmlns
I have the following xml file: <?xml version=1.0 encoding=utf-8?> <Cus> <Customer Location=NJ> <Male Value=True
I have a PHP script which reads from an xml file to generate a
I have the following XML in a string named 'xml': <?xml version=1.0 encoding=ISO-8859-1?> <Book>
I have a windows service which is trying to access an xml file from
I have the following XML LINQ query from my XDocument. var totals = (from
I have following object structure, deseralized from XML (WS): <ns2:Category> <ns2:CategoryId>800003</ns2:CategoryId> <ns2:CategoryName>Name1</ns2:CategoryName> <ns2:Categories> <ns2:Category>
I have to read the xml node name from the following XML, but I
I have following code for loading image from url in xml parsing endElement method
I have following xml and I want to fetch the value of node which

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.