0% found this document useful (0 votes)
0 views

XML100-1.0.0-introduction-to-xml-xsl-and-xml-schema_sample

This document serves as an introduction to XML, XSL, and XML Schema, providing fundamental concepts, benefits, and practical applications of XML. It includes lessons on XML basics, DTDs, XML Schema, and XSLT, along with hands-on exercises for better understanding. The author, Nat Dunn, emphasizes XML's role in data management and sharing across various applications and formats.

Uploaded by

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

XML100-1.0.0-introduction-to-xml-xsl-and-xml-schema_sample

This document serves as an introduction to XML, XSL, and XML Schema, providing fundamental concepts, benefits, and practical applications of XML. It includes lessons on XML basics, DTDs, XML Schema, and XSLT, along with hands-on exercises for better understanding. The author, Nat Dunn, emphasizes XML's role in data management and sharing across various applications and formats.

Uploaded by

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

Introduction to XML,

XSL, and XML


Schema

with examples and


hands-on exercises

WEBUCATOR
Copyright © 2023 by Webucator. All rights reserved.

No part of this manual may be reproduced or used in any manner without written permission of the
copyright owner.

Version: 1.0.0

The Author

Nat Dunn

Nat Dunn is the founder of Webucator (www.webucator.com), a company that has provided training
for tens of thousands of students from thousands of organizations. Nat started the company in 2003
to combine his passion for technical training with his business expertise, and to help companies benefit
from both. His previous experience was in sales, business and technical training, and management. Nat
has an MBA from Harvard Business School and a BA in International Relations from Pomona College.

Follow Nat on Twitter at @natdunn and Webucator at @webucator.

Class Files

Download the class files used in this manual at


https://static.webucator.com/media/public/materials/classfiles/XML100-1.0.0-introduction-to-xml-xsl-and-xml-schema.zip.

Errata

Corrections to errors in the manual can be found at https://www.webucator.com/books/errata/.


Table of Contents
LESSON 1. XML Basics...................................................................................................................1
What is XML?............................................................................................................................1
XML Benefits............................................................................................................................2
XML in Practice.........................................................................................................................4
XML Documents.......................................................................................................................5
Creating a Simple XML File.....................................................................................................10
Exercise 1: Editing an XML File.....................................................................................12
Exercise 2: Recognizing XML........................................................................................18
LESSON 2. DTDs..........................................................................................................................21
Well-formed vs. Valid.............................................................................................................21
The Purpose of DTDs..............................................................................................................22
Creating DTDs.........................................................................................................................22
Validating an XML Document with a DTD...............................................................................26
Exercise 3: Writing a DTD............................................................................................28
LESSON 3. XML Schema Basics....................................................................................................31
The Purpose of XML Schema..................................................................................................31
The Power of XML Schema.....................................................................................................32
A First Look.............................................................................................................................32
Validating an XML Instance Document...................................................................................35
LESSON 4. Simple-Type Elements................................................................................................37
Overview................................................................................................................................37
Built-in Simple Types..............................................................................................................38
Exercise 4: Building a Simple Schema..........................................................................42
User-derived Simple Types.....................................................................................................44
Exercise 5: Restricting Element Content.......................................................................52
Specifying Element Type Locally.............................................................................................54
Nonatomic Types....................................................................................................................55
Exercise 6: Adding Nonatomic Types...........................................................................59
Declaring Global Simple-Type Elements.................................................................................61
Exercise 7: Converting Simple-Type Element Declarations from Local to Global...........64
Default Values.........................................................................................................................67
Fixed Values............................................................................................................................68
Nil Values................................................................................................................................70

Table of Contents | i
LESSON 5. XSLT Basics................................................................................................................73
eXtensible Stylesheet Language.............................................................................................73
The Transformation Process...................................................................................................74
An XSLT Stylesheet..................................................................................................................75
Output Types..........................................................................................................................85
Elements and Attributes.........................................................................................................89

ii | Table of Contents
LESSON 1
XML Basics
EVALUATION COPY: Not to be used in class.

Topics Covered

What XML is.

Benefits of XML.

XML syntax rules.

Introduction
In this lesson, you will learn what XML is, the benefits of XML, the XML syntax rules, and to create
a simple XML file.

EVALUATION COPY: Not to be used in class.

1.1. What is XML?


XML (eXtensible Markup Language) is a meta-language; that is, it is a language in which other languages
are created. In XML, data is “marked up” with tags, similar to HTML tags. In fact, the latest version
of HTML, called XHTML, is an XML-based language, which means that XHTML follows the syntax
rules of XML.

XML is used to store data or information; this data might be intended to be read by people or by
machines. It can be highly structured data such as data typically stored in databases or spreadsheets, or
loosely structured data, such as data stored in letters or manuals.

LESSON 1: XML Basics | 1


EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.

1.2. XML Benefits


Initially XML received a lot of excitement, which has now died down some. This isn’t because XML
is not as useful, but rather because it doesn’t provide the Wow! factor that other technologies, such as
HTML do. When you write an HTML document, you see a nicely formatted page in a browser -
instant gratification. When you write an XML document, you see an XML document - not so exciting.
However, with a little more effort, you can make that XML document sing!

This section discusses some of the major benefits of XML.

1.2.1. XML Holds Data, Nothing More


XML does not really do much of anything. Rather, developers can create XML-based languages that
store data in a structure way. Applications can then use this data to do any number of things.

1.2.2. XML Separates Structure from Formatting


One of the difficulties with HTML documents, word processor documents, spreadsheets, and other
forms of documents is that they mix structure with formatting. This makes it difficult to manage
content and design, because the two are intermingled.

As an example, in HTML, there is a <u> tag used for underlining text. Very often, it is used for emphasis,
but it also might be used to mark a book title. It would be very difficult to write an application that
searched through such a document for book titles.

In XML, the book titles could be placed in <book_title> tags and the emphasized text could be place
in <em> tags. The XML document does not specify how the content of either tag should be displayed.
Rather, the formatting is left up to an external stylesheet. Even though the book titles and emphasized
text might appear the same, it would be relatively straight forward to write an application that finds
all the book titles. It would simply look for text in <book_title> tags. It also becomes much easier
to reformat a document; for example, to change all emphasized text to be italicized rather than
underlined, but leave book titles underlined.

2 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
1.2.3. XML Promotes Data Sharing
Very often, applications that hold data in different structures must share data with one another. It can
be very difficult for a developer to map the different data structures to each other. XML can serve as a
go between. Each application’s data structure is mapped to an agreed-upon XML structure. Then all
the applications share data in this XML format. Each application only has to know two structures, its
own and the XML structure, to be able to share data with many other applications.

1.2.4. XML is Human-Readable


XML documents are (or can be) read by people. Perhaps this doesn’t sound so exciting, but compare
it to data stored in a database. It is not easy to browse through a database and read different segments
of it as you would a text file. Take a look at the XML document below.

Demo 1.1: XMLBasics/Demos/Paul.xml


1. <?xml version="1.0"?>
2. <person>
3. <name>
4. <firstname>Paul</firstname>
5. <lastname>McCartney</lastname>
6. </name>
7. <job>Singer</job>
8. <gender>Male</gender>
9. </person>

Code Explanation

It is not hard to tell from looking at this that the XML is describing a person named Paul McCartney,
who is a singer and is male.

Do people read XML documents? Programmers do (hey, we’re people too!). And it is easier for us if
the documents we work with are easy to read.

1.2.5. XML is Free


XML doesn’t cost anything to use. It can be written with a simple text editor or one of the many freely
available XML authoring tools, such as XML Notepad. In addition, many web development tools,
such as Dreamweaver and Visual Studio .NET have built-in XML support. There are also many free
XML parsers, such as Microsoft’s MSXML (downloadable from microsoft.com) and Xerces
(downloadable at apache.org).

LESSON 1: XML Basics | 3


EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.

1.3. XML in Practice

1.3.1. Content Management


Almost all of the leading content management systems use XML in one way or another. A typical use
would be to store a company’s marketing content in one or more XML documents. These XML
documents could then be transformed for output on the Web, as Word documents, as PowerPoint
slides, in plain text, and even in audio format. The content can also easily be shared with partners who
can then output the content in their own formats.

Storing the content in XML makes it much easier to manage content for two reasons.

1. Content changes, additions, and deletions are made in a central location and the changes will
cascade out to all formats of presentation. There is no need to be concerned about keeping
the Word documents in sync with the website, because the content itself is managed in one
place and then transformed for each output medium.
2. Formatting changes are made in a central location. To illustrate, suppose a company had
many marketing web pages, all of which were produced from XML content being transformed
to HTML. The format for all of these pages could be controlled from a single XSLT and a
sitewide formatting change could be made modifying that XSLT.

1.3.2. Web Services


XML Web services are small applications or pieces of applications that are made accessible on the
Internet using open standards based on XML. Web services generally consist of three components:1

SOAP - an XML-based protocol used to transfer Web services over the Internet.
WSDL (Web Services Description Language) - an XML-based language for describing a Web
service and how to call it.
Universal Discovery Description and Integration (UDDI) - the yellow pages of Web services.
UDDI directory entries are XML documents that describe the Web services a group offers.
This is how people find available Web services.

1. For more information on Web Services see https://en.wikipedia.org/wiki/Web_service.

4 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
1.3.3. RDF / RSS Feeds
RDF (Resource Description Framework) is a framework for writing XML-based languages to describe
information on the Web (e.g., web pages). RSS (RDF Site Summary) is an implementation of this
framework; it is a language that adheres to RDF and is used to describe web content. Website publishers
can use RSS to make content available as a “feed”, so that web users can access some of their content
without actually visiting their site. Often, RSS is used to provide summaries with links to the company’s
website for additional information.2

EVALUATION COPY: Not to be used in class.

1.4. XML Documents


An XML document is made up of the following parts.

An optional prolog.
A document element, usually containing nested elements.
Optional comments or processing instructions.

1.4.1. The Prolog


The prolog of an XML document can contain the following items.

An XML declaration
Processing instructions
Comments
A Document Type Declaration

The XML Declaration


The XML declaration, if it appears at all, must appear on the very first line of the document with no
preceding whitespace. It looks like this:

2. For more information on RSS Feeds, see https://www.mnot.net/rss/tutorial/.

LESSON 1: XML Basics | 5


EVALUATION COPY: Not to be used in class.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

This declares that the document is an XML document. The version attribute is required, but the
encoding and standalone attributes are not. If the XML document uses any markup declarations
that set defaults for attributes or declare entities then standalone must be set to no.

Processing Instructions
Processing instructions are used to pass parameters to an application. These parameters tell the
application how to process the XML document. For example, the following processing instruction tells
the application that it should transform the XML document using the XSL stylesheet beatles.xsl.

<?xml-stylesheet href="beatles.xsl" type="text/xsl"?>

As shown above, processing instructions begin with and <? end with ?>.

Comments

Comments can appear throughout an XML document. Like in HTML, they begin with <!-- and end
with -->.

<!--This is a comment-->

A Document Type Declaration

The Document Type Declaration (or DOCTYPE Declaration) has three roles.

1. It specifies the name of the document element.


2. It may point to an external Document Type Definition (DTD).
3. It may contain an internal DTD.

The DOCTYPE Declaration shown below simply states that the document element of the XML document
is beatles.

<!DOCTYPE beatles>

6 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
If a DOCTYPE Declaration points to an external DTD, it must either specify that the DTD is on the
same system as the XML document itself or that it is in some public location. To do so, it uses the
keywords SYSTEM and PUBLIC. It then points to the location of the DTD using a relative Uniform
Resource Indicator (URI) or an absolute URI. Here are a couple of examples.

<!--DTD is on the same system as the XML document-->


