University of Bahrain

Information Technology College

Computer Science Department

Semester 1, Year 2006/2007

ITCS 373: Class Lab Work

XML2

 

 

Instructor: Dr. Eshaa M. Alkhalifa

 

You should have completed your first XML and XSL scripts by now and know how to write a basic XML file.

 

  1. Start a new document with HTML Kit.  You can look back into your notes to know what initial statements to write to define your XML file.
  2. Copy the following into your file;

<CATALOG>

  <CD>

    <TITLE>Empire Burlesque</TITLE>

    <ARTIST>Bob Dylan</ARTIST>

    <COUNTRY>USA</COUNTRY>

    <COMPANY>Columbia</COMPANY>

    <PRICE>10.90</PRICE>

    <YEAR>1985</YEAR>

  </CD>

  <CD>

    <TITLE>Hide your heart</TITLE>

    <ARTIST>Bonnie Tylor</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>CBS Records</COMPANY>

    <PRICE>9.90</PRICE>

    <YEAR>1988</YEAR>

  </CD>

  <CD>

    <TITLE>Greatest Hits</TITLE>

    <ARTIST>Dolly Parton</ARTIST>

    <COUNTRY>USA</COUNTRY>

    <COMPANY>RCA</COMPANY>

    <PRICE>9.90</PRICE>

    <YEAR>1982</YEAR>

  </CD>

  <CD>

    <TITLE>Still got the blues</TITLE>

    <ARTIST>Gary More</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Virgin redords</COMPANY>

    <PRICE>10.20</PRICE>

    <YEAR>1990</YEAR>

  </CD>

  <CD>

    <TITLE>Eros</TITLE>

    <ARTIST>Eros Ramazzotti</ARTIST>

    <COUNTRY>EU</COUNTRY>

    <COMPANY>BMG</COMPANY>

    <PRICE>9.90</PRICE>

    <YEAR>1997</YEAR>

  </CD>

  <CD>

    <TITLE>One night only</TITLE>

    <ARTIST>Bee Gees</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Polydor</COMPANY>

    <PRICE>10.90</PRICE>

    <YEAR>1998</YEAR>

  </CD>

  <CD>

    <TITLE>Sylvias Mother</TITLE>

    <ARTIST>Dr.Hook</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>CBS</COMPANY>

    <PRICE>8.10</PRICE>

    <YEAR>1973</YEAR>

  </CD>

  <CD>

    <TITLE>Maggie May</TITLE>

    <ARTIST>Rod Stewart</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Pickwick</COMPANY>

    <PRICE>8.50</PRICE>

    <YEAR>1990</YEAR>

  </CD>

  <CD>

    <TITLE>Romanza</TITLE>

    <ARTIST>Andrea Bocelli</ARTIST>

    <COUNTRY>EU</COUNTRY>

    <COMPANY>Polydor</COMPANY>

    <PRICE>10.80</PRICE>

    <YEAR>1996</YEAR>

  </CD>

  <CD>

    <TITLE>When a man loves a woman</TITLE>

    <ARTIST>Percy Sledge</ARTIST>

    <COUNTRY>USA</COUNTRY>

    <COMPANY>Atlantic</COMPANY>

    <PRICE>8.70</PRICE>

    <YEAR>1987</YEAR>

  </CD>

  <CD>

    <TITLE>Black angel</TITLE>

    <ARTIST>Savage Rose</ARTIST>

    <COUNTRY>EU</COUNTRY>

    <COMPANY>Mega</COMPANY>

    <PRICE>10.90</PRICE>

    <YEAR>1995</YEAR>

  </CD>

  <CD>

    <TITLE>1999 Grammy Nominees</TITLE>

    <ARTIST>Many</ARTIST>

    <COUNTRY>USA</COUNTRY>

    <COMPANY>Grammy</COMPANY>

    <PRICE>10.20</PRICE>

    <YEAR>1999</YEAR>

  </CD>

  <CD>

    <TITLE>For the good times</TITLE>

    <ARTIST>Kenny Rogers</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Mucik Master</COMPANY>

    <PRICE>8.70</PRICE>

    <YEAR>1995</YEAR>

  </CD>

  <CD>

    <TITLE>Big Willie style</TITLE>

    <ARTIST>Will Smith</ARTIST>

    <COUNTRY>USA</COUNTRY>

    <COMPANY>Columbia</COMPANY>

    <PRICE>9.90</PRICE>

    <YEAR>1997</YEAR>

  </CD>

  <CD>

    <TITLE>Tupelo Honey</TITLE>

    <ARTIST>Van Morrison</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Polydor</COMPANY>

    <PRICE>8.20</PRICE>

    <YEAR>1971</YEAR>

  </CD>

  <CD>

    <TITLE>Soulsville</TITLE>

    <ARTIST>Jorn Hoel</ARTIST>

    <COUNTRY>Norway</COUNTRY>

    <COMPANY>WEA</COMPANY>

    <PRICE>7.90</PRICE>

    <YEAR>1996</YEAR>

  </CD>

  <CD>

    <TITLE>The very best of</TITLE>

    <ARTIST>Cat Stevens</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Island</COMPANY>

    <PRICE>8.90</PRICE>

    <YEAR>1990</YEAR>

  </CD>

  <CD>

    <TITLE>Stop</TITLE>

    <ARTIST>Sam Brown</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>A and M</COMPANY>

    <PRICE>8.90</PRICE>

    <YEAR>1988</YEAR>

  </CD>

  <CD>

    <TITLE>Bridge of Spies</TITLE>

    <ARTIST>T`Pau</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Siren</COMPANY>

    <PRICE>7.90</PRICE>

    <YEAR>1987</YEAR>

  </CD>

  <CD>

    <TITLE>Private Dancer</TITLE>

    <ARTIST>Tina Turner</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>Capitol</COMPANY>

    <PRICE>8.90</PRICE>

    <YEAR>1983</YEAR>

  </CD>

  <CD>

    <TITLE>Midt om natten</TITLE>

    <ARTIST>Kim Larsen</ARTIST>

    <COUNTRY>EU</COUNTRY>

    <COMPANY>Medley</COMPANY>

    <PRICE>7.80</PRICE>

    <YEAR>1983</YEAR>

  </CD>

  <CD>

    <TITLE>Pavarotti Gala Concert</TITLE>

    <ARTIST>Luciano Pavarotti</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>DECCA</COMPANY>

    <PRICE>9.90</PRICE>

    <YEAR>1991</YEAR>

  </CD>

  <CD>

    <TITLE>The dock of the bay</TITLE>

    <ARTIST>Otis Redding</ARTIST>

    <COUNTRY>USA</COUNTRY>

    <COMPANY>Atlantic</COMPANY>

    <PRICE>7.90</PRICE>

    <YEAR>1987</YEAR>

  </CD>

  <CD>

    <TITLE>Picture book</TITLE>

    <ARTIST>Simply Red</ARTIST>

    <COUNTRY>EU</COUNTRY>

    <COMPANY>Elektra</COMPANY>

    <PRICE>7.20</PRICE>

    <YEAR>1985</YEAR>

  </CD>

  <CD>

    <TITLE>Red</TITLE>

    <ARTIST>The Communards</ARTIST>

    <COUNTRY>UK</COUNTRY>

    <COMPANY>London</COMPANY>

    <PRICE>7.80</PRICE>

    <YEAR>1987</YEAR>

  </CD>

  <CD>

    <TITLE>Unchain my heart</TITLE>

    <ARTIST>Joe Cocker</ARTIST>

    <COUNTRY>USA</COUNTRY>

    <COMPANY>EMI</COMPANY>

    <PRICE>8.20</PRICE>

    <YEAR>1987</YEAR>

  </CD>

</CATALOG>  

 

 

  1. Now write an XSL file to display this list with the following format.

 

 

  1. The values you need to know are as follows; border=2, bgcolor=#ffe4c4 and you have five columns in a table and the first row is composed of <th> table header tags.
  2. You should know by now, how to complete what is required.  If you do not remember, look at the first exercise you did in XML.
  3. Once you get the output shown above, you can start to alter it by using the various functions possible.
  4. Hiding some of the information: Go to your XSL file and delete the line that displays the YEAR and look at your output.  Rewrite the line that displays the year to display it again.
  5. Using Sort: Go to your XSL file and replace the line <xsl:for-each select="CATALOG/CD"> with the line <xsl:for-each select="CATALOG/CD" order-by="+ YEAR"> Save your file and check if the output of the XML file is sorted according to increasing years.
  6. Using Filter:  A filter allows only content that belongs to a particular group appear in the data.  We can implement a filter by altering the "CATALOG/CD" part of the for-each line to make it "CATALOG/CD[COUNTRY='UK']" This means that only the records that conform to that the country is UK will appear in the data.  Save you file and check your XML file to make sure your filter works.
  7. Using IF: We want to write the line with the information about Rod Stewart in bold while all others appear normally.  Go to your XSL file and add the lines

<xsl:if match=".[ARTIST='Rod Stewart']"> 

      <tr>

        <td><b><xsl:value-of select="TITLE"/></b></td>

        <td><b><xsl:value-of select="ARTIST"/></b></td>

        <td><b><xsl:value-of select="COUNTRY"/></b></td>

        <td><b><xsl:value-of select="COMPANY"/></b></td>

        <td><b><xsl:value-of select="PRICE"/></b></td>

        <td><b><xsl:value-of select="YEAR"/></b></td>

      </tr>

</xsl:if>

  1. Save your work and check the preview of the xml file to see that the record with Rod Stewart appears in bold but it appear twice.  Do you know why? Do not change it, let it appear twice to show that you did this part correctly.
  2. Using CHOOSE:  Choose allows you to display a group of items in one way and other groups other ways.  In our example we will have only two groups.
  3. Use the command structure as follows:

<xsl:choose>

            <xsl:when test="PRICE[. $le$ 9]">

                        <tr>

{Give this group the background color number #cccc66 and display all fields here}

                        </tr>

         </xsl:when>

 

         <xsl:otherwise>

                      <tr>

{display all fields without any change in the background from the original specified above this line of code}

                      </tr>

         </xsl:otherwise>

       </xsl:choose>

  1. Your output page must look like this;

 

 

  1. Notice that Rod Stewart must appear twice to show that you did the coding correctly.
  2. You are done!!