0% found this document useful (0 votes)
24 views202 pages

Week 6 XML

XML

Uploaded by

cooperkreates
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views202 pages

Week 6 XML

XML

Uploaded by

cooperkreates
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 202

XML

EXTENSIBLE MARKUP
LANGUAGE
IMPORTANT RESOURCE

W3Schools Tutorials
•XML Tutorial •XML Schema Tutorial
•Recommended parts: •Recommended parts:
•XML Introduction •XSD Introduction
•XML How to Use
•XML Tree
•XSD How To
•XSD <schema>
Optional tutorials that
•XML Syntax
•XML Elements
•XSD Elements
•XSD Attributes
cover XML and XML
•XML Attributes
•XML Namespaces
•XSD Complex Elements
•XSD Elements Only
Schemas.
•XML Display •XSD Text-Only Elements
•XML Validator •XSD Indicators Great resource if you
•XML Schema •XSD String Data Types
•XSD Date and Time Data Types need extra help with XML.
•XSD Numeric Data Types
•XSD Miscellaneous Data Types
3

COMPUTER

To understand at a conceptual Level:


» What is a computer?
» “This instrument can teach, it can illuminate; yes, and even it can inspire,” spoke Murrow. “But
it can do so only to the extent that humans are determined to use it to those ends. Otherwise,
it’s nothing but wires and lights in a box.” - Edward R. Murrow (originally referencing television)
» Still true today when talking about computers.
» It needs software to be anything more than this.
» Software gives the box the “intelligence”.
4

COMPUTER

What does a computer “know”?

» Presence or absences of a charge


5

COMPUTER

What does a computer “know”?

» Encoded as binary numbers (0s or 1s)


6

COMPUTER

What does a computer “know”?


7

COMPUTER

What does a computer “know”?

10100110 →B
» Binary numbers can represent text, images, videos, etc.
8

COMPUTER

Now you want to deliver (transmit) those ones and zeros to all your friends.

How?

Connection.
9

COMPUTER

Now you want to deliver (transmit) those ones and zeros to all your friends.

How?

… internet and the world wide web


10

MARKUP LANGUAGES

Annotations to text
» instructions on how text is to be displayed/used.
Types:
» Presentational
» Procedural
» Descriptive
⋄ GML/SGML
⋄ HTML/XHTML
⋄ XML
MESSAGE & DATA EXCHANGE
11

TECHNOLOGIES

• Telephone
• Paper (Fax, Postal mail)
• Email
• Electronic Data Interchange (EDI)
• eXtensible Markup Language (XML)
12

XML

So…

• eXtensible Markup Language (XML)

- Data Transport Mechanism


13

XML

DATA -
From one computer
To another computer
14

XML

DATA -
From one computer
To another computer
15

XML
A method (mechanism) to copy (upload) large amounts
of data into a database.

DATA
16

XML
17

XML: EXAMPLE
Add a list of all of your friends:
- their names, ages and hobby

Database: Steve 21 Skip Rope


Jane 19 Comics
Idra 20 Baseball
18

XML: EXAMPLE
Add a list of all of your friends:
- their names, ages and hobby

Database: Steve 21 Skip Rope


Jane 19 Comics
Idra 20 Baseball

Multiply that by the


Number of students
Enrolled in CS1032
19

XML: EXAMPLE
Add a list of all of your friends:
- their names, ages and hobby

Steve 21 Skip Rope


Database: Jane 19 Comics
Idra 20 Baseball

Way to much
data to enter
by hand!
20

XML

Have each
student send
their list by
Email:
21

XML

Have each
student send
their list by
Email:
22

XML
Each student sends data formatted differently!

Have each
student send
their list by
Email:
23

XML
Each student sends data formatted differently!

Have each
student send
their list by
Email:
No standardized
format used.
Still need to reinput
in database by hand!
24

XML

XML:

Provides a
standardized
format for data.
25

XML

XML:

Computer readable.
Can automate
import!
26

XML

How do we get/write XML???


27

XML

EASY:

data
- enclosed (encased) by tags

everything in an XML document is one of two things ONLY!

- it either DATA or it is a TAG


28

XML

EASY:

data
Joe Bloggs
29

XML

EASY:

data
Joe Bloggs

tag ( a label ‘bookended’ by angle brackets )


<tag1>
30

XML

EASY:

data
Joe Bloggs

tag ( a label ‘bookended’ by angle brackets )


<tag1>
XML statement ( combine the data with tags )
<tag1>Joe Bloggs</tag1>
31

XML

EASY: (everything is either data or it is a tag)

<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3>
<tag4>English</tag4>
<tag5>2020</tag5>
</tag2>
</tag1>
32

XML

EASY: (everything is either data or it is a tag)


DATA
<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3>
<tag4>English</tag4>
<tag5>2020</tag5>
</tag2>
</tag1>
33

XML

EASY: (everything is either data or it is a tag)

<tag1> NOTE: TAG


<tag2> - any character string
<tag3>Hope Ferdebest</tag3>
TAGS <tag4>English</tag4>
- begins and ends with
<tag5>2020</tag5>
angle brackets < >
</tag2>
</tag1>
- no spaces before/after
the brackets
TAGS
34

XML

Tags - every tag has a START tag


and MUST have a matching END tag
<tag1> START TAGS
<tag2> NOTE: END TAG
<tag3>Hope Ferdebest</tag3> - must match the start tag
<tag4>English</tag4> EXACTLY (including case)
<tag5>2020</tag5>
</tag2> - end tag denoted by the
</tag1>

END TAGS
forward slash/
35

XML

Tags - every tag has a START tag


and MUST have a matching END tag
<tag1>
<tag2> NOTE: TAG RULES
<tag3>Hope Ferdebest</tag3> - no spaces in the labels
<tag4>English</tag4>
<tag5>2020</tag5> - can not start with a number
</tag2> or with the letters “XML”
</tag1>
36

XML

Tags - simple: only enclose (encase) data

<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3> tag3, tag4 and tag5
<tag4>English</tag4> are simple tags
<tag5>2020</tag5>
</tag2>
</tag1>
SIMPLE TAGS
37

XML

Tags - complex: enclose (encase) other tags

<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3> tag1 and tag2 are
<tag4>English</tag4> complex tags
<tag5>2020</tag5>
</tag2>
</tag1>

COMPLEX TAGS
38

XML
Tags - complex: allows “grouping” of data
<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3>
<tag4>English</tag4> GROUP #1
<tag5>2020</tag5>
</tag2>
<tag2>
<tag3>Isma Braineful</tag3>
<tag4>Math</tag4> GROUP #2
<tag5>2019</tag5>
</tag2>
</tag1>
39

XML
Tags - complex: allows “grouping” of data
<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3>
<tag4>English</tag4> GROUP #1
<tag5>2020</tag5>
</tag2> MAJOR
<tag2> GROUP
<tag3>Isma Braineful</tag3>
<tag4>Math</tag4> GROUP #2
<tag5>2019</tag5>
</tag2>
</tag1>
40

XML
Tags - complex: grouping tags have the same (repeating) labels
<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3>
<tag4>English</tag4> GROUP #1
<tag5>2020</tag5>
</tag2> MAJOR
<tag2> GROUP
<tag3>Isma Braineful</tag3>
<tag4>Math</tag4> GROUP #2
<tag5>2019</tag5>
</tag2>
</tag1>
41

XML
Tags - simple: grouping tags have the same (repeating) labels
<tag1>
<tag2>
<tag3>Hope Ferdebest</tag3>
<tag4>English</tag4> GROUP #1
<tag5>2020</tag5>
</tag2> MAJOR
<tag2> GROUP
<tag3>Isma Braineful</tag3>
<tag4>Math</tag4> GROUP #2
<tag5>2019</tag5>
</tag2>
</tag1>
42

