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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T21:13:01+00:00 2026-05-23T21:13:01+00:00

I have xml which contain some documents: <document> <line id=0> <field id=0><![CDATA[H:doc1]]></field> </line> <line

  • 0

I have xml which contain some documents:

<document>
    <line id="0">
        <field id="0"><![CDATA[H:doc1]]></field>
    </line>
    <line id="1">
        <field id="0"><![CDATA[L:1]]></field>
    </line>
    <line id="2">
        <field id="0"><![CDATA[L:2]]></field>
    </line>
    <line id="3">
        <field id="0"><![CDATA[L:3]]></field>
    </line>
    <line id="4">
        <field id="0"><![CDATA[H:doc2]]></field>
    </line>
    <line id="5">
        <field id="0"><![CDATA[L:1]]></field>
    </line> 
</document>

H=header of document and L=Line-item. in this example whe have two H that means two documents which number doc1 and doc2. doc1 have three line items and doc2 have one line item.

how to convert data using xslt version 1 to get this result:

<documents>
    <document>
        <header>
            <number>doc1</number>
        </header>
        <line-item>
            <line-number>1</line-number>
            <line-number>2</line-number>
            <line-number>3</line-number>
        </line-item>
    </document>
    <document>
        <header>
            <number>doc2</number>
        </header>
        <line-item>
            <line-number>1</line-number>
        </line-item>
    </document>
</documents>
  • 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-23T21:13:02+00:00Added an answer on May 23, 2026 at 9:13 pm

    This XSLT 1.0 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="line[starts-with(field,'L:')]"
       use="generate-id(preceding-sibling::line
                             [starts-with(field,'H:')]
                             [1]
                        )"/>
    
     <xsl:template match="/">
         <documents>
          <xsl:apply-templates/>
         </documents>
     </xsl:template>
    
     <xsl:template match="line[starts-with(field,'H:')]">
      <document>
       <header>
        <number><xsl:value-of select="substring-after(field,'H:')"/></number>
        <line-item>
         <xsl:apply-templates mode="inGroup" select=
            "key('kFollowing', generate-id())"/>
        </line-item>
       </header>
      </document>
     </xsl:template>
    
     <xsl:template match="line" mode="inGroup">
      <line-number>
       <xsl:value-of select="substring-after(field,'L:')"/>
      </line-number>
     </xsl:template>
     <xsl:template match="text()"/>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <document>
        <line id="0">
            <field id="0"><![CDATA[H:doc1]]></field>
        </line>
        <line id="1">
            <field id="0"><![CDATA[L:1]]></field>
        </line>
        <line id="2">
            <field id="0"><![CDATA[L:2]]></field>
        </line>
        <line id="3">
            <field id="0"><![CDATA[L:3]]></field>
        </line>
        <line id="4">
            <field id="0"><![CDATA[H:doc2]]></field>
        </line>
        <line id="5">
            <field id="0"><![CDATA[L:1]]></field>
        </line>
    </document>
    

    produces the wanted, correct result:

    <documents>
       <document>
          <header>
             <number>doc1</number>
             <line-item>
                <line-number>1</line-number>
                <line-number>2</line-number>
                <line-number>3</line-number>
             </line-item>
          </header>
       </document>
       <document>
          <header>
             <number>doc2</number>
             <line-item>
                <line-number>1</line-number>
             </line-item>
          </header>
       </document>
    </documents>
    

    Explanation: Using keys to conveniently specify and select the complete group of adjacent “lines” following a “header”.

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

Sidebar

Related Questions

I have in PHP a string which contain stuff like this: <div>some html</div> <?xml:namespace
I have some xml files that contain text, which are displayed on my website.
I have some XML which contains records and sub records, like this: <data> <record
I have an xml column which contain data like this: <AuthorList CompleteYN=Y> <Author ValidYN=Y>
Currently I have some xml documents which are converted via xsl into html. The
I have an xml document that contains some html. <begin-line> <verse-num>6</verse-num>a mixed people<footnote id=f2>
i have an xml file which contain some data like: <?xml version=1.0 encoding=utf-8 ?>
I need some help. I have this xml document: <?xml version="1.0" encoding="utf-8"?> <MyItems> <Parent
I have a custom XML file format which can contain blocks of code within
I have an XML document which contains nodes like following:- <a class=custom>test</a> <a class=xyz></a>

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.