Web Services Made Easy With Delphi - Why use XML (eXtensible Markup Language)?
(Page 2 of 4 )
XML is an extensible mark-up language that enables you to identify and organize your information in a more accurate and flexible way. It is called extensible because it does not have a fixed format, as does HTML, which is an SML or Single Markup Language.
For example if you want text to appear bold in HTML you place the text between the "<b></b>" tags. Every browser knows what to do when they encounter these tags. If however, you change these tags to "<bold></bold>" then browsers would not know what to do with them, since they are only programmed to recognize the "<b></b>" as meaning bold.
In contrast to this, XML does not have any predefined tags; rather, it allows you to create your own tag sets to both display and format text. All you have to do is add a (XSL) stylesheet to enable the browser to format your text. This language can therefore be read and understood on any computer platform, and it is precisely because of this flexibility that web services uses XML to format its messages. The most visible use of XML is in RSS documents, which are used to make news content available on the Internet. Below is an example of an RSS document:
XML code:
<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="generic.xsl"?>
<!-- Copy and paste the url into your newsreader application" -->
<!-- generator="Movable Type/2.64" -->
<rss version="0.91">
<channel>
<item>
<title>RSS Syndication </title>
<link>http://www.examplesite.com</link>
<description> The UK newspaper that does not have an RSS
feed??</description>
</item>
<item>
<title>Education in Crisis??</title>
<link>http://www.examplesite.com</link>
<description>The problem of classroom shortage in UK Schools</description>
</item>
<item>
<title>RSS XML Reader</title>
<link>http://www.examplesite.com</link>
<description>A new program that reads xml files. </description>
</item>
</channel>
</rss>
The line "<?xml-stylesheet type="text/xsl" href="generic.xsl"?>" specifies the name of the style sheet that is used to format the information. In this case the file is called "generic.xsl." Below is the style sheet code that enables the XML code to be formatted:
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" encoding="iso-8859-1" omit-xml-
declaration="yes" indent="yes"/>
<xsl:template match="*">
<table border="1" width="600" align="center">
<tr><td valign="top" align="center" class="title"
bgcolor="silver" >
<a>
<xsl:attribute name="href">
<xsl:value-of select="*[local-name()='channel']/*[local-name()
='link']"/>
</xsl:attribute>
<xsl:attribute name="target">
<xsl:text>top</xsl:text>
</xsl:attribute>
<xsl:value-of select="*[local-name()='channel']/*[local-name()
='title']" disable-output-escaping="yes"/>
</a>
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="*[local-name()='channel']/*[local-name()
='lastBuildDate']"/>
</td></tr><tr><td valign="top" bgcolor="ghostwhite"
class="headlines" >
<ul>
<xsl:for-each select="//*[local-name()='item']">
<li>
<a>
<xsl:attribute name="href">
<xsl:value-of select="*[local-name()='link']"/>
</xsl:attribute>
<xsl:attribute name="target">
<xsl:text>top</xsl:text>
</xsl:attribute>
<xsl:value-of select="*[local-name()='title']" disable-output-
escaping="yes"/>
</a>
<xsl:text disable-output-escaping="yes">&nbsp;</xsl:text>
<xsl:value-of select="*[local-name()='description']" disable-
output-escaping="yes"/>
</li>
</xsl:for-each>
</ul>
</td></tr>
</table>
</xsl:template>
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>
I will not be explaining how style sheets are created because that is an entire subject in its own right. But here is what you get when you combine the two:

To summarize:
- XML gives you the ability to 'package' your information in anyway you like, i.e. you can create your own tag sets and still have the same advantages offered by HTML, like text formatting.
- XML is platform independent.
- XML is language independent, i.e. you can create a xml document in any language
Next: Why SOAP (Simple Object Access Protocol)? >>
More Delphi-Kylix Articles
More By Leidago