Well Formed XML The basics. A Simple XML Document Smith Alice.

17
Well Formed XML Well Formed XML The basics

Transcript of Well Formed XML The basics. A Simple XML Document Smith Alice.

Page 1: Well Formed XML The basics. A Simple XML Document Smith Alice.

Well Formed XMLWell Formed XMLThe basics

Page 2: Well Formed XML The basics. A Simple XML Document Smith Alice.

A Simple XML DocumentA Simple XML Document<?xml version=“1.0” encoding=“UTF-8”

standalone=“yes”?>

<!– this is the root element-->

<employees>

<employee enum=“34512”>

<name>

<lastname>Smith</lastname>

<firstname>Alice</firstname>

</name>

<status dept=“accounting” paytype=“salary” />

</employee>

</employees>

Page 3: Well Formed XML The basics. A Simple XML Document Smith Alice.

XML TagsXML TagsStarting Tag <firstname>Ending Tag</firstname>

<firstname>Alice</firstname>

Page 4: Well Formed XML The basics. A Simple XML Document Smith Alice.

Empty ElementsEmpty ElementsSome tags contain no dataOften this is because all their

content is in the form of attributes

These can be closed in the regular way with an opending and closing tag, or they can end with />

<status dept=“accounting” paytype=“salary” />

Page 5: Well Formed XML The basics. A Simple XML Document Smith Alice.

Case SensitivityCase SensitivityXML is case sensitive<firstname> does not match

with the closing tag </Firstname> or </firstName>

This makes it easier for programs to parse

Page 6: Well Formed XML The basics. A Simple XML Document Smith Alice.

XML TreesXML TreesEmployees

Employee

Name

Lastname

Firstname

Status ( dept=“Accounting” Paytype=“Salary”)

“Smith

“Alice”

Page 7: Well Formed XML The basics. A Simple XML Document Smith Alice.

Parents and ChildrenParents and ChildrenIn the diagram on the previous

slide, the employee element is the parent to the name and the status elements

name is the parent of the firstname and lastname elements

firstname and lastname are children of the name element. Etc.

Page 8: Well Formed XML The basics. A Simple XML Document Smith Alice.

Root ElementRoot ElementAll XML documents must have a root

element that encloses all the other elements (like HTML in an HTML document)<employees>. . .</employees>

Employees is the root element in our xml example

Page 9: Well Formed XML The basics. A Simple XML Document Smith Alice.

AttributesAttributesAn element can have 0 to several

attributes. The values of an attribute must

be enclosed in quotes (single or double)

<employee enum=“34512”>

<status dept=“accounting” paytype=“salary” />

Page 10: Well Formed XML The basics. A Simple XML Document Smith Alice.

XML NamesXML NamesElement and attribute names follow

the same rulesXML names my contain any

alphanumeric letter They can also include any of three

punctuation marks ◦ The hyphen –◦ The underscore __◦ The period .

Page 11: Well Formed XML The basics. A Simple XML Document Smith Alice.

XML Names Cont.XML Names Cont.XML names must begin with a

letter or the underscoreThe cannot begin with a numberThere is no limit to the length of

the names but they cannot contain spaces

Any string beginning with “xml” (lower or upper case) is reserved

Page 12: Well Formed XML The basics. A Simple XML Document Smith Alice.

Entity ReferencesEntity ReferencesXML entities are almost identical

to HTML entities. You use them to substitute for illegal or potentially confusing characters

XML defines 5 entities &amp; &lt; &gt; &quot; &apos;

Page 13: Well Formed XML The basics. A Simple XML Document Smith Alice.

CDATA SectionsCDATA SectionsWhen you need to quote large

sections of say HTML code you can use CDATA sections rather than entities for each character

<![CDATA[

<svg xlmns=“http://www.w3.org/2000/svg” width=“12cm” height=“10cm”>

<ellipse rx=“110” ry=“130” /></svg>

]]>

Page 14: Well Formed XML The basics. A Simple XML Document Smith Alice.

CommentsCommentsXML comments are just like HTML

commentsThey begin with <!-- and end with -->

<!--This is the root element-->

You cannot put a comment in an element

And the double -- is forbidden in the text of the comment since it would confuse the parser

Page 15: Well Formed XML The basics. A Simple XML Document Smith Alice.

Processing InstructionsProcessing InstructionsProcessing instructions contain

information to be used by other applications such as browsers

Processing instructions begin with <? And end with ?>

<?xml-stylesheet type=“text/xsl” href=“employee.xslt”?>

The XML declaration is a not a processing instruction

Page 16: Well Formed XML The basics. A Simple XML Document Smith Alice.

XML Declaration or PrologXML Declaration or PrologVersion (currently 1.0 or 1.1)Standalone (“yes” means there is no

DTD or schema definition for this document)

Encoding (the particular Unicode or ISO character encoding)

<?xml version=“1.0” encoding=“UTF-8” standalone=“yes”?>

Page 17: Well Formed XML The basics. A Simple XML Document Smith Alice.

Checking for Well-Checking for Well-FormednessFormednessEvery start tag must have an end tag or

be self closedElements may not overlap (proper nesting)There must be exactly one root elementAttribute values must be quotedAn element may not have two attributes

with the same nameComments and processing instructions

may not appear inside elementsNo unescaped < or & signs may occur in

the element’s or attribute’s character data