tags:

views:

591

answers:

3

given the following xml:

<student studentID="001"> 
    <dateOfBirth> 1-1-1990 </dateOfBirth>
    <name> Ayse Ozer </name>
    <sex> F </sex>
    <takes> CMPE351 </takes>
    <takes> CMPE111 </takes>
<takes> CMPE418 </takes>
 </student>

 <student studentID="002"> 
    <dateOfBirth> 2-2-1992 </dateOfBirth>
    <name> Bircan Korkmaz </name>
    <sex> F </sex>
    <takes> CMPE418 </takes>
    <takes> CMPE111 </takes>
<takes> CMPE352 </takes>
    </student>

<course courseCode="CMPE351">
     <courseName>
           Database systems I
     </courseName>
     <description>
         First course in databases
     </description>       
 </course>

 <course courseCode="CMPE111">
     <courseName>
           Introduction to C
     </courseName>
     <description>
         First course in programming
     </description>       
 </course>

 <course courseCode="CMPE352">
     <courseName>
           Database systems II
     </courseName>
     <description>
         Second course in databases
     </description>       
 </course>

 <course courseCode="CMPE418">
     <courseName>
           Internet Programming
     </courseName>
     <description>
         Programming for the Internet.
     </description>       
 </course>

I want output like that

Courses Taken By Students:

* Ayse Ozer
      o CMPE351 Database systems I
      o CMPE111 Introduction to C
* Bircan Korkmaz
      o CMPE418 Internet Programming
      o CMPE111 Introduction to C

I wrote this xsl code:

<xsl:template match = "school">
<head>
<title>Student</title>    
 </head>
 <body>
 <p>Courses Taken By Students:</p>
   <xsl:for-each select = "student">
 <ul>
 <li><xsl:value-of select = "name"/> </li>    
 <xsl:for-each select = "takes">
 <ul>
 <li><xsl:value-of select="text()" />
<xsl:value-of select = "../../course[@courseCode=text()]/courseName"/> </li>
 </ul> 
</xsl:for-each>
 </ul>  
    </xsl:for-each>
 </body>
 </xsl:template>


 <xsl:value-of select = "../../course[@courseCode=text()]/courseName"/>
 Problem about above line about text()
+1  A: 

The problem is the whitespace:

<takes> CMPE351 </takes>

should be:

<takes>CMPE351</takes>

I'll try to compensate... example on the way... To avoid a lot of problems (and improve performance), I'll also use an xsl index...


Fixed (caveat - it won't work with course codes with multiple spaces in the middle of them, but that seems unlikely):

<xsl:variable name="code" select="normalize-space()"/>
<ul>
  <li>
    <xsl:value-of select="$code"/>
    <xsl:value-of select="key('courses',$code)/courseName"/>
  </li>
</ul>

with (at the top) the xsl index:

<xsl:key name="courses" match="/school/course" use="@courseCode"/>
Marc Gravell
You are right the problem spaces in takes node. Also I changed text() to current() then it worked properly thx. <xsl:value-of select = "../../course[@courseCode=current()]/courseName"/>But your code is also working with spaces thx very much.
A: 

thank you for the codes

A: 

I worked up this example just before you already had a good answer. Since it's just a slightly different approach, I thought I'd post it anyways.


<?xml version="1.0" encoding="utf-8"?>

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

  <xsl:output method="html" indent="no" encoding="UTF-8" omit-xml-declaration="yes" />

  <xsl:key name="courses" match="course" use="@courseCode"/>

  <xsl:template match="school">
      <html>
        <body>
          <p>Courses taken by:</p>
          <ul>
            <xsl:apply-templates select="student"/>
          </ul>
        </body>
      </html>
  </xsl:template>

  <xsl:template match="student">
    <li>
      <xsl:value-of select="name"/>
      <ul>
        <xsl:apply-templates select="takes"/>
      </ul>
    </li>
  </xsl:template>

  <xsl:template match="takes">
    <li>
      <xsl:value-of select="."/>
      <xsl:value-of select="key('courses', normalize-space(.))/courseName"/>
    </li>
  </xsl:template>

</xsl:stylesheet> 

Mattio
That isn't very different... the same use of normalize-space and xsl:key - the only difference is the use of xsl:variable, which isn't an important difference.
Marc Gravell
True, but the slight difference I had in mind was the original use of xsl:for-each versus xsl:template here.
Mattio