introduction to xml - kth to xml xml document type definition, dtd xml namespaces xml schema xml...

178
Introduction to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Introduction to XML Internet Applications, ID1354 1 / 66

Upload: vudung

Post on 30-Apr-2018

357 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XMLInternet Applications, ID1354

1 / 66

Page 2: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Contents

XML

Document Type Definition, DTD

XML Namespaces

XML Schema

XML Processors

Other XML Standards

2 / 66

Page 3: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Section

XML

Document Type Definition, DTD

XML Namespaces

XML Schema

XML Processors

Other XML Standards

3 / 66

Page 4: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What Is XML?

I XML is a meta-markup language that canbe used to define markup languages, forany kind of information.

I XML is not a replacement for HTML.

I HTML is a markup language used todescribe the parts of a document. HTMLmight be defined using XML.

4 / 66

Page 5: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What Is XML?

I XML is a meta-markup language that canbe used to define markup languages, forany kind of information.

I XML is not a replacement for HTML.I HTML is a markup language used to

describe the parts of a document. HTMLmight be defined using XML.

4 / 66

Page 6: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What Is XML?

I XML is a meta-markup language that canbe used to define markup languages, forany kind of information.

I XML is not a replacement for HTML.I HTML is a markup language used to

describe the parts of a document. HTMLmight be defined using XML.

4 / 66

Page 7: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XML

I XML is a universal way of storing andtransferring data of any kind.

I Specification maintained by W3C.I All documents written with an XML-derived

markup language can be parsed with thesame parser.

5 / 66

Page 8: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XML

I XML is a universal way of storing andtransferring data of any kind.

I Specification maintained by W3C.

I All documents written with an XML-derivedmarkup language can be parsed with thesame parser.

5 / 66

Page 9: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XML

I XML is a universal way of storing andtransferring data of any kind.

I Specification maintained by W3C.I All documents written with an XML-derived

markup language can be parsed with thesame parser.

5 / 66

Page 10: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XML

I XML is a universal way of storing andtransferring data of any kind.

I Specification maintained by W3C.I All documents written with an XML-derived

markup language can be parsed with thesame parser.

5 / 66

Page 11: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XML (Cont’d)

I An XML document contains only text.I Data is marked up using tags:

<name>Stina

</name>

I Human readable and machine readable.

6 / 66

Page 12: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XML (Cont’d)

I An XML document contains only text.I Data is marked up using tags:

<name>Stina

</name>

I Human readable and machine readable.

6 / 66

Page 13: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Introduction to XML (Cont’d)

I An XML document contains only text.I Data is marked up using tags:

<name>Stina

</name>

I Human readable and machine readable.

6 / 66

Page 14: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology

I An XML-based markup language is a tagset, or an XML application.

I A document using an XML-based markuplanguage is an XML document.

I An XML processor is a program that parsesXML documents and provides the parts toan application.

7 / 66

Page 15: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology

I An XML-based markup language is a tagset, or an XML application.

I A document using an XML-based markuplanguage is an XML document.

I An XML processor is a program that parsesXML documents and provides the parts toan application.

7 / 66

Page 16: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology

I An XML-based markup language is a tagset, or an XML application.

I A document using an XML-based markuplanguage is an XML document.

I An XML processor is a program that parsesXML documents and provides the parts toan application.

7 / 66

Page 17: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology (Cont’d)

I A tag defines an element. The XML belowhas the opening tag <name>, the closingtag </name> and the whole line is anelement.<name>Sara</name>

I The text between the opening and closingtag, Sara in the example above, is theelements content.

8 / 66

Page 18: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology (Cont’d)

I A tag defines an element. The XML belowhas the opening tag <name>, the closingtag </name> and the whole line is anelement.<name>Sara</name>

I The text between the opening and closingtag, Sara in the example above, is theelements content.

8 / 66

Page 19: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology (Cont’d)

I There are empty elements,<optional/>.

I Tags may have attributes,<order id=abc123/>.

I A nested element is located between thestart and end tags of another element, as<name>Olle</name> in the xml below.<person>

<name>Olle</name></person>

9 / 66

Page 20: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology (Cont’d)

I There are empty elements,<optional/>.

I Tags may have attributes,<order id=abc123/>.

I A nested element is located between thestart and end tags of another element, as<name>Olle</name> in the xml below.<person>

<name>Olle</name></person>

9 / 66

Page 21: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Terminology (Cont’d)

I There are empty elements,<optional/>.

I Tags may have attributes,<order id=abc123/>.

I A nested element is located between thestart and end tags of another element, as<name>Olle</name> in the xml below.<person>

<name>Olle</name></person>

9 / 66

Page 22: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

An Example

10 / 66

Page 23: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Another Example

11 / 66

Page 24: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Syntax

I The syntax of XML is divided in two distinctlevels.

1. The general low-level rules that apply to allXML documents and tag sets.

2. A particular XML tag set, defined with either aDocument Type Definition (DTD) or an XMLschema.

12 / 66

Page 25: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Syntax

I The syntax of XML is divided in two distinctlevels.

1. The general low-level rules that apply to allXML documents and tag sets.

2. A particular XML tag set, defined with either aDocument Type Definition (DTD) or an XMLschema.

12 / 66

Page 26: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Syntax

I The syntax of XML is divided in two distinctlevels.

1. The general low-level rules that apply to allXML documents and tag sets.

2. A particular XML tag set, defined with either aDocument Type Definition (DTD) or an XMLschema.

12 / 66

Page 27: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules

I The document contains only Unicodecharacters.

I The special characters (e.g. < or &) areused only for markup.

I Tags are correctly nested, with nonemissing and none overlapping.

I Tags are case-sensitive, the start and endtags must match exactly.

13 / 66

Page 28: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules

I The document contains only Unicodecharacters.

I The special characters (e.g. < or &) areused only for markup.

I Tags are correctly nested, with nonemissing and none overlapping.

I Tags are case-sensitive, the start and endtags must match exactly.

13 / 66

Page 29: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules

I The document contains only Unicodecharacters.

I The special characters (e.g. < or &) areused only for markup.

I Tags are correctly nested, with nonemissing and none overlapping.

I Tags are case-sensitive, the start and endtags must match exactly.

13 / 66

Page 30: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules

I The document contains only Unicodecharacters.

I The special characters (e.g. < or &) areused only for markup.

I Tags are correctly nested, with nonemissing and none overlapping.

I Tags are case-sensitive, the start and endtags must match exactly.

13 / 66

Page 31: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules (Cont’d)I Tag names cannot start with -, ., or a digit.I Tag names cannot contain a space

character or any of the characters % ! “# & ( ) * + , / ; < = > ? @[ \ ] ^’ { |}

I A single root element contains all the otherelements.

I All XML documents begin with an XMLdeclaration specifying XML standardversion and character encoding:<?xml version = "1.0" encoding = "utf-8"?>

I An XML document that follows all of theserules is well formed.

14 / 66

Page 32: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules (Cont’d)I Tag names cannot start with -, ., or a digit.I Tag names cannot contain a space

character or any of the characters % ! “# & ( ) * + , / ; < = > ? @[ \ ] ^’ { |}

I A single root element contains all the otherelements.

