1/14 ITApplications XML Module Session 2: Using and Creating XML Documents
2/14 Using and Creating XML Documents XML is comprised of elements, attributes and values. XML is comprised of elements, attributes and values. An XML document should start with a processing instruction that indicates the version of XML that is used. An XML document should start with a processing instruction that indicates the version of XML that is used. Example: Example:
3/14 Every XML document has a “root element” that contains every element in the document. Every XML document has a “root element” that contains every element in the document. Each element must have a matching closing tag Each element must have a matching closing tag Elements can contain elements or character data Elements can contain elements or character data Example: Example: <money><currency><code>USD</code><exchangerate> </exchangerate> US Dollars US Dollars </currency><currency><code>EUR</code><exchangerate> </exchangerate><description>Euro</description></currency></money> Filename: Currency.xml Filename: Currency.xml
4/14 XML tags are case sensitive, always use lower case to describe your elements. XML tags are case sensitive, always use lower case to describe your elements. Comments may be expressed in XML and the syntax is similar to HTML comments. Comments may be expressed in XML and the syntax is similar to HTML comments.Example: <software> Microsoft Windows XP Microsoft Windows XP Microsoft Office 2003 Microsoft Office 2003 Altova XMLSpy 2006 Altova XMLSpy 2006 </software> Filename: SoftwareVersion.xml
5/14 Elements can have attributes Elements can have attributes Attributes are added to the start tag of an element Attributes are added to the start tag of an element Attributes must have a value Attributes must have a value Cannot have duplicate attribute names on an element Cannot have duplicate attribute names on an element Attributes can contain significant data, be reference markers or “describe” the data in the element (meta-data) Attributes can contain significant data, be reference markers or “describe” the data in the element (meta-data)
6/14 Example: Example: <exchangerate>1.2500</exchangerate></month> <exchangerate>1.2500</exchangerate></month> <exchangerate>1.2600</exchangerate></month></index></currencyforecast> Filename: CurrencyForecast.xml
7/14 XML Documents must be well-formed XML Documents must be well-formed Matching start and end tags for elements Matching start and end tags for elements Elements must not overlap Elements must not overlap Example of an invalid XML document: Example of an invalid XML document: </example> One root element One root element Example of an invalid XML document: Example of an invalid XML document: <incorrect></incorrect> <incorrect></incorrect>
8/14 Attribute values must be quoted Attribute values must be quoted <stafflist> <firstname>Eran</firstname><surname>Stern</surname></name></stafflist> Filename: AttributeExample.xml
9/14 Element may not have two attributes with the same name. Element may not have two attributes with the same name. Example of an invalid xml document: <stafflist> <firstname>Eran</firstname><surname>Stern</surname></name></stafflist> Filename: InvaildXMLExample.xml Filename: InvaildXMLExample.xml
10/14 Comments must not appear inside tags Comments must not appear inside tags “<“ and “&” cannot appear “unescaped”. “<“ and “&” cannot appear “unescaped”. Escape characters: Escape characters: & & & & ' ' ' ' > > > > < < < < " " " "
11/14 Make use of Elements or Attributes? Elements can describe structure as attributes can not. Elements can describe structure as attributes can not. It is not possible for attributes to contain multiple values. Elements can contain child elements which can contain multiple values. It is not possible for attributes to contain multiple values. Elements can contain child elements which can contain multiple values. Elements can be extended as attributes may not. Elements can be extended as attributes may not. In general, it easier to manipulate XML elements rather than attributes in most programming languages. In general, it easier to manipulate XML elements rather than attributes in most programming languages.
12/14 Validating XML documents An XML document with correct syntax is called a XML document. An XML document with correct syntax is called a WELL FORMED XML document. XML documents may be validated against Document Type Definitions (DTD) or XML Schemas. DTDs will be covered in more details in the next two sessions. XML documents may be validated against Document Type Definitions (DTD) or XML Schemas. DTDs will be covered in more details in the next two sessions.
13/14 There are various XML validators: There are various XML validators: W3Schools on-line XML validator W3Schools on-line XML validator Browsers such as Microsoft Explorer have a built-in XML parser. Browsers such as Microsoft Explorer have a built-in XML parser. Altova's XMLSPY. This software can be installed on your machine and can assist you in creating and validating XML files. You can download a trial copy from the following link: Altova's XMLSPY. This software can be installed on your machine and can assist you in creating and validating XML files. You can download a trial copy from the following link: Equivalent tools that run on Mac OS X and Linux: Equivalent tools that run on Mac OS X and Linux:
14/14 References Useful links: Useful links: penguin.dcs.bbk.ac.uk/academic/xml/more-xml/index.php penguin.dcs.bbk.ac.uk/academic/xml/more-xml/index.php penguin.dcs.bbk.ac.uk/academic/xml/more-xml/index.php webdesign.about.com/library/nosearch/bl_xmlclass1-2.htm webdesign.about.com/library/nosearch/bl_xmlclass1-2.htm webdesign.about.com/library/nosearch/bl_xmlclass1-2.htm