1 / 27

Chapter 2: Well-Formed XML

Chapter 2: Well-Formed XML. Chapter 2 Objectives. How to create SML elements using start-tags and end-tags How to further describe elements with attributes How to declare your document as being XML How to send instructions to applications that are processing the XML document

yoland
Download Presentation

Chapter 2: Well-Formed XML

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 2: Well-Formed XML

  2. Chapter 2 Objectives • How to create SML elements using start-tags and end-tags • How to further describe elements with attributes • How to declare your document as being XML • How to send instructions to applications that are processing the XML document • Which characters aren’t allowed in XML – and how to use them in your documents anyway!

  3. Parsing XML Microsoft Internet Explorer Parser Apache Xerces James Clark's Expat

  4. Tags and Text and Elements,Oh My! <name> <first>John</first> <middle>Fitzgerald Johanson</middle> <last>Doe</last> • <first> is a start-tag • </first> is and end-tag • <first>John</first> is an element

  5. Try It Out Creating a Distribution Process

  6. Rules for Elements ❑Every start-tag must have a matching end-tag, or be a self-closing tag. ❑Tags can’t overlap; elements must be properly nested. ❑XML documents can have only one root element. ❑Element names must obey XML naming conventions. ❑XML is case sensitive. ❑XML will keep whitespace in your PCDATA. Every Start Tag Must Have An End Tag

  7. Rules for Elements Bad Example <name>John</name> <name>Jane</name> Good Example <names> <name>John</name> <name>Jane</name> </names> Can Have Only One Root Element

  8. Rules for Elements • Names can start with letters, no numbers • After first character, numbers, hyphens and periods are allowed • Names can’t contain spaces • There are reserved characters like “:” • Names can’t start with the letters “xml”, “XML”, or “Xml”, or any other combination • No spaces after the “<“, but before the “>” if desired Elements Must Obey XML Naming Conventions

  9. Rules for Elements These are two different elements <name>John</name> <NAME>John</NAME> Case Sensitivity

  10. Rules for Elements Whitespace stripping takes place in HTML… <P>This is a paragraph. &nbsp;&nbsp;&nbsp;&nbsp;It has a whole bunch<BR>&nbsp;&nbsp;of space.</P> …but not in XML Example <tag>This is a paragraph. It has a whole Bunch of space.</tag> This is a paragraph. It has a whole Bunch of space. Whitespace in PCDATA

  11. Rules for Elements • Windows uses both the line feed and the carriage return • UNIX uses only line feed • XML parsers will convert all Windows “line feed and carriage returns” to just line feed characters to standardize end-of-line logic End-of-Line Whitespace

  12. Rules for Elements <Tag> <AnotherTag>This is some XML</AnotherTag> </Tag> Readability Whitespace This is known as extraneous whitespace in the markup.

  13. Attributes <name nickname=“Shiny John”> <first>John</first> <middle>Fitzgerald Johansen</middle> <last>Doe</last> </name>

  14. Why use attributes • There is nothing that an attribute can do that an element can’t, but not vice-versa • They can be handy for “meta” data • Suppose you wanted to include the number of individual orders? • They are smaller than elements, but • Attributes are unordered • Some people just like them

  15. Try It Out Adding Attributes to Our Orders

  16. Comments <name nickname=‘Shiny John’> <first>John</first> <!--John lost his middle name in a fire--> <middle></middle> <last>Doe</last> </name>

  17. Try It Out Some Comments on Orders

  18. Empty Elements <name nickname=‘Shiny John’> <first>John</first> <!--John lost his middle name in a fire--> <middle></middle> <last>Doe</last> </name> <middle/>

  19. XML Declaration <?xml version=‘1.0’ encoding=‘UTF-16’ standalone=‘yes’?> <name nickname=‘Shiny John’> <first>John</first> <!--John lost his middle name in a fire--> <middle/> <last>Doe</last> </name> Define the Encoding and Standalone Attributes

  20. Try It Out Declaring Our Orders to the World

  21. Processing Instructions <?xml version=‘1.0’?> <name nickname=‘Shiny John’> <first>John</first> <!--John lost his middle name in a fire--> <middle/> <?nameprocessor SELECT * FROM blah?> <last>Doe</last> </name>

  22. Illegal PCDATA Characters <!--This is not well-formed XML!--> <comparison>6 is < 7 & 7 > 6</comparison> Ouch

  23. Escaping Characters <comparison>6 is &lt; 7 &amp; 7 &gt; 6 </comparison>

  24. CDATA Sections <script language=‘JavaScript’> <![CDATA[ function myFunc() { if(0 < 1 && 1 < 2) alert(“Hello”); } ]]> </script>

  25. Try It Out Talking about HTML in XML

  26. Errors in XML There Are Errors, and Then There Are Fatal Errors • Errors • Violations • May recover • Continue processing • Fatal errors • Draconian error handling • Not allowed to continue

  27. Try It Out • Adding Attributes to Our Orders • Some Comments to Our Orders • Declaring Our Orders To The World • An Order To Be Processes • Talking About HTML in XML

More Related