JavaScript by A. Sadat


Information in this document came from A Technical Introduction to XML, Norman Walsh and the  W3C Recommendation approved on 10th February 1998.

What is XML?

XML is a markup language for structured documentation.

Structured documents are documents that contain both content (words, pictures, etc.) and some indication of what role that content plays (for example, content in a section heading has a different meaning from content in a footnote, which means something different than content in a figure caption, etc.). Almost all documents have some structure.

A markup language is a mechanism to identify structures in a document. The XML specification defines a standard way of adding markup to documents.

XML documents are made up of storage units called entities, which contain either parsed or unparsed data. Parsed data is made up of characters, some of which form character data, and some of which form markup. Markup encodes a description of the document's storage layout and logical structure. XML provides a mechanism to impose constraints on the storage layout and logical structure.

Extensible Markup Language (XML) is the universal format for data on the Web. XML allows developers to easily describe and deliver rich, structured data from any application in a standard, consistent way. XML does not replace HTML; rather, it is a complementary format.

The design goals for XML are:

  1. XML shall be straightforwardly usable over the Internet.

  2. XML shall support a wide variety of applications.

  3. XML shall be compatible with SGML.

  4. It shall be easy to write programs which process XML documents.

  5. The number of optional features in XML is to be kept to the absolute minimum, ideally zero.

  6. XML documents should be human-legible and reasonably clear.

  7. The XML design should be prepared quickly.

  8. The design of XML shall be formal and concise.

  9. XML documents shall be easy to create.

  10. Terseness in XML markup is of minimal importance.

Example

<?xml version="1.0"?>
<portfolio xmlns:dt="urn:schemas-microsoft-com:datatypes">
	<stock exchange="nyse">
		<name>zacx corp</name>
		<symbol>ZCX</symbol>
		<price dt:dt="number">28.875</price>
	</stock>
	<stock exchange="NASDAQ">
		<name>4Qs corp</name>
		<symbol>QQQQ</symbol>
		<price dt:dt="number">32.50</price>
	</stock>
	<stock exchange="nyse">
		<name>qwerty corp</name>
		<symbol>QWT</symbol>
		<price dt:dt="number">21.375</price>
	</stock>
</portfolio>

Explanation of the example

<?xml version="1.0"?>
The document begins with a processing instruction: <?XML ...?>. This is the XML markup declaration. While it is not required, its presence explicitly identifies the document as an XML document and indicates the version of XML to which it was authored.

<name>zacx corp</name>
Elements are the most common form of markup. Delimited by angle brackets, most elements identify the nature of the content they surround. Some elements may be empty in which case they have no content(i.e <price/>). If an element is not empty, it begins with a start-tag, <element>, and ends with an end-tag, </element>.

<stock exchange="nyse">
Attributes are name-value pairs that occur inside tags after the element name. For example, <stock exchange="nyse""> is the stock element with the attribute exchange having the value nyse. In XML, all attribute values must be quoted.

<portfolio xmlns:dt="urn:scemas-microsoft-com:datatype">
Portfolio is an element that contains other elements. In this case, the portfolio element contains three elements of type stock. Portfolio, also, has an attribute "xmlns:dt". This is a special attribute which identifies the namespace. In the above XML code, the information about portfolio is of the "dt" namespace. This allows us to validate and process the information about the portfolio according to their specific type, making the data more flexible and precise.

Display XML in an HTML Document

By adding the following processing instruction <?xml-stylesheet type="text/xsl" href="stock.xsl"?> to the above XML document, the XML can be dispayed in an HTML document.

XML/XSL example

The processing directive contains a reference to the stock.xsl file. XSL is a language for expressing stylesheets. XSL Transformations is a language for transforming XML document. The stock.xsl file contains XSLT statements for processing the XML and HTML for formatting the processed XML.

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:template match="/">
    <HTML>
      <BODY>
        <TABLE BORDER="2">
          <TR>
            <TD>Symbol</TD>
            <TD>Name</TD>
            <TD>Price</TD>
          </TR>
          <xsl:for-each select="portfolio/stock">
            <TR>
              <TD><xsl:value-of select="symbol"/></TD>
              <TD><xsl:value-of select="name"/></TD>
              <TD><xsl:value-of select="price"/></TD>
            </TR>
          </xsl:for-each>
        </TABLE>
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>