OK, so you may have heard about XML, and you may even know what it stands for, but what you need uis a realworld example. During this article I will tell you about a recent project that I worked on, and how XML was useful.
I suppose that I should introduce XML before I dive into any examples. XML stands for Extensible Markup Language and, in a way, it's a bit like HTML. But, unlike HTML that has fixed tags such as <p> and <font> with fixed attributes <font face="Arial"> XML allows you to create your own tags and attributes. In this way you can build a structered data file where the tags and attributes describe that data they contain.
A few weeks ago I was involved in a project where an existing application needed to be upgraded. The existing system worked by gathering a large amount of data, formatting and outputting it to an HTML page.
The problem was that this process took quite a long time, and only produced one HTML page. The idea was to get the program to output the raw data to a structered XML file, and then use a set of XSL files (XML style sheets) to format the data into different HTML pages. And I only had two weeks to do it in!
As you can see, I was having to learn all about XML and XSL and therefore I feel it would be good to share this knowledge.
In my view, without XSL (XSL is basically like a stylesheet, except it has its own syntax) XML is not very practical. XSL allows you to extract the data of a tag and format it however you want.
Say you have an XML document that looks like this:
<person> <name>Sam Huggill</name></person>
This is very simple, but with XSL you can extractthe value in-between the <name> and </name> tags and formatit. We extract the value of a tag using the <xsl:value-of> tag:
(Notice the closing />. This means that the tag does not need another </xsl:value-of> tag). Easy.
But what happens when we have a more complicated XML document:
<people> <person> <name>Sam Huggill</name> <email>firstname.lastname@example.org</email> </person> <person> <name>Yourname</name> <email>youremail</email> </person></people>
For this we will have to set up a loop to write the data from both <person> tags. We loop using the <xsl:for-each> tag:
<xsl:for-eachselect="people/person"> <xsl:value-of select="name"/> <xsl:value-of select="email"/></xsl:for-each>
Attributes can make things a lot easier. For example, our previous XMl document could be changed so that it uses attributes quite easily:
<people> <person name="Sam Huggill"email="email@example.com"/> <person name="Sam Huggill"email="firstname.lastname@example.org"/>> </people>
We can read attributes by using the <xsl:value-of> tag and prefixing the attribute name with an @ sign:
Our loop now looks like this:
<xsl:for-eachselect="people/person"> <xsl:value-of select="@name"/> <xsl:value-of select="@email"/> </xsl:for-each>
In XSL, you can encapsulate code in a "template". This acts rather like a function, and can be very useful. Here is a typical template:
<xsl:template name="demo"match="people/person"> <xsl:value-of select="@name"/> <xsl:value-of select="@email"/></xsl:template>
You can call a template using the <xsl:call-template> tag.
We can now rework our loop to use a template:
<xsl:template name="person"match="people/person"> <xsl:value-of select="@name"/> <xsl:value-of select="@email"/></xsl:template><xsl:template match="/"><xsl:for-eachselect="people/person"> <xsl:call-template name="person"/></xsl:for-each>
In XSL, you do have variables but you cannot assign them values at runtime, so I suppose it would be more approriate to call them constants. XSL does have one useful function that returns a number which corressponds to the number of times a loop has occurred:
Anyway, getting back to variables, you declare them like this:
To get the value of a variable use:
As you have seen both of these technologies show great promise for the web, but how can we use them for VB? Well, instead of the XML file containing numbers, why not contain Visual Basic code? And, instead of outputting to an HTML file, why not output to a Visual Basic class module?
Using XML you can create smart database classes that work on a database schema. For example, take a look at the following XML document:
<tables> <table name="people"> <columns> <column name="id"value="1"/> <column name="name" value="SamHuggill"/> </columns> </table> </table>
This shows a simple database structure that you can use to generate VB code from.
Resources for XML
Here are some useful resources that you should check out:
XT - James Clarks implementation of an XML parser(IE5 comes with one but I think that this is better)
XML Zone - The DevX have set up a web site espically for XML
XML on MSDN - If you're using the XML parser that comes with IE5, you better look here
XML at the W3C - The home of XML and all web related technologies. Check here for the latest developments and the XML/XSL specifications.
Oh well, I am afraid that's about all you're going to get. This article has by no means covered all that you need to know, but hopefully it has shown you why these technologies are useful. Take a look at the resources I have listed and download the zip file which contains a sample XML and XSL document.
Download the files