XML
Tags - They should be meaningful to the data context
<course>
<student>
<name>Hope Ferdebest</name>
<major>English</major>
<year>2020</year> Tags should have
</student > descriptive labels
<student > (not just tag1, tag2, etc.)
<name>Isma Braineful</name>
<major>Math</major>
<year>2018</year>
</student >
</course>
43

XML

Tags - BUT: they can be anything*


<foo> *TAG RULES
<bar> - no spaces in the labels
<xyz>Hope Ferdebest</xyz>
<CAT>English</CAT> - can not start with a number
<Duck>2020</Duck> or with the letters “XML”
</bar>
</foo> - Start tag must match end
tag, including capitalization
44

MORE COMPLEX EXAMPLE

List of all of your closest friends, including….

⋄ names(s)
⋄ age(s)
⋄ hobbies(s)
45

MORE COMPLEX EXAMPLE

List of all of your closest friends, including….

⋄ Billy
⋄ 31
DATA
⋄ Avengers
⋄ DandD
46

HOW TO MAKE AN XML DOCUMENT?

How do we turn this into an XML document?

⋄ Create a plain text file (no formatting)


⋄ Use a plain text/flat file editor (notepad++, brackets, etc.)
⋄ File must have the extension of .xml
47

HOW TO MAKE AN XML DOCUMENT?

How do we turn this into an XML document?

⋄ Create a plain text file (no formatting)


⋄ Use a plain text/flat file editor (notepad++, brackets, etc.)
⋄ File must have the extension of .xml

