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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T04:55:02+00:00 2026-05-23T04:55:02+00:00

I need to loop a XML document. No problem there. The problem come when

  • 0

I need to loop a XML document. No problem there.
The problem come when I need text from the previous line I have just skipped.

The XML will look some thing like this

<lines>
  <line>
    <id>1</id>
    <text>Some fancy text here 1</text>
  </line>
  <line>
    <id></id>
    <text>This I need in the next line with a ID</text>
  </line>
  <line>
    <id></id>
    <text>Also need this.</text>
  </line>
  <line>
    <id>4</id>
    <text>Here we go</text>
  </line>
</lines>

The output XML file need to look like this

<output>
  <line>
    <id>1</id>
    <note>Some fancy text here 1</note>
  </line>
  <line>
    <id>4</id>
    <note>Here we go</note>
    <extra>
      <note>This I need in the next line with a ID</note>
      <note>Also need this.</note>
    </extra>
  </line>
</output>

The XSL I have so fare is simple to sort out the line that have no ID set.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <output>
    <xsl:for-each select="lines/line">
      <xsl:if test="id/text() &gt; 0">
        <line>
          <id>
            <xsl:value-of select="id" />
          </id>
          <note>
            <xsl:value-of select="text" />
          </note>
        </line>
      </xsl:if>
    </xsl:for-each>
    </output>
  </xsl:template>
</xsl:stylesheet>
  • 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-23T04:55:03+00:00Added an answer on May 23, 2026 at 4:55 am

    I did not resist to completely review your code 🙂

    Here follows a complete XSLT 1.0 solution more functional-oriented (no procedural approach). It might look at first seen harder to see, but, imho, it’s a very good example to get started into XSLT templating mechanism.

    Also using xsl:for-each in your specific case is not that easy, because at a certain step of the loop you want to get all preceding adjacent siblings with empty id, without knowing how many they are a priori.

    I’ve also used the identity template to simplify the work of recreating your target.

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="xml" indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <!-- identity template -->
        <xsl:template match="node()|@*">
            <xsl:copy>
                <xsl:apply-templates select="node()|@*"/>
            </xsl:copy>
        </xsl:template>
    
        <!-- match line with empty id and do not output -->
        <xsl:template match="line[not(boolean(id/text()))]"/>
    
        <!-- match line with id and build output -->
        <xsl:template match="line[boolean(id/text())]">
            <xsl:copy>
                <xsl:copy-of select="id"/>
                <xsl:apply-templates select="text"/>
                <extra>
                    <!-- apply recursive template to the first preceding 
                    sibling adajacent node with empty id -->
                    <xsl:apply-templates select="(preceding-sibling::*[1])
                        [name()='line' and not(boolean(id/text()))]/text" 
                        mode="extra"/>
                </extra>
            </xsl:copy>
        </xsl:template>
    
        <!-- change text element to note --> 
        <xsl:template match="text">
            <note>
                <xsl:value-of select="."/>
            </note>
        </xsl:template>
    
        <!-- recursive template for extra note elements -->
        <xsl:template match="text" mode="extra">
            <note>
                <xsl:value-of select="."/>
            </note>
            <xsl:apply-templates select="(parent::line/preceding-sibling::*[1])
                [name()='line' and not(boolean(id/text()))]/text" 
                mode="extra"/>
        </xsl:template>
    
    </xsl:stylesheet>
    

    Applied on your input, gives:

    <?xml version="1.0" encoding="UTF-8"?>
    <lines>
       <line>
          <id>1</id>
          <note>Some fancy text here 1</note>
          <extra/>
       </line>
       <line>
          <id>4</id>
          <note>Here we go</note>
          <extra>
             <note>Also need this.</note>
             <note>This I need in the next line with a ID</note>
          </extra>
       </line>
    </lines>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number rather large, complex xml documents that I need to loop
I need to Convert a CSV into an XML document. The examples I have
i need to loop through all the nodes in the xml document and append
I have an array that is created dynamic from an xml document looking something
I have some xml files that will be read-only that I need to access
The XML feed is located at: http://xml.betclick.com/odds_fr.xml I need a php loop to echo
i have the following code ..i need to loop through end of the file
I need a for loop which would print from year 2000 to 2099. [Bindable]
I am creating XML using a while loop, but now I need to prepend
I have an XML document of store locations which have a Type node indicating

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.