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 Which characters aren’t allowed in XML – and how to use them in your documents anyway!
Parsing XML
Tags and Text and Elements, Oh My! John Fitzgerald Johanson Doe is a start-tag is and end-tag John is an element
Try It Out Creating a Distribution Process
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.
Rules for Elements Bad Example John Jane Good Example John Jane
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 “ ” if desired
Rules for Elements These are two different elements John
Rules for Elements Whitespace stripping takes place in HTML… This is a paragraph. It has a whole bunch of space. …but not in XML Example This is a paragraph. It has a whole Bunch of space. This is a paragraph. It has a whole Bunch of space.
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
Rules for Elements This is some XML This is known as extraneous whitespace in the markup.
Attributes John Fitzgerald Johansen Doe
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
Try It Out Adding Attributes to Our Orders
Comments John Doe
Try It Out Some Comments on Orders
Empty Elements John Doe
XML Declaration John Doe
Try It Out Declaring Our Orders to the World
Processing Instructions John Doe
Illegal PCDATA Characters 6 is 6
Escaping Characters 6 is < 7 & 7 > 6
CDATA Sections <![CDATA[ function myFunc() { if(0 < 1 && 1 < 2) alert(“Hello”); } ]]>
Try It Out Talking about HTML in XML
Errors in XML Errors Violations May recover Continue processing Fatal errors Draconian error handling Not allowed to continue
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