XML
EXtensible Markup Language
Agenda
Introduction to XML XML Rules XML Elements XML Attributes XML Validation XML Exercises XML Namespaces XML CDATA XML Q&A XML Quiz
Introduction to XML
What is XML?XML stands for EXtensible Markup LanguageXML was designed to describe dataXML tags are not predefined. You can define
your own tags XML uses a Document Type Definition (DTD)
or an XML Schema to describe the data XML is a W3C (World Wide Web Consortium)
Recommendation
Introduction to XML
What is the main difference between XML and HTML?XML was designed to describe data and to
focus on what data is. HTML was designed to display data and to
focus on how data looks. HTML is about displaying information, while
XML is about describing information.
Introduction to XML
Why XML is free and extensible?XML was created to structure, store and to send
information. XML tags are not predefined. You can "invent"
your own tags.The author of HTML documents can only use
tags that are defined in the HTML standard (like <p>, <h1>, etc.).
But the author of XML documents can define his own tags and his own document structure.
Note : XML is not a replacement for HTML.
Introduction to XML
Sample XML document<?xml version="1.0"?> <note>
<to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>
</note>
Introduction to XML XML is going to be everywhere. What are the other technologies which are related
to the XML? XSLT - EXtensible Stylesheet Language
Transformations XSL-FO - EXtensible Stylesheet Language Formatting
Objects XPath XQuery XLink XML Schema Many more…
XML Rules
What are the XML rules?All XML Elements Must Have a Closing TagXML Tags are Case SensitiveXML Elements Must be Properly NestedXML Documents Must Have a Root ElementXML Attribute Values Must be QuotedWith XML, White Space is PreservedComments in XML
XML Rules
All XML Elements Must Have a Closing TagDo not have a closing tag - Incorrect<p>This is a paragraph <p>This is another paragraph
Correct <p>This is a paragraph</p> <p>This is another paragraph</p>
XML Tags are Case SensitiveIncorrect<Message>This is incorrect</message> Correct <message>This is correct</message>
XML Rules
XML Elements Must be Properly NestedIncorrect<b><i>This text is bold and italic</b></i>
Correct <b><i>This text is bold and italic</i></b>
XML Documents Must Have a Root ElementCorrect<root>
<child> <subchild>.....</subchild>
</child> </root>
XML Rules
XML Attribute Values Must be QuotedIncorrect<?xml version="1.0"?>
<note date=12/11/2002><to>Tove</to><from>Jani</from>
</note>
Correct <?xml version="1.0"?>
<note date="12/11/2002"><to>Tove</to><from>Jani</from>
</note>
XML Rules
With XML, White Space is PreservedWith XML, the white space in your document is
not truncated.
Comments in XMLThe syntax for writing comments in XML is
similar to that of HTML. <!-- This is a comment -->
XML Elements
XML Elements are ExtensibleXML documents can be extended to carry more
information.XML documents are Extensible.
XML Elements have RelationshipsElements are related as parents and children.<book>
<title>My First XML</title><prod id="33-657" media="paper"></prod><chapter>Introduction to XML
<para>Wat is HTML</para><para>What is XML</para>
</chapter></book>
XML Elements Elements have Content
Elements can have different content types. An element can have,
element content mixed content simple content or empty content Attributes
Element Naming Names can contain letters, numbers, and other
characters Names must not start with a number or punctuation
character Names must not start with the letters xml (or XML, or
Xml, etc) Names cannot contain spaces
XML Attributes
Attributes are used to provide additional information about elements.
Attribute values must always be enclosed in quotes, but either single or double quotes can be used.
<person sex="female">or <gangster name='George "Shotgun" Ziegler'>or<gangster name="George 'Shotgun' Ziegler">
XML Validation XML with correct syntax is Well Formed
XML. XML documents must have a root element XML elements must have a closing tag XML tags are case sensitive XML elements must be properly nested XML attribute values must always be quoted
XML validated against a DTD or a XML schema is Valid XML. A "Valid" XML document is a "Well Formed"
XML document,
XML Exercises BIT Subjects
Year 1 Year 2 Year 3
Information Systems & Technology
Object Oriented Systems Development
Professional Issues in IT
Computer Systems I Mathematics for Computing-II Security of Information Systems
Application Packages Operational Research Final Year Project
Internet & World Wide Web Business Management Intelligent Systems
Mathematics for Computing I Web Development Techniques Internet Application Development
Programming I Data Communications & Networks
Image Processing & Computer Vision
Database Systems I Software Project Management Systems & Network Administration
Systems Analysis & Design Rapid Application Development Advanced Database Management Systems
Convert above table into XML document.
XML Exercises Check the Well-Formedness of following elements
<list><title>The first list</title><item>An item</list>
<item>An item</item><item>Another item</item>
<para>Bathing a cat is a <emph>relatively</emph> easy task as long as the cat is willing.</para>
<bibl><title>How to Bathe a Cat<author></title>Merlin Bauer<author></bibl>
XML Exercises Create a well-formed XML document using
following elements
<PROCEDURE><TITLEHow to Bathe a Cat</TITLE><OVERVIEW>This procedure tells you how to bathe a cat. <WARNING></OVERVIEW>Cats don't like to take baths. You could get hurt doing this. Be sure to obtain all the required protective gear before you start. </WARNING><EQUIPEMENT><ITEM>Hockey Mask <ITEM>Padded Full-body Kevlar Armor</ITEM><ITEM>Tub full of warm water</ITEM><ITEM>Towels </ITEM><ITEM>First Aid kit</ITEM><ITEM>Cat Shampoo</ITEM> <EQUIPMENT><INSTRUCTIONS> <STEP> Locate the cat, who by now is hiding under the bed.</STEP><STEP>Place the cat in the tub of water.</STEP> <ITEM>Using the First Aid kit, repair the damage to your head and arms.</STEP> <STEP>Place the cat back in the tub and hold it down.</STEP> <STEP>Wash it really fast, then make an effort to dry it with the towels.</STEP> <STEP>Decide not to do this again. </STEP> </INSTRUCTIONS>
XML Namespaces
XML Namespaces provide a method to avoid element name conflicts.
Name Conflicts<!-- This table element carries information in a table <table>
<tr> <td>Apples</td> <td>Bananas</td>
</tr> </table> <!-- This table element carries information about a table <table>
<name>African Coffee Table</name> <width>80</width> <length>120</length>
</table>
XML Namespaces
XML Namespaces provide a method to avoid element name conflicts.
Name Conflicts<!-- This table element carries information in a table <table>
<tr> <td>Apples</td> <td>Bananas</td>
</tr> </table> <!-- This table element carries information about a table <table>
<name>African Coffee Table</name> <width>80</width> <length>120</length>
</table>
XML Namespaces Solving Name Conflicts Using a Prefix No name conflict, use a different name for their
<table> element (<h:table> and <f:table>).
<!-- This table element carries information in a table <h:table>
<h:tr> <h:td>Apples</h:td> <h:td>Bananas</h:td>
</h:tr> </h:table> <!-- This table element carries information about a table <f:table>
<f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length>
</f:table>
XML Namespaces Using Namespaces Instead of using only prefixes, we have added an
xmlns attribute to the <table> tag to give the prefix a qualified name associated with a namespace.
<!-- This table element carries information in a table <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> <!-- This table element carries information about a table <f:table xmlns:f="http://www.w3schools.com/furniture">
<f:name>African Coffee Table</f:name> <f:width>80</f:width> <f:length>120</f:length>
</f:table>
XML Namespaces
The XML namespace attribute is placed in the start tag of an element
The only purpose is to give the namespace a unique name.
xmlns:namespace-prefix="namespaceURI"
XML CDATA All text in an XML document will be parsed
by the parser. Only text inside a CDATA section will be
ignored by the parser.
<!-- Generate an error -> <message>if salary < 1000 then</message>
<!– Working properly <message>if salary < 1000 then</message>
Illegal XML characters have to be replaced by entity references.
There are 5 predefined entity references in XML
XML CDATA
Note: Only the characters "<" and "&" are strictly illegal in XML. Apostrophes, quotation marks and greater than signs are legal, but it is a good habit to replace them.
< < less than
> > greater than
& & ampersand
' ' apostrophe
" " quotation mark
XML CDATA
CDATAEverything inside a CDATA section is ignored
by the parser.<script>
<![CDATA[ function matchwo(a,b) {
if (a < b && a < 0) then {return 1
} else { return 0
} }
]]> </script>
XML Q&A
XML Quiz1. What does XML stand for?
X-Markup LanguageeXtensible Markup LanguageExample Markup LanguageeXtra Modern Link
2. There is a way of describing XML data, how?XML uses a DTD to describe the dataXML uses XSL to describe dataXML uses a description node to describe data
3. XML's goal is to replace HTMLTrueFalse
4. What is the correct syntax of the declaration which defines the XML version?
<?xml version="1.0"?><?xml version="1.0" /><xml version="1.0" />
XML Quiz5. What does DTD stand for?
Document Type DefinitionDirect Type DefinitionDo The DanceDynamic Type Definition
6. Is this a "well formed" XML document?<?xml version="1.0"?><note>
<to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body>
</note>
NoYes
XML Quiz7. Which statement is true?
All XML documents must have a DTDAll XML elements must be lower caseAll the statements are trueAll XML elements must be properly closed
8. Is this a "well formed" XML document?<?xml version="1.0"?><to>Tove</to><from>Jani</from><heading>Reminder</heading><body>Don't forget me this weekend!</body>
YesNo
9. Which statement is true?XML documents must have a root tagXML elements must be properly nestedXML tags are case sensitiveAll the statements are true
XML Quiz10. XML preserves white spaces
FalseTrue
11. Is this a "well formed" XML document?<?xml version="1.0"?><note>
<to age="29">Tove</to><from>Jani</from>
</note>
NoYes
12.XML elements cannot be emptyFalseTrue
XML Quiz13. Is this a "well formed" XML document?
<?xml version="1.0"?><note><to age=29>Tove</to><from>Jani</from></note>
NoYes
14. Which is not a correct name for an XML element?<h1><1dollar><Note>All 3 names are incorrect
XML Quiz15. Which is not a correct name for an XML element?
All 3 names are incorrect<NAME><first name><age>
16. Which is not a correct name for an XML element?<phone number><xmldocument><7eleven>All 3 names are incorrect
17. XML attribute values must always be enclosed in quotesTrueFalse
XML Quiz18. What does XSL stand for?
eXtensible Style ListingeXtensible Stylesheet LanguageeXtra Style LanguageeXpandable Style Language
19. For the XML parser to ignore a certain section of your XML document, which syntax is correct?
<![CDATA[ Text to be ignored ]]><PCDATA> Text to be ignored </PCDATA><xml:CDATA[ Text to be ignored ]><CDATA> Text to be ignored </CDATA>
20.Which is not a correct name for an XML element? <first name> <age> <NAME>