Home : Network Programming
Experiments with XML

(For viewing the XML examples in this page, you will require a recent browser (IE 6, Netscape 7, Mozilla). People in CSA can use Mozilla 1.0 in CL or Netscape 7 (under /usr/local/netscape/) in Litec )

For a change, I am not going to write a tutorial on Extensible Markup Language (XML). There are lots of good sites on the internet where you can learn about XML. I'll assume you are atleast familiar with the basics of XML (see references given below). Those who know HTML and some programming should not have great difficulty in following the rest of this page.

Being a music lover, the first thing that came to my mind when I wanted to create an XML document was song lyrics. It would be really nice to have a repository of XML documents containing lyrics of my favourite songs. The first task was to decide the format of the document. The format which came naturally is shown below :

   <song>
      <title>Title of song</title>
      <artists>
         <artist>Name of Artist</artist>
             ...more artists...
      </artists>
      <album>Name of Album</album>
      <year>Year in which song was released</year>
      <lyrics>
         <stanza>
             <line>a line of the song</line>
                  ...more lines...
         </stanza>
         ...more stanzas...
      </lyrics>
   </song>

Basically, what the above code means is that each song has a title, one or more artists, an album, a year of release and lyrics. The lyrics are composed of multiple stanzas and each stanza is composed of multiple lines.

I decided to start with the song "Everything is Broken" by Bob Dylan. The xml file created for the song is shown below.

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

   <song>
      <title>Everything is Broken</title>
      <artists>
          <artist>Bob Dylan</artist>
      </artists>
      <album>Oh Mercy</album>
      <year>1989</year>
      <lyrics>
          <stanza>
          <line>Broken lines, broken strings,</line>
          <line>Broken threads, broken springs, </line>
          <line>Broken idols, broken heads,</line>
          <line>People sleeping in broken beds.</line>
          <line>Ain't no use jiving</line>
          <line>Ain't no use joking</line>
          <line>Everything is broken.</line>
          </stanza>
          <stanza>
          <line>Broken bottles, broken plates,</line>
          <line>Broken switches, broken gates,</line>
          <line>Broken dishes, broken parts,</line>
          <line>Streets are filled with broken hearts.</line>
          <line>Broken words never meant to be spoken,</line>
          <line>Everything is broken.</line>
          </stanza>
          <stanza>
          <line>Seem like every time you stop and turn around</line&  gt;
          <line>Something else just hit the ground </line>
          <line>Broken cutters, broken saws,</line>
          <line>Broken buckles, broken laws,</line>
          <line>Broken bodies, broken bones,</line>
          <line>Broken voices on broken phones.</line>
          <line>Take a deep breath, feel like you're chokin', </line>  ;
          <line>Everything is broken.</line>
          </stanza>
          <stanza>
          <line>Every time you leave and go off someplace </line>
          <line>Things fall to pieces in my face</line>
          <line>Broken hands on broken ploughs,</line>
          <line>Broken treaties, broken vows,</line>
          <line>Broken pipes, broken tools,</line>
          <line>People bending broken rules.</line>
          <line>Hound dog howling, bull frog croaking,</line>
          <line>Everything is broken.</line>
          </stanza>
      </lyrics>
   </song>

The XML file is also available here . Depending on your browser, you may have to view the page-source to see the xml file correctly.

To display the XML file in the browser, a stylesheet had to be created using the Extensible Stylesheet Language (XSL). The XSL stylesheet I created is given below.

   <?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="song">
     <html>
     <body style="background-color: #ffffff">
     <span style="text-align: center">
       <div style="font-size: x-large; color: #00008b">
          <b><xsl:value-of select="title"/></b>
       </div>   by

       <div style="font-size: large; color: #00008b">
       <xsl:for-each select="artists/artist">
          <xsl:if test="position()!=last()">
             <xsl:value-of select="."/>,
          </xsl:if>
          <xsl:if test="position()=last()">
             <xsl:value-of select="."/>
          </xsl:if>
       </xsl:for-each>
       </div><br />

       <xsl:for-each select="lyrics/stanza">
           <p >
          <xsl:for-each select="line">
             <xsl:value-of select="."/><br />
          </xsl:for-each>
          </p>
       </xsl:for-each>
       </span>

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

The stylesheet can also be viewed here.

The XML file was modified to use the above stylesheet. The result can be seen here. The only modification was the addition of a line below the xml declaration in the XML file. The line added is shown below.

   <?xml-stylesheet type="text/xsl" href="lyrics.xsl"?>

I know, it may not seem like a big deal at first, but it really makes a lot of sense separating the data from its presentation. This is all I have time for today. Some day I plan to create a huge searchable database of lyrics. See you then.

Further References

For those of you who want to learn XML, here are some good links.


Back to Network Programming Valid XHTML 1.0! Valid CSS!