Notepad++ (https://notepad-plus-plus.org/)
48

HOW TO MAKE AN XML DOCUMENT?

DO THE
TAGS LAST!
49
GOAL <?xml version="1.0"?>
<FriendList>
<Friend>
<name>Billy</name>
<age>31</age>
<hobbies>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
Billy 31 Cosplay, Avengers, DandD <name>Idris</name>
<age>27</age>
Idris 27 Dr. Who <hobbies>
<hobby>Dr. Who</hobby>
Jordan 19 Star Wars, My Little Pony </hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
50
STEP 1: BREAK IT DOWN

Billy 31 Cosplay, Avengers, DandD


Idris 27 Dr. Who
Jordan 19 Star Wars, My Little Pony
51
STEP 1: BREAK IT DOWN

Billy 31 Cosplay,Avengers, DandD

Idris 27 Dr. Who

Jordan 19 Star Wars, My Little Pony


52
STEP 1: BREAK IT DOWN
Billy
31
cosplay LOGICAL UNITS
Avengers
DandD - Have come commonality

Idris GROUPINGS!
27
Dr. Who
• One data point per line
Jordan
• Break it down into groups
19 separated by spaces
Star Wars • Keep data in group together
My Little Pony
• One piece of data on each line
STEP 2: 53

IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD

Idris
27
Dr. Who

Jordan
19
Star Wars
My Little Pony
STEP 2: 54

IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD

Idris
27
LOGICAL UNITS
Dr. Who
- Have come commonality
Jordan
19
Star Wars GROUPINGS!
My Little Pony
STEP 2: 55

IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD

Idris
27
LOGICAL UNITS
Dr. Who
- Have come commonality
Jordan
19
Star Wars GROUPINGS!
My Little Pony
STEP 2: 56

IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD

Idris
27
LOGICAL UNITS
Dr. Who
- Have come commonality
Jordan
19
Star Wars GROUPINGS!
My Little Pony
STEP 2: 57

IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD

Idris
27
LOGICAL UNITS
Dr. Who
- Have come commonality
Jordan
19
Star Wars GROUPINGS!
My Little Pony
STEP 2: 58

IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD

Idris
27 Give descriptive
Dr. Who
names to the
groupings
Jordan
19
Star Wars
My Little Pony
STEP 2: 59
FriendList
IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD

Idris
27 Give descriptive
Dr. Who
names to the
groupings
Jordan
19
Star Wars
My Little Pony
STEP 2: 60
FriendList
IDENTIFY
GROUPINGS Billy
31
cosplay
Avengers
DandD Friend

Idris
27 Give descriptive
Dr. Who
names to the
groupings
Jordan
19 Friend
Star Wars
My Little Pony

Friend
STEP 2: 61
FriendList
IDENTIFY
GROUPINGS Billy
31 Hobbies
cosplay
Avengers
DandD Friend

Idris
27 Give descriptive
Dr. Who
Hobbies names to the
groupings
Jordan
19 Friend
Star Wars
My Little Pony

Hobbies

Friend
STEP 3: 62
FriendList
NAME THE
DATA Billy
31 Hobbies
cosplay
Avengers
DandD Friend

Idris
27
Dr. Who

Jordan
Hobbies
Give descriptive
Friend
19
Star Wars names to each
My Little Pony

Hobbies data element


Friend
STEP 3: 63
FriendList
NAME THE
DATA name
Billy
31 Hobbies
cosplay
Avengers
DandD Friend

Idris
27
Dr. Who
name
Hobbies
Jordan
Give descriptive
Friend
19
Star Wars
My Little Pony
names to each
name
Hobbies data element
Friend
STEP 3: 64
FriendList
NAME THE
DATA name
Billy
31 Hobbies
cosplay
Avengers
DandD Friend

Idris
27
Dr. Who
name

Jordan
Hobbies
Give descriptive
Friend
19
Star Wars names to each
My Little Pony
name
Hobbies data element
Friend
STEP 3: 65
FriendList
NAME THE
DATA name
Billy
31 Hobbies
age cosplay
Avengers
DandD Friend

Idris
27
Dr. Who
name
age
Jordan
Hobbies
Give descriptive
Friend
19
Star Wars names to each
My Little Pony
name
age
Hobbies data element
Friend
STEP 3: 66
FriendList
NAME THE
DATA name
Billy
31 Hobbies
age cosplay
hobby Avengers
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age
hobby Jordan
Hobbies
Give descriptive
Friend
19
Star Wars names to each
My Little Pony
name
age
Hobbies data element
hobby
hobby Friend
STEP 3: 67
FriendList
NAME THE
DATA name
Billy
31 Hobbies
age cosplay
hobby Avengers
hobby
DandD Friend
hobby

Now we are ready to


Idris
27
Dr. Who
name
age Hobbies make an XML
hobby Jordan
19
Star Wars
Friend
document
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 4: 68

ADD THE XML DOCUMENT FriendList


IDENTIFIER name Billy
31 Hobbies
1st:
age cosplay
hobby
XML Document Identifier
Avengers
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 4: FriendList
69

ADD THE XML DOCUMENT


<?xml version="1.0"?>
IDENTIFIER name Billy
31 Hobbies
age cosplay
hobby Avengers
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 4: FriendList
70

ADD THE XML DOCUMENT


<?xml version="1.0"?>
IDENTIFIER name Billy
31 Hobbies
age cosplay
hobby Avengers
hobby
DandD Friend
hobby
Idris
Tells the computer this
is an XML document.
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
Every XML file MUST
name
age
Hobbies have this as the FIRST
hobby
hobby Friend line!
STEP 5: FriendList
71

ADD TAGS <?xml version="1.0"?>

AND DATA name


Billy
31 Hobbies
age cosplay
hobby Avengers
hobby
DandD Friend
hobby
Idris
Start adding tags
(groups) and data in
27
Dr. Who
name
age Hobbies
hobby Jordan
Friend
order they appear.
19
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
72

ADD TAGS <?xml version="1.0"?>

AND DATA Billy


name
age
31
cosplay
Hobbies The RULES:
hobby Avengers
DandD Friend • Everything is either a tag or
hobby
hobby data
Idris
27
• Tags are case sensitive
name
Dr. Who • Start tags MUST have a
age Hobbies
matching end tag
hobby Jordan
19 Friend • Tags can not overlap, must
Star Wars
My Little Pony have proper nesting
name
age
Hobbies • XML Documents can have
hobby one (and only one) Root
hobby Friend
Element
• Tags must obey XML
Naming Conventions
STEP 5: FriendList
73

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy
name
31 Hobbies
age cosplay
hobby Avengers
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
74

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies
age cosplay
hobby Avengers
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
75

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay
hobby Avengers
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
76

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
77

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
78

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>

hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
79

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
80

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris
27
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
81

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27
Dr. Who
name
age Hobbies
hobby Jordan
Friend
Important!
19
Star Wars
My Little Pony
name
age
Hobbies We must end each
hobby
hobby Friend grouping with an end
tag.
STEP 5: FriendList
82

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies We must end each
hobby
hobby Friend grouping with an end
tag.
STEP 5: FriendList
83

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name> One friend has
age cosplay <age>31</age>
hobby Avengers <hobbies> been input into
DandD Friend <hobby>cosplay</hobby>
hobby
<hobby>Avengers</hobby> the XML
hobby
Idris
<hobby>DandD</hobby>
</hobbies> document so
27
Dr. Who
</Friend>
far.
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
84

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
85

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>

hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
86

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
87

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
88

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
89

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
90

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
91

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>

name
27
Dr. Who
</Friend>
<Friend> 2nd friend has
age Hobbies <name>Idris</name>
<age>27</age> been input into
hobby Jordan
19 Friend
<hobbies>
<hobby>Dr. Who</hobby>
the XML
Star Wars
My Little Pony
</hobbies>
</Friend>
document.
name
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
92

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies
hobby
hobby Friend
STEP 5: FriendList
93

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>

hobby
hobby Friend
STEP 5: FriendList
94

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby
hobby Friend
STEP 5: FriendList
95

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby <hobbies>
hobby Friend <hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
STEP 5: FriendList
96

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby <hobbies>
hobby Friend <hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
STEP 5: FriendList
97

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name
Hobbies
<Friend> Last friend has
age <name>Jordan</name>

hobby
<age>19</age>
<hobbies>
been input into
hobby Friend the XML
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies> document.
</Friend>
STEP 5: FriendList
98

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby <hobbies>
hobby Friend <hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
?
STEP 5: FriendList
99

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby <hobbies>
hobby Friend <hobby>Star Wars</hobby> Don’t forget to
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
end the root
? element!
STEP 5: FriendList
100

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby <hobbies>
hobby Friend <hobby>Star Wars</hobby> Don’t forget to
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
end the root
</FriendList> element!
STEP 5: FriendList
101

ADD TAGS <?xml version="1.0"?>

AND DATA
<FriendList>
Billy <Friend>
name
31 Hobbies <name>Billy</name>
age cosplay <age>31</age>
hobby Avengers <hobbies>
hobby
DandD Friend <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
27 </Friend>
Dr. Who <Friend>
name
age Hobbies <name>Idris</name>
<age>27</age>
hobby Jordan <hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
My Little Pony </Friend>
name <Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby <hobbies>
hobby Friend <hobby>Star Wars</hobby> Don’t forget to
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
end the root
</FriendList> element!
FriendList
102
- The RULES
<?xml version="1.0"?>
- everything Billy
is either <FriendList>
<Friend>
name
a tagage
or data
31
cosplay
Hobbies <name>Billy</name>
<age>31</age>
hobby Avengers <hobbies>
Friend
- tags
hobbyare case sensitive
DandD <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
hobby <hobby>DandD</hobby>
Idris </hobbies>
- every start27tag </Friend>

MUST
name have
Dr. Who <Friend>
age Hobbies <name>Idris</name>
a hobby
matching end
Jordan
tag <age>27</age>
<hobbies>
19 Friend <hobby>Dr. Who</hobby>
Star Wars </hobbies>
- tags can not overlap
My Little Pony </Friend>
name
- proper nesting
<Friend>
age
Hobbies <name>Jordan</name>
<age>19</age>
hobby <hobbies>
- XML Documents can
hobby Friend <hobby>Star Wars</hobby>

have one (one only one)


<hobby>My Little Pony</hobby>
</hobbies>
Root Element </Friend>
</FriendList>
103
Raw Data FriendList.xml
<?xml version="1.0"?>
Billy 31 cosplay Avengers DandD
<FriendList>
Idris 27 Dr. Who
<Friend>
Jordan 19 Star Wars My Little Pony
<name>Billy</name>
<age>31</age>
<hobbies>
<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
104
GOAL FriendList
<?xml version="1.0"?>
Complete XML Document <FriendList>
<Friend>
<name>Billy</name>
<age>31</age>

… take raw data … <hobbies>


<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
Billy 31 cosplay, Avengers, DandD <hobby>DandD</hobby>
Idris 27 Dr. Who </hobbies>
Jordan 19 Star Wars, My Little Pony </Friend>
…. <Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
105
GOAL FriendList
<?xml version="1.0"?>
Complete XML Document <FriendList>
<Friend>
<name>Billy</name>
<age>31</age>

… take raw data … <hobbies>


<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
Billy 31 cosplay <hobby>DandD</hobby>
Idris 27 Dr. Who </hobbies>
Jordan 19 Star Wars </Friend>
Sharmin 28 Comics <Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
… into a computer file … </FriendList>
106
GOAL FriendList
<?xml version="1.0"?>
Complete XML Document <FriendList>
<Friend>
<name>Billy</name>
<age>31</age>

… to import into
<hobbies>
<hobby>cosplay</hobby>

a database…
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
… into a computer file … </FriendList>
107

REMEMBER!

DO THE
TAGS LAST!
QUICK REVIEW
108
<?xml version="1.0"?>
<FriendList>
<Friend>
<name>Billy</name> Top Level Grouping
<age>31</age>
<hobbies>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
<name>Idris</name> Top Level Grouping
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend>
<name>Jordan</name> Top Level Grouping
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
QUICK REVIEW
109
<?xml version="1.0"?>
<FriendList>
<Friend>
<name>Billy</name>
<age>31</age>
<hobbies>
<hobby>Avengers</hobby>
Inner Level Grouping
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby> Inner Level Grouping
</hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby> Inner Level Grouping
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
QUICK REVIEW
110
<?xml version="1.0"?>
<FriendList>
<Friend>
<name>Billy</name>
<age>31</age>
<hobbies>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies> Data ‘Simple’ Groupings
</Friend>
<Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
111

QUICK REVIEW

<?xml version="1.0"?>

<FriendList> Pre-processor command

<Friend>

<name>Billy</name>
<age>31</age>

<hobbies>
<hobby>cosplay</hobby>
<hobby>D and D</hobby>
</hobbies>

</Friend>

</FriendList>
112

QUICK REVIEW

<?xml version="1.0"?>

<FriendList> Root
- and ‘end tag’ for Root
<Friend>

<name>Billy</name>
<age>31</age>

<hobbies>
<hobby>cosplay</hobby>
<hobby>D and D</hobby>
</hobbies>

</Friend>

</FriendList>
113

QUICK REVIEW

<?xml version="1.0"?>

<FriendList>
Top Level Grouping
<Friend> … and end tag for grouping

<name>Billy</name>
<age>31</age>

<hobbies>
<hobby>cosplay</hobby>
<hobby>D and D</hobby>
</hobbies>

</Friend>

</FriendList>
114

QUICK REVIEW

<?xml version="1.0"?>

<FriendList> Inner Level Grouping


… and end tag for grouping
<Friend>

<name>Billy</name>
<age>31</age>

<hobbies>
<hobby>cosplay</hobby>
<hobby>D and D</hobby>
</hobbies>

</Friend>

</FriendList>
115

QUICK REVIEW

<?xml version="1.0"?>

<FriendList> Data ‘Simple’ Groupings

<Friend>

<name>Billy</name>
<age>31</age>

<hobbies>
<hobby>cosplay</hobby>
<hobby>D and D</hobby>
</hobbies>

</Friend>

</FriendList>
116

QUICK REVIEW
Pre-processor command
<?xml version="1.0"?>

<FriendList> Root

<Friend>

<name>Billy</name> Data ‘Simple’ Groupings


<age>31</age>

<hobbies>
<hobby>cosplay</hobby> Inner Level Grouping
<hobby>D and D</hobby>
</hobbies>

</Friend> Top Level Grouping

</FriendList>
XML ELEMENTS
117
<?xml version="1.0"?>
<FriendList>
<Friend> Every Start Tag - End Tag combination
<name>Billy</name> is known as an
<age>31</age> ELEMENT
<hobbies> in an XML Document
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend>
<name>Jordan</name>
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
XML ELEMENTS
118
<?xml version="1.0"?>
<FriendList>
<Friend> Every Start Tag - End Tag combination
<name>Billy</name> is known as an
<age>31</age> ELEMENT
<hobbies> in an XML Document
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend> Hint:
<name>Jordan</name> How many elements are in this XML Document ?
i.e. How many unique start tags can you count?
<age>19</age>
<hobbies>
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
XML ELEMENTS
119
<?xml version="1.0"?>
<FriendList>
<Friend> Every Start Tag - End Tag combination
<name>Billy</name> is known as an
<age>31</age> ELEMENT
<hobbies> in an XML Document
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
<Friend>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</Friend>
<Friend> Hint:
<name>Jordan</name> How many elements are in this XML Document ?
i.e. How many unique start tags can you count?
<age>19</age>
<hobbies>

6
<hobby>Star Wars</hobby>
<hobby>My Little Pony</hobby>
</hobbies>
</Friend>
</FriendList>
120

TAG ATTRIBUTES
<Friend>
Billy <name>Billy</name>
31 <age>31</age>
cosplay <hobbies>
Avengers <hobby>cosplay</hobby>
DandD <hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
121

TAG ATTRIBUTES
(information about the TAG and NOT about the DATA)

<Friend Order = “1”>


Billy <name>Billy</name>
31 <age>31</age>
cosplay <hobbies>
Avengers <hobby>cosplay</hobby>
DandD <hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>

<Friend GroupType = “human”>


Billy <name>Billy</name>
31 <age>31</age>
cosplay <hobbies>
Avengers <hobby>cosplay</hobby>
DandD <hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
122

TAG ATTRIBUTES
(information about the TAG and NOT about the DATA)

<Friend Order = “1”> {correct}


Billy <name>Billy</name>
31 <age>31</age>
cosplay <hobbies>
Avengers <hobby>cosplay</hobby>
DandD <hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>

<Friend GroupType = “human”> {correct}


Billy <name>Billy</name>
31 <age>31</age>
cosplay <hobbies>
Avengers <hobby>cosplay</hobby>
DandD <hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
123

TAG ATTRIBUTES
(information about the TAG and NOT about the DATA)

<Friend gender = “Male”>


Billy <name>Billy</name>
31 <age>31</age>
cosplay <hobbies>
Avengers <hobby>cosplay</hobby>
DandD <hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>

<Friend>
Billy <name>Billy</name>
31 <age>31</age>
cosplay <gender>Male</gender>
Avengers </hobbies>
DandD <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
124

TAG ATTRIBUTES
(information about the TAG and NOT about the DATA)

<Friend gender = “Male”> {WRONG!}


Billy <name>Billy</name>
31 <age>31</age>
cosplay <hobbies>
Avengers <hobby>cosplay</hobby>
DandD <hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>

<Friend>
Billy <name>Billy</name>
31 <age>31</age>
cosplay <gender>Male</gender> {correct}
Avengers </hobbies>
DandD <hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
125

ELEMENT COMPONENTS
<city id = “nyc”> New York City </city>
126

ELEMENT COMPONENTS
<city id = “nyc”> New York City </city>

| start tag |
127

ELEMENT COMPONENTS
<city id = “nyc”> New York City </city>
|tag |
|name|

| start tag |
128

ELEMENT COMPONENTS
<city id = “nyc”> New York City </city>
|tag | | tag attribute |
|name| (optional)

| start tag |
129

ELEMENT COMPONENTS
<city id = “nyc”> New York City </city>

|end tag|
130

ELEMENT COMPONENTS
<city id = “nyc”> New York City </city>
|terminator|

|end tag|
131

ELEMENT COMPONENTS
<city id = “nyc”> New York City </city>

| content |
|-data-|
132

ELEMENT COMPONENTS
<city id = “nyc”> <town>York</town> </city>

| content |
|-data-|

- OR -

|- other elements - |
133

ELEMENT COMPONENTS
<city id = “nyc”>
<town>York</town>
</city>
| content |
|-data-|

- OR -

|- other elements- |

Either way - data (simple) or components (complex)


- ‘city’ is still the name of the element
134

ELEMENT COMPONENTS
Empty Elements
<city></city>
| NO content |
|- NO data-|

NOTICE - NO SPACE - {a space is data…}


135

ELEMENT COMPONENTS
Empty Elements
<city></city> - NOTHING between the ‘start’ tag and the ‘end’ tag

| NO content |
|- NO data-|

NOTICE - NO SPACE - {a space is data…}

Alternate – Empty Elements


<city/>
NOTICE – single tag – self terminator AFTER tag name
136

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

XML tag names are case sensitive: <TAG> <Tag> and <tag> are all different

XML Name may NOT begin with a number: <1Tag> is NOT allowed

XML Name may otherwise contain numbers: <Tag1> IS allowed

XML Names MAY contain underscore, hyphens and period characters:


<Tag_Name> <Tag-Name> and <Tag.Name> are allowed

XML Names may NOT contain any other punctuation characters: <Tag%> NO

XML Names can NOT have whitespace: <Tag Name> is NOT allowed

XML Name may NOT begin with XML in any case: <XMLTag> <xmlTag> NO
137

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

XML tag names are case sensitive: <TAG> <Tag> and <tag> are all different

XML Name may NOT begin with a number: <1Tag> is NOT allowed

XML Name may otherwise contain numbers: <Tag1> IS allowed

XML Names MAY contain underscore, hyphens and period characters:


<Tag_Name> <Tag-Name> and <Tag.Name> are allowed

XML Names may NOT contain any other punctuation characters: <Tag%> NO

XML Names can NOT have whitespace: <Tag Name> is NOT allowed

XML Name may NOT begin with XML in any case: <XMLTag> <xmlTag> NO
138

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

XML tag names are case sensitive: <TAG> <Tag> and <tag> are all different

XML Name may NOT begin with a number: <1Tag> is NOT allowed

XML Name may otherwise contain numbers: <Tag1> IS allowed

XML Names MAY contain underscore, hyphens and period characters:


<Tag_Name> <Tag-Name> and <Tag.Name> are allowed

XML Names may NOT contain any other punctuation characters: <Tag%> NO

XML Names can NOT have whitespace: <Tag Name> is NOT allowed

XML Name may NOT begin with XML in any case: <XMLTag> <xmlTag> NO
139

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

XML tag names are case sensitive: <TAG> <Tag> and <tag> are all different

XML Name may NOT begin with a number: <1Tag> is NOT allowed

XML Name may otherwise contain numbers: <Tag1> IS allowed

XML Names MAY contain underscore, hyphens and period characters:


<Tag_Name> <Tag-Name> and <Tag.Name> are allowed

XML Names may NOT contain any other punctuation characters: <Tag%> NO

XML Names can NOT have whitespace: <Tag Name> is NOT allowed

XML Name may NOT begin with XML in any case: <XMLTag> <xmlTag> NO
140

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

XML tag names are case sensitive: <TAG> <Tag> and <tag> are all different

XML Name may NOT begin with a number: <1Tag> is NOT allowed

XML Name may otherwise contain numbers: <Tag1> IS allowed

XML Names MAY contain underscore, hyphens and period characters:


<Tag_Name> <Tag-Name> and <Tag.Name> are allowed

XML Names may NOT contain any other punctuation characters: <Tag%> NO

XML Names can NOT have whitespace: <Tag Name> is NOT allowed

XML Name may NOT begin with XML in any case: <XMLTag> <xmlTag> NO
141

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

XML tag names are case sensitive: <TAG> <Tag> and <tag> are all different

XML Name may NOT begin with a number: <1Tag> is NOT allowed

XML Name may otherwise contain numbers: <Tag1> IS allowed

XML Names MAY contain underscore, hyphens and period characters:


<Tag_Name> <Tag-Name> and <Tag.Name> are allowed

XML Names may NOT contain any other punctuation characters: <Tag%> NO

XML Names can NOT have whitespace: <Tag Name> is NOT allowed

XML Name may NOT begin with XML in any case: <XMLTag> <xmlTag> NO
142

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

XML tag names are case sensitive: <TAG> <Tag> and <tag> are all different

XML Name may NOT begin with a number: <1Tag> is NOT allowed

XML Name may otherwise contain numbers: <Tag1> IS allowed

XML Names MAY contain underscore, hyphens and period characters:


<Tag_Name> <Tag-Name> and <Tag.Name> are allowed

XML Names may NOT contain any other punctuation characters: <Tag%> NO

XML Names can NOT have whitespace: <Tag Name> is NOT allowed

XML Name may NOT begin with XML in any case: <XMLTag> <xmlTag> NO
143

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

Whitespace IS preserved in XML Content (data):

<sData>This is a paragraph. It has a whole whack


of spaces.</sData>

Will be processed as:

This is a paragraph. It has a whole whack


of spaces.
144

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

Whitespace IS preserved in XML Content (data):

<sData>This is a paragraph. It has a whole whack


of spaces.</sData>

Will be processed as:

This is a paragraph. It has a whole whack


of spaces.
145

ELEMENT COMPONENTS
More Rules
<city id = “nyc”> New York City </city>

Whitespace IS preserved in XML Content (data):

<sData>This is a paragraph. It has a whole whack


of spaces.</sData>

Will be processed as:

This is a paragraph. It has a whole whack


of spaces.
146

ELEMENT COMPONENTS
More Rules
<compare>6 <7 & 7> 6 ‘natch’</compare>

Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt; 7 &amp; 7 &gt; 6 &apos;natch&apos;</compare>

&amp; – the & (ampersand) character


&lt; - the <(less than) character
&gt; - the> (greater than) character
&apos; - the ‘ (apostrophe – single quote) character
&quot; - the “ (double quote) character
147

ELEMENT COMPONENTS
More Rules
<compare>6
W R <7 O &N
7> 6G‘natch’</compare>
! ! ! ! !
Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt 7 &amp 7 &gt 6 &aposnatch&apos</compare>

&amp – the & (ampersand) character


&lt - the <(less than) character
&gt - the> (greater than) character
&apos - the ‘ (apostrophe – single quote) character
&quot - the “ (double quote) character
148

ELEMENT COMPONENTS
More Rules
<compare>6 <7 & 7> 6 ‘natch’</compare>

Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt; 7 &amp; 7 &gt; 6 &apos;natch&apos;</compare>

&amp; – the & (ampersand) character


&lt; - the <(less than) character
&gt; - the> (greater than) character
&apos; - the ‘ (apostrophe – single quote) character
&quot; - the “ (double quote) character
149

ELEMENT COMPONENTS
More Rules
<compare>6 <7 & 7> 6 ‘natch’</compare>

Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt; 7 &amp; 7 &gt; 6 &apos;natch&apos;</compare>

&amp; – the & (ampersand) character


&lt; - the <(less than) character
&gt; - the> (greater than) character
&apos; - the ‘ (apostrophe – single quote) character
&quot; - the “ (double quote) character
150

ELEMENT COMPONENTS
More Rules
<compare>6 <7 & 7> 6 ‘natch’</compare>

Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt; 7 &amp; 7 &gt; 6 &apos;natch&apos;</compare>

&amp; – the & (ampersand) character


&lt; - the <(less than) character
&gt; - the> (greater than) character
&apos; - the ‘ (apostrophe – single quote) character
&quot; - the “ (double quote) character
151

ELEMENT COMPONENTS
More Rules
<compare>6 <7 & 7> 6 ‘natch’</compare>

Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt; 7 &amp; 7 &gt; 6 &apos;natch&apos;</compare>

&amp; – the & (ampersand) character


&lt; - the <(less than) character
&gt; - the> (greater than) character
&apos; - the ‘ (apostrophe – single quote) character
&quot; - the “ (double quote) character
152

ELEMENT COMPONENTS
More Rules
<compare>6 <7 & 7> 6 ‘natch’</compare>

Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt; 7 &amp; 7 &gt; 6 &apos;natch&apos;</compare>

&amp; – the & (ampersand) character


&lt; - the <(less than) character
&gt; - the> (greater than) character
&apos; - the ‘ (apostrophe – single quote) character
&quot; - the “ (double quote) character
153

ELEMENT COMPONENTS
More Rules
<compare>6 <7 & 7> 6 ‘natch’</compare>

Escaping Characters NOT allowed:

- entity reverences are used instead

<compare>6 &lt; 7 &amp; 7 &gt; 6 &apos;natch&apos;</compare>

&amp; – the & (ampersand) character


&lt; - the <(less than) character
&gt; - the> (greater than) character
&apos; - the ‘ (apostrophe – single quote) character
&quot; - the “ (double quote) character
154

ELEMENT ATTRIBUTES
More Rules
<city id = “nyc”> New York City </city>

XML Attributes Values MUST be enclosed with quotes:

- single or double : BUT must match !

“nyc” or ‘nyc’ NOT “nyc’ or ‘nyc”

- can imbed:

“This is Michael’s Bicycle”

‘I can “air quote” for real’


155

ELEMENT ATTRIBUTES
More Rules
<city id = “nyc”> New York City </city>

ALL XML Attributes Values MUST be enclosed with quotes:

- single or double : BUT must match !

“nyc” or ‘nyc’ NOT “nyc’ or ‘nyc”

- can imbed:

“This is Michael’s Bicycle”

‘I can “air quote” for real’


156

ELEMENT ATTRIBUTES
More Rules
<city id = “nyc”> New York City </city>

XML Attributes Values MUST be enclosed with quotes:

- single or double : BUT must match !

“nyc” or ‘nyc’ NOT “nyc’ or ‘nyc”

- can imbed different quotes:

“This is Michael’s Bicycle”

‘I can “air quote” for real’


157

ELEMENT ATTRIBUTES
More Rules
<city id = “nyc”> New York City </city>

XML Attributes can NOT have the same name inside a tag:
158

ELEMENT ATTRIBUTES
More Rules
<city id = “nyc”> New York City </city>

XML Attributes can NOT have the same name inside a tag:

<city id = “1” id = “nyc”>New York City</city> {WRONG!}


159

ELEMENT ATTRIBUTES
More Rules
<city id = “nyc”> New York City </city>

XML Attributes can NOT have the same name inside a tag:

<city id = “1” id = “nyc”>New York City</city> {WRONG!}

<city id = “1”>New York City</city>


<city id = “2”>Chicago</city> {RIGHT!}
160

ELEMENT ATTRIBUTES
More Rules
<city id = “nyc”> New York City </city>

XML Attributes can NOT have the same name inside a tag:

<city id = “1” id = “nyc”> New York City </city> {WRONG!}

<city id = “1”>New York City</city>


<city id = “2”>Chicago</city> {RIGHT!}

XML Attributes can NOT be in the end tag:

<city> New York City </city id = “nyc”> {WRONG!}


161

COMMENTS
<!-- This is an XML Comment -->

Comments are allowed in XML

XML Comments are exactly the same as HTML Comments

Can appear anywhere in the XML Document


- ignored by the computer (XML Processors)
<Friend> <!-- this is a comment -->
<name>Billy</name>
<age>31</age>
<!-- so is this … -->
<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</Friend>
162

COMMENTS
<!-- This is an XML Comment -->

Comments are allowed in XML

XML Comments are exactly the same as HTML Comments

Can appear anywhere in the XML Document


- ignored by the computer (XML Processors)
<Friend> <!-- this is a comment -->
<name>Billy</name>
<age>31</age>
<!-- so is this … -->
<hobbies>
<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
163

COMMENTS
<!-- This is an XML Comment -->

Comments are allowed in XML

XML Comments are exactly the same as HTML Comments

Can appear anywhere in the XML Document


- ignored by the computer (XML Processors)
<Friend> <!-- this is a comment -->
<name>Billy</name>
<age>31</age>
<!-- so is this … -->
<hobbies>
<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
164

COMMENTS
<!-- This is an XML Comment -->

Comments are allowed in XML

XML Comments are exactly the same as HTML Comments

Can appear anywhere in the XML Document


- ignored by the computer (XML Processors)
<Friend> <!-- this is a comment -->
<name>Billy</name>
<age>31</age>
<!-- so is this … -->
<hobbies>
<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</Friend>
165

XML - EXTENSIBLE MARKUP LANGUAGE


Why is it an EXTENSIBLE markup language
Is user defined
Is extensible (can be easily expanded)

<Friend>
<name>Joe Bloggs</name>
<age>25</age>
</Friend>
Can be easily changed (expanded) to

<Friend>
<name>Joe Bloggs</name>
<age>25</age>
<job>Student</job>
<address>123 Fake St.</address>
<courses>
<course>CS1032</course>
<course>CS2034</course>
</courses>
</Friend>
166

XML - EXTENSIBLE MARKUP LANGUAGE


Why is it an EXTENSIBLE markup language
Is user defined
Is extensible (can be easily expanded)

<Friend>
<name>Joe Bloggs</name>
<age>25</age>
</Friend>
Can be easily changed (expanded) to

<Friend>
<name>Joe Bloggs</name>
<age>25</age>
<job>Student</job>
<address>123 Fake St.</address>
<courses>
<course>CS1032</course>
<course>CS2034</course>
</courses>
</Friend>
167

XML - EXTENSIBLE MARKUP LANGUAGE


Why is it an EXTENSIBLE markup language
Is user defined
Is extensible (can be easily expanded)

<Friend>
<name>Joe Bloggs</name>
<age>25</age>
</Friend>
Can be easily changed (expanded) to

<Friend>
<name>Joe Bloggs</name>
<age>25</age>
<job>Student</job>
<address>123 Fake St.</address>
<courses>
<course>CS1032</course>
<course>CS2034</course>
</courses>
</Friend>
168

XML - EXTENSIBLE MARKUP LANGUAGE


Why is it an EXTENSIBLE markup language
Is user defined
Is extensible (can be easily expanded)

<Friend>
<name>Joe Bloggs</name>
<age>25</age> In most cases the computer
</Friend> will ignore the extra tags (if it
Can be easily changed (expanded) to has not yet been programed
<Friend> to understand them).
<name>Joe Bloggs</name>
<age>25</age>
<job>Student</job> Backwards combability.
<address>123 Fake St.</address>
<courses>
<course>CS1032</course>
<course>CS2034</course>
</courses>
</Friend>
169

XML - EXTENSIBLE MARKUP LANGUAGE


Quick Definitions ….
- Elements : Complex versus Simple Elements

<Friend>
<name>Billy Bob Hillbilly</name>
<age>31</age>
</Friend>

<Friend>
<name>
<first>Billy</first>
<middle>Bob</middle>
<last>Hillbilly</last>
</name>
<age>31</age>
</Friend>
170

XML - EXTENSIBLE MARKUP LANGUAGE


Quick Definitions ….
- Elements : Complex versus Simple Elements

<Friend>
<name>Billy Bob Hillbilly</name>
<age>31</age>
</Friend>

<Friend>
<name>
<first>Billy</first>
<middle>Bob</middle>
<last>Hillbilly</last>
</name>
<age>31</age>
</Friend>
171

XML - EXTENSIBLE MARKUP LANGUAGE


Quick Definitions ….
- Elements : Complex versus Simple Elements

<Friend> <!-- complex -->


<name>Billy Bob Hillbilly</name> <!-- simple -->
<age>31</age> <!-- simple -->
</Friend>

<Friend>
<name>
<first>Billy</first>
<middle>Bob</middle>
<last>Hillbilly</last>
</name>
<age>31</age>
</Friend>
172

XML - EXTENSIBLE MARKUP LANGUAGE


Quick Definitions ….
- Elements : Complex versus Simple Elements

<Friend> <!-- complex -->


<name>Billy Bob Hillbilly</name> <!-- simple -->
<age>31</age> <!-- simple -->
</Friend>

<Friend> <!-- complex -->


<name> <!-- complex -->
<first>Billy</first> <!-- simple -->
<middle>Bob</middle> <!-- simple -->
<last>Hillbilly</last> <!-- simple -->
</name>
<age>31</age> <!-- simple -->
</Friend>
173

XML NAMESPACE

Processing XM Documents:

- may have to combine two or more documents into a single work unit

- remember the rule: tag names must be unique

- how to ensure Doc1 never uses the same tag as Doc2 ???
XML NAMESPACE
174

Conflict Example:
File1.xml: File2.xml:
<?xml version="1.0"?> <?xml version="1.0"?>
<Friends> <Worker>
<person> <person>
<name>Billy</name> <name>
<age>31</age> <first>Frazer</first>
<hobbies> <last>Fur</last>
<hobby>cosplay</hobby> </name>
<hobby>Avengers</hobby> <age>31</age>
<hobby>DandD</hobby> <schedule>
</hobbies> <day>Tuesday</day>
</person> <day>Thursday</day>
</schedule>
<person> </person>
<name>Idris</name> </Worker>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</person>
</Friends>
XML NAMESPACE 175
Combined XML Documents in One File - fCombined.xml
<?xml version="1.0"?> <!– File1.xml →
<Friends>
<person>
<name>Billy</name>
<age>31</age>
<hobbies>
<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</person>
<person>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</person>
</Friends>

<Worker> <!– File2.xml →


<person>
<name>
<first>Frazer</first>
<last>Fur</last>
</name>
<age>31</age>
<schedule>
<day>Tuesday</day>
<day>Thursday</day>
</schedule>
</person>
</n2:Worker>
XML NAMESPACE
176

Conflict Example:
File1.xml: File2.xml:
<?xml version="1.0"?> <?xml version="1.0"?>
<Friends> <Worker>
<person> <person>
<name>Billy</name> <name>
<age>31</age> <first>Frazer</first>
<hobbies> <last>Fur</last>
<hobby>cosplay</hobby> </name>
<hobby>Avengers</hobby> <age>31</age>
<hobby>DandD</hobby> <schedule>
</hobbies> <day>Tuesday</day>
</person> <day>Thursday</day>
</schedule>
<person> </person>
<name>Idris</name> </Worker>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</person>
</Friends>
XML NAMESPACE 177
Combined XML Documents in One File - fCombined.xml
<?xml version="1.0"?> <!– File1.xml →
<Friends>
<person>
<name>Billy</name>
<age>31</age>
<hobbies>
<hobby>cosplay</hobby>
<hobby>Avengers</hobby>
<hobby>DandD</hobby>
</hobbies>
</person>
<person>
<name>Idris</name>
<age>27</age>
<hobbies>
<hobby>Dr. Who</hobby>
</hobbies>
</person>
</Friends>

<Worker> <!– File2.xml →


<person>
<name>
<first>Frazer</first>
<last>Fur</last>
</name>
<age>31</age>
<schedule>
<day>Tuesday</day>
<day>Thursday</day>
</schedule>
</person>
</n2:Worker>
XML NAMESPACE
178

Conflict Resolution: (how to solve the problem)

- give each document a Namespace.

- fancy way of just saying ‘associate a last name with each document’
- give each a document a ‘last name’ and treat the tag as a ‘first name’

David Smith,David Jones, David Bond, David


Jordan Jones, Jordan Whales, Jordan
Yue Jones, Yue Bond, Yue Xi, Yue
Matthew Smith, Matthew
Cameron Bond, Cameron Maggs, Cameron
Sam Maggs, Sam Jones, Sam
XML NAMESPACE
179

Conflict Resolution: (how to solve the problem)

- give each document a Namespace.

- fancy way of just saying ‘associate a last name with each document’
- give each a document a ‘last name’ and treat the tag as a ‘first name’

David Smith,David Jones, David Bond, David


Jordan Jones, Jordan Whales, Jordan
Yue Jones, Yue Bond, Yue Xi, Yue
Matthew Smith, Matthew
Cameron Bond, Cameron Maggs, Cameron
Sam Maggs, Sam Jones, Sam
XML NAMESPACE
180

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- how to ensure the ‘Last Name’ is unique


XML NAMESPACE
181

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- how to ensure the ‘Last Name’ is unique

Declare a Namespace in XML:


Change Root tag:

<Friends>

To:

<ns1:Friends xmlns:ns1=“http://cs1.ca/cs1032.html">
XML NAMESPACE
182

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- how to ensure the ‘Last Name’ is unique

Declare a Namespace in XML:


Change Root tag:

<Friends> NamespaceURI
Namespace Uniform Resource Identifier
To: root ( usually a URL )

<ns1:Friends xmlns:ns1="http://cs1.ca/cs1032.html">

Namespace Identifier character sequence for namespace


XML NAMESPACE
183

Declare a Namespace in XML: - File1.xml


<ns1:group xmlns:ns1="http://cs1.ca/friends">
<?xml version="1.0"?> <?xml version="1.0"?>
<Friends> <ns1:Friends xmlns:ns1="http://cs1.ca/friends">
<person> <ns1:person>
<name>Billy</name> <ns1:name>Billy</ns1:name>
<age>31</age> <ns1:age>31</ns1:age>
<hobbies> <ns1:hobbies>
<hobby>cosplay</hobby> <ns1:hobby>cosplay</ns1:hobby>
<hobby>Avengers</hobby> <ns1:hobby>Avengers</ns1:hobby>
<hobby>DandD</hobby> <ns1:hobby>DandD</ns1:hobby>
</hobbies> </ns1:hobbies>
</person> </ns1:person>

<person> <ns1:person>
<name>Idris</name> <ns1:name>Idris</ns1:name>
<age>27</age> <ns1:age>27</ns1:age>
<hobbies> <ns1:hobbies>
<hobby>Dr. Who</hobby> <ns1:hobby>Dr. Who</ns1:hobby>
</hobbies> </ns1:hobbies>
</person> </ns1:person>
</Friends> </ns1:Friends>
XML NAMESPACE
184

Declare a Namespace in XML: - File1.xml


<ns1:group xmlns:ns1="http://cs1.ca/friends">
<?xml version="1.0"?> <?xml version="1.0"?>
<Friends> <ns1:Friends xmlns:ns1="http://cs1.ca/friends">
<person> <ns1:person>
<name>Billy</name> <ns1:name>Billy</ns1:name>
<age>31</age> <ns1:age>31</ns1:age>
<hobbies> <ns1:hobbies>
<hobby>cosplay</hobby> <ns1:hobby>cosplay</ns1:hobby>
<hobby>Avengers</hobby> <ns1:hobby>Avengers</ns1:hobby>
<hobby>DandD</hobby> <ns1:hobby>DandD</ns1:hobby>
</hobbies> </ns1:hobbies>
</person> </ns1:person>

<person> <ns1:person>
<name>Idris</name> <ns1:name>Idris</ns1:name>
<age>27</age> <ns1:age>27</ns1:age>
<hobbies> <ns1:hobbies>
<hobby>Dr. Who</hobby> <ns1:hobby>Dr. Who</ns1:hobby>
</hobbies> </ns1:hobbies>
</person> </ns1:person>
</Friends> </ns1:Friends>
XML NAMESPACE
185

Declare a Namespace in XML: - File2.xml


<ns2:group xmlns:ns2=“http://cs1.ca/workers">

<?xml version="1.0"?>
<?xml version="1.0"?>
<ns2:Worker xmlns:ns2="http://cs1.ca/workers">
<Worker>
<ns2:person>
<person>
<ns2:name>
<name>
<ns2:first>Frazer</ns2:first>
<first>Frazer</first>
<ns2:last>Fur</ns2:last>
<last>Fur</last>
</ns2:name>
</name>
<ns2:age>31</ns2:age>
<age>31</age>
<ns2:schedule>
<schedule>
<ns2:day>Tuesday</ns2:day>
<day>Tuesday</day>
<ns2:day>Thursday</ns2:day>
<day>Thursday</day>
</ns2:schedule>
</schedule>
</ns2:person>
</person>
</n2:Worker>
</Worker>
XML NAMESPACE
186

Declare a Namespace in XML: - File2.xml


<ns2:group xmlns:ns2=“http://cs1.ca/workers">

<?xml version="1.0"?>
<?xml version="1.0"?>
<ns2:Worker xmlns:ns2="http://cs1.ca/workers">
<Worker>
<ns2:person>
<person>
<ns2:name>
<name>
<ns2:first>Frazer</ns2:first>
<first>Frazer</first>
<ns2:last>Fur</ns2:last>
<last>Fur</last>
</ns2:name>
</name>
<ns2:age>31</ns2:age>
<age>31</age>
<ns2:schedule>
<schedule>
<ns2:day>Tuesday</ns2:day>
<day>Tuesday</day>
<ns2:day>Thursday</ns2:day>
<day>Thursday</day>
</ns2:schedule>
</schedule>
</ns2:person>
</person>
</n2:Worker>
</Worker>
XML NAMESPACE
Combined XML Documents (Files) - fCombined.xml
187

<?xml version="1.0"?>
<ns1:Friends xmlns:ns1="http://cs1.ca/friends">
<ns1:person>
<ns1:name>Billy</ns1:name>
<ns1:age>31</ns1:age>
<ns1:hobbies>
<ns1:hobby>cosplay</ns1:hobby>
<ns1:hobby>Avengers</ns1:hobby>
<ns1:hobby>DandD</ns1:hobby>
</ns1:hobbies>
</ns1:person>
<ns1:person>
<ns1:name>Idris</ns1:name>
<ns1:age>27</ns1:age>
<ns1:hobbies>
<ns1:hobby>Dr. Who</ns1:hobby>
</ns1:hobbies>
</ns1:person>
</ns1:Friends>

<ns2:Worker xmlns:ns2="http://cs1.ca/workers">
<ns2:person>
<ns2:name>
<ns2:first>Frazer</ns2:first>
<ns2:last>Fur</ns2:last>
</ns2:name>
<ns2:age>31</ns2:age>
<ns2:schedule>
<ns2:day>Tuesday</ns2:day>
<ns2:day>Thursday</ns2:day>
</ns2:schedule>
</ns2:person>
</n2:Worker>
XML NAMESPACE
Combined XML Documents (Files) - fCombined.xml
188

<?xml version="1.0"?>
<ns1:Friends xmlns:ns1="http://cs1.ca/friends">
<ns1:person>
<ns1:name>Billy</ns1:name>
<ns1:age>31</ns1:age>
<ns1:hobbies>
<ns1:hobby>cosplay</ns1:hobby>
<ns1:hobby>Avengers</ns1:hobby>
<ns1:hobby>DandD</ns1:hobby>
</ns1:hobbies>
</ns1:person>
<ns1:person>
<ns1:name>Idris</ns1:name>
<ns1:age>27</ns1:age> Still have an issue.
<ns1:hobbies>
<ns1:hobby>Dr. Who</ns1:hobby>
</ns1:hobbies>
</ns1:person> XML rules say we can
</ns1:Friends>
only have one root
<ns2:Worker xmlns:ns2="http://cs1.ca/workers">
<ns2:person> element!
<ns2:name>
<ns2:first>Frazer</ns2:first>
<ns2:last>Fur</ns2:last>
</ns2:name>
<ns2:age>31</ns2:age>
<ns2:schedule>
<ns2:day>Tuesday</ns2:day>
<ns2:day>Thursday</ns2:day>
</ns2:schedule>
</ns2:person>
</n2:Worker>
XML NAMESPACE
189

Combined XML Documents (Files) - fCombined.xml

Add new root element


<?xml version="1.0"?>
<newRoot>
<ns1:Friends xmlns:ns1="http://cs1.ca/friends">
<ns1:person>
<ns1:name>Billy</ns1:name>
(could have any valid label)
<ns1:age>31</ns1:age>
<ns1:hobbies>
<ns1:hobby>cosplay</ns1:hobby>
<ns1:hobby>Avengers</ns1:hobby>
<ns1:hobby>DandD</ns1:hobby>
</ns1:hobbies>
</ns1:person>
<ns1:person>
<ns1:name>Idris</ns1:name>
<ns1:age>27</ns1:age>
<ns1:hobbies>
<ns1:hobby>Dr. Who</ns1:hobby>
</ns1:hobbies>
</ns1:person>
</ns1:Friends>
<ns2:Worker xmlns:ns2="http://cs1.ca/workers">
<ns2:person>
<ns2:name>
<ns2:first>Frazer</ns2:first>
<ns2:last>Fur</ns2:last>
</ns2:name>
<ns2:age>31</ns2:age>
<ns2:schedule>
<ns2:day>Tuesday</ns2:day>
<ns2:day>Thursday</ns2:day>
</ns2:schedule>
</ns2:person>
</n2:Worker>
</newRoot>
XML NAMESPACE
190

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- Why a web site?????

<ns1:Friends xmlns:ns1=“foobar"> <!-- Valid Namespace -->


<ns2:Worker xmlns:ns2=“qewrtyuiop"> <!-- Valid Namespace -->

What happens of the other developer is also uses foobar or mashes the first row of their keyboard?

Using a different web space guarantees uniqueness …

<ns1:Friends xmlns:ns1="http://cs1.ca/cs1032.html">
XML NAMESPACE
191

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- Why a web site?????

<ns1:Friends xmlns:ns1=“foobar"> <!-- Valid Namespace -->


<ns2:Worker xmlns:ns2=“qewrtyuiop"> <!-- Valid Namespace -->

What happens of the other developer is also uses foobar or mashes the first row of their keyboard?

Using a different web space guarantees uniqueness …

<ns1:Friends xmlns:ns1="http://cs1.ca/cs1032.html">
XML NAMESPACE
192

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- Why a web site?????

<ns1:Friends xmlns:ns1=“foobar"> <!-- Valid Namespace -->


<ns2:Worker xmlns:ns2=“qewrtyuiop"> <!-- Valid Namespace -->

What happens of the other developer is also uses foobar or mashes the first row of their keyboard?

Using a different web space guarantees uniqueness …

<ns1:Friends xmlns:ns1="http://cs1.ca/cs1032.html">
XML NAMESPACE
193

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- Why a web site?????

<ns1:Friends xmlns:ns1=“foobar"> <!-- Valid Namespace -->


<ns2:Worker xmlns:ns2=“qewrtyuiop"> <!-- Valid Namespace -->

What happens of the other developer is also uses foobar or mashes the first row of their keyboard?

Using a different web space guarantees uniqueness …

<ns1:Friends xmlns:ns1="http://cs1.ca/cs1032.html">
XML NAMESPACE
194

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- Why a web site?????

<ns1:Friends xmlns:ns1=“foobar"> <!-- Valid Namespace -->


<ns2:Worker xmlns:ns2=“qewrtyuiop"> <!-- Valid Namespace -->

What happens of the other developer is also uses foobar or mashes the first row of their keyboard?

Using a different web space guarantees uniqueness …

<ns1:Friends xmlns:ns1="http://cs1.ca/cs1032.html">
XML NAMESPACE
195

Conflict Resolution: (how to solve the problem)

- BUT !!!!

- Why a web site?????

<ns1:Friends xmlns:ns1=“foobar"> <!-- Valid Namespace -->


<ns2:Worker xmlns:ns2=“qewrtyuiop"> <!-- Valid Namespace -->

What happens of the other developer is also uses foobar or mashes the first row of their keyboard?

Using a different web space guarantees uniqueness …


Note: Some Namespaces are set
<ns1:Friends xmlns:ns1="http://cs1.ca/cs1032.html"> aside for consistency (see XSDs)
XML NAMESPACE
196

Namespace:
ONLY ONLY ONLY ONLY ONLY ONLY USED …..
XML NAMESPACE
197

Namespace:
ONLY ONLY ONLY ONLY ONLY ONLY USED …..
- 1.) when combing two or more XML documents into a SINGLE
computer file.
XML NAMESPACE
198

