1 Linking in XML A way of creating hyperlinks in XML documents, Internal-Link and External-Link.
1/14 ITApplications XML Module Session 2: Using and Creating XML Documents.
-
Upload
samuel-york -
Category
Documents
-
view
216 -
download
0
Transcript of 1/14 ITApplications XML Module Session 2: Using and Creating XML Documents.
1/14
ITApplications ITApplications XML ModuleXML Module
Session 2: Session 2: Using and Creating XML Using and Creating XML
DocumentsDocuments
2/14
Using and Creating XML DocumentsUsing and Creating XML Documents
XML is comprised of elements, attributes XML is comprised of elements, attributes and values.and values.
An XML document should start with a An XML document should start with a processing instructionprocessing instruction that indicates the that indicates the version of XML that is used.version of XML that is used.
Example:Example:<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>
3/14
Every XML document has a “root element” that contains Every XML document has a “root element” that contains every element in the document.every element in the document.
Each element must have a matching closing tagEach element must have a matching closing tag Elements can contain elements or character dataElements can contain elements or character data Example:Example:
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>
<money><money><currency><currency>
<code>USD</code><code>USD</code><exchangerate>1.77659</exchangerate><exchangerate>1.77659</exchangerate><description>US Dollars</description><description>US Dollars</description>
</currency></currency><currency><currency>
<code>EUR</code><code>EUR</code><exchangerate>1.47045</exchangerate><exchangerate>1.47045</exchangerate><description>Euro</description><description>Euro</description>
</currency></currency></money></money>
Filename: Filename: Currency.xmlCurrency.xml
4/14
XML tags are case sensitive, always use lower case to describe XML tags are case sensitive, always use lower case to describe your elements.your elements.
Comments may be expressed in XML and the syntax is similar to Comments may be expressed in XML and the syntax is similar to HTML comments.HTML comments.
Example:Example:
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><software><software> <!-- The license attribute indicates the number of Client licenses --><!-- The license attribute indicates the number of Client licenses -->
<cd id="WinXP" license="3">Microsoft Windows XP</cd><cd id="WinXP" license="3">Microsoft Windows XP</cd><cd id="Office2003" license="5">Microsoft Office 2003</cd><cd id="Office2003" license="5">Microsoft Office 2003</cd><cd id="XmlSpy" license="1">Altova XMLSpy 2006</cd><cd id="XmlSpy" license="1">Altova XMLSpy 2006</cd>
</software></software>
Filename: SoftwareVersion.xmlFilename: SoftwareVersion.xml
5/14
Elements can have attributesElements can have attributes Attributes are added to the start tag of an Attributes are added to the start tag of an
elementelement Attributes must have a valueAttributes must have a value Cannot have duplicate attribute names on Cannot have duplicate attribute names on
an elementan element Attributes can contain significant data, be Attributes can contain significant data, be
reference markers or “describe” the data in reference markers or “describe” the data in the element (meta-data)the element (meta-data)
6/14
Example:Example:
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?><currencyforecast title="Currency Forecast" year="2005"><currencyforecast title="Currency Forecast" year="2005">
<index id="EUR/USD"><index id="EUR/USD"><month id="January"><month id="January">
<exchangerate>1.2500</exchangerate><exchangerate>1.2500</exchangerate></month></month><month id="March"><month id="March">
<exchangerate>1.2500</exchangerate><exchangerate>1.2500</exchangerate></month></month><month id="December"><month id="December">
<exchangerate>1.2600</exchangerate><exchangerate>1.2600</exchangerate></month></month>
</index></index></currencyforecast></currencyforecast>
Filename: CurrencyForecast.xmlFilename: CurrencyForecast.xml
7/14
XML Documents XML Documents mustmust be be well-formedwell-formed
Matching start and end tags for elementsMatching start and end tags for elements
Elements must not overlapElements must not overlap Example of an Example of an invalidinvalid XML document: XML document:
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?> <example><example>
<incorrect><incorrect><element><element></incorrect></incorrect></element></element></example></example>
One root elementOne root element
Example of an Example of an invalidinvalid XML document: XML document:
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?> <example><example>
<incorrect></incorrect><incorrect></incorrect> </example></example> <example><example>
<incorrect></incorrect><incorrect></incorrect> </example></example>
8/14
Attribute values must be quotedAttribute values must be quoted
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>
<stafflist><stafflist>
<name id=<name id="124899""124899">>
<firstname>Eran</firstname><firstname>Eran</firstname>
<surname>Stern</surname><surname>Stern</surname>
</name></name>
</stafflist></stafflist>
Filename: Filename: AttributeExample.xmlAttributeExample.xml
9/14
Element may not have two attributes with the Element may not have two attributes with the same name.same name.
Example of an Example of an invalidinvalid xml document: xml document:
<?xml version="1.0" encoding="UTF-8"?><?xml version="1.0" encoding="UTF-8"?>
<stafflist><stafflist>
<name id=<name id="124899" "124899" id=id="NI7877DW""NI7877DW">>
<firstname>Eran</firstname><firstname>Eran</firstname>
<surname>Stern</surname><surname>Stern</surname>
</name></name>
</stafflist></stafflist>
Filename: InvaildXMLExample.xmlFilename: InvaildXMLExample.xml
10/14
Comments must not appear inside tagsComments 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?Make use of Elements or Attributes?
Elements can describe structure as attributes Elements can describe structure as attributes can not.can not.
It is not possible for attributes to contain multiple It is not possible for attributes to contain multiple values. Elements can contain child elements values. Elements can contain child elements which can contain multiple values.which can contain multiple values.
Elements can be extended as attributes may Elements can be extended as attributes may not.not.
In general, it easier to manipulate XML elements In general, it easier to manipulate XML elements rather than attributes in most programming rather than attributes in most programming languages. languages.
12/14
Validating XML documentsValidating XML documents
An XML document with correct syntax is An XML document with correct syntax is called a called a WELL FORMED XML document. XML document.
XML documents may be validated against XML documents may be validated against Document Type Definitions (DTD) or XML Document Type Definitions (DTD) or XML Schemas. DTDs will be covered in more Schemas. DTDs will be covered in more details in the next two sessions.details in the next two sessions.
13/14
There are various XML validators:There are various XML validators:
W3Schools on-line XML validatorW3Schools on-line XML validatorwww.w3schools.com/dom/dom_validate.asp
Browsers such as Microsoft Explorer have a built-in XML parser.Browsers such as Microsoft Explorer have a built-in XML parser.
Altova's Altova's XMLSPYXMLSPY. This software can be installed on your . This software can be installed on your machine and can assist you in creating and validating XML files.machine and can assist you in creating and validating XML files.You can download a trial copy from the following link: You can download a trial copy from the following link:
www.altova.com/download_components.html
Equivalent tools that run on Mac OS X and Linux:Equivalent tools that run on Mac OS X and Linux: www.oxygenxml.com
www.exchangerxml.comwww.exchangerxml.com
14/14
ReferencesReferences
Useful links:Useful links: penguin.dcs.bbk.ac.uk/academic/xml/more-xml/index.phppenguin.dcs.bbk.ac.uk/academic/xml/more-xml/index.php webdesign.about.com/library/nosearch/bl_xmlclass1-2.htmwebdesign.about.com/library/nosearch/bl_xmlclass1-2.htm www.w3schools.com/xml/xml_elements.aspwww.w3schools.com/xml/xml_elements.asp www.w3schools.com/xml/xml_attributes.aspwww.w3schools.com/xml/xml_attributes.asp