Web technologies -- Laboratory 3 -- 2009-2010 -- info.uvt.ro

XML edit

XML is derived from SGML (Standard Generalized Markup Language - ISO 8879) and has a plain text format designed to be human readable (although a binary version is under consideration by W3C). There are two versions 1.0 (5th edition) and 1.1 (2nd edition). The former one is widely used and still recommended for usage.

In the last years it has started to play an important role in the exchange of data on the Web (Web services, SOAP protocol).

The W3C specifies both the lexical grammar and the parsing prerequisites for it. An XML document needs to have two main characteristics:

  • well-formed: in general every XML document can be represented as a tree with only one root element (or document element), and each start-tag must have a corresponding end-tag. Additionally, the root element can be proceeded by an XML declaration which specifies the version of XML being used, the character encoding and optional external dependencies. Each element can contain several attributes with values enclosed in quotes (either double or single).
  • valid: it must be compliant with a particular XML schema or DTD (Document Type Definition).

Example of an XML:

<?xml version="1.0" encoding="ISO-8859-1"?>
	<queue>
		<link>
			<name>Paul Graham -- The Roots of Lisp</name>
			<uri>http://www.paulgraham.com/rootsoflisp.html</uri>
		</link>
		<link>
			<name>LL1: Lightweight Languages Workshop</name>
			<uri>http://ll1.ai.mit.edu/</uri>
		</link>
		<link>
			<name>The Java Language Environment</name>
			<uri>http://java.sun.com/docs/white/langenv/</uri>
		</link>
		<link>
			<name>The Evolution of Lisp</name>
			<uri>http://citeseer.ist.psu.edu/steele93evolution.html</uri>
		</link>
		<link>
			<name>Delimited continuations in operating systems</name>
			<uri>http://okmij.org/ftp/papers/context-OS.pdf</uri>
		</link>
		<link>
			<name>Pascal Costanza's Highly Opinionated Guide to Lisp</name>
			<uri>http://p-cos.net/lisp/guide.html</uri>
		</link>
	<!--
		<link>
			<name></name>
			<uri></uri>
		</link>
	-->
	</queue>

Example of DTD (for the previous XML):

	<!ELEMENT queue (link*)>
	<!ELEMENT link (name,uri)>
	<!ELEMENT name (#PCDATA)>
	<!ELEMENT uri (#PCDATA)>

Supposing we call this DTD queue.dtd we validate an XML against it by placing:

	<!DOCTYPE queue SYSTEM "queue.dtd">

under:

	<?xml version="1.0" encoding="ISO-8859-1"?>

Example of XML Schema (for the previous XML):

<?xml version="1.0" encoding="utf-8"?>
	<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
	<xs:element name="queue">
		<xs:complexType>
			<xs:sequence>
				<xs:element name="link" />
				<xs:complexType>
					<xs:element name="name" type="xs:string" />
					<xs:element name="uri" type="xs:string" />
				</xs:complexType>
			</xs:sequence>
		</xs:complexType>
	</xs:element>
</xs:schema>

Supposing we call this XML Schema file queue.xsd we validate the XML document by placing:

	<queue xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="queue.xsd">

under:

	<?xml version="1.0" encoding="ISO-8859-1"?>

Links:

XSL edit

XSL (Extensible Stylesheet Language) is used to refer to a family of languages used for transforming and rendering XML documents.

It is nowadays used to express many things including:

  • XSLT: language for transforming of the XML document
  • XSL-PO: language for visually editing the document (generating PDFs)
  • XPath: non-XML language used to address XML elements (or parts of it). Used mainly from inside XSLT.

For example given the previous XML the following XPath query:

	/queue/link

selects all the links in the document.

Links:

XSLT edit

XSLT (XSL Transformations) is used for transforming XML documents. As far as we are concerned it can be used to produce HTML documents out of XML ones

Example:

<?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="/queue">
		<html>
			<head>
				<title>Reading queue</title>
			</head>
			<body>
				<ul>
					<xsl:for-each select="./link">
						<li>
							<strong><xsl:value-of select="./name" /></strong>
							<a>
								<xsl:attribute name="href">
									<xsl:value-of select="./uri" />
								</xsl:attribute>
								<xsl:value-of select="./uri" />
							</a>
						</li>
					</xsl:for-each>
				</ul>
			</body>
		</html>
		</xsl:template>
	</xsl:stylesheet>

Supposing we call this XSLT file queue.xsl we transform the previous XML document by placing:

	<?xml-stylesheet type="text/xsl" href="queue.xsl"?>

under:

	<?xml version="1.0" encoding="ISO-8859-1"?>

We obtain the following HTML document:

	<html>
		<head>
			<title>Reading queue</title>
		</head>
		<body>
			<ul>
				<li>
					<strong>Paul Graham -- The Roots of Lisp</strong>
					<a href="http://www.paulgraham.com/rootsoflisp.html">http://www.paulgraham.com/rootsoflisp.html</a>
				</li>
				<li>
					<strong>LL1: Lightweight Languages Workshop</strong>
					<a href="http://ll1.ai.mit.edu/">http://ll1.ai.mit.edu/</a>
				</li>
				<li>
					<strong>The Java Language Environment</strong>
					<a href="http://java.sun.com/docs/white/langenv/">http://java.sun.com/docs/white/langenv/</a>
				</li>
				<li>
					<strong>The Evolution of Lisp</strong>
					<a href="http://citeseer.ist.psu.edu/steele93evolution.html">http://citeseer.ist.psu.edu/steele93evolution.html</a>
				</li>
				<li>
					<strong>Delimited continuations in operating systems</strong>
					<a href="http://okmij.org/ftp/papers/context-OS.pdf">http://okmij.org/ftp/papers/context-OS.pdf</a>
				</li>
				<li>
					<strong>Pascal Costanza's Highly Opinionated Guide to Lisp</strong>
					<a href="http://p-cos.net/lisp/guide.html">http://p-cos.net/lisp/guide.html</a>
				</li>

			</ul>
		</body>
	<html>

Links:

Exercises edit

Create a database like XML (like the one in the example from above) file with:

  • at least 10 record like elements (for example the link element);
  • each record should have at least 4 attributes (for example the name or uri);
  • each record should have at least 3 sub-records (for example I could extend the link elements to also contain multiple comment elements);

Create an XSLT file -- that is used by the previous XML file -- to transform the database into a HTML file. Each information available in the XML file should be presented in the generated HTML.

The XML should be validated against both a DTD and XML Schema (they need to be created).