well formed xml the basics. a simple xml document smith alice
TRANSCRIPT
Well Formed XMLWell Formed XMLThe basics
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>
XML TagsXML TagsStarting Tag <firstname>Ending Tag</firstname>
<firstname>Alice</firstname>
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” />
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
XML TreesXML TreesEmployees
Employee
Name
Lastname
Firstname
Status ( dept=“Accounting” Paytype=“Salary”)
“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.
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
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” />
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 .
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
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 & < > " '
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>
]]>
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
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
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”?>
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