<?xml version="1.0"?> <?xml version="1.0" encoding="ISO-8859-1"?> The content type of the response of CGI scripts to return XML data must be set to "text/xml". XSL style sheets are used to transform XML documents into other formats, like HTML. -------------------------------------------------------------------------------- XML with correct syntax is "Well Formed" XML. XML validated against a DTD is "Valid" XML. A "Well Formed" XML document is a document that conforms to the XML syntax rules: o XML documents must begin with the XML declaration o XML documents must have a root element o XML elements must have a closing tag o XML tags are case sensitive o XML elements must be properly nested o XML attribute values must always be quoted XML Documents Must Have a Root Element All XML documents must contain a single tag pair to define a root element. All other elements must be within this root element. All elements can have sub elements (child elements). Sub elements must be correctly nested within their parent element: <root> <child> <subchild>.....</subchild> </child> </root> XML Notes o With XML, White Space is Preserved (not a rule, just a fact) o With XML, CR / LF is Converted to LF o Comments in XML are the same as in HTML With XML, a new line is always stored as LF. In Windows applications, a new line is normally stored as a pair of characters: carriage return (CR) and line feed (LF). The character pair bears some resemblance to the typewriter actions of setting a new line. In Unix applications, a new line is normally stored as a LF character. Macintosh applications use only a CR character to store a new line. Elements can have different content types. An XML element is everything from (including) the element's start tag to (including) the element's end tag. An element can have element content, mixed content, simple content, or empty content. An element can also have attributes. XML elements must follow these naming rules: o Names can contain letters, numbers, and other characters o Names must not start with a number or punctuation character o Names must not start with the letters xml (or XML, or Xml, etc) o Names cannot contain spaces Data can be stored in child elements or in attributes, for example: <person sex="female"> <firstname>Anna</firstname> <lastname>Smith</lastname> </person> <person> <sex>female</sex> <firstname>Anna</firstname> <lastname>Smith</lastname> </person> In the first example sex is an attribute. In the last, sex is a child element. Both examples provide the same information. There are no rules about when to use attributes, and when to use child elements. Use child elements if the information feels like data, use attributes if the data feels like metadata (data about data). Examples of DATE Data, Each Valid XML Syntax: <note date="12/11/2002"> <note> <date>12/11/2002</date> </note> <note> <date> <day>12</day> <month>11</month> <year>2002</year> </date> </note> Some of the problems with using attributes are: o attributes cannot contain multiple values (child elements can). o attributes are not easily expandable (for future changes). o attributes cannot describe structures (child elements can). o attributes are more difficult to manipulate by program code. o attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document. -------------------------------------------------------------------------------- XML Namespaces Since element names in XML are not predefined, a name conflict will occur when two different documents use the same element names. To prevent name conflicts, namespaces can be used. <namespace-prefix:XMLTag xmlns:namespace-prefix="http://path/to/rddl"> <h:table xmlns:h="http://www.w3.org/TR/html4/"> <h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td> </h:tr> </h:table> Note that the address used to identify the namespace is not used by the parser to look up information. The only purpose is to give the namespace a unique name. Default Namespaces Defining a default namespace for an element saves us from using prefixes in all the child elements. It has the following syntax: xmlns="namespaceURI" <table xmlns="http://www.w3.org/TR/html4/"> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table> When you start using XSL, you will soon see namespaces in real use. Many of the tags in the XSL stylesheet contain the "xsl" prefix which at the begginging will have a namespace defined.