XML

So you want be a master of markup languages? Well XML is the way to start, this is not only used for webpages but for all development projects,
this could be used as a init script for a web/business application, or some init files for a game. The use for XML is endless, but let’s get on to coding.

You always start the document like this:

 <?xml version="1.0"?>

but this stuff you can easily get from http://www.w3schools.com/xml/ , so let’s get cracking with something usefull.

 <?xml version="1.0"?>
<personell>
  <employee>
     <name>
        <forname>Katherine</forname>
        <surname>Boghart</surname>  
     </name>
  </employee>
</personell>

This is basically all you need to be able to create your own XML documents, just add your own tags and remember to quit them off by a matching </endtag>

So you want to add a .css stylesheet? No problem! We just add the following:

 <?xml version="1.0"?>
<?xml-stylesheet type="text/css" href="urStyleSheet.css"?>
<personell>
  <employee>
     <name>
        <forname>Katherine</forname>
        <surname>Boghart</surname>  
     </name>
  </employee>
</personell>

So urStyleSheet.css will be like this:


forname{
  display  : block;
  font-size: 12pt;
  color    : black;
}

lastname{
  display   : block;
  font-size : 12pt;
  color     : black;
}

or:

forname, lastname {
   display   : block;
   font-size : 12pt;
   color     : black;
}

so yeah.. If the XML documents get large, it is probably best to introduce XSL which will help you display the page the way you want it. But this time, instead of .css, we use the extension .xsl and of course type has changed to “text/xsl”. But else everything is unchanged. The following file will still be called personell.xml:


<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="personell.xsl"?>
  <personell>
   <employee>
     <name>
       <forname>Katherine</forname>
       <surname>Boghart</surname>
      </name>
    </employee>
  </personell>

this file you save as personell.xsl


<?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>
  <body>
  <h2>Personell</h2>
 <table border="1">
    <tr>
      <th>Forname:</th>
      <th>Surname:</th>
    </tr>
    <xsl:for-each select="personell/employee">
    <tr>
       <td><xsl:value-of select="name/forname"/></td>
       <td><xsl:value-of select="name/surname"/></td>
    </tr>
   </xsl:for-each>
  </table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

ok, so what does this file do once it’s linked to the xml file? Well, it will first search for the root element to get all the children once that is done, a for each is being called to get all elements that matches /personell/employee, in this example it is only 1 match, however if you add more employees the table will just be longer.

Seeing that your now on /personell/employee, you will need to define the next child which is /name, but we want the value off forname/surname so it will be: <xsl:value-of select=”name/forname”/> instead off <xsl:value-of select=”forname”/> which will get you nothing at all. In this example you could use for-each select=”personell/employee/name” but in the next you probably won’t get away as easily.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s