Title: Chapter 20 Extensible Markup Language XML
1Chapter 20 Extensible Markup Language (XML)
Outline 20.1 Introduction 20.2 Structuring
Data 20.3 XML Namespaces 20.4 Document Type
Definitions (DTDs) and Schemas 20.4.1 Document
Type Definitions 20.4.2 W3C XML Schema Documents
2Objectives
- In this lesson, you will learn
- To understand XML.
- To be able to mark up data using XML.
- To become familiar with the types of markup
languages created with XML. - To understand the relationships among DTDs,
Schemas and XML. - To understand the fundamentals of DOM-based and
SAX-based parsing. - To understand the concept of an XML namespace.
- To be able to create simple XSL documents.
- To become familiar with Web services and related
technologies.
320.1 Introduction
- XML stands for EXtensible Markup Language.
- XML is designed to describe data and to focus on
what data is. - You will learn
- About XML
- The difference between XML and HTML
- How to start using XML in your applications.
420.1 Introduction
- What is XML?
- XML stands for EXtensible Markup Language
- XML is a markup language much like HTML
- XML was designed to describe data
- XML tags are not predefined. You must define your
own tags - XML uses a Document Type Definition (DTD) or an
XML Schema to describe the data - XML with a DTD or XML Schema is designed to be
self-descriptive - XML is a W3C Recommendation
520.1 Introduction
- The Main Difference Between XML and HTML
- XML was designed to carry data.
- XML is not a replacement for HTML.
- XML and HTML were designed with different goals
- XML was designed to describe data and to focus on
what data is. - HTML was designed to display data and to focus on
how data looks. - HTML is about displaying information, while XML
is about describing information.
620.1 Introduction
- XML Does not DO Anything
- XML was not designed to DO anything.
- ltnotegt
- lttogtTovelt/togt
- ltfromgtJanilt/fromgt
- ltheadinggtReminderlt/headinggt
- ltbodygtDon't forget me this weekend!lt/bodygt
- lt/notegt
720.1 Introduction
- XML is Free and Extensible
- XML tags are not predefined. You must "invent"
your own tags. - XML is a Complement to HTML
- XML is not a replacement for HTML.
- XML in Future Web Development
- XML is going to be everywhere.
820.1 Introduction
- How can XML be Used?
- XML can Separate Data from HTML
- XML is Used to Exchange Data
- XML Can be Used to Share Data
- XML Can be Used to Store Data
- XML Can be Used to Create New Languages
920.1 Introduction
- The syntax rules of XML are very simple and very
strict. The rules are very easy to learn, and
very easy to use. - Because of this, creating software that can read
and manipulate XML is very easy.
10article.xml(1 of 1)
11(No Transcript)
12(No Transcript)
1320.2 Structuring Data
- All XML Elements Must Have a Closing Tag
- XML Tags are Case Sensitive
- XML Elements Must be Properly Nested
- XML Documents Must Have Only One Root Element
- XML Attribute Values Must be Quoted
- Comments in XML is the same as XHTML
1420.2 Structuring Data
- Again from the example, XML document has
- XML declaration
- Value version
- Indicates the XML version to which the document
conforms - Root element
- Element that encompasses every other elements
- Container element
- Any element contains other elements
- Child elements
- Elements inside a container element
- Empty element flag
- Does not contain any text
15letter.xml(1 of 2)
16letter.xml(2 of 2)
17(No Transcript)
18(No Transcript)
1920.2 Structuring Data
- Use of Elements vs. Attributes?!
- Data can be stored in child elements or in
attributes. - ltperson sex"female"gt
- ltfirstnamegtAnnalt/firstnamegt
- ltlastnamegtSmithlt/lastnamegt
- lt/persongt
- Vs.
- ltpersongt
- ltsexgtfemalelt/sexgt
- ltfirstnamegtAnnalt/firstnamegt
- ltlastnamegtSmithlt/lastnamegt
- lt/persongt
- Which one to use?
2020.2 Structuring Data
- The following is valid, but do not do it (this is
not what XML is for) - ltnote day"12" month"11" year"2002"
- to"Tove" from"Jani" heading"Reminder"
- body"Don't forget me this weekend!"gt
- lt/notegt
- Do it in case of id
- ltmessagesgt
- ltnote id"p501"gt
- lttogtTovelt/togt
- ltfromgtJanilt/fromgt
- ltheadinggtReminderlt/headinggt
- ltbodygtDon't forget me this weekend!lt/bodygt
- lt/notegt
- ltnote id"p502"gt
- lttogtJanilt/togt
- ltfromgtTovelt/fromgt
- ltheadinggtRe Reminderlt/headinggt
- ltbodygtI will not!lt/bodygt
2120.3 XML Namespaces
- Since element names in XML are not predefined, a
name conflict will occur when two different
documents use the same element names. - This XML document carries information in a table
- lttablegt
- lttrgt
- lttdgtAppleslt/tdgt
- lttdgtBananaslt/tdgt
- lt/trgt
- lt/tablegt
- This XML document carries information about a
table (a piece of furniture) - lttablegt
- ltnamegtAfrican Coffee Tablelt/namegt
- ltwidthgt80lt/widthgt
- ltlengthgt120lt/lengthgt
- lt/tablegt
2220.3 XML Namespaces
- Solving Name Conflicts Using a Prefix
- This XML document carries information in a table
- lthtablegt
- lthtrgt
- lthtdgtAppleslt/htdgt
- lthtdgtBananaslt/htdgt
- lt/htrgt
- lt/htablegt
- This XML document carries information about a
piece of furniture - ltftablegt
- ltfnamegtAfrican Coffee Tablelt/fnamegt
- ltfwidthgt80lt/fwidthgt
- ltflengthgt120lt/flengthgt
- lt/ftablegt
2320.3 XML Namespaces
- Solving Name Conflicts Using Namespaces
- The XML Namespace (xmlns) Attribute
- The XML namespace attribute is placed in the
start tag of an element and has the following
syntax - xmlnsnamespace-prefix"namespaceURI"
24namespace.xml(1 of 1)
2520.3 XML Namespaces
- Default Namespaces
- Defining a default namespace for an element saves
us from using prefixes in all the child elements.
It has the following syntax - xmlns"namespaceURI"
26defaultnamespace.xml(1 of 1)
2720.4 Document Type Definitions (DTDs) and
Schemas
- Two types of documents for specifying XML
document structure - Document Type Definition (DTDs)
- Schemas
2820.4.1 Document Type Definitions
- The purpose of a DTD (Document Type Definition)
is to define the legal building blocks of an XML
document. - It defines the document structure with a list of
legal elements and attributes. - A DTD can be declared inline inside an XML
document, or as an external reference.
2920.4.1 Document Type Definitions
- External DTD Declaration
- If the DTD is declared in an external file, it
should be wrapped in a DOCTYPE definition with
the following syntax - lt!DOCTYPE root-element SYSTEM "filename"gt
- For Example
- lt?xml version"1.0"?gt
- lt!DOCTYPE note SYSTEM "note.dtd"gt
- ltnotegt
- lttogtTovelt/togt
- ltfromgtJanilt/fromgt
- ltheadinggtReminderlt/headinggt
- ltbodygtDon't forget me this weekend!lt/bodygt
- lt/notegt
3020.4.1 Document Type Definitions
- "note.dtd" contains
- lt!ELEMENT note (to,from,heading,body)gt
- lt!ELEMENT to (PCDATA)gt
- lt!ELEMENT from (PCDATA)gt
- lt!ELEMENT heading (PCDATA)gt
- lt!ELEMENT body (PCDATA)gt
3120.4.1 Document Type Definitions
- Why Use a DTD?
- Each of your XML files can carry a description of
its own format. - Independent groups of people can agree to use a
standard DTD for interchanging data. - Your application can use a standard DTD to verify
that the data you receive from the outside world
is valid. - You can also use a DTD to verify your own data.
3220.4.1 Document Type Definitions
- The Building Blocks of XML Documents
- ELEMENT type declaration
- Defines rules
- ATTLIST attribute-list declaration
- Defines an attribute
33letter.dtd(1 of 1)
34letter.xml(1 of 2)
35letter.xml(2 of 2)
3620.4.2 W3C XML Schema Documents
- XML Schema is an XML-based alternative to DTDs.
- An XML Schema describes the structure of an XML
document. - The XML Schema language is also referred to as
XML Schema Definition (XSD).
3720.4.2 W3C XML Schema Documents
- XML File
- lt?xml version"1.0"?gt
- ltnotegt
- lttogtTovelt/togt
- ltfromgtJanilt/fromgt
- ltheadinggtReminderlt/headinggt
- ltbodygtDon't forget me this weekend!lt/bodygt
- lt/notegt
3820.4.2 W3C XML Schema Documents
- DTD File
- lt!ELEMENT note (to, from, heading, body)gt
- lt!ELEMENT to (PCDATA)gt
- lt!ELEMENT from (PCDATA)gt
- lt!ELEMENT heading (PCDATA)gt
- lt!ELEMENT body (PCDATA)gt
3920.4.2 W3C XML Schema Documents
- XML Schema
- lt?xml version"1.0"?gt
- ltxsschema xmlnsxs"http//www.w3.org/2001/XMLSch
ema" - targetNamespace"http//www.w3schools.com"
- xmlns"http//www.w3schools.com"
- elementFormDefault"qualified"gt
- ltxselement name"note"gt
- ltxscomplexTypegt
- ltxssequencegt
- ltxselement name"to" type"xsstring"/gt
- ltxselement name"from" type"xsstring"/gt
- ltxselement name"heading" type"xsstring"/gt
- ltxselement name"body" type"xsstring"/gt
- lt/xssequencegt
- lt/xscomplexTypegt
- lt/xselementgt
- lt/xsschemagt
4020.4.2 W3C XML Schema Documents
- Root element schema
- Contains elements that define the XML document
structure - targetNamespace
- Namespace of XML vocabulary the schema defines
- element tag
- Defines element to be included in XML document
structure - name and type attributes
- Specify elements name and data type respectively
- Built-in simple types
- date, int, double, time, etc
4120.4.2 W3C XML Schema Documents
- Two categories of data types
- Simple types
- Cannot contain attributes or child elements
- Complex types
- May contain attributes and child elements
- complexType
- Define complex type
- Simple content
- Cannot have child elements
- Complex content
- May have child elements
42book.xml(1 of 1)
43book.xsd(1 of 1)
44(No Transcript)
45computer.xsd(1 of 2)
46computer.xsd(2 of 2)
47laptop.xml(1 of 1)