home personal workZone travel about
XML Basic - eXtra Messed up Life
Tired of the X in everything? .....Xml Xsl Xpath Xhtml Xslt saX Xform Xquery Xpointer Xlink seX..... I've added the last one! Try forming a pattern - hint: a simple crossword. I'm no book writer so see if you find anything useful to follow your obession for the X.
Things to note when writing XML:
  • Element names are Case Sensitive
  • A document may be well-formed but not valid
  • PUBLIC and SYSTEM identifiers: The SYSTEM identifier is used to locate a local resource and must be relative to the document, while the PUBLIC is a global URI
  • Special Attribute - XML:lang specifies a language for a tag
  • Special Attribute - XML:space "preserve" or "default" specifies if space should be preserved or left to the default setting of the parser
DTDs
There can four components of a DTD - ENTITY, ELEMENT, ATTLIST and NOTATION. A DTD and a corresponding XML file. Whats wrong in this one?
<!ELEMENT AddressBook (Contact)*>
<!ELEMENT Contact (Name, Address?, Telephone*, Mobile*, Email*, Homepage*)*>
<!ELEMENT Name (First+, Last*)>
<!ELEMENT Address (Street*, City*, Country*, PostCode*)>
<!ELEMENT Telephone (#PCDATA)>
<!ELEMENT Mobile (#PCDATA)>
<!ELEMENT Email (#PCDATA)>
<!ELEMENT Homepage (#PCDATA)>
<!ELEMENT First (#PCDATA)>
<!ELEMENT Last (#PCDATA)>
<!ELEMENT Street (#PCDATA)>
<!ELEMENT City (#PCDATA)>
<!ELEMENT Country (#PCDATA)>
<!ELEMENT PostCode (#PCDATA)>
<!DOCTYPE AddressBook SYSTEM "dtds/contacts.dtd" >
<AddressBook>
	<Contact>
		<Name>
		<First>Text</First>
			<Last>Text</Last>
		</Name>
		<Address>
			<Street>Text</Street>
			<City>Text</City>
			<Country>Text</Country>
			<PostCode>Text</PostCode>
		</Address>
		<Telephone>Text</Telephone>
		<Mobile>Text</Mobile>
		<Email>Text</Email>
		<Homepage>Text</Homepage>
	</Contact>
	<Contact>
		<Name>
			<First>Text</First>
			<Last>Text</Last>
		</Name>
		<Address>
			<Street>Text</Street>
			<City>Text</City>
			<Country>Text</Country>
			<PostCode>Text</PostCode>
		</Address>
		<Telephone>Text</Telephone>
		<Mobile>Text</Mobile>
		<Email>Text</Email>
		<Homepage>Text</Homepage>
	</Contact>
</AddressBook>					
It all depends on the DTD designer, data can be presented in any way so there is no one correct way!
XML Schema

XML Schemas (May 2001) are pretty new - as of time of writing this atleast so many of its parsers are still beta releases and there are few validators. A Schema validator XSV by University of Edinburgh is web-based and pretty ok, Xerces-J by Apache (C version also) is the parser I am working with and its open source so you can also extend it to suit your purpose. A XSD and a corresponding XML file.
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
	<xsd:annotation>
	 <xsd:documentation xml:lang="en">
	  Sample Stock Quote Schema, adapted from web and 
	  W3C XSD Primer Part 0
	 </xsd:documentation>
	</xsd:annotation>
	<!-- global element -->
	<xsd:element name="stockfile">
	 <xsd:complexType>
	  <xsd:sequence>
		<xsd:element name="mystock" type="mystockType" maxOccurs="unbounded"/>
	  </xsd:sequence>
	 </xsd:complexType>
	</xsd:element>
	<xsd:element name="comment" type="xsd:string"/>
	<!-- Main complex type -->
	<xsd:complexType name="mystockType">
		<xsd:sequence>
			<xsd:element name="symbol" type="xsd:string"/>
			<xsd:element name="company" type="companyAddrType"/>
			<xsd:element name="currentQuote" type="quoteType"/>
			<xsd:element name="lastCloseQuote" type="quoteType"/>
			<xsd:element name="change" type="xsd:double"/>
			<xsd:element name="volume" type="xsd:long"/>
			<xsd:element ref="comment" minOccurs="0"/>
		</xsd:sequence>
		<xsd:attribute name="dateBought" type="xsd:date"/>
	</xsd:complexType>
	<!-- Following two complex types nested inside Main -->
	<xsd:complexType name="quoteType">
		<xsd:all>
			<xsd:element name="date" type="xsd:date"/>
			<xsd:element name="time" type="xsd:time"/>
			<!-- date time may be in any order -->
		</xsd:all>
		<xsd:attributeGroup ref="priceAttrib"/>
	</xsd:complexType>
	<xsd:complexType name="companyAddrType">
		<xsd:sequence>
			<xsd:element name="name" type="xsd:string"/>
			<xsd:element name="address" type="xsd:string"/>
		</xsd:sequence>
		<xsd:attribute name="country" type="xsd:string" use="required"/>
	</xsd:complexType>
	<!-- Attrib group -->
	<xsd:attributeGroup name="priceAttrib">
		<xsd:attribute name="currentPrice" type="xsd:float" use="required"/>
		<xsd:attribute name="currency" type="xsd:string" default="SGD"/>
	</xsd:attributeGroup>
</xsd:schema>
			
<stockfile xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 xsi:noNamespaceSchemaLocation="../xsd/mystock.xsd">
<mystock dateBought="1967-08-13">
	<symbol>String</symbol>
	<company country="String">
		<name>String</name>
		<address>String</address>
	</company>
	<currentQuote currentPrice="3.14159" currency="SGD">
		<date>1967-08-13</date>
		<time>14:20:00-05:00</time>
	</currentQuote>
	<lastCloseQuote currentPrice="3.14159" currency="SGD">
		<date>1967-08-13</date>
		<time>14:20:00-05:00</time>
	</lastCloseQuote>
	<change>3.14159265358979</change>
	<volume>2147483647</volume>
	<comment>String</comment>
</mystock>
<mystock dateBought="1967-08-13">
	<symbol>String</symbol>
	<company country="String">
		<name>String</name>
		<address>String</address>
	</company>
	<currentQuote currentPrice="3.14159" currency="SGD">
		<date>1967-08-13</date>
		<time>14:20:00-05:00</time>
	</currentQuote>
	<lastCloseQuote currentPrice="3.14159" currency="SGD">
		<date>1967-08-13</date>
		<time>14:20:00-05:00</time>
	</lastCloseQuote>
	<change>3.14159265358979</change>
	<volume>2147483647</volume>
	<comment>String</comment>
</mystock>
</stockfile>
			
Again it all depends on the DTD designer, data can be presented in any way so there is no one correct way!