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 December 14th, 2007, 12:39 PM
Registered User
 
Join Date: Dec 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT: ONE template to transform all the elements

Hi,

I am new in this forum. I've started learnig XML and XSLT recently. There are many doubts I have and our exam is also next week. Can anyone pls help me out. Here is my problem:

Assignment 13.1:

Create an XML document which contains information about three IT-courses in Bite program. Create an XSLT document which transforms this XML document to an XHTML document. Use an independent template for each element type.

Assignment 13.2:

Create another XSLT document for the same XML document you created in assignment 13.1. Use this time only one template that does the transformation and another empty template to avoid the use of default template for elements.



Pls tell me how can I have one template for transformng the whole document

Thnks.
Neha


 
Old December 14th, 2007, 12:48 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

I guess you course tutor is asking you to implement the transformation using nested xsl:for-each instructions. This isn't a good way of doing it -- perhaps they are hoping that you will discover this when you attempt it. But it seems you discovered it without trying.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old December 14th, 2007, 12:51 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Can you show us the XML document that you created for the assignment?
As for using just one template that will then probably result in using xsl:for-each or xsl:for-each-group instead of xsl:apply-templates.

 
Old December 14th, 2007, 12:58 PM
Registered User
 
Join Date: Dec 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks, I was also thinking about for-each.

here is the xml doc

<?xml version="1.0" encoding="iso-8859-1" standalone="no"?>
<?xml-stylesheet type="text/xsl"
href="./courseTransform.xsl" ?>

<!DOCTYPE courses [
  <!ELEMENT courses (course*)>
  <!ELEMENT course (info, advisors*)>
  <!ELEMENT advisors (#PCDATA)>
  <!ATTLIST course code CDATA #REQUIRED>
  <!ATTLIST course type (elective | compulsory) #REQUIRED>
  <!ELEMENT info (name, extent, timing, language, level)>
  <!ELEMENT name (#PCDATA)>
  <!ELEMENT extent (#PCDATA)>
  <!ELEMENT timing (#PCDATA)>
  <!ELEMENT language (#PCDATA)>
<!ELEMENT level (#PCDATA)>

]>

<courses>
  <course code="SYS1TF048" type="compulsory">
    <info>
      <name>Developing Information Systems</name>
      <extent>18cr</extent>
      <timing>Semester 3</timing>
      <language>english</language>
      <level>core studies</level>
    </info>
    <advisors>Markku Tarkki</advisors>
    <advisors>Ulla Vanhanen</advisors>
    <advisors>Juha Pispa</advisors>
    <advisors>Seija Wolfer</advisors>
  </course>

  <course code="MAT1TF001" type="compulsory">
    <info>
      <name>Applied Mathematics</name>
      <extent>4 cr </extent>
      <timing>Semester 3</timing>
      <language>English</language>
      <level>Core studies</level>
    </info>
    <advisors>
      Kalevi Keinänen

    </advisors>
  </course>

  <course code="SYS8TF060" type="elective">
    <info>
      <name>Information System Requirements Engineering</name>
      <extent>6 cr</extent>
      <timing>Semester 6 - 7</timing>
      <language>English</language>
      <level>professional studies</level>
    </info>
    <advisors>
      Kirsti Jalasoja
    </advisors>
  </course>
</courses>


 
Old December 14th, 2007, 01:19 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Once you have the XML document you now need to design an XHTML document structure you want transform that XML to, for instance an XHTML table showing the details for each course in a row. Then you need to write the stylesheet, for instance using xsl:for-each over the course elements. Try that, if you run into problems then show us the XHTML you want to output and your XSLT attempt and tell us where exactly you have problems.

 
Old December 14th, 2007, 03:35 PM
Registered User
 
Join Date: Dec 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

I have created the xhtml and xslt now. <advisors> element is more than once. And i want them all to be printed in the table. However my code prints only the first advisor and ignores the others (code marked in red color). Please help. Please also comment if my code is unprofessional as I have just begun learning this.

<?xml version="1.0" encoding="iso-8859-1"?>

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

<xsl:template match="/">
    <html>
      <head>
        <link href="courses.css" rel="stylesheet" type="text/css" />
        <title>Bite courses</title>
      </head>
    <body>
      <h2>BITE courses</h2>


      <xsl:for-each select="//course">

        <h4>
          <xsl:value-of select=".//name" />
          (<xsl:value-of select="./@code" />)
        </h4>
        <table>
          <tr>
            <td>Extent:</td>
            <td>
              <xsl:value-of select=".//extent" />
            </td>
          </tr>
          <tr>
            <td>Timing:</td>
            <td>
              <xsl:value-of select=".//timing" />
            </td>
          </tr>
          <tr>
            <td>Language:</td>
            <td>
              <xsl:value-of select=".//language" />
            </td>
          </tr>
          <tr>
            <td>Level:</td>
            <td>
              <xsl:value-of select=".//level" />
            </td>
          </tr>
          <tr>
            <td>Type:</td>
            <td>
              <xsl:value-of select="./@type" />
            </td>
          </tr>
          <tr>
            <td class="adv">Advisors</td>
            <td class="adv"></td>
          </tr>
          <tr>
            <td class="adv1">
              <xsl:value-of select=".//advisors" />
            </td>
            <td class="adv1"></td>
          </tr>
        </table>


      </xsl:for-each>

    </body>
    </html>
</xsl:template>

</xsl:stylesheet>



 
Old December 15th, 2007, 08:45 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

As for the advisors problem, with XSLT/XPath 1.0 the string value of a node-set with several nodes is the string value of the first node, that is why it only shows the first advisor. To fix that you can use a second for-each instead of the value-of e.g.
Code:
<xsl:for-each select="advisors">
  <xsl:value-of select="."/>
  <xsl:if test="position() != last()">
    <xsl:text>, </xsl:text>
  </xsl:if>
</xsl:for-each>
As for "unprofessional" coding, you have made your task easy by using a lot of // or .//. That saves typing and is a working solution but you should be aware that // or .// is expensive in general so most people will tell you to avoid if possible and in all your uses you could as well spell out the paths you want to select.

And as already pointed out, the one template solution is not a flexible solution but as it was your assignment to create it you can't change that.


 
Old December 15th, 2007, 06:49 PM
Registered User
 
Join Date: Dec 2007
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks so much for your help. I can see all the advisors now. Thanks also for your feedback. But this was the way we were taught in the lessons. What is the other way instead of // you mentioned? give some example pls. if u dont mind.

Thanks a lot.
Neha



 
Old December 15th, 2007, 07:31 PM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Instead of //course you could use /courses/course.
Instead of .//name you could use info/name.

/- Sam Judson : Wrox Technical Editor -/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Implement HTML elements in my template with Zend snowbydave1 PHP How-To 0 October 25th, 2008 02:00 AM
XSLT Time transform blackest XSLT 10 April 1st, 2008 03:07 AM
transform in a loop vs recursive template ramarc XSLT 3 April 10th, 2006 04:40 PM
transform in xslt kgoldvas XSLT 1 January 24th, 2006 07:14 AM
XSLT Transform scubaduba C# 0 November 30th, 2004 06:31 AM





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