<!DOCTYPE beatles SYSTEM "dtds/beatles.dtd">

<!--DTD is publicly available-->


<!DOCTYPE beatles PUBLIC "-//Webucator//DTD Beatles 1.0//EN"
"http://www.webucator.com/beatles/DTD/beatles.dtd">

As shown in the second declaration above, public identifiers are divided into three parts:

1. An organization (e.g., Webucator)


2. A name for the DTD (e.g., Beatles 1.0)
3. A language (e.g., EN for English)

1.4.2. Elements
Every XML document must have at least one element, called the document element. The document
element usually contains other elements, which contain other elements, and so on. Elements are denoted
with tags. Let’s look again at the Paul.xml.

Demo 1.2: XMLBasics/Demos/Paul.xml


1. <?xml version="1.0"?>
2. <person>
3. <name>
4. <firstname>Paul</firstname>
5. <lastname>McCartney</lastname>
6. </name>
7. <job>Singer</job>
8. <gender>Male</gender>
9. </person>

Code Explanation

The document element is person. It contains three elements: name, job and gender. Further, the
name element contains two elements of its own: firstname and lastname. As you can see, XML

LESSON 1: XML Basics | 7


EVALUATION COPY: Not to be used in class.
elements are denoted with tags, just as in HTML. Elements that are nested within another element are
said to be children of that element.

Empty Elements
Not all elements contain other elements or text. For example, in XHTML, there is an img element
that is used to display an image. It does not contain any text or elements within it, so it is called an
empty element. In XML, empty elements must be closed, but they do not require a separate close tag.
Instead, they can be closed with a forward slash at the end of the open tag as shown below.

<img src="images/paul.jpg"/>

The above code is identical in function to the code below.

<img src="images/paul.jpg"></img>

1.4.3. Attributes
XML elements can be further defined with attributes, which appear inside of the element’s open tag
as shown below.

<name title="Sir">
<firstname>Paul</firstname>
<lastname>McCartney</lastname>
</name>

1.4.4. CDATA
Sometimes it is necessary to include sections in an XML document that should not be parsed by the
XML parser. These sections might contain content that will confuse the XML parser, perhaps because
it contains content that appears to be XML, but is not meant to be interpreted as XML. Such content
must be nested in CDATA sections. The syntax for CDATA sections is shown below.

<![CDATA[
This section will not get parsed
by the XML parser.
]]>

8 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
1.4.5. Whitespace
In XML data, there are only four whitespace characters.

1. Tab
2. Line-feed
3. Carriage-return
4. Single space

There are several important rules to remember with regards to whitespace in XML.

1. Whitespace within the content of an element is significant; that is, the XML processor will
pass these characters to the application or user agent.
2. Whitespace in attributes is normalized; that is, neighboring whitespaces are condensed to a
single space.
3. Whitespace in between elements is ignored.

xml:space Attribute

The xml:space attribute is a special attribute3 in XML. It can only take one of two values: default
and preserve. This attribute instructs the application how to treat whitespace within the content of
the element. Note that the application is not required to respect this instruction.

1.4.6. XML Syntax Rules


XML has relatively straightforward, but very strict, syntax rules. A document that follows these syntax
rules is said to be well-formed.

1. There must be one and only one document element.


2. Every open tag must be closed.
3. If an element is empty, it still must be closed.
Poorly-formed: <tag>
Well-formed: <tag></tag>
Also well-formed: <tag />

4. Elements must be properly nested.

3. There is only one other special XML attribute: xml:lang, which is used to specify the language used within an element.

LESSON 1: XML Basics | 9


EVALUATION COPY: Not to be used in class.
Poorly-formed: <a><b></a></b>
Well-formed: <a><b></b></a>

5. Tag and attribute names are case sensitive.


6. Attribute values must be enclosed in single or double quotes.

1.4.7. Special Characters


There are five special characters that cannot be included in XML documents. These characters are
replaced with predefined entity references as shown in the table below.

Special Characters
Character Entity Reference
< &lt;

> &gt;

& &amp;

" &quot;

' &apos;

EVALUATION COPY: Not to be used in class.

1.5. Creating a Simple XML File


The following is a relatively simple XML file describing the Beatles.

10 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
Demo 1.3: XMLBasics/Demos/Beatles.xml
1. <?xml version="1.0"?>
2. <beatles>
3. <beatle link="http://www.paulmccartney.com">
4. <name>
5. <firstname>Paul</firstname>
6. <lastname>McCartney</lastname>
7. </name>
8. </beatle>
9. <beatle link="http://www.johnlennon.com">
10. <name>
11. <firstname>John</firstname>
12. <lastname>Lennon</lastname>
13. </name>
14. </beatle>
15. <beatle link="http://www.georgeharrison.com">
16. <name>
17. <firstname>George</firstname>
18. <lastname>Harrison</lastname>
19. </name>
20. </beatle>
21. <beatle link="http://www.ringostarr.com">
22. <name>
23. <firstname>Ringo</firstname>
24. <lastname>Starr</lastname>
25. </name>
26. </beatle>
27. <beatle link="http://www.webucator.com" real="no">
28. <name>
29. <firstname>Nat</firstname>
30. <lastname>Dunn</lastname>
31. </name>
32. </beatle>
33. </beatles>

LESSON 1: XML Basics | 11


EVALUATION COPY: Not to be used in class.
Exercise 1: Editing an XML File
10 to 15 minutes
In this exercise, you will be modifying an existing XML file.

1. Open XMLBasics/Exercises/Xml101.xml
2. Add a required prerequisite: “Experience with computers”.
3. Add the following to the topics list:
XML Documents
The Prolog Elements
Attributes
CDATA
XML Syntax Rules
Special Characters

Creating a Simple XML File

4. Add a modifications element that shows the modifications you’ve made.

12 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
Exercise Code 1.1: XMLBasics/Exercises/Xml101.xml
1. <?xml version="1.0"?>
2. <course>
3. <head>
4. <title>Introduction to XML</title>
5. <course_num>XML101</course_num>
6. <course_length>3 days</course_length>
7. </head>
8. <body>
9. <prerequisites>
10. <prereq>Experience with Word Processing</prereq>
11. <prereq optional="true">Experience with HTML</prereq>
12. <!-- Add a required prerequisite: "Experience with computers" -->
13. </prerequisites>
14. <outline>
15. <topics>
16. <topic>XML Basics
17. <topics>
18. <topic>What is XML?</topic>
19. <topic>XML Benefits
20. <topics>
21. <topic>XML Holds Data, Nothing More</topic>
22. <topic>XML Separates Structure from Format ↵↵
ting</topic>
23. <topic>XML Promotes Data Sharing</topic>
24. <topic>XML is Human-Readable</topic>
25. <topic>XML is Free</topic>
26. </topics>
27. </topic>
28. <!--
29. Add the following to the topics list ("XML Documents"
and "Creating a Simple XML File" should be siblings of "What is XML?"
and "XML Benefits"):
30.
31. -XML Documents
32. -The Prolog
33. -Elements
34. -Attributes
35. -CDATA
36. -XML Syntax Rules
37. -Special Characters
38. -Creating a Simple XML File
39. -->
40. </topics>
41. </topic>
42. </topics>

LESSON 1: XML Basics | 13


EVALUATION COPY: Not to be used in class.
43. </outline>
44. </body>
45. <foot>
46. <creator>Josh Lockwood</creator>
47. <date_created>2023-07-25</date_created>
48. <modifications madeby="Colby Germond" date="2023-05-05">
49. <modification type="insert">Added HTML prerequisite</modification>
50. <modification type="edit">Fixed some typos</modification>
51. </modifications>
52. <!--
53. Add a modifications element that shows the modifications you've made.
54. -->
55. </foot>
56. </course>

14 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
LESSON 1: XML Basics | 15
EVALUATION COPY: Not to be used in class.
Solution: XMLBasics/Solutions/Xml101.xml
1. <?xml version="1.0"?>
2. <course>
3. <head>
4. <title>Introduction to XML</title>
5. <course_num>XML101</course_num>
6. <course_length>3 days</course_length>
7. </head>
8. <body>
9. <prerequisites>
10. <prereq>Experience with Word Processing</prereq>
11. <prereq optional="true">Experience with HTML</prereq>
12. <prereq>Experience with computers</prereq>
13. </prerequisites>
14. <outline>
15. <topics>
16. <topic>XML Basics
17. <topics>
18. <topic>What is XML?</topic>
19. <topic>XML Benefits
20. <topics>
21. <topic>XML Holds Data, Nothing More</topic>
22. <topic>XML Separates Structure from Format ↵↵
ting</topic>
23. <topic>XML Promotes Data Sharing</topic>
24. <topic>XML is Human-Readable</topic>
25. <topic>XML is Free</topic>
26. </topics>
27. </topic>
28. <topic>XML Documents
29. <topics>
30. <topic>The Prolog</topic>
31. <topic>Elements</topic>
32. <topic>Attributes</topic>
33. <topic>CDATA</topic>
34. <topic>XML Syntax Rules</topic>
35. <topic>Special Characters</topic>
36. </topics>
37. </topic>
38. <topic>Creating a Simple XML File</topic>
39.
40. </topics>
41. </topic>
42. </topics>
43. </outline>

16 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
44. </body>
45. <foot>
46. <creator>Josh Lockwood</creator>
47. <date_created>2023-07-25</date_created>
48. <modifications madeby="Colby Germond" date="2023-05-05">
49. <modification type="insert">Added HTML prerequisite</modification>
50. <modification type="edit">Fixed some typos</modification>
51. </modifications>
52. <modifications madeby="Ted Ferris" date="2023-03-05">
53. <modification type="insert">
54. Added prerequisite: Experience with Computers
55. </modification>
56. <modification type="insert">
57. Added XML Documents and Creating a Simple XML Document Topics
58. </modification>
59. </modifications>
60. </foot>
61. </course>

LESSON 1: XML Basics | 17


EVALUATION COPY: Not to be used in class.
Exercise 2: Recognizing XML
5 to 10 minutes
In 1 sentence - what is this XML document about?

<?xml version="1.0"?>
<album>
<artist>
<firstname>John</firstname>
<lastname>Doe</lastname>
</artist>
<title>Love is All</title>
<year>2023</year>
</album>

18 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
LESSON 1: XML Basics | 19
EVALUATION COPY: Not to be used in class.
Solution
It describes an album by John Doe entitled "Love is All" and released in 2023.

Conclusion
In this lesson, you have learned the benefits of XML, how XML is used, and how to write a well-formed
XML document.

20 | LESSON 1: XML Basics


EVALUATION COPY: Not to be used in class.
LESSON 2
DTDs
EVALUATION COPY: Not to be used in class.

Topics Covered

Well-formed XML vs. valid XML documents.

The purpose of DTDs.

Validating with a DTD.

The limitations of DTDs.

Introduction
In this lesson, you will learn the difference between well-formed and valid XML documents, the purpose
of DTDs, to validate an XML document according to a DTD, and the limitations of DTDs.

EVALUATION COPY: Not to be used in class.

2.1. Well-formed vs. Valid


A well-formed XML document is one that follows the syntax rules described in “XML Syntax Rules”
(see page 9). A valid XML document is one that conforms to a specified structure. For an XML
document to be validated, it must be checked against a schema, which is a document that defines the
structure for a class of XML documents. XML documents that are not intended to conform to a schema
can be well-formed, but they cannot be valid.

LESSON 2: DTDs | 21
EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.

2.2. The Purpose of DTDs


A Document Type Definition (DTD) is a type of schema. The purpose of DTDs is to provide a
framework for validating XML documents. By defining a structure that XML documents must conform
to, DTDs allow different organizations to create shareable data files.