Namespace:
ONLY ONLY ONLY ONLY ONLY ONLY USED …..
- 1.) when combing two or more XML documents into a SINGLE
computer file.
- then you MUST use a namespace to differentiate !
XML NAMESPACE
199

Namespace:
ONLY ONLY ONLY ONLY ONLY ONLY USED …..
- 1.) when combing two or more XML documents into a SINGLE
computer file.
- then you MUST use a namespace to differentiate !
- 2.) in an XML Schema
- you ALWAYS use namespace in a schema
(… explanation when we cover Schemas )
XML NAMESPACE
200

Namespace:
ONLY ONLY ONLY ONLY ONLY ONLY USED …..
- 1.) when combing two or more XML documents into a SINGLE
computer file.
- then you MUST use a namespace to differentiate !
- 2.) in an XML Schema
- you ALWAYS use namespace in a schema
(… explanation when we cover Schemas )
OTHERWISE – Namespace is optional
- and never really used except 1. and 2. above
201

XML DOCUMENTS
Scenario:

I need you to send me data in an XML Document (file) so I can import it


into my database.

question 1.) how do I tell you exactly:


- what data I need
- the format of each data item (text, integer, date, etc.)
- the structure of the data (tables)
- the names, usage and position of the tags in the XML (fields)

question 2.) how do I verify your XML is correct:


- well formed
- valid

SOLUTION: XML Schema Document


- ‘blueprint’ of the required XML Data Document
202

XML DOCUMENTS
Scenario:

I need you to send me data in an XML Document (file) so I can import it


into my database.

question 1.) how do I tell you exactly:


- what data I need
- the format of each data item (text, integer, date, etc.)
- the structure of the data (tables)
- the names, usage and position of the tags in the XML (fields)

question 2.) how do I verify your XML is correct:


- well formed
- valid

SOLUTION: XML Schema Document


- ‘blueprint’ of the required XML Data Document

You might also like