Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 23rd, 2007, 01:35 PM
Registered User
 
Join Date: Nov 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default help using saxon to get some XSLT code to work

Hi, I have already read some of the other posts on here, and they recommended using saxon. This is for some work im doing in my third year of uni doing some work on XML, XSLT etc.. but i dont want the answers, just some help in getting saxon to do something so I can see where im going wrong!

I have tried to use some XSLT code I found online, which uses the Muenchian Method to group data in an XML document in a particular way. I got a simple bit of XSLT code to work in IE, but every time i try the code I found online it gives me the error : Keyword xsl:stylesheet may not contain PCDATA nodes.
So I then found this forum, and tried using saxon to see if that fixed it.

However, I have managed to install saxon, but then am not sure how to get it to check my code.

I may be doing something really stupid, but there doesn't seem to be much documentation out there, or if there is, its quite complex.

any help would be appreciated ;)

The XML im using is:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="collegestaff2.xsl"?>
<collegestaff>
<department dname="Computer Science" code="dcs">
<staff>
<sname>Steve Barker</sname>
<title>Lecturer</title>
<tel>1587</tel>
</staff>
<staff>
<sname>David Clark</sname>
<title>Lecturer</title>
<tel>2472</tel>
</staff>
</department>
<department dname="Philosophy" code="philosophy">
<staff>
<sname>Peter Adamson</sname>
<title>Dr</title>
<tel>2118</tel>
</staff>
<staff>
<sname>John Milton</sname>
<title>Professor</title>
<tel>2340</tel>
</staff>
</department>
</collegestaff>




and the xslt im trying to get working is:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:key name="collegestaff" match="department" use="dname"/>

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

    <xsl:template match="collegestaff">
<xsl:call-template name="Style"/>
-
    <h2>
College Members of Staff
</h2>

    <xsl:for-each select="//department[generate-id(.)=generate-id(key('collegestaff',dname))]">

<xsl:sort select="tel" order="descending"/>

    <h3>
    Department of
<xsl:value-of select="department"/>
</h3>

    <table border="1">
-
    <tr>
<th>Name</th>
<th>Job Title</th>
<th>Tel</th>
</tr>

    <xsl:for-each select="key('staff',dname)">

<xsl:sort select="tel"/>
    <tr>
    <td>
<xsl:value-of select="sname"/>
</td>
    <td>
<xsl:value-of select="title"/>
</td>
    <td>
<xsl:value-of select="tel"/>
</td>
</tr>
</xsl:for-each>
</table>
<br/>
<br/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>


 
Old November 23rd, 2007, 01:54 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 430
Thanks: 28
Thanked 5 Times in 5 Posts
Send a message via Yahoo to bonekrusher
Default

Hi,

If you are using SAXON, use XSLT 2.0 and the <xsl:for-each-group>. Its a hel-la-va lot easier.

Bones
 
Old November 23rd, 2007, 02:02 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Here is a corrected stylesheet, you mainly need to make sure that you select attributes as @attribute-name (e.g. @dname). There were also a few XPath expressions to be corrected:
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:key name="collegestaff" match="department" use="@dname"/>

  <xsl:template match="/">
    <html>
      <head>
        <title>Example</title>
      </head>
      <body>
        <xsl:apply-templates/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="collegestaff">
    <h2>College Members of Staff</h2>

    <xsl:for-each select="department[generate-id() = generate-id(key('collegestaff', @dname)[1])]">

      <h3>Department of <xsl:value-of select="@dname"/></h3>

      <table border="1">
        <thead>
        <tr>
          <th>Name</th>
          <th>Job Title</th>
          <th>Tel</th>
        </tr>
        </thead>

        <tbody>
          <xsl:for-each select="key('collegestaff', @dname)/staff">

          <xsl:sort select="tel" data-type="number"/>
          <tr>
            <td>
              <xsl:value-of select="sname"/>
            </td>
            <td>
              <xsl:value-of select="title"/>
            </td>
            <td>
              <xsl:value-of select="tel"/>
            </td>
          </tr>
        </xsl:for-each>
        </tbody>
      </table>
      <br/>
      <br/>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
 
Old November 23rd, 2007, 02:14 PM
Registered User
 
Join Date: Nov 2007
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi Guys, thanks very much for the help,

much appreciated!






Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing Saxon XSLT Processor booboop BOOK: Beginning XML 3rd Edition 2 April 7th, 2011 12:10 PM
[xslt 2.0] Discrepancy between Altova and Saxon-B luchm XSLT 3 December 4th, 2008 11:19 AM
XSLT not executing java function(using SAXON) dved XSLT 2 January 15th, 2008 08:46 PM
xslt-process-mode and Saxon 8 thalliley XSLT 7 October 11th, 2005 11:01 AM
How does XSLT work? vpkamath XSLT 2 October 22nd, 2003 03:21 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.