I All XML documents begin with an XMLdeclaration specifying XML standardversion and character encoding:<?xml version = "1.0" encoding = "utf-8"?>

I An XML document that follows all of theserules is well formed.

14 / 66

Page 33: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules (Cont’d)I Tag names cannot start with -, ., or a digit.I Tag names cannot contain a space

character or any of the characters % ! “# & ( ) * + , / ; < = > ? @[ \ ] ^’ { |}

I A single root element contains all the otherelements.

I All XML documents begin with an XMLdeclaration specifying XML standardversion and character encoding:<?xml version = "1.0" encoding = "utf-8"?>

I An XML document that follows all of theserules is well formed.

14 / 66

Page 34: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules (Cont’d)I Tag names cannot start with -, ., or a digit.I Tag names cannot contain a space

character or any of the characters % ! “# & ( ) * + , / ; < = > ? @[ \ ] ^’ { |}

I A single root element contains all the otherelements.

I All XML documents begin with an XMLdeclaration specifying XML standardversion and character encoding:<?xml version = "1.0" encoding = "utf-8"?>

I An XML document that follows all of theserules is well formed.

14 / 66

Page 35: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

General Low Level Rules (Cont’d)I Tag names cannot start with -, ., or a digit.I Tag names cannot contain a space

character or any of the characters % ! “# & ( ) * + , / ; < = > ? @[ \ ] ^’ { |}

I A single root element contains all the otherelements.

I All XML documents begin with an XMLdeclaration specifying XML standardversion and character encoding:<?xml version = "1.0" encoding = "utf-8"?>

I An XML document that follows all of theserules is well formed.

14 / 66

Page 36: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Tags Instead of AttributesI Attributes are used more restrictively in

XML than in HTML.I In XML, you normally define a nested tag

instead of an attribute to provide moreinformation about the content of a tag.

I Nested tags are preferred, since attributescannot describe structure. Think of tags asobjects and attributes as fields in theobjects.

I Attributes should be used primarily toidentify numbers or names of elements (likeHTML id and name attributes).

15 / 66

Page 37: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Tags Instead of AttributesI Attributes are used more restrictively in

XML than in HTML.I In XML, you normally define a nested tag

instead of an attribute to provide moreinformation about the content of a tag.

I Nested tags are preferred, since attributescannot describe structure. Think of tags asobjects and attributes as fields in theobjects.

I Attributes should be used primarily toidentify numbers or names of elements (likeHTML id and name attributes).

15 / 66

Page 38: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Tags Instead of AttributesI Attributes are used more restrictively in

XML than in HTML.I In XML, you normally define a nested tag

instead of an attribute to provide moreinformation about the content of a tag.

I Nested tags are preferred, since attributescannot describe structure. Think of tags asobjects and attributes as fields in theobjects.

I Attributes should be used primarily toidentify numbers or names of elements (likeHTML id and name attributes).

15 / 66

Page 39: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Tags Instead of AttributesI Attributes are used more restrictively in

XML than in HTML.I In XML, you normally define a nested tag

instead of an attribute to provide moreinformation about the content of a tag.

I Nested tags are preferred, since attributescannot describe structure. Think of tags asobjects and attributes as fields in theobjects.

I Attributes should be used primarily toidentify numbers or names of elements (likeHTML id and name attributes).

15 / 66

Page 40: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Tags Instead of Attributes (Cont’d)<!-- Attribute --><patient name = "Maggie Dee Magpie">

...</patient>

<!-- Nested tag --><patient>

<name> Maggie Dee Magpie </name>...

</patient>

<!-- Nested tag, which has nested tags --><patient>

<name><first> Maggie </first><middle> Dee </middle><last> Magpie </last>

</name>...

</patient>16 / 66

Page 41: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Entities

I A reference to an entity has the form&entity_name;

I Predefined entities (as in HTML):< &lt;> &gt;& &amp;" &quot;’ &apos;

For instance<message>

if salary &lt; 1000 then</message>

17 / 66

Page 42: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Entities

I A reference to an entity has the form&entity_name;

I Predefined entities (as in HTML):< &lt;> &gt;& &amp;" &quot;’ &apos;

For instance<message>

if salary &lt; 1000 then</message>

17 / 66

Page 43: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Character Data Section

I CDATA is text that will not be parsed by anXML parser.

I If several predefined entities must appearnear each other in a document, it is betterto use a character data section,<![CDATA[ content ]]>

I For example, it is better to write:<![CDATA[Start >>> HERE <<<]]>

instead of writing:Start &gt; &gt; &gt; HERE &lt; &lt; &lt;

18 / 66

Page 44: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Character Data Section

I CDATA is text that will not be parsed by anXML parser.

I If several predefined entities must appearnear each other in a document, it is betterto use a character data section,<![CDATA[ content ]]>

I For example, it is better to write:<![CDATA[Start >>> HERE <<<]]>

instead of writing:Start &gt; &gt; &gt; HERE &lt; &lt; &lt;

18 / 66

Page 45: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Character Data Section

I CDATA is text that will not be parsed by anXML parser.

I If several predefined entities must appearnear each other in a document, it is betterto use a character data section,<![CDATA[ content ]]>

I For example, it is better to write:<![CDATA[Start >>> HERE <<<]]>

instead of writing:Start &gt; &gt; &gt; HERE &lt; &lt; &lt;

18 / 66

Page 46: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Question 1

19 / 66

Page 47: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Section

XML

Document Type Definition, DTD

XML Namespaces

XML Schema

XML Processors

Other XML Standards

20 / 66

Page 48: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What is a DTD?I A Document Type Definition (DTD) defines

the structure of an XML document.I The DTD defines which elements are

allowed, their order, their attributes andtheir content.

I An XML document that conforms to a DTDis called valid.

I It is not required to use a DTD. An XMLdocument without a reference to a DTD isnot valid, but can still be a legal XMLdocument as long as it is well-formed(obeys the general syntax rules).

21 / 66

Page 49: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What is a DTD?I A Document Type Definition (DTD) defines

the structure of an XML document.I The DTD defines which elements are

allowed, their order, their attributes andtheir content.

I An XML document that conforms to a DTDis called valid.

I It is not required to use a DTD. An XMLdocument without a reference to a DTD isnot valid, but can still be a legal XMLdocument as long as it is well-formed(obeys the general syntax rules).

21 / 66

Page 50: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What is a DTD?I A Document Type Definition (DTD) defines

the structure of an XML document.I The DTD defines which elements are

allowed, their order, their attributes andtheir content.

I An XML document that conforms to a DTDis called valid.

I It is not required to use a DTD. An XMLdocument without a reference to a DTD isnot valid, but can still be a legal XMLdocument as long as it is well-formed(obeys the general syntax rules).

21 / 66

Page 51: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What is a DTD?I A Document Type Definition (DTD) defines

the structure of an XML document.I The DTD defines which elements are

allowed, their order, their attributes andtheir content.

I An XML document that conforms to a DTDis called valid.

I It is not required to use a DTD. An XMLdocument without a reference to a DTD isnot valid, but can still be a legal XMLdocument as long as it is well-formed(obeys the general syntax rules).

21 / 66

Page 52: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Why Use a DTD?

I With a DTD it is possible to validate thecontent of the XML document, therebyeliminating typos, forgotten tags and othersyntactic mistakes.

I A DTD can be used to enforce correctformat when exchanging data.

