Chapter 2: Well-Formed XML
description
Transcript of Chapter 2: Well-Formed XML
![Page 1: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/1.jpg)
Chapter 2: Well-Formed XML
![Page 2: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/2.jpg)
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!
![Page 3: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/3.jpg)
Parsing XML
![Page 4: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/4.jpg)
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
![Page 5: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/5.jpg)
Try It Out
Creating a Distribution Process
![Page 6: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/6.jpg)
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.
![Page 7: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/7.jpg)
Rules for Elements
Bad Example
<name>John</name>
<name>Jane</name>
Good Example
<names>
<name>John</name>
<name>Jane</name>
</names>
![Page 8: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/8.jpg)
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
![Page 9: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/9.jpg)
Rules for Elements
These are two different elements
<name>John</name><NAME>John</NAME>
![Page 10: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/10.jpg)
Rules for Elements
Whitespace stripping takes place in HTML…<P>This is a paragraph. It has a whole bunch<BR> 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.
![Page 11: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/11.jpg)
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
![Page 12: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/12.jpg)
Rules for Elements
<Tag>
<AnotherTag>This is some XML</AnotherTag>
</Tag>
This is known as extraneous whitespace in the markup.
![Page 13: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/13.jpg)
Attributes
<name nickname=“Shiny John”>
<first>John</first>
<middle>Fitzgerald Johansen</middle>
<last>Doe</last>
</name>
![Page 14: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/14.jpg)
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
![Page 15: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/15.jpg)
Try It Out
Adding Attributes to Our Orders
![Page 16: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/16.jpg)
Comments
<name nickname=‘Shiny John’> <first>John</first><!--John lost his middle name in a fire--> <middle></middle> <last>Doe</last></name>
![Page 17: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/17.jpg)
Try It Out
Some Comments on Orders
![Page 18: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/18.jpg)
Empty Elements
<name nickname=‘Shiny John’> <first>John</first> <!--John lost his middle name in a fire--> <middle></middle> <last>Doe</last></name>
<middle/>
![Page 19: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/19.jpg)
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>
![Page 20: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/20.jpg)
Try It Out
Declaring Our Orders to the World
![Page 21: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/21.jpg)
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>
![Page 22: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/22.jpg)
Illegal PCDATA Characters
<!--This is not well-formed XML!-->
<comparison>6 is < 7 & 7 > 6</comparison>
![Page 23: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/23.jpg)
Escaping Characters
<comparison>6 is < 7 & 7 > 6 </comparison>
![Page 24: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/24.jpg)
CDATA Sections
<script language=‘JavaScript’>
<![CDATA[
function myFunc()
{
if(0 < 1 && 1 < 2)
alert(“Hello”);
}
]]>
</script>
![Page 25: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/25.jpg)
Try It Out
Talking about HTML in XML
![Page 26: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/26.jpg)
Errors in XML
• Errors
•Violations
•May recover
•Continue processing
• Fatal errors
•Draconian error handling
•Not allowed to continue
![Page 27: Chapter 2: Well-Formed XML](https://reader036.fdocuments.us/reader036/viewer/2022081420/56813c41550346895da5be68/html5/thumbnails/27.jpg)
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