user3362977
user3362977

Reputation: 3

How to change the column value in XSLT?

I have the status in the third column but its value is coming on the first column... How do I change that?

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
   xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
    <html>
      <body>
<table border="1">
  <tr bgcolor="#9acd32">
    <th>Title</th>
    <th>Artist</th>
    <th>status</th>
  </tr>
  <xsl:for-each select="catalog/cd[artist='Bob Dylan']">
  <tr>
    <td><xsl:value-of select="title"/></td>
    <td><xsl:value-of select="artist"/></td>
  </tr>
  </xsl:for-each>
 <xsl:for-each select="catalog/cd/dude" >
  <tr>
    <td><xsl:value-of select="status"/></td>
  </tr>
  </xsl:for-each>
</table>
 </body>
   </html>
 </xsl:template>
  </xsl:stylesheet>

Basically it's coming out like:

Title              Artist       status
Empire Burlesque   Bob Dylan
hello

I want:

Title              Artist       status
Empire Burlesque   Bob Dylan    hello

How do I go about doing that?

This is the source XML:

<catalog> 
    <cd> 
        <title>Empire Burlesque</title> 
        <artist>Bob Dylan</artist> 
        <country>USA</country> 
        <company>Columbia</company> 
        <price>10.90</price> 
        <year>1985</year> 
        <dude> 
            <status>hello</status> 
        </dude> 
    </cd> 
</catalog>

Upvotes: 0

Views: 287

Answers (1)

helderdarocha
helderdarocha

Reputation: 23637

You were creating a separate row for status. Since you have three columns in one row, you should add the dude column together with the other two to obtain what you desire:

<table border="1">
    <tr bgcolor="#9acd32">
         <th>Title</th>
         <th>Artist</th>
         <th>status</th>
    </tr>

    <xsl:for-each select="catalog/cd[artist='Bob Dylan']">
        <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="artist"/></td>
            <td><xsl:value-of select="dude/status"/></td>
        </tr>
     </xsl:for-each>
</table>

This iterates in all cd elements which contain an artist child element with the text Bod Dylan, and when it finds one, it adds a <tr> table row containing three <td> table cells which are your columns.

The way you were doing it iterated in all dude elements (there is only one) and placed the contents of the status element in a new <tr> row. That's why you got an extra row.

Upvotes: 1

Related Questions