0
votes

hi there i am a newbie to XML & what i want to do is to be able to take the cd example where there are a number of cds by various artists in a catalog & I then want to be able to extract all of the bob dylan cds and display them in a table ignoring the rest of the cds in the catalog.

So far I have been able to extract all the cds and display the Artist, Album, Company, country, Year etc in a table but so far have been unable to figure out how to solve this problem. I have included my XSL below & also the xml which i am trying to extract the info from. I hope i have supplied enough info! Thanks for your help..

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<catalog>
<cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
</cd>
<cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
</cd>
<cd>
    <title>Greatest Hits</title>
    <artist>Dolly Parton</artist>
    <country>USA</country>
    <company>RCA</company>
    <price>9.90</price>
    <year>1982</year>
</cd>
<cd>
    <title>Still got the blues</title>
    <artist>Gary Moore</artist>
    <country>UK</country>
    <company>Virgin records</company>
    <price>10.20</price>
    <year>1990</year>
</cd>
 </catalog>
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">

<html>


<xsl:variable name="XSLTEST" select="catalog/cd"/>  
<tr>
    count:<xsl:value-of select= "count($XSLTEST)"/>  
</tr>

<body>

 <h2>My CD Collection</h2>
  <table border="1">
    <tr bgcolor="#9acd32">
      <th>Title</th>
      <th>Artist</th>
      <th>country</th>
      <th>Company</th>
      <th>price</th>
      <th>year</th>
    </tr>



 <xsl:for-each select="$XSLTEST">       

   <!--<xsl:sort select="Bee Gees"/>-->

   <xsl:variable name="XSLTEST101" select="."/>                         
   <xsl:variable name="this_title" select="$XSLTEST101/title"/>
   <xsl:variable name="this_artist" select="$XSLTEST101/artist"/>       
   <xsl:variable name="this_country" select="$XSLTEST101/country"/>
   <xsl:variable name="this_company" select="$XSLTEST101/company"/>    
   <xsl:variable name="this_price" select="$XSLTEST101/price"/>         
   <xsl:variable name="this_year" select="$XSLTEST101/year"/>

  <tr>
    <td><xsl:value-of select="$this_title"/></td>
    <td><xsl:value-of select="$this_artist"/></td>      
    <td><xsl:value-of select="$this_country"/></td>
    <td><xsl:value-of select="$this_company"/></td>
    <td><xsl:value-of select="$this_price"/></td>
    <td><xsl:value-of select="$this_year"/></td>
  </tr>
</xsl:for-each select>


</table>

 </body>
 </html>

</xsl:template>
</xsl:stylesheet>
1

1 Answers

1
votes

You select cds only when Bob Dylan is involved like this:

<xsl:variable name="dylan-cds" select="catalog/cd[artist='Bob Dylan']"/>  

Note:

  • I simplified your stylesheet and removed the many variables that were clearly unnecessary. Instead, I directly selected the values where needed.
  • In the end, I assume you want to produce HTML. Even if the XSLT processing yields no error as such, a freestanding <tr> element is not deemed best practice.

Here is the entire stylesheet

<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:output method="xml" indent="yes"/>

<xsl:template match="/">

  <html>

  <xsl:variable name="dylan-cds" select="catalog/cd[artist='Bob Dylan']"/>  
  <tr>
  count:<xsl:value-of select= "count($dylan-cds)"/>  
  </tr>

  <body>
     <h2>My CD Collection</h2>
     <table border="1">
        <tr bgcolor="#9acd32">
           <th>Title</th>
           <th>Artist</th>
           <th>country</th>
           <th>Company</th>
           <th>price</th>
           <th>year</th>
        </tr>

        <xsl:for-each select="$dylan-cds">       
           <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="artist"/></td>      
            <td><xsl:value-of select="country"/></td>
            <td><xsl:value-of select="company"/></td>
            <td><xsl:value-of select="price"/></td>
            <td><xsl:value-of select="year"/></td>
           </tr>
        </xsl:for-each>
     </table>
  </body>
  </html>

</xsl:template>
</xsl:stylesheet>

And the output you get with this:

<?xml version="1.0" encoding="UTF-8"?>
<html>
 <tr>
  count:1</tr>
 <body>
  <h2>My CD Collection</h2>
  <table border="1">
     <tr bgcolor="#9acd32">
        <th>Title</th>
        <th>Artist</th>
        <th>country</th>
        <th>Company</th>
        <th>price</th>
        <th>year</th>
     </tr>
     <tr>
        <td>Empire Burlesque</td>
        <td>Bob Dylan</td>
        <td>USA</td>
        <td>Columbia</td>
        <td>10.90</td>
        <td>1985</td>
     </tr>
  </table>
 </body>
</html>