CS 80: Internet Programming
Instructor: Mark Edmonds
John 10 Bill 15 Judy 25
XML allows us to share data efficiently
Consider the following
<!-- XML representing a family - notice the explicit structure -->
<family>
<member>
<name>John</name>
<age>10</age>
</member>
<member>
<name>Bill</name>
<age>15</age>
</member>
<member>
<name>Judy</name>
<age>25</age>
</member>
</family>
<data>
<data>
\
after the
<
,
e.g. </data>
/
before the closing <
of the
opening tag. e.g. with
<data/>
as the start tag<html>
.xml
and are readable by any text editor.html
file
into the DOM tree (it has to parse it!)article.xml
<?xml version="1.0"?>
<!-- Fig. 15.2: article.xml -->
<!-- Article structured with XML -->
<article>
<title>Simple XML</title>
<date>July 4, 2007</date>
<author>
<firstName>John</firstName>
<lastName>Doe</lastName>
</author>
<summary>XML is pretty easy.</summary>
<content>This chapter presents examples that use XML.</content>
</article>
<?xml
version="1.0"?>
declares the document as a XML
document
<!DOCTYPE
HTML>
Suppose we want to use the use "subject" in multiple ways: one for subjects in high school, the other for subjects in medical schools
<subject>Geometry</subject>
<subject>Radiology</subject>
We have an ambiguity in our data format as we probably don't want to mix high school and medical school subjects!
Namespaces allow us to give more specific scope to an XML element
:
) before the XML element
nameFor our example
<highschool:subject>Geometry</highschool:subject>
<medicalschool:subject>Radiology</medicalschool:subject>
xmlns
defines a namespace
xmlns:prefix="URI"
urn:schooltypes
xml
(it is reserved)namespaces.xml
<?xml version="1.0"?>
<!-- Fig. 15.5: namespace.xml -->
<!-- Demonstrating namespaces -->
<text:directory xmlns:text="urn:deitel:textInfo" xmlns:image="urn:deitel:imageInfo">
<text:file filename="book.xml">
<text:description>A book list</text:description>
</text:file>
<image:file filename="funny.jpg">
<image:description>A funny picture</image:description>
<image:size width="200" height="100" />
</image:file>
</text:directory>
xmlns = "URI"
specifies a
default namespace for the entire documentdefault_namespaces.xml
<?xml version="1.0"?>
<!-- Fig. 15.6: defaultnamespace.xml -->
<!-- Using default namespaces -->
<directory xmlns="urn:deitel:textInfo" xmlns:image="urn:deitel:imageInfo">
<file filename="book.xml">
<description>A book list</description>
</file>
<image:file filename="funny.jpg">
<image:description>A funny picture</image:description>
<image:size width="200" height="100" />
</image:file>
</directory>
SUBJECT
followed by a PREDICATE
, but also has
many optional arguments.xsd
book.xml
<?xml version="1.0"?>
<!-- Fig. 15.9: book.xml -->
<!-- Book list marked up as XML -->
<deitel:books xmlns:deitel="http://www.deitel.com/booklist">
<book>
<title>Visual Basic 2010 How to Program</title>
</book>
<book>
<title>Visual C# 2010 How to Program, 4/e</title>
</book>
<book>
<title>Java How to Program, 9/e</title>
</book>
<book>
<title>C++ How to Program, 8/e</title>
</book>
<book>
<title>Internet and World Wide Web How to Program, 5/e</title>
</book>
</deitel:books>
book.xsd
<?xml version = "1.0"?>
<!-- Fig. 15.10: book.xsd
-->
<!-- Simple W3C XML Schema document -->
<!--
The first xmlns defines the namespace for this document, which is a schema.
xmlns:deitel defines a namespace of "dietel", used to differentiate between names used for the XML schema and names used by our schema
targetNamespace defines which namespace will use this schema for validation
-->
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
xmlns:deitel = "http://www.deitel.com/booklist"
targetNamespace = "http://www.deitel.com/booklist">
<!-- declaring an element named "books" and its schema type, "BooksType" in the "dietel" namespace -->
<element name = "books" type = "deitel:BooksType"/>
<!-- declare the complext type "BooksType" used with the "books" element -->
<complexType name = "BooksType">
<!-- sequence specifies the order in which child elements must appear -->
<sequence>
<!-- declare an element names "book" of type "SingleBookType" that must occur at least once and can occur an infinite amount of times -->
<element name = "book" type = "deitel:SingleBookType"
minOccurs = "1" maxOccurs = "unbounded"/>
</sequence>
</complexType>
<!-- declare the "SingleBookType" complex type used with the "book" element -->
<complexType name = "SingleBookType">
<sequence>
<!-- specify that the "title" element is a string -->
<element name = "title" type = "string"/>
</sequence>
</complexType>
</schema>
xmlns
,
which can be used to validate the schemaxmlns:deitel
, which is
used to define names created by ustargetNamespace
is the URI of the
XML vocabulary that this schema defineslaptop.xml
<?xml version="1.0"?>
<!-- Fig. 15.13: laptop.xml
-->
<!-- Laptop components marked up as XML -->
<!-- declare a laptop computer with manufacturer "IBM" -->
<computer:laptop xmlns:computer="http://www.deitel.com/computer" manufacturer="IBM">
<processor model="Centrino">Intel</processor>
<monitor>17</monitor>
<CPUSpeed>2.4</CPUSpeed>
<RAM>256</RAM>
</computer:laptop>
laptop.xsd
<?xml version = "1.0"?>
<!-- Fig. 15.12: computer.xsd -->
<!-- W3C XML Schema document
-->
<schema xmlns = "http://www.w3.org/2001/XMLSchema"
xmlns:computer = "http://www.deitel.com/computer"
targetNamespace = "http://www.deitel.com/computer">
<!-- declare a simple type of "gigahertz"-->
<simpleType name = "gigahertz">
<!-- sepcify a restriction on the base type decimal -->
<restriction base = "decimal">
<!-- set minimum value -->
<minInclusive value = "2.1"/>
</restriction>
</simpleType>
<!-- declare a complex type of CPU -->
<complexType name = "CPU">
<!-- create simple content -->
<simpleContent>
<!-- here we "extend" the simple content to contain a string -->
<extension base = "string">
<!-- set the name and the type of CPU -->
<attribute name = "model" type = "string"/>
</extension>
</simpleContent>
</complexType>
<!-- declare a complex type "portable" -->
<complexType name = "portable">
<!-- All specifies that each child element must be included -->
<all>
<!-- declare elements and their type -->
<element name = "processor" type = "computer:CPU"/>
<element name = "monitor" type = "int"/>
<element name = "CPUSpeed" type = "computer:gigahertz"/>
<element name = "RAM" type = "int"/>
</all>
<!-- declare an attribute for the manufacturer of the laptop -->
<attribute name = "manufacturer" type = "string"/>
</complexType>
<!-- declare a single laptop element -->
<element name = "laptop" type = "computer:portable"/>
</schema>