Class 2 Notes for XML

Learning Objectives:

You will be able to create a simple XML document

You will be able to identify the componets of well formed XML document

.You will be able to identify the difference between well formed and valid XML documents

Homework: First XML Document

Read: Chapter 1 & 2

You need to read the W3schools XML ElementsAttributes, &   DTD tutorials!

Well formed vs Valid:

Well formed mean the XML document follows proper XML syntax!

Valid means the document is well-formed and follows the structure defined in a DTD or Schema

Create a Fake XML Resume & XML Address book for posting next week.


Topics Covered:

Syntax Rules

XML documents must start with <?xml version="1.0"?> or <?xml version="1.0" encoding="ISO-8859-1"?> to be well formed.

Typical XML encoding ISO-8859-1 or UTF-8 or UTF-16 defines the characters used within the document

XML Elements are Extensible (expandable) but must follow these rules
  • Names cannot start with a number or punctuation character

  • Names can contain letters, numbers, and other characters

  • Names cannot start with the letters xml (or XML, or Xml, etc)

  • Names cannot contain spaces
All XML Elements Must Have a Closing Tag!

<element> info </element> or <element />

XML Elements are Case Sensitive

<element> is different from <Element>

<element>This is not closed so the code is </Element>
<element>This is correct</element>

XML Attributes Must be Quoted

<info name="Jon"> or <info name='Jon'>

Attribute values must always be quoted. Either single or double quotes can be used.

Please Note: There are no rules about when to use attributes or when to use elements. I would avoid them and use elements instead

XML Elements Must be Properly Nested

<jon> <tom> </jon> </tom> is not nested properly

<jon> is nested </jon>

Element Content can not contain &, >, <, ", ', instead use:

&amp; for &
&gt; for >
&lt; for <
&quot; for "
and &apos; for '


Create an XML file of your family tree (go back at least 2 generations) We will go over this next week!



You need a DTD or a Schema to Validate an XML Document:

Schema uses XML to define an XML document.

DTD use a separate language for defining the structure of an XML document

DTD Info:

DTD = Document Type Definition is used to define the structure you want the document to follow. DTD use a separate language to define the structure the XML should follow. You can have an internal or external DTD

Internal DTD Reference: <!DOCTYPE RootElement [

<!ELEMENT name category> for defining Elements

<!ATTLIST element-name attribute-name attribute-type default-value > for defining Attributes of Elements




See nba-dtd.xml for a simple example of an internal DTD


External DTD Reference: <!DOCTYPE RootElement SYSTEM "filelocation">

The external DTD is a separate document with the following syntax:

You need only the Element and Attritbute references in the document!

<!ELEMENT name category> for defining Elements

<!ATTLIST element-name attribute-name attribute-type default-value > for defining Attributes of Elements.

See nba.dtd for a simple example of an External DTD



Create a DTD for these two files: Me and Me2 (We will review this next class)


Summary of Syntax

XML Rules for Well formness Element Name Rules
<?xml version=“1.0” ? > Names can not start with numbers!

Must Have a Root Element

Names can start with letters or _ (under-bar)

<Start> </Start> or <Start />

You start a tag, you must end it in some format

No Spaces for element names !

Elements Are Case Sensitive!!

<start> is not the same as <Start>

Can Not Start with XML
<!-- Comment -->

No Space after the start of a tag <

XML Keeps White Space

Tag Order Counts


Remember the difference:

XML with correct syntax is a "Well Formed" XML document.

XML is validated against a DTD or Schema




Final Step:

Review in your mind everything we covered in class and remember to cover it again before you go to bed so your mind to process this information while you sleep.