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

Following on from my recent question regarding parsing XML files in Java I have
I have the following XML structure: <?xml version=1.0 ?> <course xml:lang=nl> <body> <item id=787900813228567
I have the following xml that's sent to me from a web service. I'm
I have the following xml I'd like to deserialize into a class <?xml version=1.0
If i have the following directory structure: Project1/bin/debug Project2/xml/file.xml I am trying to refer
I'm trying to parse xml from SharePoint service (lists) using jquery. I have XMLHttpRequest
I'm trying to generate an XML output from a type in C#. I'm Using
I have the following XML which I load via XDocument.Load(uri) or XElement.Load(uri) . I
I have the following XML document: <projects> <project> <name>Shockwave</name> <language>Ruby</language> <owner>Brian May</owner> <state>New</state> <startDate>31/10/2008
Is it possible to merge elements using XSLT. If I have the following XML

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.