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

  • Home
  • SEARCH
  • 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 8252507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T00:34:54+00:00 2026-06-08T00:34:54+00:00

I have such XML <?xml version=1.0 encoding=utf-8?> <?xml-stylesheet type=text/xsl href=do.xsl?> <catalog> <cd> <title_>Empire Burlesque</title_>

  • 0

I have such XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="do.xsl"?>
<catalog>
  <cd>
    <title_>Empire Burlesque</title_>
    <artist>Bobby</artist>
    <company>Columbia</company>
  </cd>
  <cd>
    <title_>Shirt2</title_>
    <artist>Bobby</artist>
    <company>Columbia2</company>
  </cd>
  <cd>
    <title_>Fingers</title_>
    <artist>Bobby</artist>
    <company>Columbia3</company>
  </cd>
  <cd>
    <title_>Zip1</title_>
    <artist>Bobby</artist>
    <company>---</company>
  </cd>
  <cd>
    <title_>Zip2</title_>
    <artist>Bobby</artist>
    <company>---</company>
  </cd>
</catalog>

I need to replace — with previous meaningful data – Columbia3
I do next

<?xml version="1.0" encoding="utf-8"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/>

<xsl:template match="/">
  <html>
  <body>
  <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
    </tr>
    <xsl:for-each select="catalog/cd">
    <tr>
      <td><xsl:value-of select="title_"/></td>
      <td><xsl:value-of select="artist"/></td>
      <td>
          <xsl:variable name="NotStarted" select="preceding-sibling::cd[1]/company" />
          <xsl:choose>
            <xsl:when test="company != '---'">
              <xsl:value-of select="company"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:copy-of select="$NotStarted" />
            </xsl:otherwise>
          </xsl:choose>
      </td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 

As expected i got next html

Title   Artist
Empire Burlesque    Bobby   Columbia
Shirt2  Bobby   Columbia2
Fingers Bobby   Columbia3
Zip1    Bobby   Columbia3
Zip2    Bobby   ---

How i can iterate back to get last meaningful string to replace with or any other way? 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-06-08T00:34:56+00:00Added an answer on June 8, 2026 at 12:34 am

    This is can be achieved by changing your NotStarted variable

    <xsl:variable name="NotStarted" select="preceding-sibling::cd[1]/company" />
    

    What this is currently doing is finding the immediately preceding sibling, regardless of content, and the getting its company element. What you need to do is find the first preceding sibling which has a valid company name

    <xsl:variable name="NotStarted" 
       select="preceding-sibling::cd[company != '---'][1]/company/text()"/>
    

    It is worth noting, it is usually preferable to avoid the use of xsl:for-each and xsl:choose in XSLT, and try to utilitise the power of template matching. Here is an alternate XSLT which demonstrates this

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html" version="4.0" encoding="utf-8" indent="yes"/>
    
       <xsl:template match="/">
          <html>
             <body>
                <h2>My CD Collection</h2>
                <table border="1">
                   <tr bgcolor="#9acd32">
                      <th>Title</th>
                      <th>Artist</th>
                   </tr>
                   <xsl:apply-templates select="catalog/cd"/>
                </table>
             </body>
          </html>
       </xsl:template>
    
       <xsl:template match="cd">
          <tr>
             <td>
                <xsl:value-of select="title_"/>
             </td>
             <td>
                <xsl:value-of select="artist"/>
             </td>
             <td>
                <xsl:apply-templates select="company"/>
             </td>
          </tr>
       </xsl:template>
    
       <xsl:template match="company[text() = '---']">
          <xsl:value-of select="../preceding-sibling::cd[company != '---'][1]/company"/>
       </xsl:template>
    </xsl:stylesheet>
    

    When applied to your XML sample, the following is output

    <html>
       <body>
          <h2>My CD Collection</h2>
          <table border="1">
             <tr bgcolor="#9acd32">
                <th>Title</th>
                <th>Artist</th>
             </tr>
             <tr>
                <td>Empire Burlesque</td>
                <td>Bobby</td>
                <td>Columbia</td>
             </tr>
             <tr>
                <td>Shirt2</td>
                <td>Bobby</td>
                <td>Columbia2</td>
             </tr>
             <tr>
                <td>Fingers</td>
                <td>Bobby</td>
                <td>Columbia3</td>
             </tr>
             <tr>
                <td>Zip1</td>
                <td>Bobby</td>
                <td>Columbia3</td>
             </tr>
             <tr>
                <td>Zip2</td>
                <td>Bobby</td>
                <td>Columbia3</td>
             </tr>
          </table>
       </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I have such code for my application <?xml version=1.0 encoding=utf-8?> <s:Application xmlns:fx=http://ns.adobe.com/mxml/2009 xmlns:s=library://ns.adobe.com/flex/spark
I have 2 files catalog.xml <?xml version=1.0 encoding=UTF-8?> <catalog> <CD Title=Still got the blues
I have 2 POCOs mapped as such: <?xml version=1.0 encoding=utf-8 ?> <hibernate-mapping xmlns=urn:nhibernate-mapping-2.2> <class
I have an Xml document: <?xml version=1.0 encoding=utf-8?> <Family xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema> <Person member=father id=0>
Lets say I have the following XML file: <?xml version=1.0 encoding=utf-8?> <Customers xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xsi:noNamespaceSchemaLocation=Customer.xsd>
I have the following application.xml <?xml version=1.0 encoding=utf-8?> <manifest xmlns:android=http://schemas.android.com/apk/res/android package=com.currency.mobile.android> <uses-permission android:name=android.permission.INTERNET/> <application
Consider we have the following xml document <?xml version=1.0 encoding=utf-8?> <Root> <Child /> </Root>
I have an xml document with the following structure: <?xml version=1.0 encoding=UTF-8?> <items> <item>
I have the standard server.xml from tomcat's conf dir: <?xml version='1.0' encoding='utf-8'?> <!-- Licensed
I have PreferenceActivity with some sample content: <?xml version=1.0 encoding=utf-8?> <PreferenceScreen xmlns:android=http://schemas.android.com/apk/res/android> <PreferenceCategory android:title=First

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.