XSL question

Soldato
Joined
25 Jan 2003
Posts
11,550
Location
Newark, Notts
Just a quicky, i'm a beginner to xsl so this is proving tricky for me, might be easy for some of you though. Anyway..

In my xml file I have a book element with a "category" attribute, as follows:

Code:
<book category="fiction">
  <title lang="en">A title</title>
  <author>An Author</author>
  <year>3000</year>
  <price>100.00</price>
</book>

In my XSL file I want it to display the category as a heading in the table, then all the books below which apply to this category, and then do the same for the next category and so forth.

At the moment though I can only get it to repeat the category for every book, rather than just once. So for example, if I have two books with the category "fiction" it list them as:

Fiction
Book1
Fiction
Book2

When I would like it to list as:

Fiction
Book1
Book2

XSL is below:

Code:
<xsl:template match="/">
  <table border="1" bordercolor="black" >
    <tr>
      <th>Title</th>
      <th>Author</th>
      <th>Year</th>
      <th>Price</th>
    </tr>

    <xsl:for-each select="bookstore/book">
    
    <tr>
      <td colspan="4" align="center">
        <xsl:value-of select="@category" />
         </td>
    </tr>
         <tr>
        <td>
          <xsl:value-of select="title"/>
        </td>
        <td>
          <xsl:for-each select="author">
            <xsl:value-of select="."/>
            <br />
          </xsl:for-each>
        </td>
        <td>
          <xsl:value-of select="year"/>
        </td>
        <td>
          <xsl:value-of select="price"/>
        </td>
      </tr>
    </xsl:for-each>
  </table>

</xsl:template>

Anyone help me out? Probably really simple I'm guessing.
 
Back
Top Bottom