XML

Developer.com content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More.

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:

<xsl:value-ofselect="/person/name"/>

(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>sam@vbsquare.com</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="sam@vbsquare.com"/>     <person name="Sam Huggill"email="sam@vbsquare.com"/>> </people> 

We can read attributes by using the <xsl:value-of> tag and prefixing the attribute name with an @ sign:

 <xsl:value-ofselect="people/person/@name"> <xsl:value-ofselect="people/person/@email"> 

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:

<xsl:for-eachselect="people/person">    <xsl:value-ofselect="position()"/></xsl:for-each>

Anyway, getting back to variables, you declare them like this:

<xsl:variable name="myvar">myvalue</xsl:variable>

To get the value of a variable use:

<xsl:value-of select="$myvar"/>

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:

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Latest Posts

Related Stories