Imagine, for example, a company that creates technical courseware and sells it to technical training
companies. Those companies may want to display the outlines for that courseware on their websites,
but they do not want to display it in the same way as every other company who buys the courseware.
By providing the course outlines in a predefined XML format, the courseware vendor makes it possible
for the training companies to write programs to read those XML files and transform them into HTML
pages with their own formatting styles (perhaps using XSLT or CSS). If the XML files had no predefined
structure, it would be very difficult to write such programs.

EVALUATION COPY: Not to be used in class.

2.3. Creating DTDs


DTDs are simple text files that can be created with any basic text editor. Although they look a little
cryptic at first, they are not terribly complicated once you get used to them.

A DTD outlines what elements can be in an XML document and the attributes and subelements that
they can take. Let’s start by taking a look at a complete DTD and then dissecting it.

22 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
Demo 2.1: DTDs/Demos/Beatles.dtd
1. <!ELEMENT beatles (beatle+)>
2. <!ELEMENT beatle (name)>
3. <!ATTLIST beatle
4. link CDATA #IMPLIED
5. real (yes|no) "yes">
6. <!ELEMENT name (firstname, lastname)>
7. <!ELEMENT firstname (#PCDATA)>
8. <!ELEMENT lastname (#PCDATA)>

2.3.1. The Document Element


When creating a DTD, the first step is to define the document element.

<!ELEMENT beatles (beatle+)>

The element declaration above states that the beatles element must contain one or more beatle
elements.

Child Elements
When defining child elements in DTDs, you can specify how many times those elements can appear
by adding a modifier after the element name. If no modifier is added, the element must appear once
and only once. The other options are shown in the table below.

Modifier Description
? Zero or one times.
+ One or more times.
* Zero or more times.

It is not possible to specify a range of times that an element may appear (e.g, 2-4 appearances).

2.3.2. Other Elements


The other elements are declared in the same way as the document element - with the <!ELEMENT>
declaration. The Beatles DTD declares four additional elements.

Each beatle element must contain a child element name, which must appear once and only once.

LESSON 2: DTDs | 23
EVALUATION COPY: Not to be used in class.
<!ELEMENT beatle (name)>

Each name element must contain a firstname and lastname element, which each must appear once
and only once and in that order.

<!ELEMENT name (firstname, lastname)>

Some elements contain only text. This is declared in a DTD as #PCDATA. PCDATA stands for parsed
character data, meaning that the data will be parsed for XML tags and entities. The firstname and
lastname elements contain only text.

<!ELEMENT firstname (#PCDATA)>


<!ELEMENT lastname (#PCDATA)>

2.3.3. Choice of Elements


It is also possible to indicate that one of several elements may appear as a child element. For example,
the declaration below indicates that an img element may have a child element name or a child element
id, but not both.

<!ELEMENT img (name|id)>

2.3.4. Empty Elements


Empty elements are declared as follows.

<!ELEMENT img EMPTY>

2.3.5. Mixed Content


Sometimes elements can have elements and text intermingled. For example, the following declaration
is for a body element that may contain text in addition to any number of link and img elements.

<!ELEMENT body (#PCDATA | link | img)*>

24 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
2.3.6. Location of Modifier
The location of modifiers in a declaration is important. If the modifier is outside of a set of parentheses,
it applies to the group; whereas, if the modifier is immediately next to an element name, it applies only
to that element. The following examples illustrate.

In the example below, the body element can have any number of interspersed child link and img
elements.

<!ELEMENT body (link | img)*>

In the example below, the body element can have any number of child link elements or any number
of child img elements, but it cannot have both link and img elements.

<!ELEMENT body (link* | img*)>

In the example below, the body element can have any number of child link and img elements, but
they must come in pairs, with the link element preceding the img element.

<!ELEMENT body (link, img)*>

In the example below, the body element can have any number of child link elements followed by any
number of child img elements.

<!ELEMENT body (link*, img*)>

2.3.7. Using Parentheses for Complex Declarations


Element declarations can be more complex than the examples above. For example, you can specify that
a person element either contains a single name element or a firstname and lastname element. To
group elements, wrap them in parentheses as shown below.

<!ELEMENT person (name | (firstname,lastname))>

LESSON 2: DTDs | 25
EVALUATION COPY: Not to be used in class.
2.3.8. Declaring Attributes
Attributes are declared using the <!ATTLIST > declaration. The syntax is shown below.

<!ATTLIST ElementName
AttributeName AttributeType State DefaultValue?
AttributeName AttributeType State DefaultValue?>

ElementName is the name of the element taking the attributes.


AttributeName is the name of the attribute.
AttributeType is the type of data that the attribute value may hold. Although there are
many types, the most common are CDATA (unparsed character data) and ID (a unique
identifier). A list of options can also be given for the attribute type.
DefaultValue is the value of the attribute if it is not included in the element.
State can be one of three values: #REQUIRED, #FIXED (set value), and #IMPLIED (optional).

The beatle element has two possible attributes: link, which is optional and may contain any valid
XML text, and real, which defaults to yes if it is not included.

<!ATTLIST beatle
link CDATA #IMPLIED
real (yes|no) "yes">

EVALUATION COPY: Not to be used in class.

2.4. Validating an XML Document with a DTD


The DOCTYPE declaration in an XML document specifies the DTD to which it should conform. In the
code sample below, the DOCTYPE declaration indicates the file should be validated against Beatles.dtd
in the same directory.

26 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
Demo 2.2: DTDs/Demos/Beatles.xml
1. <?xml version="1.0"?>
2. <!DOCTYPE beatles SYSTEM "Beatles.dtd">
3. <beatles>
4. <beatle link="http://www.paulmccartney.com">
5. <name>
6. <firstname>Paul</firstname>
7. <lastname>McCartney</lastname>
8. </name>
9. </beatle>
10. <beatle link="http://www.johnlennon.com">
11. <name>
12. <firstname>John</firstname>
13. <lastname>Lennon</lastname>
14. </name>
15. </beatle>
16. <beatle link="http://www.georgeharrison.com">
17. <name>
18. <firstname>George</firstname>
19. <lastname>Harrison</lastname>
20. </name>
21. </beatle>
22. <beatle link="http://www.ringostarr.com">
23. <name>
24. <firstname>Ringo</firstname>
25. <lastname>Starr</lastname>
26. </name>
27. </beatle>
28. <beatle link="http://www.webucator.com" real="no">
29. <name>
30. <firstname>Nat</firstname>
31. <lastname>Dunn</lastname>
32. </name>
33. </beatle>
34. </beatles>

LESSON 2: DTDs | 27
EVALUATION COPY: Not to be used in class.
Exercise 3: Writing a DTD
60 to 90 minutes
In this exercise, you will write a DTD for the business letter shown below. You will then give your
DTD to another student, who will mark up the business letter as a valid XML document according to
your DTD. Likewise, you will markup the business letter according to someone else’s DTD. Make
sure that the XML file contains a DOCTYPE declaration.

Both documents should be saved in the DTDs/Exercises folder. To test whether the XML file is valid,
visit https://www.xmlvalidation.com/ and upload or copy and paste your XML document and
DTD (this will be an option after you submit your XML code).

28 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
Exercise Code 3.1: DTDs/Exercises/BusinessLetter.txt
1. November 29, 2023
2.
3. Joshua Lockwood
4. Lockwood & Lockwood
5. 291 Broadway Ave.
6. New York, NY 10007
7. United States
8.
9. Dear Mr. Lockwood:
10.
11. Along with this letter, I have enclosed the following items:
12.
13. - two original, execution copies of the Webucator Master Services Agreement
14. - two original, execution copies of the Webucator Premier Support for
15. Developers Services Description between
16. Lockwood & Lockwood and Webucator, Inc.
17.
18. Please sign and return all four original, execution copies to me at your
19. earliest convenience. Upon receipt of the executed copies, we will
20. immediately return a fully executed, original copy of both agreements to you.
21.
22. Please send all four original, execution copies to my attention as follows:
23.
24. Webucator, Inc.
25. 4933 Jamesville Rd.
26. Jamesville, NY 13078 USA
27. Attn: Bill Smith
28.
29. If you have any questions, feel free to call me at 800-555-1000 x123
30. or e-mail me at [email protected].
31.
32. Best regards,
33.
34. Bill Smith
35. VP, Operations

Conclusion
In this lesson, you learned to created DTDs to validate XML documents.

LESSON 2: DTDs | 29
EVALUATION COPY: Not to be used in class.
30 | LESSON 2: DTDs
EVALUATION COPY: Not to be used in class.
LESSON 3
XML Schema Basics
EVALUATION COPY: Not to be used in class.

Topics Covered

The purpose of XML Schema.

The limitations of DTDs.

The power of XML Schema.

Validating an XML Instance with an XML schema.

Introduction
In this lesson, you will learn the purpose of XML Schema, the limitations of DTDs, the power of XML
Schema, and to validate an XML Instance with an XML schema.

EVALUATION COPY: Not to be used in class.

3.1. The Purpose of XML Schema


XML Schema is an XML-based language used to create XML-based languages and data models. An
XML schema defines element and attribute names for a class of XML documents. The schema also
specifies the structure that those documents must adhere to and the type of content that each element
can hold.

XML documents that attempt to adhere to an XML schema are said to be instances of that schema. If
they correctly adhere to the schema, then they are valid instances. This is not the same as being well
formed. A well-formed XML document follows all the syntax rules of XML, but it does not necessarily
adhere to any particular schema. So, an XML document can be well formed without being valid, but
it cannot be valid unless it is well formed.

LESSON 3: XML Schema Basics | 31


EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.

3.2. The Power of XML Schema


You might be wondering why you need XML Schema when DTDs can be for the same purpose? The
reason is that XML Schemas are more powerful than DTDs.

DTDs are similar to XML schemas in that they are used to create classes of XML documents. DTDs
were around long before the advent of XML. They were originally created to define languages based
on SGML, the parent of XML. Although DTDs are still common, XML Schema is a much more
powerful language.

As a means of understanding the power of XML Schema, let’s look at the limitations of DTD.

1. DTDs do not have built-in datatypes.


2. DTDs do not support user-derived datatypes.
3. DTDs allow only limited control over cardinality (the number of occurrences of an element
within its parent).
4. DTDs do not support Namespaces or any simple way of reusing or importing other schemas.

EVALUATION COPY: Not to be used in class.

3.3. A First Look


An XML schema describes the structure of an XML instance document by defining what each element
must or may contain. An element is limited by its type. For example, an element of complex type can
contain child elements and attributes, whereas a simple-type element can only contain text. The diagram
below gives a first look at the types of XML Schema elements.

32 | LESSON 3: XML Schema Basics


EVALUATION COPY: Not to be used in class.
Schema authors can define their own types or use the built-in types. Throughout this course, we will
refer back to this diagram as we learn to define elements. You may want to bookmark this page, so that
you can easily reference it.

The following is a high-level overview of Schema types.

1. Elements can be of simple type or complex type.


2. Simple type elements can only contain text. They cannot have child elements or attributes.
3. All the built-in types are simple types (e.g, xs:string).
4. Schema authors can derive simple types by restricting another simple type. For example, an
email type could be derived by limiting a string to a specific pattern.
5. Simple types can be atomic (e.g, strings and integers) or non-atomic (e.g, lists).
6. Complex-type elements can contain child elements and attributes as well as text.
7. By default, complex-type elements have complex content, meaning that they have child
elements.
8. Complex-type elements can be limited to having simple content, meaning they only contain
text. They are different from simple type elements in that they have attributes.
9. Complex types can be limited to having no content, meaning they are empty, but they may
have attributes.
10. Complex types may have mixed content - a combination of text and child elements.

LESSON 3: XML Schema Basics | 33


EVALUATION COPY: Not to be used in class.
3.3.1. A Simple XML Schema
Let’s take a look at a simple XML schema, which is made up of one complex type element with two
child simple type elements.

Demo 3.1: SchemaBasics/Demos/Author.xsd


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="Author">
4. <xs:complexType>
5. <xs:sequence>
6. <xs:element name="FirstName" type="xs:string" />
7. <xs:element name="LastName" type="xs:string" />
8. </xs:sequence>
9. </xs:complexType>
10. </xs:element>
11. </xs:schema>

Code Explanation

As you can see, an XML schema is an XML document and must follow all the syntax rules of any other
XML document; that is, it must be well formed. XML schemas also have to follow the rules defined
in the “Schema of schemas,” which defines, among other things, the structure of and element and
attribute names in an XML schema.

Although it is not required, it is a common practice to use the xs qualifier4 to identify Schema elements
and types.

The document element of XML schemas is xs:schema. It takes the attribute xmlns:xs with the value
of http://www.w3.org/2001/XMLSchema, indicating that the document should follow the rules of
XML Schema. This will be clearer after you learn about namespaces.

In this XML schema, we see a xs:element element within the xs:schema element. xs:element is
used to define an element. In this case it defines the element Author as a complex type element, which
contains a sequence of two elements: FirstName and LastName, both of which are of the simple type,
string.

4. Qualifiers are used to distinguish between elements and attributes from different namespaces or XML classes.

34 | LESSON 3: XML Schema Basics


EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.

3.4. Validating an XML Instance Document


In the last section, you saw an example of a simple XML schema, which defined the structure of an
Author element. The code sample below shows a valid XML instance of this XML schema.

Demo 3.2: SchemaBasics/Demos/MarkTwain.xml


1. <?xml version="1.0"?>
2. <Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Author.xsd">
4. <FirstName>Mark</FirstName>
5. <LastName>Twain</LastName>
6. </Author>

Code Explanation

This is a simple XML document. Its document element is Author, which contains two child elements:
FirstName and LastName, just as the associated XML schema requires.

The xmlns:xsi attribute of the document element indicates that this XML document is an instance
of an XML schema. The document is tied to a specific XML schema with the
xsi:noNamespaceSchemaLocation attribute.

There are many ways to validate the XML instance. If you are using an XML authoring tool, it very
likely is able to perform the validation for you. Alternatively, there is a simple online XML Schema
validator tool listed below.

https://www.corefiling.com/opensource/schemaValidate/ provided by DecisionSoft.

Conclusion
In this lesson, you have learned to create a very simple XML Schema and to use it to validate an XML
instance document. You are now ready to learn more advanced features of XML Schema.

LESSON 3: XML Schema Basics | 35


EVALUATION COPY: Not to be used in class.
36 | LESSON 3: XML Schema Basics
EVALUATION COPY: Not to be used in class.
LESSON 4
Simple-Type Elements
EVALUATION COPY: Not to be used in class.

Topics Covered

Built-in simple types.

Deriving your own types.

List types.

Union types.

Global simple-type elements.

Default and fixed values.

Nil values.

Introduction
In this lesson, you will learn to use XML Schema’s built-in simple types, to derive your own types, to
define list types and union types, to declare global simple-type elements, to set default and fixed values,
and to allow for nil values.

EVALUATION COPY: Not to be used in class.

4.1. Overview
Simple-type elements have no children or attributes. For example, the Name element below is a
simple-type element; whereas the Person and HomePage elements are not.

LESSON 4: Simple-Type Elements | 37


EVALUATION COPY: Not to be used in class.
Demo 4.1: SimpleTypes/Demos/SimpleType.xml
1. <?xml version="1.0"?>
2. <Person>
3. <Name>Mark Twain</Name>
4. <HomePage URL="http://www.webucator.com/marktwain"/>
5. </Person>

Code Explanation

As the diagram below shows, a simple type can either be built-in or user-derived. In this lesson, we will
examine both.

EVALUATION COPY: Not to be used in class.

4.2. Built-in Simple Types


XML Schema specifies 44 built-in types, 19 of which are primitive.

4.2.1. 19 Primitive Data Types


The 19 built-in primitive types are listed below.

38 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
1. string
2. boolean
3. decimal
4. float
5. double
6. duration
7. dateTime
8. time
9. date
10. gYearMonth
11. gYear
12. gMonthDay
13. gDay
14. gMonth
15. hexBinary
16. base64Binary
17. anyURI
18. QName
19. NOTATION

4.2.2. Built-in Derived Data Types


The other 25 built-in data types are derived from one of the primitive types listed above.

1. normalizedString
2. token
3. language
4. NMTOKEN
5. NMTOKENS
6. Name
7. NCName

LESSON 4: Simple-Type Elements | 39


EVALUATION COPY: Not to be used in class.
8. ID
9. IDREF
10. IDREFS
11. ENTITY
12. ENTITIES
13. integer
14. nonPositiveInteger
15. negativeInteger
16. long
17. int
18. short
19. byte
20. nonNegativeInteger
21. unsignedLong
22. unsignedInt
23. unsignedShort
24. unsignedByte
25. positiveInteger

4.2.3. Defining a Simple-type Element


A simple-type element is defined using the type attribute.

40 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
Demo 4.2: SimpleTypes/Demos/Author.xsd
1. <?xml version="1.0" ?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="Author">
4. <xs:complexType>
5. <xs:sequence>
6. <xs:element name="FirstName" type="xs:string"/>
7. <xs:element name="LastName" type="xs:string"/>
8. </xs:sequence>
9. </xs:complexType>
10. </xs:element>
11. </xs:schema>

Code Explanation

Notice the FirstName and LastName elements in the code sample above. They are not explicitly
defined as simple type elements. Instead, the type is defined with the type attribute. Because the value
(string in both cases) is a simple type, the elements themselves are simple-type elements.

LESSON 4: Simple-Type Elements | 41


EVALUATION COPY: Not to be used in class.
Exercise 4: Building a Simple Schema
10 to 15 minutes
In this exercise, you will build a simple XML schema.

1. Open SimpleTypes/Exercises/Song.xsd for editing.


2. Between the open and close xs:sequence tags, declare three new elements:
Title of type xs:string.
Year of type xs:gYear.
Artist of type xs:string.

3. Save the file.


4. Try to validate LoveMeDo.xml against the schema you just created. If the XML document is
invalid, fix your schema.

Exercise Code 4.1: SimpleTypes/Exercises/Song.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="Song">
4. <xs:complexType>
5. <xs:sequence>
6. <!--
7. Add three simple-type elements:
8. 1. Title
9. 2. Year
10. 3. Artist
11. -->
12. </xs:sequence>
13. </xs:complexType>
14. </xs:element>
15. </xs:schema>

42 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
LESSON 4: Simple-Type Elements | 43
EVALUATION COPY: Not to be used in class.
Solution: SimpleTypes/Solutions/Song.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="Song">
4. <xs:complexType>
5. <xs:sequence>
6. <xs:element name="Title" type="xs:string"/>
7. <xs:element name="Year" type="xs:gYear"/>
8. <xs:element name="Artist" type="xs:string"/>
9. </xs:sequence>
10. </xs:complexType>
11. </xs:element>
12. </xs:schema>

EVALUATION COPY: Not to be used in class.

4.3. User-derived Simple Types


A schema author can derive a new simple type using the <xs:simpleType> element. This simple type
can then be used in the same way that built-in simple types are.

Simple types are derived by restricting built-in simple types or other user-derived simple types. For
example, you might want to create a simple type called password that is an eight-character string. To
do so, you would start with the xs:string type and restrict its length to eight characters. This is done
nesting the <xs:restriction> element inside of the <xs:simpleType> element.

44 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
Demo 4.3: SimpleTypes/Demos/Password.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Password">
4. <xs:restriction base="xs:string">
5. <xs:length value="8"/>
6. </xs:restriction>
7. </xs:simpleType>
8. <xs:element name="User">
9. <xs:complexType>
10. <xs:sequence>
11. <xs:element name="PW" type="Password"/>
12. </xs:sequence>
13. </xs:complexType>
14. </xs:element>
15. </xs:schema>

Demo 4.4: SimpleTypes/Demos/Password.xml


1. <?xml version="1.0"?>
2. <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Password.xsd">
4. <PW>MyPasWrd</PW>
5. </User>

4.3.1. Applying Facets


Simple types can be derived by applying one or more of the following facets.

length

minLength

maxLength

pattern

enumeration

whiteSpace

minInclusive

minExclusive

maxInclusive

LESSON 4: Simple-Type Elements | 45


EVALUATION COPY: Not to be used in class.
maxExclusive

totalDigits

fractionDigits

4.3.2. Controlling Length


The length of a string can be controlled with the length, minLength, and maxLength facets. We
used the length facet in the example above to create a Password simple type as an eight-character
string. We could use minLength and maxLength to allow passwords that were between six and twelve
characters in length.

The schema below shows how this is done. The two XML instances shown below it are both valid,
because the length of the password is between six and twelve characters.

Demo 4.5: SimpleTypes/Demos/Password2.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Password">
4. <xs:restriction base="xs:string">
5. <xs:minLength value="6"/>
6. <xs:maxLength value="12"/>
7. </xs:restriction>
8. </xs:simpleType>
9. <xs:element name="User">
10. <xs:complexType>
11. <xs:sequence>
12. <xs:element name="PW" type="Password"/>
13. </xs:sequence>
14. </xs:complexType>
15. </xs:element>
16. </xs:schema>

Demo 4.6: SimpleTypes/Demos/Password2.xml


1. <?xml version="1.0"?>
2. <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Password2.xsd">
4. <PW>MyPass</PW>
5. </User>

46 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
Demo 4.7: SimpleTypes/Demos/Password2b.xml
1. <?xml version="1.0"?>
2. <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Password2.xsd">
4. <PW>MyPassWord</PW>
5. </User>

4.3.3. Specifying Patterns


Patterns are specified using the xs:pattern element and regular expressions.5 For example, you could
use the xs:pattern element to restrict the Password simple type to consist of between six and twelve
characters, which can only be lowercase and uppercase letters and underscores.

Demo 4.8: SimpleTypes/Demos/Password3.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Password">
4. <xs:restriction base="xs:string">
5. <xs:pattern value="[A-Za-z_]{6,12}"/>
6. </xs:restriction>
7. </xs:simpleType>
8. <xs:element name="User">
9. <xs:complexType>
10. <xs:sequence>
11. <xs:element name="PW" type="Password"/>
12. </xs:sequence>
13. </xs:complexType>
14. </xs:element>
15. </xs:schema>

Demo 4.9: SimpleTypes/Demos/Password3.xml


1. <?xml version="1.0"?>
2. <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Password3.xsd">
4. <PW>MyPassword</PW>
5. </User>

5. Regular expressions are not covered in this course. For more information on regular expressions in XML Schema, see
https://www.w3.org/TR/xmlschema-2/#dt-regex.

LESSON 4: Simple-Type Elements | 47


EVALUATION COPY: Not to be used in class.
4.3.4. Working with Numbers
Numeric simple types can be derived by limiting the value to a certain range using minExclusive,
minInclusive, maxExclusive, and maxInclusive. You can also limit the total number of digits and
the number of digits after the decimal point using totalDigits and fractionDigits, respectively.

Mins and Maxs

The following example shows how to derive a simple type called Salary, which is a decimal between
10,000 and 90,000.

Demo 4.10: SimpleTypes/Demos/Employee.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Salary">
4. <xs:restriction base="xs:decimal">
5. <xs:minInclusive value="10000"/>
6. <xs:maxInclusive value="90000"/>
7. </xs:restriction>
8. </xs:simpleType>
9. <xs:element name="Employee">
10. <xs:complexType>
11. <xs:sequence>
12. <xs:element name="Salary" type="Salary"/>
13. </xs:sequence>
14. </xs:complexType>
15. </xs:element>
16. </xs:schema>

Demo 4.11: SimpleTypes/Demos/JohnSmith.xml


1. <?xml version="1.0"?>
2. <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Employee.xsd">
4. <Salary>55000</Salary>
5. </Employee>

Number of Digits

Using totalDigits and fractionDigits, we can further specify that the Salary type should consist
of seven digits, two of which come after the decimal point. Both totalDigits and fractionDigits

48 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
are maximums. That is, if totalDigits is specified as 7 and fractionDigits is specified as 2, a valid
number could have no more than five digits total and no more than two digits after the decimal point.

Demo 4.12: SimpleTypes/Demos/Employee2.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Salary">
4. <xs:restriction base="xs:decimal">
5. <xs:minInclusive value="10000"/>
6. <xs:maxInclusive value="90000"/>
7. <xs:fractionDigits value="2"/>
8. <xs:totalDigits value="7"/>
9. </xs:restriction>
10. </xs:simpleType>
11. <xs:element name="Employee">
12. <xs:complexType>
13. <xs:sequence>
14. <xs:element name="Salary" type="Salary"/>
15. </xs:sequence>
16. </xs:complexType>
17. </xs:element>
18. </xs:schema>

Demo 4.13: SimpleTypes/Demos/MarySmith.xml


1. <?xml version="1.0"?>
2. <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Employee2.xsd">
4. <Salary>55000.00</Salary>
5. </Employee>

4.3.5. Enumerations
A derived type can be a list of possible values. For example, the JobTitle element could be a list of
pre-defined job titles.

LESSON 4: Simple-Type Elements | 49


EVALUATION COPY: Not to be used in class.
Demo 4.14: SimpleTypes/Demos/Employee3.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Salary">
4. <xs:restriction base="xs:decimal">
5. <xs:minInclusive value="10000"/>
6. <xs:maxInclusive value="90000"/>
7. <xs:fractionDigits value="2"/>
8. <xs:totalDigits value="7"/>
9. </xs:restriction>
10. </xs:simpleType>
11. <xs:simpleType name="JobTitle">
12. <xs:restriction base="xs:string">
13. <xs:enumeration value="Sales Manager"/>
14. <xs:enumeration value="Salesperson"/>
15. <xs:enumeration value="Receptionist"/>
16. <xs:enumeration value="Developer"/>
17. </xs:restriction>
18. </xs:simpleType>
19. <xs:element name="Employee">
20. <xs:complexType>
21. <xs:sequence>
22. <xs:element name="Salary" type="Salary"/>
23. <xs:element name="Title" type="JobTitle"/>
24. </xs:sequence>
25. </xs:complexType>
26. </xs:element>
27. </xs:schema>

Demo 4.15: SimpleTypes/Demos/SteveSmith.xml


1. <?xml version="1.0"?>
2. <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Employee3.xsd">
4. <Salary>90000.00</Salary>
5. <Title>Sales Manager</Title>
6. </Employee>

4.3.6. Whitespace-handling
By default, whitespace in elements of the datatype xs:string is preserved in XML documents; however,
this can be changed for datatypes derived from xs:string. This is done with the xs:whiteSpace
element, the value of which must be one of the following.

50 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
preserve - whitespace is not normalized. That is to say, it is kept as is.
replace - all tabs, line feeds, and carriage returns are replaced by single spaces.
collapse - all tabs, line feeds, and carriage returns are replaced by single spaces and then all
groups of single spaces are replaced with one single space. All leading and trailing spaces are
then removed (i.e, trimmed).

In SimpleTypes/Demos/Password.xsd, we looked at restricting the length of a Password datatype


to eight characters using the xs:length element. If whitespace is preserved, then leading and trailing
spaces are considered part of the password. In the following example, we set xs:whiteSpace to collapse,
thereby discounting any leading or trailing whitespace. As you can see, this allows the XML instance
author to format the document without consideration of whitespace.

Demo 4.16: SimpleTypes/Demos/Password4.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Password">
4. <xs:restriction base="xs:string">
5. <xs:length value="8"/>
6. <xs:whiteSpace value="collapse"/>
7. </xs:restriction>
8. </xs:simpleType>
9. <xs:element name="User">
10. <xs:complexType>
11. <xs:sequence>
12. <xs:element name="PW" type="Password"/>
13. </xs:sequence>
14. </xs:complexType>
15. </xs:element>
16. </xs:schema>

Demo 4.17: SimpleTypes/Demos/Password4.xml


1. <?xml version="1.0"?>
2. <User xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Password4.xsd">
4. <PW>
5. 12345678
6. </PW>
7. </User>

LESSON 4: Simple-Type Elements | 51


EVALUATION COPY: Not to be used in class.
Exercise 5: Restricting Element Content
15 to 20 minutes
In this exercise, you will further restrict the Song schema, so that the Title and Artist elements will
have a specified pattern and the Year will be 1950 or later.

1. Open SimpleTypes/Exercises/Song.xsd and save it as Song2.xsd in the same directory.


2. Define a simple type called ProperName that follows this pattern. Note that the only space
in the pattern is the one before the question mark.

([A-Z0-9][A-Za-z0-9\-']* ?)+

3. Change the Title and Artist elements to be of the ProperName type.


4. Define another simple type called Year, which is derived from gYear and only accepts years
between 1950 and 1970, inclusive.
5. Change the Year element to be of the Year type.
6. Try to validate SimpleTypes/Exercises/CantBuyMeLove.xml against the schema you
just created. If the XML document is invalid, fix your schema.

Exercise Code 5.1: SimpleTypes/Exercises/Song.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="Song">
4. <xs:complexType>
5. <xs:sequence>
6. <!--
7. Add three simple-type elements:
8. 1. Title
9. 2. Year
10. 3. Artist
11. -->
12. </xs:sequence>
13. </xs:complexType>
14. </xs:element>
15. </xs:schema>

52 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
LESSON 4: Simple-Type Elements | 53
EVALUATION COPY: Not to be used in class.
Solution: SimpleTypes/Solutions/Song2.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="ProperName">
4. <xs:restriction base="xs:string">
5. <xs:pattern value="([A-Z0-9][A-Za-z0-9\-']* ?)+"/>
6. <xs:whiteSpace value="collapse"/>
7. </xs:restriction>
8. </xs:simpleType>
9. <xs:simpleType name="Year">
10. <xs:restriction base="xs:gYear">
11. <xs:minInclusive value="1950"/>
12. <xs:maxInclusive value="1970"/>
13. </xs:restriction>
14. </xs:simpleType>
15. <xs:element name="Song">
16. <xs:complexType>
17. <xs:sequence>
18. <xs:element name="Title" type="ProperName"/>
19. <xs:element name="Year" type="Year"/>
20. <xs:element name="Artist" type="ProperName"/>
21. </xs:sequence>
22. </xs:complexType>
23. </xs:element>
24. </xs:schema>

EVALUATION COPY: Not to be used in class.

4.4. Specifying Element Type Locally


So far in this lesson, we have been defining simple types globally and then setting the type attribute
of element declarations to be of our derived simple types. This makes it easy to reuse a simple type
across multiple elements, as we saw with the ProperName type in the last exercise.

It is also possible to define the type of an element locally. The type is then unnamed and applicable
only to that element. The only reason to do this is to clearly show that the type is specific to that element
and not meant for reuse.

54 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
Demo 4.18: SimpleTypes/Demos/PasswordLocal.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="User">
4. <xs:complexType>
5. <xs:sequence>
6. <xs:element name="PW">
7. <xs:simpleType>
8. <xs:restriction base="xs:string">
9. <xs:length value="8"/>
10. <xs:whiteSpace value="collapse"/>
11. </xs:restriction>
12. </xs:simpleType>
13. </xs:element>
14. </xs:sequence>
15. </xs:complexType>
16. </xs:element>
17. </xs:schema>

EVALUATION COPY: Not to be used in class.

4.5. Nonatomic Types


All of XML Schema’s built-in types are atomic, meaning that they cannot be broken down into
meaningful bits. XML Schema provides for two nonatomic types: lists and unions.

4.5.1. Lists
List types are sequences of atomic types separated by whitespace; you can have a list of integers or a list
of dates. Lists should not be confused with enumerations. Enumerations provide optional values for
an element. Lists represent a space delimited list of multiple values within an element’s text node.

LESSON 4: Simple-Type Elements | 55


EVALUATION COPY: Not to be used in class.
Demo 4.19: SimpleTypes/Demos/EmployeeList.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Salary">
4. <xs:restriction base="xs:decimal">
5. <xs:minInclusive value="10000"/>
6. <xs:maxInclusive value="90000"/>
7. <xs:fractionDigits value="2"/>
8. <xs:totalDigits value="7"/>
9. </xs:restriction>
10. </xs:simpleType>
11. <xs:simpleType name="JobTitle">
12. <xs:restriction base="xs:string">
13. <xs:enumeration value="Sales Manager"/>
14. <xs:enumeration value="Salesperson"/>
15. <xs:enumeration value="Receptionist"/>
16. <xs:enumeration value="Developer"/>
17. </xs:restriction>
18. </xs:simpleType>
19. <xs:simpleType name="DateList">
20. <xs:list itemType="xs:date"/>
21. </xs:simpleType>
22. <xs:element name="Employee">
23. <xs:complexType>
24. <xs:sequence>
25. <xs:element name="Salary" type="Salary"/>
26. <xs:element name="Title" type="JobTitle"/>
27. <xs:element name="VacationDays" type="DateList"/>
28. </xs:sequence>
29. </xs:complexType>
30. </xs:element>
31. </xs:schema>

Demo 4.20: SimpleTypes/Demos/SandySmith.xml


1. <?xml version="1.0"?>
2. <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="EmployeeList.xsd">
4. <Salary>44000</Salary>
5. <Title>Salesperson</Title>
6. <VacationDays>2006-08-13 2006-08-14 2006-08-15</VacationDays>
7. </Employee>

56 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
4.5.2. Unions
Union types are groupings of types, essentially allowing for the value of an element to be of more than
one type. In the example below, two atomic simple types are derived: RunningRace and Gymnastics.
A third simple type, Event, is then derived as a union of the previous two. The Event element is of
the Event type, which means that it can either be of the RunningRace or the Gymnastics type.

Demo 4.21: SimpleTypes/Demos/Program.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="RunningRace">
4. <xs:restriction base="xs:string">
5. <xs:enumeration value="100 meters"/>
6. <xs:enumeration value="10 kilometers"/>
7. <xs:enumeration value="440 yards"/>
8. <xs:enumeration value="10 miles"/>
9. <xs:enumeration value="Marathon"/>
10. </xs:restriction>
11. </xs:simpleType>
12. <xs:simpleType name="Gymnastics">
13. <xs:restriction base="xs:string">
14. <xs:enumeration value="Vault"/>
15. <xs:enumeration value="Floor"/>
16. <xs:enumeration value="Rings"/>
17. <xs:enumeration value="Beam"/>
18. <xs:enumeration value="Uneven Bars"/>
19. </xs:restriction>
20. </xs:simpleType>
21. <xs:simpleType name="Event">
22. <xs:union memberTypes="RunningRace Gymnastics"/>
23. </xs:simpleType>
24. <xs:element name="Program">
25. <xs:complexType>
26. <xs:sequence>
27. <xs:element name="Event" type="Event"/>
28. </xs:sequence>
29. </xs:complexType>
30. </xs:element>
31. </xs:schema>

LESSON 4: Simple-Type Elements | 57


EVALUATION COPY: Not to be used in class.
Demo 4.22: SimpleTypes/Demos/100Meters.xml
1. <?xml version="1.0"?>
2. <Program xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="Program.xsd">
4. <Event>100 meters</Event>
5. </Program>

58 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
Exercise 6: Adding Nonatomic Types
10 to 15 minutes
In this exercise, you will add a nonatomic type to the song schema.

1. Open SimpleTypes/Exercises/Song2.xsd and save it as Song3.xsd in the same directory.


2. Define a new simple type called SongLength, which is an enumeration of three values: “Short”,
“Medium’, and ”Long".
3. Define another new simple type called SongTime, which is a union of xs:duration and
SongLength.

4. At the end of the sequence of elements within the Song element, insert an additional element,
Length, which is of the SongTime datatype.

5. Try to validate SimpleTypes/Exercises/TicketToRide.xml and SimpleTypes/Exer


cises/EleanorRigby.xml against the schema you just created. If either XML document is
invalid, fix your schema.

LESSON 4: Simple-Type Elements | 59


EVALUATION COPY: Not to be used in class.
Solution: SimpleTypes/Solutions/Song3.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="ProperName">
4. <xs:restriction base="xs:string">
5. <xs:whiteSpace value="collapse"/>
6. <xs:pattern value="([A-Z0-9][A-Za-z0-9\-']* ?)+"/>
7. </xs:restriction>
8. </xs:simpleType>
9. <xs:simpleType name="Year">
10. <xs:restriction base="xs:gYear">
11. <xs:minInclusive value="1950"/>
12. <xs:maxInclusive value="1970"/>
13. </xs:restriction>
14. </xs:simpleType>
15. <xs:simpleType name="SongLength">
16. <xs:restriction base="xs:string">
17. <xs:enumeration value="Short"/>
18. <xs:enumeration value="Medium"/>
19. <xs:enumeration value="Long"/>
20. </xs:restriction>
21. </xs:simpleType>
22. <xs:simpleType name="SongTime">
23. <xs:union memberTypes="xs:duration SongLength"/>
24. </xs:simpleType>
25. <xs:element name="Song">
26. <xs:complexType>
27. <xs:sequence>
28. <xs:element name="Title" type="ProperName"/>
29. <xs:element name="Year" type="Year"/>
30. <xs:element name="Artist" type="ProperName"/>
31. <xs:element name="Length" type="SongTime"/>
32. </xs:sequence>
33. </xs:complexType>
34. </xs:element>
35. </xs:schema>

EVALUATION COPY: Not to be used in class.

60 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
4.6. Declaring Global Simple-Type Elements
When an element declaration is a child of the xs:schema element, the declared element is global.
Global elements can be referenced by other element declarations, allowing for element reuse. Take a
look at the following example.

Demo 4.23: SimpleTypes/Demos/AuthorGlobal.xsd


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="FirstName" type="xs:string"/>
4. <xs:element name="LastName" type="xs:string"/>
5. <xs:element name="Author">
6. <xs:complexType>
7. <xs:sequence>
8. <xs:element ref="FirstName"/>
9. <xs:element ref="LastName"/>
10. </xs:sequence>
11. </xs:complexType>
12. </xs:element>
13. </xs:schema>

Code Explanation

In this example, the FirstName and LastName elements are both declared globally. The global elements
are then referenced as children of the Author sequence.

4.6.1. Global vs. Local Simple-Type Elements


The major advantage of declaring an element globally is that the element can then be referenced
throughout the schema. This makes the code more modular and easier to maintain. For example,
suppose that the song schema contained MusicWriter, LyricsWriter, and Singer elements. Each
of these elements might have the child element Name. If the Name element is declared globally, any
changes to that element can be made in one location.

The major disadvantage of declaring elements globally is that all global elements must have unique
names.

LESSON 4: Simple-Type Elements | 61


EVALUATION COPY: Not to be used in class.
Demo 4.24: SimpleTypes/Demos/BookLocal.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="PersonTitle">
4. <xs:restriction base="xs:string">
5. <xs:enumeration value="Mr."/>
6. <xs:enumeration value="Ms."/>
7. <xs:enumeration value="Dr."/>
8. </xs:restriction>
9. </xs:simpleType>
10. <xs:element name="Book">
11. <xs:complexType>
12. <xs:sequence>
13. <xs:element name="Title" type="xs:string"/>
14. <xs:element name="Author">
15. <xs:complexType>
16. <xs:sequence>
17. <xs:element name="Title" type="PersonTitle"/>
18. <xs:element name="Name" type="xs:string"/>
19. </xs:sequence>
20. </xs:complexType>
21. </xs:element>
22. </xs:sequence>
23. </xs:complexType>
24. </xs:element>
25. </xs:schema>

Code Explanation

Notice that there are two elements named Title, which can appear in different locations in the XML
instance and are of different types. When the Title element appears at the root of the XML instance,
its value can be any string; whereas, when it appears as a child of Author, its value is limited to “Mr.”,
“Ms.”, or “Dr.”

The example below defines a similar content model; however, because the elements are declared globally,
the name Title cannot be used twice.

62 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
Demo 4.25: SimpleTypes/Demos/BookGlobal.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="PersonTitle">
4. <xs:restriction base="xs:string">
5. <xs:enumeration value="Mr."/>
6. <xs:enumeration value="Ms."/>
7. <xs:enumeration value="Dr."/>
8. </xs:restriction>
9. </xs:simpleType>
10. <xs:element name="BookTitle" type="xs:string"/>
11. <xs:element name="Title" type="PersonTitle"/>
12. <xs:element name="Name" type="xs:string"/>
13. <xs:element name="Book">
14. <xs:complexType>
15. <xs:sequence>
16. <xs:element ref="BookTitle"/>
17. <xs:element name="Author">
18. <xs:complexType>
19. <xs:sequence>
20. <xs:element ref="Title"/>
21. <xs:element ref="Name"/>
22. </xs:sequence>
23. </xs:complexType>
24. </xs:element>
25. </xs:sequence>
26. </xs:complexType>
27. </xs:element>
28. </xs:schema>

LESSON 4: Simple-Type Elements | 63


EVALUATION COPY: Not to be used in class.
Exercise 7: Converting Simple-Type
Element Declarations from Local to Global
10 to 20 minutes
In this exercise, you will convert the element declarations in the song schema from local to global.

1. Open SimpleTypes/Exercises/Song3.xsd and save it as Song4.xsd in the same directory.


2. Change the Title, Year, Artist, and Length elements to be declared globally.
3. Try to validate SimpleTypes/Exercises/StrawberryFields.xml against the schema you
just created. If the XML document is invalid, fix your schema.
4. Try to validate StrawberryFields.xml against Songs3.xsd.
5. Try to validate EleanorRigby.xml and TicketToRide.xml against Song4.xsd.

64 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
LESSON 4: Simple-Type Elements | 65
EVALUATION COPY: Not to be used in class.
Solution: SimpleTypes/Solutions/Song4.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="ProperName">
4. <xs:restriction base="xs:string">
5. <xs:whiteSpace value="collapse"/>
6. <xs:pattern value="([A-Z0-9][A-Za-z0-9\-']* ?)+"/>
7. </xs:restriction>
8. </xs:simpleType>
9. <xs:simpleType name="Year">
10. <xs:restriction base="xs:gYear">
11. <xs:minInclusive value="1950"/>
12. <xs:maxInclusive value="1970"/>
13. </xs:restriction>
14. </xs:simpleType>
15. <xs:simpleType name="SongLength">
16. <xs:restriction base="xs:string">
17. <xs:enumeration value="Short"/>
18. <xs:enumeration value="Medium"/>
19. <xs:enumeration value="Long"/>
20. </xs:restriction>
21. </xs:simpleType>
22. <xs:simpleType name="SongTime">
23. <xs:union memberTypes="xs:duration SongLength"/>
24. </xs:simpleType>
25. <xs:element name="Title" type="ProperName"/>
26. <xs:element name="Year" type="Year"/>
27. <xs:element name="Artist" type="ProperName"/>
28. <xs:element name="Length" type="SongTime"/>
29. <xs:element name="Song">
30. <xs:complexType>
31. <xs:sequence>
32. <xs:element ref="Title"/>
33. <xs:element ref="Year"/>
34. <xs:element ref="Artist"/>
35. <xs:element ref="Length"/>
36. </xs:sequence>
37. </xs:complexType>
38. </xs:element>
39. </xs:schema>

66 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.

4.7. Default Values


Elements that do not have any children can have default values. To specify a default value, use the
default attribute of the xs:element element.

Demo 4.26: SimpleTypes/Demos/EmployeeDefault.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
-------Lines 3 through 18 Omitted-------
19. <xs:element name="Employee">
20. <xs:complexType>
21. <xs:sequence>
22. <xs:element name="Salary" type="Salary"/>
23. <xs:element name="Title" type="JobTitle" default="Salesperson"/>
24. </xs:sequence>
25. </xs:complexType>
26. </xs:element>
27. </xs:schema>

Code Explanation

When defaults are set in the XML schema, the following rules apply for the instance document.

1. If the element appears in the document with content, the default value is ignored.
2. If the element appears without content, the default value is applied.
3. If the element does not appear, the element is left out. In other words, providing a default
value does not imply that the element should be inserted if the XML instance author leaves
it out.

Examine the following XML instance. The Title element cannot be empty; it requires one of the
values from the enumeration defined in the JobTitle simple type. However, in accordance with
number 2 above, the schema processor applies the default value of Salesperson to the Title element,
so the instance validates successfully.

LESSON 4: Simple-Type Elements | 67


EVALUATION COPY: Not to be used in class.
Demo 4.27: SimpleTypes/Demos/MikeSmith.xml
1. <?xml version="1.0"?>
2. <Employee xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="EmployeeDefault.xsd">
4. <Salary>90000</Salary>
5. <Title/>
6. </Employee>

EVALUATION COPY: Not to be used in class.

4.8. Fixed Values


Element values can be fixed, meaning that, if they appear in the instance document, they must contain
a specified value. Fixed elements are often used for boolean switches.

68 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
Demo 4.28: SimpleTypes/Demos/EmployeeFixed.xsd
1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:simpleType name="Salary">
4. <xs:restriction base="xs:decimal">
5. <xs:minInclusive value="10000"/>
6. <xs:maxInclusive value="90000"/>
7. <xs:fractionDigits value="2"/>
8. <xs:totalDigits value="7"/>
9. </xs:restriction>
10. </xs:simpleType>
11. <xs:simpleType name="JobTitle">
12. <xs:restriction base="xs:string">
13. <xs:enumeration value="Sales Manager"/>
14. <xs:enumeration value="Salesperson"/>
15. <xs:enumeration value="Receptionist"/>
16. <xs:enumeration value="Developer"/>
17. </xs:restriction>
18. </xs:simpleType>
19. <xs:element name="Employee">
20. <xs:complexType>
21. <xs:sequence>
22. <xs:element name="Salary" type="Salary"/>
23. <xs:element name="Title" type="JobTitle"/>
24. <xs:element name="Status" type="xs:string" fixed="current"
25. minOccurs="0"/>
26. </xs:sequence>
27. </xs:complexType>
28. </xs:element>
29. </xs:schema>

Code Explanation

The MinOccurs attribute is used to make the Status element optional. However, if it is used, it must
contain the value current or be left empty, in which case, the value current is implied. The file Simple
Types/Demos/LauraSmith.xml in the Demos folder validates against this schema.

EVALUATION COPY: Not to be used in class.

LESSON 4: Simple-Type Elements | 69


EVALUATION COPY: Not to be used in class.
4.9. Nil Values
When an optional element is left out of an XML instance, it has no clear meaning. For example, suppose
a schema declares a Name element as having required FirstName and LastName elements and an
optional MiddleName element. And suppose a particular instance of this schema does not include the
MiddleName element. Does this mean that the instance author did not know the middle name of the
person in question or does it mean the person in question has no middle name?

Setting the nillable attribute of xs:element to true indicates that such elements can be set to nil
by setting the xsi:nil attribute to true.

Demo 4.29: SimpleTypes/Demos/AuthorNillable.xsd


1. <?xml version="1.0"?>
2. <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
3. <xs:element name="Author">
4. <xs:complexType>
5. <xs:sequence>
6. <xs:element name="FirstName" type="xs:string"/>
7. <xs:element name="MiddleName" type="xs:string" nillable="true"/>
8. <xs:element name="LastName" type="xs:string"/>
9. </xs:sequence>
10. </xs:complexType>
11. </xs:element>
12. </xs:schema>

Demo 4.30: SimpleTypes/Demos/MarkTwain.xml


1. <?xml version="1.0"?>
2. <Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3. xsi:noNamespaceSchemaLocation="AuthorNillable.xsd">
4. <FirstName>Mark</FirstName>
5. <MiddleName xsi:nil="true"/>
6. <LastName>Twain</LastName>
7. </Author>

Code Explanation

By including the MiddleName element and setting xsi:nil to true, we are explicitly stating that we
do not know anything about Mark Twain’s middle name. He might have had one, but we don’t know
what it is, and if we do, we’re not saying.

70 | LESSON 4: Simple-Type Elements


EVALUATION COPY: Not to be used in class.
This is, of course, somewhat strange. It implies that if we had simply left the MiddleName out, we
would be indicating that Mark Twain has no middle name. The important thing is that the application
consuming your XML data can differentiate between nil values, empty elements, and missing elements.

Conclusion
In this lesson, you have learned to work with XML Schema’s built-in simple types, to derive your own
simple types, and to declare simple-type elements. We will now take a look at complex-type elements.

LESSON 4: Simple-Type Elements | 71


EVALUATION COPY: Not to be used in class.
72 | LESSON 4: Simple-Type Elements
EVALUATION COPY: Not to be used in class.
LESSON 5
XSLT Basics
EVALUATION COPY: Not to be used in class.

Topics Covered

The purpose of XSLT.

A simple XSLT transform.

Whitespace.

Output modes.

Outputting elements and attributes.

Introduction
In this lesson, you will learn the purpose of XSLT, to do a simple XSLT transform, to work with
whitespace, to choose the output mode, and to output elements and attributes.

EVALUATION COPY: Not to be used in class.

5.1. eXtensible Stylesheet Language


The eXtensible Stylesheet Language is divided into two sub-languages: eXtensible Stylesheet Language
Transformations (XSLT) and eXtensible Stylesheet Language - Formatting Objects (XSL-FO). In this
lesson, we will be looking at the basics of XSLT, which is used to transform XML documents.

XSLT documents are well-formed XML documents that describe how another XML document should
be transformed. For XSLT to work, it needs an XML document to transform and an engine to make
the transformation take place. In addition, parameters can be passed in to XSLTs providing further
instructions on how to do the transformation.

LESSON 5: XSLT Basics | 73


EVALUATION COPY: Not to be used in class.
The diagram below shows how this all works.

EVALUATION COPY: Not to be used in class.

5.2. The Transformation Process


An XSLT looks at an XML document as a collection of nodes of the following types:

Root node
Element nodes
Attribute nodes
Text nodes
Processing instruction nodes
Comment nodes

An XSLT document contains one or more templates, which are created with the <xsl:template />
tag. When an XML document is transformed with an XSLT, the XSLT processor reads through the
XML document starting at the root, which is one level above the document element, and progressing
from top to bottom, just as a person would read the document. Each time the processor encounters a
node, it looks for a matching template in the XSLT.

74 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
If it finds a matching template it applies it; otherwise, it uses a default template as defined by the XSLT
specification. The default templates are shown in the table below.

Default Templates
Node Type Default Template
Root Apply templates for child nodes.
Element Apply templates for child nodes.
Attribute Output attribute value.
Text Output text value.
Processing Instruction Do nothing.
Comment Do nothing.

In this context, attributes are not considered children of the elements that contain them, so attributes
get ignored by the XSLT processor unless they are explicitly referenced by the XSLT document.

EVALUATION COPY: Not to be used in class.

5.3. An XSLT Stylesheet


Let’s start by looking at a simple XML document and an XSLT stylesheet, which is used to transform
the XML to HTML.

Demo 5.1: XsltBasics/Demos/Paul.xml


1. <?xml version="1.0"?>
2. <?xml-stylesheet href="beatle.xsl" type="text/xsl"?>
3. <person>
4. <name>
5. <firstname>Paul</firstname>
6. <lastname>McCartney</lastname>
7. </name>
8. <job>Singer</job>
9. <gender>Male</gender>
10. </person>

LESSON 5: XSLT Basics | 75


EVALUATION COPY: Not to be used in class.
Code Explanation

This is a straightforward XML document. The processing instruction at the top indicates that the XML
document should be transformed using Beatle.xsl (shown below).

Demo 5.2: XsltBasics/Demos/Beatle.xsl


1. <?xml version="1.0"?>
2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Trans ↵↵
form"><xsl:output method="html"/>
3.
4. <xsl:template match="child::person">
5. <html>
6. <head>
7. <title>
8. <xsl:value-of select="descendant::firstname" />
9. <xsl:text> </xsl:text>
10. <xsl:value-of select="descendant::lastname" />
11. </title>
12. </head>
13. <body>
14. <xsl:value-of select="descendant::firstname" />
15. <xsl:text> </xsl:text>
16. <xsl:value-of select="descendant::lastname" />
17. </body>
18. </html>
19. </xsl:template>
20. </xsl:stylesheet>

Code Explanation

Note that the document begins with an XML declaration. This is because XSLTs are XML documents
themselves. As with all XML documents, the XML declaration is optional.

The second line (shown below) is the document element of the XSLT. It states that this document is
a version 1.0 XSLT document.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

The third line (shown below) indicates that the resulting output will be HTML.

<xsl:output method="html"/>

76 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
The fourth line is an open <xsl:template> element. The match attribute of this tag takes an XPath,
which indicates that this template applies to the person node of the XML document. Because person
is the document element of the source document, this template will only run once.

There are then a few lines of HTML tags followed by two <xsl:value-of /> elements separated by
one <xsl:text> element. The <xsl:value-of /> tag has a select attribute, which takes an XPath
pointing to a specific element or group of elements within the XML document. In this case, the two
<xsl:value-of /> tags point to firstname and lastname elements, indicating that they should
be output in the title of the HTML page. The <xsl:text> element is used to create a space between
the first name and the last name elements.

<xsl:value-of select="descendant::firstname" />


<xsl:text> </xsl:text>
<xsl:value-of select="descendant::lastname" />

There are then some more HTML tags followed by the same XSLT tags, re-outputting the first and
last name of the Beatle in the body of the HTML page.

5.3.1. xsl:template
The xsl:template tag is used to tell the XSLT processor what to do when it comes across a matching
node. Matches are determined by the XPath expression in the match attribute of the xsl:template
tag.

Demo 5.3: XsltBasics/Demos/FirstName.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="text"/>
5. <xsl:template match="firstname">
6. We found a first name!
7. </xsl:template>
8. </xsl:stylesheet>

Code Explanation

If we use this XSLT to transform XsltBasics/Demos/FirstName.xml, which has the same XML as
XsltBasics/Demos/Paul.xml, the result will be:

LESSON 5: XSLT Basics | 77


EVALUATION COPY: Not to be used in class.
We found a first name!
McCartneySingerMale

The text “We found a first name!” shows up only once, because only one match is found. The text
“McCartneySingerMale” shows up because the XSLT engine will continue looking through an XML
document until it is explicitly told to stop. When it reaches a text node with no matching template, it
uses the default template, which simply outputs the text. “McCartney”, “Singer” and “Male” are the
text node values inside the elements that follow the FirstName element.

Let’s see what happens if we transform an XML document with multiple FirstName elements against
this same XSLT. Take, for example, the XML document below.

78 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
Demo 5.4: XsltBasics/Demos/Beatles.xml
1. <?xml version="1.0"?>
2. <?xml-stylesheet href="FirstName.xsl" type="text/xsl"?>
3. <beatles>
4. <beatle link="http://www.paulmccartney.com">
5. <name>
6. <firstname>Paul</firstname>
7. <lastname>McCartney</lastname>
8. </name>
9. </beatle>
10. <beatle link="http://www.johnlennon.com">
11. <name>
12. <firstname>John</firstname>
13. <lastname>Lennon</lastname>
14. </name>
15. </beatle>
16. <beatle link="http://www.georgeharrison.com">
17. <name>
18. <firstname>George</firstname>
19. <lastname>Harrison</lastname>
20. </name>
21. </beatle>
22. <beatle link="http://www.ringostarr.com">
23. <name>
24. <firstname>Ringo</firstname>
25. <lastname>Starr</lastname>
26. </name>
27. </beatle>
28. <beatle link="http://www.webucator.com" real="no">
29. <name>
30. <firstname>Nat</firstname>
31. <lastname>Dunn</lastname>
32. </name>
33. </beatle>
34. </beatles>

Code Explanation

The resulting output will be as follows.

LESSON 5: XSLT Basics | 79


EVALUATION COPY: Not to be used in class.
We found a first name!
McCartney
We found a first name!
Lennon
We found a first name!
Harrison
We found a first name!
Starr
We found a first name!
Dunn

Each time a firstname element is found in the source XML document, the text “We found a first
name!” is output. For the other elements with text (in this case, only lastname elements), the actual
text is output.

5.3.2. xsl:value-of
The xsl:value-of element is used to output the text value of a node. To illustrate, let’s look at the
following example.

Demo 5.5: XsltBasics/Demos/ValueOf1.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="text"/>
5. <xsl:template match="firstname">
6. We found a first name and it's <xsl:value-of select="."/>
7. </xsl:template>
8. </xsl:stylesheet>

Code Explanation

If we use this XSLT to transform XsltBasics/Demos/Beatles-VO1.xml, which has the same XML
as XsltBasics/Demos/Beatles.xml, the result will be:

80 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
We found a first name and it's PaulMcCartney
We found a first name and it's JohnLennon
We found a first name and it's GeorgeHarrison
We found a first name and it's RingoStarr
We found a first name and it's NatDunn

The select attribute takes an XPath, which is used to indicate which node’s value to output. A single
dot (.) refers to the current node (i.e, the node that was matched by the template).

Don’t let the last names at the end of each line confuse you. These are not part of the output of the
xsl:value-of element. They are there as a result of the default template, which outputs the text value
of the element found.

To illustrate this, let’s look at another example.

Demo 5.6: XsltBasics/Demos/ValueOf2.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="text"/>
5. <xsl:template match="firstname">
6. We found a first name and it's <xsl:value-of select="."/>
7. </xsl:template>
8. <xsl:template match="lastname">
9. We found a last name and it's <xsl:value-of select="."/>
10. </xsl:template>
11. </xsl:stylesheet>

Code Explanation

If we use this XSLT to transform XsltBasics/Demos/Beatles-VO2.xml, which has the same XML
as XsltBasics/Demos/Beatles.xml, the result will be:

LESSON 5: XSLT Basics | 81


EVALUATION COPY: Not to be used in class.
We found a first name and it's Paul
We found a last name and it's McCartney
We found a first name and it's John
We found a last name and it's Lennon
We found a first name and it's George
We found a last name and it's Harrison
We found a first name and it's Ringo
We found a last name and it's Starr
We found a first name and it's Nat
We found a last name and it's Dunn

This XSLT has a template for both firstname and lastname and so it never uses the default template.

5.3.3. Whitespace and xsl:text


Whitespace in an XSLT template is output literally. If you’re not careful, this can lead to undesirable
results. To illustrate, let’s look at the following XML and XSLT documents.

Demo 5.7: XsltBasics/Demos/WhiteSpace1.xml


1. <?xml version="1.0" encoding="UTF-8"?>
2. <?xml-stylesheet type="text/xsl" href="WhiteSpace1.xsl"?>
3. <example>
4. <blurb>
5. Hello World!
6. </blurb>
7. </example>

Demo 5.8: XsltBasics/Demos/WhiteSpace1.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="text"/>
5. <xsl:template match="blurb">
6. Literal Text
7. </xsl:template>
8. </xsl:stylesheet>

When XsltBasics/Demos/WhiteSpace1.xml is transformed against XsltBasics/Demos/WhiteS


pace1.xsl, the output looks like this:

82 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
There is an empty line before and after the text. There are also two tabs preceding the text. This is
because the whitespace between <xsl:template match="blurb"> and Literal Text and the
whitespace between Literal Text and </xsl:template> is output literally.

If you did not want that extra whitespace to show up in the output, you could use the following XSLT
instead.

Demo 5.9: XsltBasics/Demos/WhiteSpace2.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="text"/>
5. <xsl:template match="blurb">
6. <xsl:text>Literal Text</xsl:text>
7. </xsl:template>
8. </xsl:stylesheet>

Code Explanation

When XsltBasics/Demos/WhiteSpace2.xml, which has the same XML as XsltBasics/De


mos/WhiteSpace1.xml, is transformed against XsltBasics/Demos/WhiteSpace2.xsl, the output
looks like this:

Because whitespace between open tags (e.g, between <xsl:template match="blurb"> and
<xsl:text>) and whitespace between close tags (e.g, </xsl:text> and </xsl:template>) is ignored,
the only content that is output is the text between the open and close xsl:text tags.

Inserting Whitespace with xsl:text

The examples above illustrate how xsl:text can be used to remove whitespace. It can also be used to
add whitespace where there otherwise wouldn’t be any. Let’s take a look at another example.

LESSON 5: XSLT Basics | 83


EVALUATION COPY: Not to be used in class.
Demo 5.10: XsltBasics/Demos/WhiteSpace3.xsl
1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="text"/>
5. <xsl:template match="name">
6. <xsl:value-of select="firstname"/>
7. <xsl:value-of select="lastname"/>
8. </xsl:template>
9. </xsl:stylesheet>

Code Explanation

When XsltBasics/Demos/Beatles-WS3.xml, which has the same XML as XsltBasics/Demos/Bea


tles.xml, is transformed against XsltBasics/Demos/WhiteSpace3.xsl, the output looks like this:

Clearly, this isn’t the desired output. We’d like to have each Beatle on a separate line and spaces between
their first and last names like this:

However, because whitespace between two open tags and between two close tags is ignored, we don’t
get any whitespace in the output. We can use xsl:text to fix this by explicitly indicating how much
whitespace we want in each location.

84 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
Demo 5.11: XsltBasics/Demos/WhiteSpace4.xsl
1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="text"/>
5. <xsl:template match="name">
6. <xsl:value-of select="firstname"/>
7. <xsl:text> </xsl:text>
8. <xsl:value-of select="lastname"/>
9. <xsl:text>
10. </xsl:text>
11. </xsl:template>
12. </xsl:stylesheet>

Code Explanation

Notice that the second close xsl:text hugs the left border. If we were to put any space before it on
that line, that space would show up in the output. To see the desired result, transform XsltBasics/De
mos/Beatles-WS4.xml against XsltBasics/Demos/WhiteSpace4.xsl.

EVALUATION COPY: Not to be used in class.

5.4. Output Types


The type of output is determined by the method attribute of the xsl:output element, which must
be a direct child of the xsl:stylesheet element.

5.4.1. Text

<xsl:output method="text"/>

As we have seen in the examples thus far, XSLT can be used to output plain text. However, XSLT is
much more commonly used to transform one XML structure into another XML structure or into
HTML.

LESSON 5: XSLT Basics | 85


EVALUATION COPY: Not to be used in class.
5.4.2. XML

<xsl:output method="xml"/>

The default output method (in most cases) is XML. That is, an XSLT with no xsl:output element
will output XML. In this case, XSLT tags are intermingled with the output XML tags. Generally, the
XSLT tags are prefixed with xsl:, so it is easy to tell them apart. The output XML tags may also be
prefixed, but not with the same prefix as the XSLT tags.

Literal Result Elements


The output XML tags are called literal result elements. That is because they are output literally rather
than used by the XSLT to determine what or how something should be output.

Useful xsl:output Attributes When Outputting XML


Attribute Description
method Should be set to xml.
indent yes or no.If yes, the resulting XML document will be pretty printed.
The default is usually no.
omit-xml-declaration yes or no. If no, the resulting XML document will begin with an XML
declaration. The default is usually no.
version Specifies the XML version of the resulting XML document.

The documents below show how XSLT can be used to output XML.

Demo 5.12: XsltBasics/Demos/Name.xml


1. <?xml version="1.0"?>
2. <?xml-stylesheet href="Name.xsl" type="text/xsl"?>
3. <person>
4. <name>
5. <firstname>Paul</firstname>
6. <lastname>McCartney</lastname>
7. </name>
8. </person>

86 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
Demo 5.13: XsltBasics/Demos/Name.xsl
1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="xml" indent="yes" version="1.0"/>
5. <xsl:template match="name">
6. <Matches>
7. <Match>We found a name!</Match>
8. <Name><xsl:value-of select="."/></Name>
9. </Matches>
10. </xsl:template>
11. </xsl:stylesheet>

Demo 5.14: XsltBasics/Demos/NameResult.xml


1. <?xml version="1.0" encoding="UTF-8"?>
2. <Matches>
3. <Match>We found a name!</Match>
4. <Name>
5. Paul
6. McCartney
7. </Name>
8. </Matches>

5.4.3. HTML

<xsl:output method="html"/>

It is very common to use XSLT to output HTML. In fact, XSLT even has a special provision for
HTML: if the document element of the resulting document is html (not case sensitive) then the default
method is changed to HTML. However, for the sake of clarity, it is better code to specify the output
method with the xsl:output tag.

The documents below show how XSLT can be used to output HTML.

LESSON 5: XSLT Basics | 87


EVALUATION COPY: Not to be used in class.
Demo 5.15: XsltBasics/Demos/NameHTML.xml
1. <?xml version="1.0"?>
2. <?xml-stylesheet href="NameHTML.xsl" type="text/xsl"?>
3. <person>
4. <name>
5. <firstname>Paul</firstname>
6. <lastname>McCartney</lastname>
7. </name>
8. </person>

Demo 5.16: XsltBasics/Demos/NameHTML.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="html"/>
5. <xsl:template match="name">
6. <html>
7. <head>
8. <title>We found a name!</title>
9. </head>
10. <body>
11. <h1>
12. <xsl:value-of select="firstname"/>
13. <xsl:text> </xsl:text>
14. <xsl:value-of select="lastname"/>
15. </h1>
16. </body>
17. </html>
18. </xsl:template>
19. </xsl:stylesheet>

Code Explanation

If you open XsltBasics/Demos/NameHTML.xml in a browser, you’ll see the following result.

88 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
EVALUATION COPY: Not to be used in class.

5.5. Elements and Attributes


When outputting XML or HTML, you will need to output tags and attributes. We saw earlier that
you can output tags using literal result elements.

5.5.1. xsl:element
The xsl:element tag can be used to explicitly specify that an element is for output. You will notice
that the xsl:element tag takes the name attribute, which is used to specify the name of the element
being output.

Also, as shown, xsl:element tags can be nested within each other.

LESSON 5: XSLT Basics | 89


EVALUATION COPY: Not to be used in class.
Demo 5.17: XsltBasics/Demos/Name2.xsl
1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="xml" indent="yes" version="1.0"/>
5. <xsl:template match="name">
6. <xsl:element name="Matches">
7. <xsl:element name="Match">We found a name!</xsl:element>
8. <xsl:element name="Name">
9. <xsl:value-of select="."/>
10. </xsl:element>
11. </xsl:element>
12. </xsl:template>
13. </xsl:stylesheet>

When XsltBasics/Demos/Name2.xml, which has the same XML as XsltBasics/Demos/Name.xml,


is transformed against XsltBasics/Demos/Name2.xsl, the output looks the same as we saw when
we used literal result elements earlier in the lesson.

The xsl:element tag is most useful when the tag name itself is generated. If the tag name is not
generated, it is generally easier to use literal result elements.

5.5.2. xsl:attribute
The xsl:attribute element is similar to the xsl:element element. It is used to explicitly output
an attribute. However, it is more commonly used than xsl:element. To see why, let’s first look at
an example in which an attribute is output literally.

Demo 5.18: XsltBasics/Demos/NameLREwithAtt.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="xml" indent="yes" version="1.0"/>
5. <xsl:template match="name">
6. <Matches>
7. <Match Name="Some Name">We found a name!</Match>
8. </Matches>
9. </xsl:template>
10. </xsl:stylesheet>

90 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.
Code Explanation

Notice that the value of the Name attribute is just “Some Name”. We would like to generate this value
from the source XML document by doing something like this:

<!--THIS IS POORLY FORMED-->


<Match Name="<xsl:value-of select='.'/>">
We found a name!
</Match>

Demo 5.19: XsltBasics/Demos/NameWithAtt.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="xml" indent="yes" version="1.0"/>
5. <xsl:template match="name">
6. <Matches>
7. <Match>
8. <xsl:attribute name="Name">
9. <xsl:value-of select="firstname"/>
10. <xsl:text> </xsl:text>
11. <xsl:value-of select="lastname"/>
12. </xsl:attribute>We found a name!</Match>
13. </Matches>
14. </xsl:template>
15. </xsl:stylesheet>

Code Explanation

The xsl:attribute tag applies to the element that it is nested within; in this case, the Match element.
When XsltBasics/Demos/NameWithAtt.xml, which has the same XML as XSLTBasics/De
mos/Name.xml, is transformed against XsltBasics/Demos/NameWithAtt.xsl, the output looks like
this:

Demo 5.20: XsltBasics/Demos/NameWithAttResult.xml


1. <?xml version="1.0" encoding="UTF-8"?>
2. <Matches>
3. <Match Name="Paul McCartney">We found a name!</Match>
4. </Matches>

LESSON 5: XSLT Basics | 91


EVALUATION COPY: Not to be used in class.
5.5.3. Attributes and Curly Brackets
Because using xsl:attribute can be a bit laborious, an abbreviated syntax is available. An XPath
within curly brackets ({}) can be embedded as the value of an attribute in the open tag. The following
example illustrates this.

Demo 5.21: XsltBasics/Demos/NameWithAttAbbr.xsl


1. <?xml version="1.0" encoding="UTF-8"?>
2. <xsl:stylesheet version="1.0"
3. xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
4. <xsl:output method="xml" indent="yes" version="1.0"/>
5. <xsl:template match="name">
6. <Matches>
7. <Match Name="{firstname} {lastname}">We found a name!</Match>
8. </Matches>
9. </xsl:template>
10. </xsl:stylesheet>

Code Explanation

This will have the exact same result as XsltBasics/Demos/NameWithAtt.xsl, but it is much quicker
and easier to write. To see the result, transform XsltBasics/Demos/NameWithAttAbbr.xml against
this XSLT.

Conclusion
In this lesson, you have learned the basics of creating XSLTs to transform XML instances into text,
HTML and other XML structures. You are now ready to learn more advanced features of XSLT.

92 | LESSON 5: XSLT Basics


EVALUATION COPY: Not to be used in class.

You might also like