Introduction To Xml

1,614 views

Published on

This XML presentation was written in 2001 and is still surprisingly accurate today.

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
1,614
On SlideShare
0
From Embeds
0
Number of Embeds
14
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Introduction To Xml

  1. 1. An Introduction to XML Based on the W3C XML Recommendations
  2. 2. Agenda <ul><li>XML Syntax </li></ul><ul><ul><li>XML vs HTML </li></ul></ul><ul><ul><li>Data Types – Elements, Attributes </li></ul></ul><ul><ul><li>White Space – Optional, Mandatory, & Preserved </li></ul></ul><ul><ul><li>Empty Content </li></ul></ul><ul><ul><li>Valid vs Well Formed </li></ul></ul><ul><li>XML Schema </li></ul><ul><ul><li>Used to Validate XML data </li></ul></ul><ul><ul><li>Before XML Schema – DTD’s </li></ul></ul><ul><ul><li>Simple Types vs Complex Types </li></ul></ul><ul><ul><li>Restricting data with Regular Expressions </li></ul></ul><ul><li>Namespaces </li></ul><ul><ul><li>Avoiding Tag Name Conflicts </li></ul></ul><ul><li>XML Tools </li></ul><ul><ul><li>XML Spy and Other Tools </li></ul></ul><ul><li>Corresponding Sample XML, XSD, DTD, XSL, and XHTML files </li></ul><ul><li>XML Resources on the web </li></ul><ul><ul><li>http://www.w3schools.com - an excellent site </li></ul></ul><ul><ul><li>http://www.xml.com </li></ul></ul><ul><ul><li>http://www.w3.org </li></ul></ul>
  3. 3. XML vs HTML <ul><li>As you can see, XML looks similar to HTML. </li></ul><ul><li><?xml version=&quot;1.0“?> </li></ul><ul><li><root> </li></ul><ul><li><child> </li></ul><ul><li><subchild attribute=“metadata”>Data</subchild> </li></ul><ul><li></child> </li></ul><ul><li></root> </li></ul>
  4. 4. XML vs HTML <ul><li>Unlike HMTL: </li></ul><ul><ul><li>XML is Case Sensitive </li></ul></ul><ul><ul><li>Tags must be properly nested </li></ul></ul><ul><ul><li>All start tags must have a corresponding end tag to close the element </li></ul></ul><ul><ul><li>All XML documents must have a root element </li></ul></ul><ul><ul><li>Attrbutes must use quotes (can be single or double) </li></ul></ul><ul><ul><li>White space between tags is preserved </li></ul></ul>
  5. 5. XML vs HTML <ul><li>Special Characters </li></ul><ul><ul><li>Handled the same way </li></ul></ul><ul><ul><li>For Example: </li></ul></ul><ul><ul><ul><li>< > ‘ “ & </li></ul></ul></ul><ul><ul><ul><li>&lt; &gt; &apos; &quot; &amp; </li></ul></ul></ul>
  6. 6. Elements <ul><li>XML Elements are extensible and they have parent/child relationships. </li></ul><ul><li>XML elements must follow these naming rules: </li></ul><ul><ul><li>Names can contain letters, numbers, underscores, periods, colons, and hyphens (last three are not normally used in element names) </li></ul></ul><ul><ul><li>Names must not start with a number or punctuation character </li></ul></ul><ul><ul><li>Names must not start with the letters xml (or XML or Xml ) </li></ul></ul><ul><ul><li>Names cannot contain spaces </li></ul></ul>
  7. 7. Attributes <ul><li>Attributes are normally used to store metadata, data about data, and the real data is stored in elements between the start and end tags. </li></ul><ul><li>Single or Double quotes can be used. </li></ul>
  8. 8. White Space <ul><li>White Space Includes: </li></ul><ul><ul><li>Carrage returns, Line feeds, Spaces, Horizontal Tabs </li></ul></ul><ul><li>Optional White Space </li></ul><ul><ul><li>White space is optional in XML files </li></ul></ul><ul><li>Mandatory White Space </li></ul><ul><ul><li>White Space must occur when using attributes </li></ul></ul><ul><li>Preserved White Space </li></ul><ul><ul><li>Between start/end tag pairs </li></ul></ul>
  9. 9. Optional White Space <ul><li>Valid </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li>< root > </li></ul><ul><li>< child > </li></ul><ul><li><subchild>Data</subchild> </li></ul><ul><li></child> </li></ul><ul><li></root> </li></ul><ul><li>Valid </li></ul><ul><li><?xml version=&quot;1.0&quot;?><root><child><subchild>Data</subchild></child> </li></ul><ul><li></root> </li></ul>
  10. 10. Mandatory White Space <ul><li>Valid </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><root> </li></ul><ul><li><child attribute=“metadata”> </li></ul><ul><li></root> </li></ul><ul><li>Invalid </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><root> </li></ul><ul><li><childattribute=“metadata”> </li></ul><ul><li></root> </li></ul>Must have white space here
  11. 11. Preserved White Space <ul><li>Valid </li></ul><ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><root> </li></ul><ul><li><child> </li></ul><ul><li><subchild>White space between </li></ul><ul><li>start/end tag pairs will be preserved</subchild> </li></ul><ul><li></child> </li></ul><ul><li></root> </li></ul>
  12. 12. Empty Content <ul><li>IF no data is “held” between a start/ end tag pair, two formats may be used: </li></ul><ul><ul><li><tag></tag> </li></ul></ul><ul><ul><li><tag/> </li></ul></ul><ul><li>The second format is called an Empty Tag (aka Null tag) and commonly used when only an attribute is needed: </li></ul><ul><ul><li><tag attribute=“data”/> </li></ul></ul>
  13. 13. Valid vs Well Formed <ul><li>XML data is defined and validated most commonly by: </li></ul><ul><ul><li>XML Schemas </li></ul></ul><ul><ul><li>DTD’s (Document Type Definition) </li></ul></ul><ul><li>XML data is well formed if it follows the W3C XML Recommendation, Version 1.0 </li></ul><ul><ul><li>This includes: </li></ul></ul><ul><ul><ul><li>The start/end tags matching up </li></ul></ul></ul><ul><ul><ul><li>White space used properly </li></ul></ul></ul><ul><li>NOTE: XML Spy does both checks </li></ul>
  14. 14. XML Schema <ul><li>Used to Define and Validate XML </li></ul><ul><ul><li>In order for the XML file to be validated by a schema, the schema’s location is referenced as an attribute of the root element </li></ul></ul><ul><ul><li><FirmOrder … schemaLocation=&quot;http://www.telcordia.com/SGG/FO </li></ul></ul><ul><ul><li>C:13.0DocumentationxsdfirmOrder.xsd&quot;/> </li></ul></ul>
  15. 15. XML Schema <ul><li>Before XML Schema, most XML documents were validated against a DTD </li></ul><?xml version=&quot;1.0&quot;?> <xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; targetNamespace=&quot;http://www.sample.org/xml&quot; xmlns=&quot;http://www.sample.org/xml&quot; elementFormDefault=&quot;qualified&quot;> <xsd:element name=“File&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=“Record&quot; minOccurs=“0&quot; maxOccurs=“unbounded&quot;/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=“Record&quot;> <xsd:complexType> <xsd:sequence> <xsd:element ref=“Fill minOccurs=“1&quot; maxOccurs=“1”/> </xsd:sequence> </xsd:complexType> </xsd:element> <xsd:element name=“Fill&quot; type=&quot;xsd:string&quot;/> </xsd:schema> <!ELEMENT File (Record)*> <!ELEMENT Record (Fill)> <!ELEMENT Fill (#PCDATA)> DTD XML Schema Mercator Type Tree
  16. 16. XML Schema <ul><li>Element Data Types </li></ul><ul><ul><li>XML Schema’s Simple Types </li></ul></ul><ul><ul><ul><li>Similar to Items in Mercator </li></ul></ul></ul><ul><ul><ul><li>44 Simple types built-in XML Schema </li></ul></ul></ul><ul><ul><li>XML Schema’s Complex Types </li></ul></ul><ul><ul><ul><li>Similar to Groups in Mercator </li></ul></ul></ul><ul><ul><ul><li>36 Complex types built-in XML Schema </li></ul></ul></ul><ul><ul><li>XML Schema’s Attributes </li></ul></ul><ul><ul><ul><li>Similar to the Properties of Items and Groups in Mercator </li></ul></ul></ul>
  17. 17. XML Schema <ul><li>Simple Types can be restricted using Regular Expressions: </li></ul><ul><ul><li><xsd:simpleType name=&quot;alphaString&quot;> </li></ul></ul><ul><ul><li> <xsd:restriction base=&quot;xsd:string&quot;> </li></ul></ul><ul><ul><li> <xsd:pattern value=&quot;([A-Z]|[a-z]|[ ])*&quot;/> </li></ul></ul><ul><ul><li> </xsd:restriction> </li></ul></ul><ul><ul><li></xsd:simpleType> </li></ul></ul>
  18. 18. Namespaces <ul><li>XML Namespaces provide a method to avoid element name conflicts. </li></ul><ul><ul><li>Since element names are not predefined as in HTML, often times a name conflict can occur when combining two different documents using the same name for two different elements </li></ul></ul>
  19. 19. Namespaces, cont. <ul><li>If the following two XML documents were added together, there would be an element name conflict because both documents contain a <table> element with different content and definition. </li></ul><table> <name>Tea Table</name> <width>80</width> <length>120</length> </table> <table> <tr> <td>Apples</td> <td>Bananas</td> </tr> </table>
  20. 20. Solving Name Conflicts using a Prefix <ul><li><h:table> </li></ul><ul><li><h:tr> </li></ul><ul><li><h:td>Apples</h:td> </li></ul><ul><li><h:td>Bananas</h:td> </li></ul><ul><li></h:tr> </li></ul><ul><li></h:table> </li></ul><ul><li><f:table> </li></ul><ul><li><f:name>African Coffee Table</f:name> </li></ul><ul><li><f:width>80</f:width> </li></ul><ul><li><f:length>120</f:length> </li></ul><ul><li></f:table> </li></ul>
  21. 21. Using Namespaces <ul><li><h:table xmlns:h=&quot;http://www.w3.org/TR/html4/&quot;> </li></ul><ul><li><h:tr> </li></ul><ul><li><h:td>Apples</h:td> </li></ul><ul><li><h:td>Bananas</h:td> </li></ul><ul><li></h:tr> </li></ul><ul><li></h:table> </li></ul><ul><li><f:table xmlns:f=&quot;http://www.w3schools.com/furniture&quot;> </li></ul><ul><li><f:name>African Coffee Table</f:name> </li></ul><ul><li><f:width>80</f:width> </li></ul><ul><li><f:length>120</f:length> </li></ul><ul><li></f:table> </li></ul>
  22. 22. Namespaces <ul><li>URI’s are used as the namespace name </li></ul><ul><ul><li>Most commonly used URI is a URL </li></ul></ul><ul><ul><li>URL’s by definition are unique to companies </li></ul></ul><ul><ul><li>The URL does NOT need to be valid </li></ul></ul><ul><ul><ul><li>They are used for creating uniqueness not validating your tags </li></ul></ul></ul><ul><ul><ul><li>Most companies put “help” documentation about their namespace, tags, and/or XML Schemas </li></ul></ul></ul>
  23. 23. XML Samples <ul><li>The next five slides have different types of XML files that correspond to each other: </li></ul><ul><ul><li>XML Data Document </li></ul></ul><ul><ul><li>XML Schema </li></ul></ul><ul><ul><li>DTD (these are not written in XML) </li></ul></ul><ul><ul><li>XSL – style sheet </li></ul></ul>
  24. 24. XML Data Sample <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><?xml-stylesheet type=&quot;text/xsl&quot; href=&quot;xmlxsl.xsl&quot;?> </li></ul><ul><li><root> </li></ul><ul><li><child> </li></ul><ul><li><name>Optional name tag used in this child tag</name> </li></ul><ul><li><description>First description start/end tag pair in child tag</description> </li></ul><ul><li></child> </li></ul><ul><li><child> </li></ul><ul><li><name>Optional name tag used in this child tag</name> </li></ul><ul><li><description>First description start/end tag pair in child tag</description> </li></ul><ul><li><description>Second description start/end tag pair in child tag</description> </li></ul><ul><li></child> </li></ul><ul><li></root> </li></ul>
  25. 25. XML Schema Sample <ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><xsd:schema xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot; </li></ul><ul><li>targetNamespace=&quot;http://www.sample.org/xml&quot; </li></ul><ul><li>xmlns=&quot;http://www.sample.org/xml&quot; </li></ul><ul><li>elementFormDefault=&quot;qualified&quot;> </li></ul><ul><li><xsd:element name=“root&quot;> </li></ul><ul><li><xsd:complexType> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element ref=“child&quot; minOccurs=&quot;0&quot; maxOccurs=&quot;unbounded&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li></xsd:element> </li></ul><ul><li><xsd:element name=&quot;child&quot;> </li></ul><ul><li><xsd:complexType> </li></ul><ul><li><xsd:sequence> </li></ul><ul><li><xsd:element ref=&quot;name&quot; minOccurs =&quot;0&quot;/> </li></ul><ul><li><xsd:element ref=&quot;description&quot; maxOccurs=&quot;unbounded&quot;/> </li></ul><ul><li></xsd:sequence> </li></ul><ul><li></xsd:complexType> </li></ul><ul><li></xsd:element> </li></ul><ul><li><xsd:element name=&quot;name&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li><xsd:element name=&quot;description&quot; type=&quot;xsd:string&quot;/> </li></ul><ul><li></xsd:schema> </li></ul>
  26. 26. XML DTD Sample <ul><li><!ELEMENT root (child)*> </li></ul><ul><li><!ELEMENT child (name?, description+)> </li></ul><ul><li><!ELEMENT name (#PCDATA)> </li></ul><ul><li><!ELEMENT description (#PCDATA)> </li></ul>
  27. 27. Style Sheet (XSL) Sample <ul><li><?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> </li></ul><ul><li><xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;http://www.w3.org/1999/XSL/Transform&quot;> </li></ul><ul><li><xsl:template match=&quot;/&quot;> </li></ul><ul><li><html> </li></ul><ul><li><body> </li></ul><ul><li><h2>XHTML Sample</h2> </li></ul><ul><li><table border=&quot;1&quot;> </li></ul><ul><li><tr bgcolor=&quot;gray&quot;> </li></ul><ul><li><th>Name</th> </li></ul><ul><li><th>Description</th> </li></ul><ul><li></tr> </li></ul><ul><li><xsl:for-each select=&quot;root/child&quot;> </li></ul><ul><li><tr> </li></ul><ul><li><td><xsl:value-of select=&quot;name&quot; /></td> </li></ul><ul><li><td><xsl:value-of select=&quot;description&quot; /></td> </li></ul><ul><li></tr> </li></ul><ul><li></xsl:for-each> </li></ul><ul><li></table> </li></ul><ul><li></body> </li></ul><ul><li></html> </li></ul><ul><li></xsl:template> </li></ul><ul><li></xsl:stylesheet> </li></ul>
  28. 28. XHTML Generated <html> <body> <h2>XHTML Sample</h2> <table border=&quot;1&quot;> <tr bgcolor=&quot;gray&quot;> <th>Name</th> <th>Description</th> </tr> <tr> <td>Optional name tag used in this child tag</td> <td>First description start/end tag pair in child tag</td> </tr> <tr> <td>Optional name tag used in this child tag</td> <td>First description start/end tag pair in child tag</td> </tr> </table> </body> </html>
  29. 29. XML Spy <ul><li>Accomplishes several XML tasks including: </li></ul><ul><ul><li>Editing a variety of XML data graphically </li></ul></ul><ul><ul><li>Allowing multiple views including: </li></ul></ul><ul><ul><ul><li>Text, browser, grid, structure (schema design), </li></ul></ul></ul><ul><ul><li>Creates test data from XML Schemas </li></ul></ul><ul><ul><li>Generates XML Schemas from XML files </li></ul></ul><ul><ul><li>Validates data </li></ul></ul><ul><ul><li>Checks data for Well-formedness </li></ul></ul>
  30. 30. Other Tools <ul><li>Internet Explorer </li></ul><ul><ul><li>Displays XML data using a default style sheet </li></ul></ul><ul><ul><li>Checks XML for Well-formedness and displays error message for troubleshooting </li></ul></ul><ul><li>UltraEdit </li></ul>
  31. 31. XML Resources on the web <ul><li>They are hundreds of XML resources on the web. </li></ul><ul><ul><li>http://www.w3schools.com (an excellent site) </li></ul></ul><ul><ul><li>http://www.xml.com </li></ul></ul><ul><ul><li>http://www.w3.org </li></ul></ul><ul><li>The easiest was to find data about a specific XML topic or syntax is to type it into google.com </li></ul>
  32. 32. Contact Us <ul><li>Barry DeBruin </li></ul><ul><li>debruinconsulting.com </li></ul><ul><li>919-434-5399 </li></ul>

×