All elements must be nested

page 12 But an XML parser will throw an exception when it sees this document. If you want the same effect, you would need to code this: bI really, ireallyibi like XML.i

1.2.2.3 All attributes must be quoted

You can quote the attributes with either single quotes or double quotes. These two XML tags are equivalent: a href=http:www.oreilly.com a href=http:www.oreilly.com If you need to define an attribute with the value , you can use single quotes inside double quotes, as we just did. If you need both single and double quotes in an attribute, use the predefined entities quot; for double quotes and apos; for single quotes. One more note: XML doesnt allow attributes without values. In other words, HTML elements like ol compact arent valid in XML. To code this element in XML, youd have to give the attribute a value, as in ol compact=yes .

1.2.2.4 XML tags are case-sensitive

In HTML, h1 and H1 are the same. In XML, theyre not. If you try to end an h1 element with H1 , the parser will throw an exception.

1.2.2.5 All end tags are required

This is another area where most HTML documents break. Your browser doesnt care whether you dont have a p or br tag, but your XML parser does.

1.2.2.6 Empty tags can contain the end marker

In other words, these two XML fragments are identical: lily age=6lily lily age=6 Notice that there is nothing, not even whitespace, between the start tag and the end tag in the first example; thats what makes this an empty tag.

1.2.2.7 XML declarations

Some XML documents begin with an XML declaration. An XML declaration is a line similar to this: ?xml version=1.0 encoding=ISO-8859-1? If no encoding is specified, the XML parser assumes youre using UTF-8, a Unicode standard that uses different numbers of bytes to represent virtually every character and ideograph from the worlds languages. Be aware that each parser supports a different set of encodings, so you need to check your parsers documentation to find out what your options are.

1.2.2.8 Document Type Definitions DTDs and XML Schemas

All of the rules weve discussed so far apply to all XML documents. In addition, you can use DTDs and Schemas to define other constraints for your XML documents. DTDs and Schemas are metalanguages that let you define the characteristics of an XML vocabulary. For example, you might want to specify that any XML document describing a purchase order must begin with a po element, and the po element in turn contains a customer-id element, one or page 13 more item-ordered elements, and an order-date element. In addition, each item- ordered element must contain a part-number attribute and a quantity attribute. Heres a sample DTD that defines the constraints we just mentioned: ?xml version=1.0 encoding=UTF-8? ELEMENT po customer-id , item-ordered+ , order-date ELEMENT customer-id PCDATA ELEMENT item-ordered EMPTY ATTLIST item-ordered part-number CDATA REQUIRED quantity CDATA REQUIRED ELEMENT order-date EMPTY ATTLIST order-date day CDATA REQUIRED month CDATA REQUIRED year CDATA REQUIRED And heres an XML Schema that defines the same document type: ?xml version=1.0 encoding=UTF-8? xsd:schema xmlns:xsd=http:www.w3.org200010XMLSchema xsd:element name=po xsd:complexType xsd:sequence xsd:element ref=customer-id xsd:element ref=item-ordered maxOccurs=unbounded xsd:element ref=order-date xsd:sequence xsd:complexType xsd:element xsd:element name=customer-id type=xsd:string xsd:element name=item-ordered xsd:complexType xsd:attribute name=part-number use=required xsd:simpleType xsd:restriction base=xsd:string xsd:pattern value=[0-9]{5}-[0-9]{4}-[0-9]{5} xsd:restriction xsd:simpleType xsd:attribute xsd:attribute name=quantity use=required type=xsd:integer xsd:complexType xsd:element xsd:element name=order-date xsd:complexType xsd:attribute name=day use=required xsd:simpleType xsd:restriction base=xsd:integer xsd:maxInclusive value=31 xsd:restriction xsd:simpleType xsd:attribute xsd:attribute name=month use=required xsd:simpleType xsd:restriction base=xsd:integer xsd:maxInclusive value=12 xsd:restriction xsd:simpleType xsd:attribute xsd:attribute name=year use=required xsd:simpleType