XML Primer
Basics of XML coding for web development
Current trends in web development may be summarized in three letters - XML. Technologies change with each day and there are innumerable developments but the incorporation of XML into all aspects of the web is one of today's most important Internet related issues. The widespread use of XML, Extensible Markup Language, furthers portability and platform-independence causes. XML allows data to be stored the same way regardless of DBMS, hardware, or the Operating System. An XML file looks the same on windows, a Mac, or a Linux system. A primer on XML is a very good way to complete a couse on developing web applications.
XML is much more flexible than HTML. XML files may describe any piece of data. For example, the orders in our order database table would be represented in the following way in XML.
XML File Example
<?xml version="1.0" encoding="ISO-8859-1"?>
<orderlist>
<order>
<orderid>1</orderid>
<orderuser>customerA</orderuser>
<orderitem>redrose</orderitem>
<orderdate>2006-10-30</orderdate>
<ordercost>199.98</company>
<orderquantity>2</price>
</order>
<order>
<orderid>2</orderid>
<orderuser>customerB</orderuser>
<orderitem>mixed</orderitem>
<orderdate>2006-10-15</orderdate>
<ordercost>49.99</company>
<orderquantity>1</price>
</order>
</orderlist>
We can define the correct format and structure of an XML file in an XML Schema. XML Schemas are very powerful. They represent a way to make the XML akin to a database table = schemas contain type definitions, attributes and other data definition elements. Also, a large XML file can be tested against its schema to see if all its fields conform to the schema. XML data types are universal. For example, the date data type in XML is always of the form YYYY-MM-DD. Date format differ across countries and are represented differently in various DBMS but the XML representation of a date is uniform.
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="order">
<xs:complexType>
<xs:sequence>
<xs:element name="orderid" type="xs:integer"/>
<xs:element name="orderuser" type="xs:string"/>
<xs:element name="orderuser" type="xs:string"/>
<xs:element name="orderdate" type="xs:date"/>
<xs:element name="ordercost" type="xs:string"/>
<xs:element name="orderquantity" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Above is a schema for the orders XML file (orders.xsd). Note that the main element 'order' is denoted using 'complextype'. The 'sequence' directive means that the elements within order should appear in the exact sequence specified in the schema. The data type of each element is also specified. If an order contained an alphabet in the orderquantity element, an error would be thrown at the time of validation with the schema.
Also, XML may be transformed to HTML or any other markup using XSL Style Sheets transformations. Following is the XSL to HTML transformation (orders.xsl) for the orders XML file. Note that the for loop iterates through each order.
<?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>All Orders</h2>
<table border="1">
<tr >
<th align="left">OrderID</th>
<th align="left">Customer</th>
<th align="left">Item</th>
<th align="left">Date</th>
<th align="left">Cost</th>
<th align="left">Quantity</th>
</tr>
<xsl:for-each select="orderlist/order">
<tr>
<td><xsl:value-of select="orderid"/></td>
<td><xsl:value-of select="orderuser"/></td>
<td><xsl:value-of select="orderitem"/></td>
<td><xsl:value-of select="orderdate"/></td>
<td><xsl:value-of select="ordercost"/></td>
<td><xsl:value-of select="orderquantity"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Using the XML, XSL, and Schema, this HTML document may be displayed on an XML-capable browser without any separate code, scripts or generators. This is a measure of the power of XML. Far more complex operations may be performed using just schemas and transformers. The following references to the schema and XSL should be added to the top of the XML document. The references assume that the schema and XSL file are in the same directory as the XML file. However, providing a complete URL for the schemalocation attribute is recommended.
<?xml version="1.0" encoding="ISO-8859-1"?> <?xml-stylesheet type="text/xsl" href="orders.xsl"?> <orderlist xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="orders.xsd">
Entire Web Applications may be represented in XML. Examine the ASP .NET example again. After this primer on XML, you will see that the entire file conforms to XML format. The .NET framework is in fact heavily XML based. Many technologies are moving into the XML umbrella. A broad conjecture that most computing related data and technology will be represented in XML some time in the future and that technology and platform differences will become a thing of the past is not far off the mark.