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.
 
in your xml are all the books of the same type together or can they be mixed i.e

One is fiction, then a horror then another fiction then horror etc ? if so then your going to have to use two templates. one to loop through the types and in that you cna call another template which will loop through all the books of that type and display them. then you can add an if statement like so

<xsl:if test="position() = first()"></xsl:if>
and put the header in that and it should only display on the very first node of that type.
 
Last edited:
Back
Top Bottom