I The DTD provides a description of the XMLdocument.

22 / 66

Page 53: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Why Use a DTD?

I With a DTD it is possible to validate thecontent of the XML document, therebyeliminating typos, forgotten tags and othersyntactic mistakes.

I A DTD can be used to enforce correctformat when exchanging data.

I The DTD provides a description of the XMLdocument.

22 / 66

Page 54: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Why Use a DTD?

I With a DTD it is possible to validate thecontent of the XML document, therebyeliminating typos, forgotten tags and othersyntactic mistakes.

I A DTD can be used to enforce correctformat when exchanging data.

I The DTD provides a description of the XMLdocument.

22 / 66

Page 55: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Defining a DTDI The following DTD defines a tag set with the

root element book, which has the nestedelements title, author and isbn.<!ELEMENT book (title,author,isbn)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT isbn (#PCDATA)>

I An XML document must refer to its DTDusing the syntax<!DOCTYPE root-element SYSTEM "filename">

<?xml version="1.0"?><!DOCTYPE book SYSTEM "book.dtd"><book><title>Web Development</title><author>Olle Olsson</author><isbn>0123456789</isbn>

</book>23 / 66

Page 56: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Defining a DTDI The following DTD defines a tag set with the

root element book, which has the nestedelements title, author and isbn.<!ELEMENT book (title,author,isbn)><!ELEMENT title (#PCDATA)><!ELEMENT author (#PCDATA)><!ELEMENT isbn (#PCDATA)>

I An XML document must refer to its DTDusing the syntax<!DOCTYPE root-element SYSTEM "filename">

<?xml version="1.0"?><!DOCTYPE book SYSTEM "book.dtd"><book><title>Web Development</title><author>Olle Olsson</author><isbn>0123456789</isbn>

</book>23 / 66

Page 57: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DTD DefinitionsA DTD can contain the following definitions.ELEMENT An XML element and its content.

ATTLIST An element’s attributes and theircontent.

PCDATA Parsed character data, characterdata is text between start and endtag of an XML element. Parsedcharacter data is interpreted by theXML parser, for example <name> isinterpreted as a XML tag.

CDATA character data, will not be parsed bya parser.

ENTITIES Shortcuts to standard text or specialcharacters.

24 / 66

Page 58: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DTD DefinitionsA DTD can contain the following definitions.ELEMENT An XML element and its content.ATTLIST An element’s attributes and their

content.

PCDATA Parsed character data, characterdata is text between start and endtag of an XML element. Parsedcharacter data is interpreted by theXML parser, for example <name> isinterpreted as a XML tag.

CDATA character data, will not be parsed bya parser.

ENTITIES Shortcuts to standard text or specialcharacters.

24 / 66

Page 59: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DTD DefinitionsA DTD can contain the following definitions.ELEMENT An XML element and its content.ATTLIST An element’s attributes and their

content.PCDATA Parsed character data, character

data is text between start and endtag of an XML element. Parsedcharacter data is interpreted by theXML parser, for example <name> isinterpreted as a XML tag.

CDATA character data, will not be parsed bya parser.

ENTITIES Shortcuts to standard text or specialcharacters.

24 / 66

Page 60: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DTD DefinitionsA DTD can contain the following definitions.ELEMENT An XML element and its content.ATTLIST An element’s attributes and their

content.PCDATA Parsed character data, character

data is text between start and endtag of an XML element. Parsedcharacter data is interpreted by theXML parser, for example <name> isinterpreted as a XML tag.

CDATA character data, will not be parsed bya parser.

ENTITIES Shortcuts to standard text or specialcharacters.

24 / 66

Page 61: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DTD DefinitionsA DTD can contain the following definitions.ELEMENT An XML element and its content.ATTLIST An element’s attributes and their

content.PCDATA Parsed character data, character

data is text between start and endtag of an XML element. Parsedcharacter data is interpreted by theXML parser, for example <name> isinterpreted as a XML tag.

CDATA character data, will not be parsed bya parser.

ENTITIES Shortcuts to standard text or specialcharacters.

24 / 66

Page 62: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DTD DefinitionsA DTD can contain the following definitions.ELEMENT An XML element and its content.ATTLIST An element’s attributes and their

content.PCDATA Parsed character data, character

data is text between start and endtag of an XML element. Parsedcharacter data is interpreted by theXML parser, for example <name> isinterpreted as a XML tag.

CDATA character data, will not be parsed bya parser.

ENTITIES Shortcuts to standard text or specialcharacters.

24 / 66

Page 63: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Element Definition

I An element declaration has one of thefollowing syntaxes<!ELEMENT element-name category>

<!ELEMENT element-name (element-content)>

I Category can be EMPTY, meaning theelement must be empty, or ANY, meaningany content is allowed.

25 / 66

Page 64: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Element Definition

I An element declaration has one of thefollowing syntaxes<!ELEMENT element-name category>

<!ELEMENT element-name (element-content)>

I Category can be EMPTY, meaning theelement must be empty, or ANY, meaningany content is allowed.

25 / 66

Page 65: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Element Content ExamplesI Children elements, must appear in the

specified sequence.<!ELEMENT note (title, author, isbn)>

I One or more occurrences of a child<!ELEMENT books (book+)>

I Zero or more occurrences of a child<!ELEMENT books (book*)>

I Zero or One occurrence of a child<!ELEMENT address (email?)>

I Alternatives<!ELEMENT msg (to,from,(attachment|body))>

26 / 66

Page 66: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Element Content ExamplesI Children elements, must appear in the

specified sequence.<!ELEMENT note (title, author, isbn)>

I One or more occurrences of a child<!ELEMENT books (book+)>

I Zero or more occurrences of a child<!ELEMENT books (book*)>

I Zero or One occurrence of a child<!ELEMENT address (email?)>

I Alternatives<!ELEMENT msg (to,from,(attachment|body))>

26 / 66

Page 67: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Element Content ExamplesI Children elements, must appear in the

specified sequence.<!ELEMENT note (title, author, isbn)>

I One or more occurrences of a child<!ELEMENT books (book+)>

I Zero or more occurrences of a child<!ELEMENT books (book*)>

I Zero or One occurrence of a child<!ELEMENT address (email?)>

I Alternatives<!ELEMENT msg (to,from,(attachment|body))>

26 / 66

Page 68: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Element Content ExamplesI Children elements, must appear in the

specified sequence.<!ELEMENT note (title, author, isbn)>

I One or more occurrences of a child<!ELEMENT books (book+)>

I Zero or more occurrences of a child<!ELEMENT books (book*)>

I Zero or One occurrence of a child<!ELEMENT address (email?)>

I Alternatives<!ELEMENT msg (to,from,(attachment|body))>

26 / 66

Page 69: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Element Content ExamplesI Children elements, must appear in the

specified sequence.<!ELEMENT note (title, author, isbn)>

I One or more occurrences of a child<!ELEMENT books (book+)>

I Zero or more occurrences of a child<!ELEMENT books (book*)>

I Zero or One occurrence of a child<!ELEMENT address (email?)>

I Alternatives<!ELEMENT msg (to,from,(attachment|body))>

26 / 66

Page 70: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attribute Definition

I An attribute definition has the syntax<!ATTLIST element-name attribute-name

attribute-type attribute-value>

I The following example declares an attributeid for the element order. The attribute isrequired and its content is character data.<!ATTLIST order id CDATA #REQUIRED>

I Valid content in an XML document could be<order id="123"/>

27 / 66

Page 71: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attribute Definition

I An attribute definition has the syntax<!ATTLIST element-name attribute-name

attribute-type attribute-value>

I The following example declares an attributeid for the element order. The attribute isrequired and its content is character data.<!ATTLIST order id CDATA #REQUIRED>

I Valid content in an XML document could be<order id="123"/>

27 / 66

Page 72: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attribute Definition

I An attribute definition has the syntax<!ATTLIST element-name attribute-name

attribute-type attribute-value>

I The following example declares an attributeid for the element order. The attribute isrequired and its content is character data.<!ATTLIST order id CDATA #REQUIRED>

I Valid content in an XML document could be<order id="123"/>

27 / 66

Page 73: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attribute Definition ExamplesI Default value

<!ATTLIST order qty CDATA "1">

<order/> <!-- qty = 1 -->

I Enumeration<!ATTLIST risk impact (low|medium|high) "high">

<risk impact="low"/>

I Optional<!ATTLIST person age CDATA #IMPLIED>

<person/><person age="10"/>

28 / 66

Page 74: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attribute Definition ExamplesI Default value

<!ATTLIST order qty CDATA "1">

<order/> <!-- qty = 1 -->

I Enumeration<!ATTLIST risk impact (low|medium|high) "high">

<risk impact="low"/>

I Optional<!ATTLIST person age CDATA #IMPLIED>

<person/><person age="10"/>

28 / 66

Page 75: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attribute Definition ExamplesI Default value

<!ATTLIST order qty CDATA "1">

<order/> <!-- qty = 1 -->

I Enumeration<!ATTLIST risk impact (low|medium|high) "high">

<risk impact="low"/>

I Optional<!ATTLIST person age CDATA #IMPLIED>

<person/><person age="10"/>

28 / 66

Page 76: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Entity

I An entity is an alias for a character, string orresource.

I Entity value is a string:<!ENTITY me "All my contact information">

<author>&me;</author>

I Entity value is a resource:<!ENTITY cright SYSTEM "http://myserver.se/cr.xml">

<condition>&cright;</condition>

The parser is supposed to fetch and insertthe content of the file cr.xml

29 / 66

Page 77: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Entity

I An entity is an alias for a character, string orresource.

I Entity value is a string:<!ENTITY me "All my contact information">

<author>&me;</author>

I Entity value is a resource:<!ENTITY cright SYSTEM "http://myserver.se/cr.xml">

<condition>&cright;</condition>

The parser is supposed to fetch and insertthe content of the file cr.xml

29 / 66

Page 78: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Entity

I An entity is an alias for a character, string orresource.

I Entity value is a string:<!ENTITY me "All my contact information">

<author>&me;</author>

I Entity value is a resource:<!ENTITY cright SYSTEM "http://myserver.se/cr.xml">

<condition>&cright;</condition>

The parser is supposed to fetch and insertthe content of the file cr.xml

29 / 66

Page 79: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

A Sample DTD<!ELEMENT NEWSPAPER (ARTICLE+)><!ELEMENT ARTICLE

(HEADLINE,BYLINE,LEAD,BODY,NOTES)><!ELEMENT HEADLINE (#PCDATA)><!ELEMENT BYLINE (#PCDATA)><!ELEMENT LEAD (#PCDATA)><!ELEMENT BODY (#PCDATA)><!ELEMENT NOTES (#PCDATA)>

<!ATTLIST ARTICLE AUTHOR CDATA #REQUIRED><!ATTLIST ARTICLE EDITOR CDATA #IMPLIED><!ATTLIST ARTICLE DATE CDATA #IMPLIED><!ATTLIST ARTICLE EDITION CDATA #IMPLIED>

<!ENTITY NEWSPAPER "Vervet Logic Times"><!ENTITY PUBLISHER "Vervet Logic Press"><!ENTITY COPYRIGHT

"Copyright 1998 Vervet Logic Press">

Taken from http://www.w3schools.com/dtd/dtd_examples.asp 30 / 66

Page 80: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Section

XML

Document Type Definition, DTD

XML Namespaces

XML Schema

XML Processors

Other XML Standards

31 / 66

Page 81: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Namespaces

I Since XML elements are defined by thedeveloper, there is a risk for name conflicts.

I Therefore, it is necessary to usenamespaces, just like we use packages inJava or namespaces in PHP.

32 / 66

Page 82: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Namespaces

I Since XML elements are defined by thedeveloper, there is a risk for name conflicts.

I Therefore, it is necessary to usenamespaces, just like we use packages inJava or namespaces in PHP.

32 / 66

Page 83: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Defining a Namespace

I A namespace is defined with the xmlnsattribute.<b:book xmlns:b="http:my.domain.se/books/"><b:title>Web Development</b:title><b:author>Olle Olsson</b:author><b:isbn>0123456789</b:isbn>

</b:book>

I When using the xmlns attribute, we alsospecify a prefix, b in the example above.

I All children to the element with the xmlnsattribute, with the defined prefix, areassociated with the same namespace.

33 / 66

Page 84: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Defining a Namespace

I A namespace is defined with the xmlnsattribute.<b:book xmlns:b="http:my.domain.se/books/"><b:title>Web Development</b:title><b:author>Olle Olsson</b:author><b:isbn>0123456789</b:isbn>

</b:book>

I When using the xmlns attribute, we alsospecify a prefix, b in the example above.

I All children to the element with the xmlnsattribute, with the defined prefix, areassociated with the same namespace.

33 / 66

Page 85: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Defining a Namespace

I A namespace is defined with the xmlnsattribute.<b:book xmlns:b="http:my.domain.se/books/"><b:title>Web Development</b:title><b:author>Olle Olsson</b:author><b:isbn>0123456789</b:isbn>

</b:book>

I When using the xmlns attribute, we alsospecify a prefix, b in the example above.

I All children to the element with the xmlnsattribute, with the defined prefix, areassociated with the same namespace.

33 / 66

Page 86: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Default Namespace

I If the prefix is omitted, the definednamespace becomes the defaultnamespace, used for tags without prefix.<book xmlns="http:my.domain.se/books/"><title>Web Development</title><author>Olle Olsson</author><isbn>0123456789</isbn>

</book>

34 / 66

Page 87: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

The Namespace Identifier

I The value of the xmlns attribute shall be aunique identifier.

I A URL is often used, since using theorganization’s domain name is an easy wayto ensure it is globally unique.

I Note that there is no request for a resourceat the specified URL, it is only used as anidentifier.

35 / 66

Page 88: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

The Namespace Identifier

I The value of the xmlns attribute shall be aunique identifier.

I A URL is often used, since using theorganization’s domain name is an easy wayto ensure it is globally unique.

I Note that there is no request for a resourceat the specified URL, it is only used as anidentifier.

35 / 66

Page 89: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

The Namespace Identifier

I The value of the xmlns attribute shall be aunique identifier.

I A URL is often used, since using theorganization’s domain name is an easy wayto ensure it is globally unique.

I Note that there is no request for a resourceat the specified URL, it is only used as anidentifier.

35 / 66

Page 90: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Question 2

36 / 66

Page 91: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Section

XML

Document Type Definition, DTD

XML Namespaces

XML Schema

XML Processors

Other XML Standards

37 / 66

Page 92: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Schema

I An XML Schema has the same purpose asa DTD: To define a tag set.

I XML Schemas are more widely used thanDTDs, since there are importantadvantages:

I XML Schemas are written in XML.I XML Schemas enable specifying data types.I XML Schemas enable specifying

namespaces.I XML Schemas are extensible. A schema can

be reused in other schemas, new data typescan be defined, an xml document can conformto multiple schemas.

38 / 66

Page 93: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Schema

I An XML Schema has the same purpose asa DTD: To define a tag set.

I XML Schemas are more widely used thanDTDs, since there are importantadvantages:

I XML Schemas are written in XML.

I XML Schemas enable specifying data types.I XML Schemas enable specifying

namespaces.I XML Schemas are extensible. A schema can

be reused in other schemas, new data typescan be defined, an xml document can conformto multiple schemas.

38 / 66

Page 94: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Schema

I An XML Schema has the same purpose asa DTD: To define a tag set.

I XML Schemas are more widely used thanDTDs, since there are importantadvantages:

I XML Schemas are written in XML.I XML Schemas enable specifying data types.

I XML Schemas enable specifyingnamespaces.

I XML Schemas are extensible. A schema canbe reused in other schemas, new data typescan be defined, an xml document can conformto multiple schemas.

38 / 66

Page 95: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Schema

I An XML Schema has the same purpose asa DTD: To define a tag set.

I XML Schemas are more widely used thanDTDs, since there are importantadvantages:

I XML Schemas are written in XML.I XML Schemas enable specifying data types.I XML Schemas enable specifying

namespaces.

I XML Schemas are extensible. A schema canbe reused in other schemas, new data typescan be defined, an xml document can conformto multiple schemas.

38 / 66

Page 96: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Schema

I An XML Schema has the same purpose asa DTD: To define a tag set.

I XML Schemas are more widely used thanDTDs, since there are importantadvantages:

I XML Schemas are written in XML.I XML Schemas enable specifying data types.I XML Schemas enable specifying

namespaces.I XML Schemas are extensible. A schema can

be reused in other schemas, new data typescan be defined, an xml document can conformto multiple schemas.

38 / 66

Page 97: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XML Schema

I An XML Schema has the same purpose asa DTD: To define a tag set.

I XML Schemas are more widely used thanDTDs, since there are importantadvantages:

I XML Schemas are written in XML.I XML Schemas enable specifying data types.I XML Schemas enable specifying

namespaces.I XML Schemas are extensible. A schema can

be reused in other schemas, new data typescan be defined, an xml document can conformto multiple schemas.

38 / 66

Page 98: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

A Schema Example<?xml version="1.0"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"targetNamespace="http:my.domain.se/books/"elementFormDefault="qualified">

<xs:element name="book"><xs:complexType>

<xs:sequence><xs:element name="title" type="xs:string"/><xs:element name="author" type="xs:string"/>

</xs:sequence></xs:complexType>

</xs:element>

</xs:schema>

<?xml version="1.0"?><b:bookxmlns:b="http:my.domain.se/books/"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http:my.domain.se/books/ books.xsd"><b:title>Web Development</b:title><b:author>Olle Olsson</b:author>

</b:book>

39 / 66

Page 99: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

The schema elementI The schema element must be the root of a

schema document.I <xsd:schema

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

defines namespace and prefix of the XMLschema namespace.

I targetNamespace="http:my.domain.se/books/"

specifies that elements defined in thisschema belong to the namespacehttp:my.domain.se/books/

I elementFormDefault="qualified"

specifies that whenever an element is usedin a document, it must be qualified with thenamespace declared in targetNamespace

40 / 66

Page 100: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

The schema elementI The schema element must be the root of a

schema document.I <xsd:schema

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

defines namespace and prefix of the XMLschema namespace.

I targetNamespace="http:my.domain.se/books/"

specifies that elements defined in thisschema belong to the namespacehttp:my.domain.se/books/

I elementFormDefault="qualified"

specifies that whenever an element is usedin a document, it must be qualified with thenamespace declared in targetNamespace

40 / 66

Page 101: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

The schema elementI The schema element must be the root of a

schema document.I <xsd:schema

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

defines namespace and prefix of the XMLschema namespace.

I targetNamespace="http:my.domain.se/books/"

specifies that elements defined in thisschema belong to the namespacehttp:my.domain.se/books/

I elementFormDefault="qualified"

specifies that whenever an element is usedin a document, it must be qualified with thenamespace declared in targetNamespace

40 / 66

Page 102: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

The schema elementI The schema element must be the root of a

schema document.I <xsd:schema

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

defines namespace and prefix of the XMLschema namespace.

I targetNamespace="http:my.domain.se/books/"

specifies that elements defined in thisschema belong to the namespacehttp:my.domain.se/books/

I elementFormDefault="qualified"

specifies that whenever an element is usedin a document, it must be qualified with thenamespace declared in targetNamespace

40 / 66

Page 103: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Data TypesI There are many built-in data types, some

common types follow below.I xs:string A string that can contain line

feeds, carriage returns, and tabs.

I xs:token A string from which the XMLprocessor removes line feeds, carriagereturns, tabs, leading and trailing spaces,and multiple spaces.

I xs:date has the form yyyy-mm-dd, andxs:time has the form hh:mm:ss.

I xs:decimal and xs:integer are twoof the numeric data types.

I xs:boolean Can be "true" or "false"

41 / 66

Page 104: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Data TypesI There are many built-in data types, some

common types follow below.I xs:string A string that can contain line

feeds, carriage returns, and tabs.I xs:token A string from which the XML

processor removes line feeds, carriagereturns, tabs, leading and trailing spaces,and multiple spaces.

I xs:date has the form yyyy-mm-dd, andxs:time has the form hh:mm:ss.

I xs:decimal and xs:integer are twoof the numeric data types.

I xs:boolean Can be "true" or "false"

41 / 66

Page 105: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Data TypesI There are many built-in data types, some

common types follow below.I xs:string A string that can contain line

feeds, carriage returns, and tabs.I xs:token A string from which the XML

processor removes line feeds, carriagereturns, tabs, leading and trailing spaces,and multiple spaces.

I xs:date has the form yyyy-mm-dd, andxs:time has the form hh:mm:ss.

I xs:decimal and xs:integer are twoof the numeric data types.

I xs:boolean Can be "true" or "false"

41 / 66

Page 106: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Data TypesI There are many built-in data types, some

common types follow below.I xs:string A string that can contain line

feeds, carriage returns, and tabs.I xs:token A string from which the XML

processor removes line feeds, carriagereturns, tabs, leading and trailing spaces,and multiple spaces.

I xs:date has the form yyyy-mm-dd, andxs:time has the form hh:mm:ss.

I xs:decimal and xs:integer are twoof the numeric data types.

I xs:boolean Can be "true" or "false"

41 / 66

Page 107: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Data TypesI There are many built-in data types, some

common types follow below.I xs:string A string that can contain line

feeds, carriage returns, and tabs.I xs:token A string from which the XML

processor removes line feeds, carriagereturns, tabs, leading and trailing spaces,and multiple spaces.

I xs:date has the form yyyy-mm-dd, andxs:time has the form hh:mm:ss.

I xs:decimal and xs:integer are twoof the numeric data types.

I xs:boolean Can be "true" or "false"41 / 66

Page 108: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Data TypesI There are many built-in data types, some

common types follow below.I xs:string A string that can contain line

feeds, carriage returns, and tabs.I xs:token A string from which the XML

processor removes line feeds, carriagereturns, tabs, leading and trailing spaces,and multiple spaces.

I xs:date has the form yyyy-mm-dd, andxs:time has the form hh:mm:ss.

I xs:decimal and xs:integer are twoof the numeric data types.

I xs:boolean Can be "true" or "false"41 / 66

Page 109: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attributes

I Attributes can be defined as below.<xs:attribute name="xxx" type="yyy" default="zzz"/>

<xs:attribute name="xxx" type="yyy" use="required"/>

I Here is an example:<xs:attribute name="qty"

type="xs:integer"default="0"/>

42 / 66

Page 110: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Attributes

I Attributes can be defined as below.<xs:attribute name="xxx" type="yyy" default="zzz"/>

<xs:attribute name="xxx" type="yyy" use="required"/>

I Here is an example:<xs:attribute name="qty"

type="xs:integer"default="0"/>

42 / 66

Page 111: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Simple Elements

I A simple element contains only text, notother elements or attributes.

I Like attributes, elements can have defaultor fixed values.

<xs:element name="color" type="xs:string" default="blue"/><xs:element name="orderDate" type="xs:date"/>

<color>green</color><orderDate>2014-09-23</orderDate>

43 / 66

Page 112: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Simple Elements

I A simple element contains only text, notother elements or attributes.

I Like attributes, elements can have defaultor fixed values.

<xs:element name="color" type="xs:string" default="blue"/><xs:element name="orderDate" type="xs:date"/>

<color>green</color><orderDate>2014-09-23</orderDate>

43 / 66

Page 113: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Simple Elements

I A simple element contains only text, notother elements or attributes.

I Like attributes, elements can have defaultor fixed values.

<xs:element name="color" type="xs:string" default="blue"/><xs:element name="orderDate" type="xs:date"/>

<color>green</color><orderDate>2014-09-23</orderDate>

43 / 66

Page 114: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Restrictions on Element ValuesI There are many ways to limit allowed

values. Here are two examples.I qty must be between 1 and 100.

<xs:element name="qty"><xs:simpleType><xs:restriction base="xs:integer">

<xs:minInclusive value="1"/><xs:maxInclusive value="100"/>

</xs:restriction></xs:simpleType>

</xs:element>

I color must be red or blue.<xs:element name="color" type="colorType"/>

<xs:simpleType name="colorType"><xs:restriction base="xs:string"><xs:enumeration value="red"/><xs:enumeration value="blue"/>

</xs:restriction></xs:simpleType>

44 / 66

Page 115: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Restrictions on Element ValuesI There are many ways to limit allowed

values. Here are two examples.I qty must be between 1 and 100.

<xs:element name="qty"><xs:simpleType><xs:restriction base="xs:integer">

<xs:minInclusive value="1"/><xs:maxInclusive value="100"/>

</xs:restriction></xs:simpleType>

</xs:element>

I color must be red or blue.<xs:element name="color" type="colorType"/>

<xs:simpleType name="colorType"><xs:restriction base="xs:string"><xs:enumeration value="red"/><xs:enumeration value="blue"/>

</xs:restriction></xs:simpleType>

44 / 66

Page 116: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Elements

I A complex element contains other elementsand/or attributes.

I There are four kinds of complex elements.

I empty elementsI elements that contain only other elementsI elements that contain only textI elements that contain both other elements and

text

I All types can also have attributes.

45 / 66

Page 117: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Elements

I A complex element contains other elementsand/or attributes.

I There are four kinds of complex elements.I empty elements

I elements that contain only other elementsI elements that contain only textI elements that contain both other elements and

text

I All types can also have attributes.

45 / 66

Page 118: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Elements

I A complex element contains other elementsand/or attributes.

I There are four kinds of complex elements.I empty elementsI elements that contain only other elements

I elements that contain only textI elements that contain both other elements and

text

I All types can also have attributes.

45 / 66

Page 119: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Elements

I A complex element contains other elementsand/or attributes.

I There are four kinds of complex elements.I empty elementsI elements that contain only other elementsI elements that contain only text

I elements that contain both other elements andtext

I All types can also have attributes.

45 / 66

Page 120: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Elements

I A complex element contains other elementsand/or attributes.

I There are four kinds of complex elements.I empty elementsI elements that contain only other elementsI elements that contain only textI elements that contain both other elements and

text

I All types can also have attributes.

45 / 66

Page 121: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Elements

I A complex element contains other elementsand/or attributes.

I There are four kinds of complex elements.I empty elementsI elements that contain only other elementsI elements that contain only textI elements that contain both other elements and

text

I All types can also have attributes.

45 / 66

Page 122: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Elements

I A complex element contains other elementsand/or attributes.

I There are four kinds of complex elements.I empty elementsI elements that contain only other elementsI elements that contain only textI elements that contain both other elements and

text

I All types can also have attributes.

45 / 66

Page 123: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Types

I A complex element has a complex type,which must be defined.

I The complex type can be defined togetherwith the complex element, in which case itcan be used only for that element.<xs:element name="name"><xs:complexType><xs:sequence>

<xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/>

</xs:sequence></xs:complexType>

</xs:element>

46 / 66

Page 124: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Types

I A complex element has a complex type,which must be defined.

I The complex type can be defined togetherwith the complex element, in which case itcan be used only for that element.<xs:element name="name"><xs:complexType><xs:sequence>

<xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/>

</xs:sequence></xs:complexType>

</xs:element>

46 / 66

Page 125: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Complex Types (Cont’d)

I The complex type can be definedseparately, in which case it can be used forany element.<xs:element name="employee" type="name"/><xs:element name="person" type="name"/>

xs:complexType name="name"><xs:sequence><xs:element name="firstname" type="xs:string"/><xs:element name="lastname" type="xs:string"/>

</xs:sequence></xs:complexType>

47 / 66

Page 126: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Elements

I The previous slide was an example of anelement with nested elements.

I The xs:sequence tag means that theelements firstname and lastnamemust appear in exactly that order.

I An XML document could contain a personelement like this:<person><firstname>Sara</firstname><lastname>Olsson</lastname>

</person>

48 / 66

Page 127: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Elements

I The previous slide was an example of anelement with nested elements.

I The xs:sequence tag means that theelements firstname and lastnamemust appear in exactly that order.

I An XML document could contain a personelement like this:<person><firstname>Sara</firstname><lastname>Olsson</lastname>

</person>

48 / 66

Page 128: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Nested Elements

I The previous slide was an example of anelement with nested elements.

I The xs:sequence tag means that theelements firstname and lastnamemust appear in exactly that order.

I An XML document could contain a personelement like this:<person><firstname>Sara</firstname><lastname>Olsson</lastname>

</person>

48 / 66

Page 129: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Empty ElementsI An empty element has an empty complex

type.<xs:element name="product"><xs:complexType></xs:complexType>

</xs:element>

<product/>

I An empty element can have an attribute.<xs:element name="product"><xs:complexType>

<xs:attribute name="id"type="xs:string"/>

</xs:complexType></xs:element>

<product id="abc123"/>

49 / 66

Page 130: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Empty ElementsI An empty element has an empty complex

type.<xs:element name="product"><xs:complexType></xs:complexType>

</xs:element>

<product/>

I An empty element can have an attribute.<xs:element name="product"><xs:complexType>

<xs:attribute name="id"type="xs:string"/>

</xs:complexType></xs:element>

<product id="abc123"/>

49 / 66

Page 131: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Text-Only ElementI To declare an element that may contain

only text we need to declare a complex typewith simple content.

I Simple content means text and attributes.

I The following schema fragment declares anelement productId that may onlycontain an integer.<xs:element name="productId">

<xs:complexType><xs:simpleContent><xs:extension base="xs:integer"></xs:extension>

</xs:simpleContent></xs:complexType>

</xs:element>

<productId>123</productId>

50 / 66

Page 132: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Text-Only ElementI To declare an element that may contain

only text we need to declare a complex typewith simple content.

I Simple content means text and attributes.I The following schema fragment declares an

element productId that may onlycontain an integer.<xs:element name="productId"><xs:complexType>

<xs:simpleContent><xs:extension base="xs:integer"></xs:extension>

</xs:simpleContent></xs:complexType>

</xs:element>

<productId>123</productId> 50 / 66

Page 133: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Text-Only ElementI To declare an element that may contain

only text we need to declare a complex typewith simple content.

I Simple content means text and attributes.I The following schema fragment declares an

element productId that may onlycontain an integer.<xs:element name="productId"><xs:complexType>

<xs:simpleContent><xs:extension base="xs:integer"></xs:extension>

</xs:simpleContent></xs:complexType>

</xs:element>

<productId>123</productId> 50 / 66

Page 134: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Question 3

51 / 66

Page 135: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Section

XML

Document Type Definition, DTD

XML Namespaces

XML Schema

XML Processors

Other XML Standards

52 / 66

Page 136: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What Does an XML Processor Do?

I Check the syntax of a document forwell-formedness.

I Replace all references to entities by theirdefinitions.

I Copy default values (from DTDs orschemas) into the document.

I If a DTD or schema is specified and theprocessor includes a validating parser, thestructure of the document is validated.

53 / 66

Page 137: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What Does an XML Processor Do?

I Check the syntax of a document forwell-formedness.

I Replace all references to entities by theirdefinitions.

I Copy default values (from DTDs orschemas) into the document.

I If a DTD or schema is specified and theprocessor includes a validating parser, thestructure of the document is validated.

53 / 66

Page 138: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What Does an XML Processor Do?

I Check the syntax of a document forwell-formedness.

I Replace all references to entities by theirdefinitions.

I Copy default values (from DTDs orschemas) into the document.

I If a DTD or schema is specified and theprocessor includes a validating parser, thestructure of the document is validated.

53 / 66

Page 139: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

What Does an XML Processor Do?

I Check the syntax of a document forwell-formedness.

I Replace all references to entities by theirdefinitions.

I Copy default values (from DTDs orschemas) into the document.

I If a DTD or schema is specified and theprocessor includes a validating parser, thestructure of the document is validated.

53 / 66

Page 140: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DOM and SAX

I There are two different standards for XMLparsers.

I Document Object Model (DOM) builds atree structure in memory containing theXML document data. The application cansearch and update the tree.

I Simple API for XML (SAX) generatesevents to applications when XMLcomponents (tags, text etc.) areencountered. The application registerslisteners for those events.

54 / 66

Page 141: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DOM and SAX

I There are two different standards for XMLparsers.

I Document Object Model (DOM) builds atree structure in memory containing theXML document data. The application cansearch and update the tree.

I Simple API for XML (SAX) generatesevents to applications when XMLcomponents (tags, text etc.) areencountered. The application registerslisteners for those events.

54 / 66

Page 142: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

DOM and SAX

I There are two different standards for XMLparsers.

I Document Object Model (DOM) builds atree structure in memory containing theXML document data. The application cansearch and update the tree.

I Simple API for XML (SAX) generatesevents to applications when XMLcomponents (tags, text etc.) areencountered. The application registerslisteners for those events.

54 / 66

Page 143: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Advantages of DOM

I Good if any part of the document must beaccessed more than once.

I Updating the document is facilitated byhaving a representation of the wholedocument in memory.

I Any part of the document can be accessed.I Processing an invalid document is avoided

since the whole document is parsed beforeany processing takes place,

55 / 66

Page 144: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Advantages of DOM

I Good if any part of the document must beaccessed more than once.

I Updating the document is facilitated byhaving a representation of the wholedocument in memory.

I Any part of the document can be accessed.

I Processing an invalid document is avoidedsince the whole document is parsed beforeany processing takes place,

55 / 66

Page 145: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Advantages of DOM

I Good if any part of the document must beaccessed more than once.

I Updating the document is facilitated byhaving a representation of the wholedocument in memory.

I Any part of the document can be accessed.I Processing an invalid document is avoided

since the whole document is parsed beforeany processing takes place,

55 / 66

Page 146: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Advantages of DOM

I Good if any part of the document must beaccessed more than once.

I Updating the document is facilitated byhaving a representation of the wholedocument in memory.

I Any part of the document can be accessed.I Processing an invalid document is avoided

since the whole document is parsed beforeany processing takes place,

55 / 66

Page 147: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Disadvantages of DOM

I Large documents require a lot of memory.I DOM is slower than SAX.

I Most DOM processors uses SAX to buildthe in-memory tree.

56 / 66

Page 148: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Disadvantages of DOM

I Large documents require a lot of memory.I DOM is slower than SAX.I Most DOM processors uses SAX to build

the in-memory tree.

56 / 66

Page 149: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Disadvantages of DOM

I Large documents require a lot of memory.I DOM is slower than SAX.I Most DOM processors uses SAX to build

the in-memory tree.

56 / 66

Page 150: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Advantages of SAX

I Less memory consumption than DOM.I Faster than DOM.

57 / 66

Page 151: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Advantages of SAX

I Less memory consumption than DOM.I Faster than DOM.

57 / 66

Page 152: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Disadvantages of SAX

I Each node in the document is handledonce, there is no way to reiterate.

I No random access, nodes can only be readsequentially.

I It is not possible to update the document.I There is no formal specification for SAX.

58 / 66

Page 153: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Disadvantages of SAX

I Each node in the document is handledonce, there is no way to reiterate.

I No random access, nodes can only be readsequentially.

I It is not possible to update the document.

I There is no formal specification for SAX.

58 / 66

Page 154: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Disadvantages of SAX

I Each node in the document is handledonce, there is no way to reiterate.

I No random access, nodes can only be readsequentially.

I It is not possible to update the document.I There is no formal specification for SAX.

58 / 66

Page 155: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Disadvantages of SAX

I Each node in the document is handledonce, there is no way to reiterate.

I No random access, nodes can only be readsequentially.

I It is not possible to update the document.I There is no formal specification for SAX.

58 / 66

Page 156: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Section

XML

Document Type Definition, DTD

XML Namespaces

XML Schema

XML Processors

Other XML Standards

59 / 66

Page 157: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Other XML Standards

I So far we have seen DTD, Schema andDOM (and SAX, which is not a standard).

I There are many more useful standards forhandling XML documents.

I Here follows a very brief overview of someof them.

60 / 66

Page 158: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Other XML Standards

I So far we have seen DTD, Schema andDOM (and SAX, which is not a standard).

I There are many more useful standards forhandling XML documents.

I Here follows a very brief overview of someof them.

60 / 66

Page 159: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

Other XML Standards

I So far we have seen DTD, Schema andDOM (and SAX, which is not a standard).

I There are many more useful standards forhandling XML documents.

I Here follows a very brief overview of someof them.

60 / 66

Page 160: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XPath

I XPath is a language for finding informationin an XML document.

I An XPath expression has the same purposeas a CSS selector for an HTML document,though they do not have the same syntax.

I Is based on path expressions.I Contains functions for comparing and

manipulating values in an XML document.

61 / 66

Page 161: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XPath

I XPath is a language for finding informationin an XML document.

I An XPath expression has the same purposeas a CSS selector for an HTML document,though they do not have the same syntax.

I Is based on path expressions.

I Contains functions for comparing andmanipulating values in an XML document.

61 / 66

Page 162: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XPath

I XPath is a language for finding informationin an XML document.

I An XPath expression has the same purposeas a CSS selector for an HTML document,though they do not have the same syntax.

I Is based on path expressions.I Contains functions for comparing and

manipulating values in an XML document.

61 / 66

Page 163: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XPath

I XPath is a language for finding informationin an XML document.

I An XPath expression has the same purposeas a CSS selector for an HTML document,though they do not have the same syntax.

I Is based on path expressions.I Contains functions for comparing and

manipulating values in an XML document.

61 / 66

Page 164: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XPath Example

I Node means any item in the document,element, attribute, text, etc

I Select all item nodes that are children ofthe first order node that is a child of aorders node./orders/order[1]/item

I Select the text from cost nodes:/orders/order/cost[text()]

62 / 66

Page 165: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XPath Example

I Node means any item in the document,element, attribute, text, etc

I Select all item nodes that are children ofthe first order node that is a child of aorders node./orders/order[1]/item

I Select the text from cost nodes:/orders/order/cost[text()]

62 / 66

Page 166: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XPath Example

I Node means any item in the document,element, attribute, text, etc

I Select all item nodes that are children ofthe first order node that is a child of aorders node./orders/order[1]/item

I Select the text from cost nodes:/orders/order/cost[text()]

62 / 66

Page 167: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

eXtensible Stylesheet LanguageTransformations, XSLT

I XSLT is a language for transforming anXML document into another XMLdocument, for example into a XHTMLdocument.

I An XSL style sheet consists of rules thatare called templates.

I A template specifies what to output fornodes in the document that matches thetemplate’s selector.

I Uses XPath to select nodes in XMLdocuments.

63 / 66

Page 168: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

eXtensible Stylesheet LanguageTransformations, XSLT

I XSLT is a language for transforming anXML document into another XMLdocument, for example into a XHTMLdocument.

I An XSL style sheet consists of rules thatare called templates.

I A template specifies what to output fornodes in the document that matches thetemplate’s selector.

I Uses XPath to select nodes in XMLdocuments.

63 / 66

Page 169: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

eXtensible Stylesheet LanguageTransformations, XSLT

I XSLT is a language for transforming anXML document into another XMLdocument, for example into a XHTMLdocument.

I An XSL style sheet consists of rules thatare called templates.

I A template specifies what to output fornodes in the document that matches thetemplate’s selector.

I Uses XPath to select nodes in XMLdocuments.

63 / 66

Page 170: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

eXtensible Stylesheet LanguageTransformations, XSLT

I XSLT is a language for transforming anXML document into another XMLdocument, for example into a XHTMLdocument.

I An XSL style sheet consists of rules thatare called templates.

I A template specifies what to output fornodes in the document that matches thetemplate’s selector.

I Uses XPath to select nodes in XMLdocuments.

63 / 66

Page 171: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XSLT ExampleBuild a XHTML document to display the contentof a XML document describing a musiccollection.<xsl:template match="/"><html>

<body><h1>My Music Collection</h1><table><tr><th>Title</th><th>Artist</th>

</tr><xsl:for-each select="catalog/track"><xsl:sort select="artist"/><tr>

<td><xsl:value-of select="title"/></td><td><xsl:value-of select="artist"/></td>

</tr></xsl:for-each>

</table></body>

</html></xsl:template> 64 / 66

Page 172: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XQuery

I XQuery is a query language for XML files.I Used to extract elements and attributes

from XML documents, like SQL selectstatements for relational databases.

I Uses XPath to find nodes.I Extract all order elements under the orders

element that have a cost element with avalue that is less than 30:doc("orders.xml")/orders/order[cost<30]

65 / 66

Page 173: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XQuery

I XQuery is a query language for XML files.I Used to extract elements and attributes

from XML documents, like SQL selectstatements for relational databases.

I Uses XPath to find nodes.

I Extract all order elements under the orderselement that have a cost element with avalue that is less than 30:doc("orders.xml")/orders/order[cost<30]

65 / 66

Page 174: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XQuery

I XQuery is a query language for XML files.I Used to extract elements and attributes

from XML documents, like SQL selectstatements for relational databases.

I Uses XPath to find nodes.I Extract all order elements under the orders

element that have a cost element with avalue that is less than 30:doc("orders.xml")/orders/order[cost<30]

65 / 66

Page 175: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

XQuery

I XQuery is a query language for XML files.I Used to extract elements and attributes

from XML documents, like SQL selectstatements for relational databases.

I Uses XPath to find nodes.I Extract all order elements under the orders

element that have a cost element with avalue that is less than 30:doc("orders.xml")/orders/order[cost<30]

65 / 66

Page 176: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

And Many More...

I Extensible Stylesheet Language FormattingObjects, XSL-FO is used to organizeformatting and layout of a page. You canthink of XSL-FO and XPath as CSSproperty-value pairs and CSS selectors.

I XLink is used to define links within andbetween XML documents.

I XPointer is used to define identifiers forfragments of XML documents. Comparewith URLs that can be used to address anentire XML document.

66 / 66

Page 177: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

And Many More...

I Extensible Stylesheet Language FormattingObjects, XSL-FO is used to organizeformatting and layout of a page. You canthink of XSL-FO and XPath as CSSproperty-value pairs and CSS selectors.

I XLink is used to define links within andbetween XML documents.

I XPointer is used to define identifiers forfragments of XML documents. Comparewith URLs that can be used to address anentire XML document.

66 / 66

Page 178: Introduction to XML - KTH to XML XML Document Type Definition, DTD XML Namespaces XML Schema XML Processors Other XML Standards Section XML …

Introduction to XML

XML

Document TypeDefinition, DTD

XML Namespaces

XML Schema

XML Processors

Other XMLStandards

And Many More...

I Extensible Stylesheet Language FormattingObjects, XSL-FO is used to organizeformatting and layout of a page. You canthink of XSL-FO and XPath as CSSproperty-value pairs and CSS selectors.

I XLink is used to define links within andbetween XML documents.

I XPointer is used to define identifiers forfragments of XML documents. Comparewith URLs that can be used to address anentire XML document.

66 / 66