PASTE NAVIGATION MENU CODE

Friday 9 March 2012

XML

| | with 0 comments |

XML
XML (eXtensible Markup Language) is a markup language which allows data to be transferred between different systems. The language is derived from SGML (Standard Generalized Markup Language), developed in the 80s as ISO8879. SGML defined a system of using tags to create the content of a document, and also defined key concepts such as hypertext and hyperlinks. When Sir Tim Berners Lee created the World Wide Web, he also created HTML, as subset of SGML which was to be used to created content for the WWW. 
Like HTML, XML is also a subset of SGML developed by a W3C working group. The first stable standard (XML 1.0) was published in 1998. The current version is XML 1.1 whilst XML 2.0 is in the pipeline. 
Taking over the Web
The popularity of XML rose, especially as a way to transfer data between different database systems or to maintain plain-text data stores for low query requirements.In the industry, a Document-Type Definition (DTD) is created which specifies the tags for a particular type of XML document. For example, a car manufacturer and parts supplier may agree on a DTD by which the manufacturer can view the supplier stock through XML. Once the DTD is created, XML documents conforming to the DTD can be created and also validated using automated tools. 
Eventually, a DTD for HTML was created, giving birth to XHTML. An XHTML document is actually just an XML document which conforms to the HTML DTD. The use of a DTD imposed further restrictions on HTML developers, such as the case of tags, the requirement to close tags, the requirement to enclose attributes in quotes and much more. Two versions of the HTML DTD were created; transitional and strict. More and more developers chose to adhere to the strict rules, meaning web browser developers could agree on one functionality of a particular set of tags, reducing web browser fragmentation. 
Ironically, DTDs are not XML-compliant. For this reason, W3C launched the XML Schema project in 2001, as a way of specifying types of XML documents using XML itself.
Language Structure
An XML document consists of a number of elements. Each element contains sub-elements and attributes. XML documents must conform to a number of rules, including:
  1. All XML elements must have a closing tag.
  2. XML tags are case sensitive.
  3. XML elements must be properly nested. 
  4. XML documents must have a root element. 
  5. XML attribute values must be properly quoted. 
An example of valid XML:
<?xml version="1.0" encoding="UTF-8"?>
<stock>
<part pid=”4567890”>
<pname>Standard Silencer</pname>
<pdesc>A standard silencer</pdesc>
<retailprice>27.89</retailprice>
<qty>200</qty>
</part>
<part pid=”0987653”>
<pname>Disc Brake</pname>
<pdesc>Standard 17 inch disc brake</pdesc>
<retailprice>178.22</retailprice>
<qty>50</qty>
</part>
</stock>
Listing 1.1 - stock.xml
Introducing XSLT
XSLT (XSL Transformations - XSL means eXtensible Stylesheet Language) is a companion language to XML which allows the transformation of XML on the fly. An example follows.
Consider the XML file in listing 1.1. The following XSLT code could be used:
<?xml version="1.0" encoding="ISO-8859-1"?>
<html xsl:version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
  <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE">
    <xsl:for-each select="stock/part">
      <div style="background-color:teal;color:white;padding:4px">
        <span style="font-weight:bold"><xsl:value-of select="pname"/></span>
        - <xsl:value-of select="retailprice"/>
      </div>
      <div style="margin-left:20px;margin-bottom:1em;font-size:10pt">
        <xsl:value-of select="pdesc"/>
        <span style="font-style:italic">
          <xsl:value-of select="qty"/> (Quantity in stock)
        </span>
      </div>
    </xsl:for-each>
  </body>
</html>
Listing 1.2 - transform.xsl
The above XSLT file would then be linked-to via the XML file, using a line such as:
<?xml-stylesheet type="text/xsl" href="transform.xsl" ?>
The result can be seen here <<TODO: Working link>>

Post a Comment

Please enter your comments here..

0 comments: