2
votes

I've started playing around with XSLT, and am having a few issues. I've managed to output my XML document using a series of value-of select instructions, but I'm really struggling when it comes to writing my XSLT templates myself.

Here's my XML:

<?xml version="1.0" ?>
<?xml-stylesheet type="text/xsl" href="lecturers.xsl"?>
<lecturers>
<lecturer>
        <name> 
             <title>Professor</title> 
        <first>Peter </first> <last>Quirk</last>
        </name>
        <teaching>
        <course code="CO3070">XML and the Web</course>
        <course code="CO3300"> Web Server Architectures</course>
    </teaching>
    <research>
        The application of Web protocols to Biology
    </research>
</lecturer>

<lecturer>
    <name> 
    <title>Doctor</title> 
    <first>Brian </first> <last>Johnson</last>
    </name>
    <teaching>
        <course code="CO9999">Computer Hacking</course>
        <course code="CO3300"> Web Server Architectures</course>
    </teaching>
    <research>
        Investigating the various complexities of Computer Hacking
    </research>
</lecturer>

Then, this is my XSL as it currently stands:

 <?xml version="1.0"?>
 <xsl:stylesheet version='1.0' xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" version="4.0" />

 <xsl:template match="/">
 <html>
 <head>
 <title>XML Week 7</title>
 </head>

  <body>
  <h1>Week 7: Lecturers file turned to XSL:Template</h1>

 <table border="1">
<tr>
    <th><b>Title</b></th>
    <th><b>Name</b></th>
    <th><b>Teaching</b></th>
    <th><b>Research</b></th>
</tr>

<tr>

    <td><xsl:value-of select="lecturers/lecturer/name/title" /></td>
    <td><xsl:value-of select="lecturers/lecturer/name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="lecturers/lecturer/name/last" /></td>
    <td><xsl:value-of select="lecturers/lecturer/teaching/course" /> and <xsl:value-of select="(lecturers/lecturer/teaching/course)[2]" /></td>
    <td><xsl:value-of select="lecturers/lecturer/research" /></td>

</tr>
 </table>

</body>

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

This does output the information I need into a table, but I've been instructed to create new templates to hold the lecturer element, and then the course children of that element. I might just be over-complicating things in my head, but I just cannot get it to work, whenever I try to apply a template to one of the td's I just get a parsing error in the browser. So, does anyone have any tips for me? Would be much appreciated, even some basic examples to explain how to get it working in my example would be superb. Cheers guys.

2

2 Answers

2
votes

Use one template to create the result document structure e.g.

 <xsl:template match="/">
 <html>
 <head>
 <title>XML Week 7</title>
 </head>

  <body>
    <xsl:apply-templates/>

</body>

</html>
 </xsl:template>

then write further templates to create the content e.g.

<xsl:template match="lecturers">
  <h1>Week 7: Lecturers file turned to XSL:Template</h1>

 <table border="1">
<tr>
    <th><b>Title</b></th>
    <th><b>Name</b></th>
    <th><b>Teaching</b></th>
    <th><b>Research</b></th>
</tr>
  <xsl:apply-templates/>
</table>
</xsl:template>

<xsl:template match="lecturer">
<tr>

    <td><xsl:value-of select="name/title" /></td>
    <td><xsl:value-of select="name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="name/last" /></td>
    <td><xsl:value-of select="teaching/course" /> and <xsl:value-of select="(teaching/course)[2]" /></td>
    <td><xsl:value-of select="lecturers/lecturer/research" /></td>

</tr>
</xsl:template>

As you see, the templates do an apply-templates to keep up the processing.

[edit] In response to your comment, if you want to use more templates for the different child or descendant elements of the lecturer element you can use

<xsl:template match="lecturer">
   <tr>
     <xsl:apply-templates/>
   </tr>
</xsl:template>

and then write templates for the elements e.g.

<xsl:template match="name/title | research">
  <td>
    <xsl:value-of select="."/>
  </td>
</xsl:template>

<xsl:template match="name/first">
  <td>
    <xsl:value-of select="concat(., ' ', ../last)"/>
  </td>
</xsl:template>

<!-- don't output name/last as the name/first template already does -->
<xsl:template match="name/last"/>

<xsl:template match="teaching">
  <xsl:apply-templates select="course"/>
</xsl:template>

<xsl:template match="course">
  <xsl:if test="position() > 1"><xsl:text> and </xsl:text></xsl:if>
  <xsl:value-of select="."/>
</xsl:template>
1
votes

Where you currenly have:

<tr>
    <td><xsl:value-of select="lecturers/lecturer/name/title" /></td>
    <td><xsl:value-of select="lecturers/lecturer/name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="lecturers/lecturer/name/last" /></td>
    <td><xsl:value-of select="lecturers/lecturer/teaching/course" /> and <xsl:value-of select="(lecturers/lecturer/teaching/course)[2]" /></td>
    <td><xsl:value-of select="lecturers/lecturer/research" /></td>
</tr>

You would want to have a call to apply-templates and have a lecturer template.

<xsl:template match="lecturer">
    <tr>

        <td><xsl:value-of select="name/title" /></td>
        <td><xsl:value-of select="name/first" /><xsl:text>&#x20;</xsl:text><xsl:value-of select="name/last" /></td>
        <td><xsl:value-of select="teaching/course" /> and <xsl:value-of select="(teaching/course)[2]" /></td>
        <td><xsl:value-of select="research" /></td>

    </tr>
</xsl:template>