1
votes

I have a problem between XML and XSL files. In XML file, there are some elements such as
*<school>
<student studentID="12345">
<name>Stud I</name>
<takes>CMPE471</takes>
<takes>CMPE412</takes>
<takes>CMPE100</takes>
</student>

<student studentID="67890">
<name>Stud II</name>
<takes>CMPE471</takes>
<takes>CMPE412</takes>
</student>

<course courseCode="CMPE471">
<courseName>NAME I </courseName>
<description>DESC I </description>
</course>

<course courseCode="CMPE412">
<courseName>NAME II </courseName>
<description>DESC II </description>
</course>

<course courseCode="CMPE100">
<courseName>NAME III </courseName>
<description>DESC III </description>
</course>

In XSL file,I want to reach "description" element which I specified "courseCode".
Output should be like this,
1. Stud I
     a. CMPE471 Desc I
     b. CMPE412 Desc II
     c. CMPE100 Desc III

2. Stud II
     a. CMPE471 Desc I
     b. CMPE412 Desc II


In XSL file, I tried to write something :



<ol>
<xsl:for-each select="/school/student">
<xsl:sort data-type="text" order="ascending" select="name"/>

<li><xsl:value-of select="name"/>

<ol type="a">
<xsl:for-each select="takes">
<xsl:sort data-type="text" select="text()" order="ascending"/>
<li>

<xsl:for-each select="/school/course">//PROBLEM
<xsl:value-of select="description [@courseCode = text()]"/>//PROBLEM
</xsl:for-each>//PROBLEM

</li> </xsl:for-each> </ol> </xsl:for-each> </ol>
Thanks.

1
You should indent your code to make it look like code. Also, the closing </school> tag is missing.djc

1 Answers

0
votes

See my XSLT 1.0 and 2.0 solution below. The problem in your code is:

<xsl:value-of select="description [@courseCode = text()]"/>

This means that you are looking for the element description whose attribute courseCode is the same as its text value. What I do is to save the course code in a variable and check for course elements whose attribute is the same as this variable.

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
  <xsl:output indent="yes" />

  <xsl:template match="/">
    <ol>
      <xsl:for-each select="/school/student">
        <xsl:sort data-type="text" order="ascending" select="name" />
        <li>
          <xsl:value-of select="name" />
          <ol type="a">
            <xsl:for-each select="takes">
              <xsl:sort data-type="text" select="." order="ascending" />
              <li>
                <xsl:variable name="coursecode" select="." />
                <xsl:value-of select="concat('(',.,') ')" />
                <xsl:for-each select="/school/course[@courseCode = $coursecode]">
                  <xsl:value-of select="description" />
                </xsl:for-each>

              </li>
            </xsl:for-each>
          </ol>
        </li>
      </xsl:for-each>
    </ol>
  </xsl:template>
</xsl:stylesheet>