Table of Contents

  Part 1: XML
Writing XML
  Part 2: DTDs
Creating a DTD
Defining Elements and Attributes in a DTD
Entities and Notations in DTDs
  Part 3: XML Schema and Namespaces
XML Schema
Defining Simple Types
Defining Complex Types
Using Namespaces in XML
Namespaces, Schemas, and Validation
  Part 4: XSLT and XPath
XPath: Patterns and Expressions
Test Expressions and Functions
  Part 5: Cascading Style Sheets
Setting up CSS
Layout with CSS
Formatting Text with CSS
  Part 6: XLink and XPointer
Links and Images: XLink and XPointer
XML Tools
Special Symbols
Colors in Hex

Q & A
Buy this book!
Cookwood Press


Here is a simple set of tables that give the name and location of each XML file included in the collection of example files for XML for the World Wide Web: Visual QuickStart Guide. Instead of just giving you the snippets of code that are shown in the illustrations (which would be of limited use), I have fleshed out those examples and commented them (within the files themselves) so that you can get more out of them, tinker with them, test them, and ultimately, see how they work. I hope they are worthwhile. Comments are welcome.

You can download all the examples for Macintosh or for Windows/Unix.

If you have any questions, suggestions, or corrections, please contact me.


The files from this chapter are in the introduction folder.

Page Section Code
12 The Problem wth HTML
13 The Power of XML
15 XML in the Real World
16 About This Book

Part 1: XML

Chapter 1, Writing XML

The files from this chapter are in the writing folder.

Page Section Code
23 Rules for Writing XML (note)
24 Declaring the XML Version version.xml
25 Creating the Root Element root.xml
26 Writing Non-Empty Elements nonempty.xml
27 Nesting Elements


28 Adding Attributes


29 Using Empty Elements empty.xml
30 Writing Comments comments.xml
31 Writing Five Special Symbols specialsymbols.xml
32 Displaying Elements as Text cdata.xml

Part 2: DTDs

Chapter 2, Creating a DTD

The files from this chapter are in the dtd_creating folder.

Page Section Code
36 Declaring an Internal DTD

with a real DTD: internal_real.xml

37 Writing an External DTD end_species.dtd

Declaring a Personal External DTD

Figure 2.4: external.xml
Figure 2.5: external_and_internal.xml

40 Declaring a Public External DTD external_public.xml

Chapter 3, Defining Elements and Attributes in a DTD

The files in this section are in the dtd_defining folder. You'll note that these files contain more than what is shown in the illustrations. I have fleshed out these examples so that you can really test them against an XML Validator. In fact, I heartily recommend doing so. Copy them to your server, play around with them, and then study the error messages that XML Validator gives you! I hope you find them useful.

Page Section Code
44 Defining an Element to Contain Only Text onlytext.dtd, onlytext.xml
45 Defining an Element to Contain One Child onechild.dtd, onechild.xml
46 Defining an Element to Contain a Sequence sequence.dtd, sequence.xml
47 Defining Choices

Figure 3.11: choices.xml, choices2.xml
Figure 3.12: choices_invalid.xml, choices_invalid2.xml

48 Defining How Many Units

howmany.dtd, howmany2.dtd

50 - 51 Defining Simple Attributes

Figure 3.16: att_optional.dtd
Figure 3.17: attribute_optional.xml, attribute_optional2.xml, attribute_optional3.xml

Figure 3:18: att_required.dtd
Figure 3:19: att_required_choice1.xml, att_required_choice2.xml, att_required_choice3.xml

Figure 3.20: att_default.dtd
Figure 3.21:att_default1.xml, att_default2.xml, att_default3.xml

Figure 3.22: att_fixed.dtd
Figure 3.23: att_fixed1.xml, att_fixed2.xml, att_fixed3.xml

52 Defining Attributes with Unique Values att_unique.dtd
Figure 3.25: att_unique1.xml (top), att_unique2.xml (bottom)
53 Referencing Attributes with Unique Values

Figures 3.26 and 3.28: idref.dtd
Figure 3.27: idref.xml
Figure 3.29: idrefs.xml

54 Restricting Attributes to Valid XML Names

nmtoken.xml, nmtoken2.xml

Chapter 4, Entities and Notations in DTDs

The files in this section are in the dtd_entities folder.

Page Section HTML
56 Creating Shortcuts for Text


57 Using Shortcuts for Text shortcuts.xml
58 Shortcuts for Text in External Files Figure 4.5: hwi.ent
Figure 4.6: shortcuts_ext.dtd
Figure 4.7: shortcuts_ext.xml
60 Creating and Using Shortcuts for DTDs

Figure 4.9: pic.dtd
Figure 4.10: dtdshortcut_used.dtd
Figure 4.11: dtdshortcut_used.dtd (same as 4.10, just focusing on a different part of it)
(Figure 4.12 does not ever exist as a file, it's just a "dramatization"
of what happens when you use a shortcut for a chunk of DTD.)

62 Creating Entites for Unparsed Content Figure 4.13: tiger.jpg
Figure 4.14, 4.15: unparsed.dtd
63 Embedding Unparsed Content

Figure 4.16 and top part of Figure 4.17: unparsed.dtd (same as Figs. 4.14 and 4.15)
Bottom part of Figure 4.17: unparsed.xml

Part 3: XML Schema and Namespaces

Note that the examples in these chapters are almost all based on the end_species.xsd and end_species.xml files, with minor changes, according to the topic in question. More importantly, I have gone to the trouble (over two excrutiating months) to flesh out the examples so that you can really play around with them, send them to XSV for validation and see how they work. Please do copy them to your own site, and then test them and retest them and analyze them. Pay special attention to the comments within the documents, in which I explain which parts are in question and what is going on with them. I hope you find them useful. Let me know.

Chapter 5, XML Schema

The files in this section are in the schemas folder.

Page Section Code
72 Beginning a Simple Schema begin.xsd
73 Indicating a Simple Schema's Location location.xml
74 Annotating Schemas annotation.xsd

Chapter 6, Defining Simple Types

The files in this section are in the schemas_simple folder.

Page Section Code
76-77 Declaring an Element with a Simple Type

Figure 6.1: simpletype.xsd
Figure 6.2: simpletype1.xml
Figure 6.3: simpletype2.xml

Figure 6.4:simpletype_date.xsd
Figure 6.5: simpletype_date.xml
Figure 6.6: simpletype_date2.xml

78-79 Using Date and Time Types

Figure 6.7: timeDuration.xsd, timeDuration.xml

Figure 6.8: time.xsd, time.xml

Figure 6.9: timeInstant.xsd, timeInstant.xml

Figure 6.10: date.xsd, date.xml

Figure 6.11: month.xsd, month.xml

Figure 6.12: year.xsd, year.xml

Figure 6.13: century.xsd, century.xml

Figure 6.14: recurringDate.xsd, recurringDate.xml

Figure 6.15: recurringDay.xsd, recurringDay.xml

80 Using Number Types numbers.xsd
Figure 6.17: numbers.xml
Figure 6.18: numbers2.xml
81 Deriving Custom Simple Types custom_simple.xsd, custom_simple.xml
82 Using Anonymous Custom Types anonymous_simple.xsd, anonymous_simple.xml
83 Specifying a Set of Acceptable Values enumeration.xsd, enumeration.xml
84-85 Specifying a Pattern for a Simple Type

Figure 6.27: pattern.xsd
Figure 6.28: pattern.xml

Figure 6.29: timeDuration_andpattern.xsd
Figure 6.30: timeDuration_andpattern.xml

Figure 6.31: pattern_choices.xsd
Figure 6.32: pattern_choices.xml

86-87 Specifying a Range of Acceptable Values

Figure 6.33: maxInclusive.xsd
Figure 6.34: maxInclusive.xml

Figure 6.35: maxExclusive.xsd
Figure 6.36: maxExclusive.xml

Figure 6.37: minInclusive.xsd
Figure 6.38: minInclusive.xml, minInclusive2.xml

Figure 6.39: minExclusive.xsd
Figure 6.40: minExclusive.xml, minExclusive2.xml

88 Limiting the Length of a Simple Type

Figures 6:41 and 6:42: length.xsd, length.xml

Figure 6:43: maxLength.xsd
Figure 6.44: maxLength.xml
Figure 6.45: maxLength2.xml

89 Limiting a Number's Digits

Figure 6.47: precision_scale.xml, precision_scale2.xml
Figure 6.48: precision_scale3.xml, precision_scale4.xml

90 Creating List Types

Figures 6.49 and 6.50: list.xsd, list.xml

Figures 6.51 and 6.52: list_length.xsd, list_length.xml

91 Predefining an Element's Content

Figure 6.53: fixed.xsd
Figures 6.54 and 6.55: fixed.xml, fixed2.xml

Figure 6.56: default.xsd
Figures 6.57 and 6.58: default.xml, default2.xml

Chapter 7, Defining Complex Types

The files in this section are in the schemas_complex folder.

Page Section Code
94 Defining Elements to Contain Only Elements onlyelements.xsd (onlyelements.xml)
95 Requiring Elements to Appear in Sequence sequence.xsd (sequence.xml)
96 Creating a Set of Choices choice.xsd
Figure 7.8: top: choice.xml
bottom: choice2.xml
97 Allowing Elements to Appear in Any Order all.xsd, all.xml
98 Defining Named Groups group.xsd
99 Referencing a Named Group group.xml
100 Referencing Already Defined Elements referencing.xsd (referencing.xml)
101 Controlling How Many

howmany.xsd, howmany.xml

Figure 7.18: howmany2.xsd (howmany2.xml)

102 Defining Elements to Contain Only Text

Figures 7.19 and 7.20: textonly.xsd
Figure 7.21: textonly.xml
Figure 7.22: textonly2.xml

103 Defining Empty Elements Figures 7.23 and 7.24: empty.xsd
Figure 7.25: empty.xml
104 Defining Elements with Mixed Content Figures 7.26 and 7.27: mixed.xsd
Figure 7.28: mixed.xml
105 Basing Complex Types on Complex Types Figures 7.29, 7.30, 7.31: basing.xsd
Figure 7.32: basing.xml
106 Declaring an Element of Complex Type

Figure 7.33: basing.xsd

Figure 7.34: declaring.xsd (declaring.xml)
Figure 7.35: declaring2.xsd (declaring2.xml)

107 Elements with Anonymous Complex Types

Figure 7.36: anonymous.xsd
Figure 7.37: anonymous.xml

108 Declaring Attributes attributes.xsd, attributes.xml
109 Requiring an Attribute

Figure 7.40: required.xsd

Figure 7.41: top: required.xml; bottom: required2.xml

required3.xml (an invalid instance, not shown in the book)

110 Predefining an Attribute¹s Content

Figure 7.42: fixed.xsd
Figure 7.43: fixed.xml
Figure 7.44: fixed2.xml

Figure 7.44: default.xsd
Figure 7.45: default.xml
Figure 7.46: default2.xml

111 Defining Attribute Groups attgroups.xsd
112 Referencing Attribute Groups

attgroups.xsd, attgroups.xml

Chapter 8, Using Namespaces in XML

The files in this section are in the namespaces folder. Note that no Schema files are given for the latter examples...we don't get to validating XML files that use namespaces until the next chapter. If there is sufficient interest, I could work out the appropriate schema files. Let me know.

Page Section Code
113 Using Namespaces in XML

Figure 8.1: rivers.xsd (rivers.xml)

Figure 8.2: global_endspecies.xsd (global_endspecies.xml)

115 Declaring Default Namespaces

Figure 8.4: default1.xml

Figure 8.5: default2.xml

116-117 Namespaces for Individual Elements

Figures 8.6 and 8.7: individual.xml

Figure 8.8: individual2.xml

Chapter 9, Namespaces, Schemas, and Validation

The files in this section are in the namespaces_sv folder.

Page Section Code
123 Populating a Namespace

populating.xsd (populating.xml, if you insist)

124 Adding All Locally Declared Elements


125 Adding Particular Locally Declared Elements


126-127 Referencing Components with Namespaces

Figure 9.4: referencing_namespaces.xsd
(referencing_namespaces.xml, for the impatient)

Figure 9.5: referencing_namespaces2.xsd
(referencing_namespaces2.xml, for the impatient)

128 The Schema of Schemas as the Default referencing_namespaces3.xsd (referencing_namespaces3.xml, for the impatient)
129 Namespaces and Validating XML validating.xml (you can validate this file with referencing_namespaces.xsd if you like by typing in both URLs--first the xml then the xsd--in the XSV)
130 Indicating Where a Schema Is location.xml (referencing_namespaces.xsd)
131 Schemas in Multiple Files threats.xsd, include.xsd (and then use include.xml as an instance)
132 Importing Components import.xsd, rivers_import.xsd (not rivers.xsd), and then import.xml as an instance. Note the errata resolved in the notes here.

Part 4: XSLT and XPath

Note that the examples in these chapters are almost all based on the tigers_rhinos.xml and tigers_complete.xsl files. The XSLT files gradually build up to the tigers_complete.xsl file, with minor changes depending on the topic in question. For maximum effect, you'll also need the tiger.jpg and rhino.jpg images.

Chapter 10, XSLT

The files in this section are in the xslt folder. Note that all of these XSLT files transform the very same tigers_rhinos.xml file. The HTML files shown were generated by transforming the tigers_rhinos.xml file with the given XSLT file, using Saxon.

Page Section Code
136-137 Transforming XML with XSLT Figure 10.1: tigers_rhinos.xml
Figure 10.3: tigers_complete.xsl
138 Beginning an XSLT Style Sheet begin.xsl
139 Creating the Root Template root.xsl, root.xml
140-141 Outputting HTML Code html.xsl, html.html
142-143 Outputting a Node's Content node.xsl, node.html
144-145 Creating and Applying Template Rules templates.xsl, templates.html
146-147 Batch-Processing Nodes for-each.xsl, for-each.html
148 Processing Nodes Conditionally if.xsl, if.html
149 Adding Additional Choices choose.xsl, choose.html
150 Sorting Nodes Before Processing sort.xsl, sort.html
151 Generating Attributes attributes.xsl, attributes.html

Chapter 11, XPath: Patterns and Expressions

The files in this section are in the xpath folder. Note that many of the examples in this chapter use the same files as examples from the XSLT chapter. That's because we've already seen some of these patterns and expressions, but they are explained and highlighted here. Note also that the HTML files shown were generated by transforming the tigers_rhinos.xml file with the given XSLT file, using Saxon.

Page Section Code
154 Determining the Current Node same as sort.xsl, sort.html (from page 150 in Chapter 10)
155 Referring to the Current Node same as sort.xsl, sort.html (from page 150 in Chapter 10)
156 Selecting a Node's Children same as sort.xsl, sort.html (from page 150 in Chapter 10)
157 Selecting a Node's Parent or Siblings siblings.xsl, siblings.html
158 Selecting All of the Descendants descend.xsl, descend.html
159 Disregarding the Current Node disregardnode.xsl, disregardnode.html
160 Selecting a Node's Attributes same as siblings.xsl, siblings.html
161 Selecting Subsets same as siblings.xsl, siblings.html

Chapter 12, Test Expressions and Functions

The files in this section are in the expressions folder. Note that the first seven HTML files (pages 164-170) shown were generated by transforming the tigers_rhinos.xml file with the given XSLT file, using Saxon. The examples from pages 171-173 are generated by transforming the tigers_rhinos_noinitials.xml file with the given XSLT file, also using Saxon. Also note that up until page 169, for simplicity's sake, I did not include the picture template. It appears again from page 170 on. You can find more information about the picture template in the Generating Attributes section on page 151 in Chapter 10.

Page Section Code
164 Comparing Two Values same as choose.xsl, choose.html (from page 149, Chapter 10)
165 Testing the Position position.xsl, position.html
166 Subtotaling Values sum.xsl, sum.html
167 Counting Nodes count.xsl, count.html
168 Multiplying, Dividing, Adding, Subtracting multiply.xsl, multiply.html
169 Formatting Numbers formatnumber.xsl, formatnumber.html
170 Rounding Numbers ceiling.xsl, ceiling.html (again, notice that the picture template from page 151 is used here--for the first time in this chapter)
171-172 Extracting Substrings

Figure 12.19: tigers_rhinos_noinitials.xml (All the other XSLT files refer to the tigers_rhinos.xml file.)

Figures 12.20 and 12.21: substring.xsl, substring.html

Figures 12.22 and 12.23: substring_after.xsl, substring_after.html

173 Capitalizing Strings translate.xsl, translate.html

Part 5: Cascading Style Sheets

Note that the examples in these chapters are almost all based on the panthers.xml and panthersblurb.xml files. I have created a separate xml file for each example that references the proper CSS stylesheet, but unless noted, all the XML files are otherwise identical. The CSS files build on each other gradually.

Chapter 13, Setting up CSS

The files in this section are in the css folder.

Page Section Code
177 Setting up CSS

Figure 13.1: panthers.xml
Figure 13.2: panthers.html

178 CSS with XML vs. CSS with HTML Figure 13.3: panthers.xml
Figure 13.4: panthers.html
180-181 Specifying Where Styles Are To Be Applied

Figure 13.6: where1.css
Figure 13.7: where1.xml

Figure 13.7: where2.css
Figure 13.8: where2.xml

182-183 Creating an External Style Sheet external.css
184-185 Calling a Style Sheet for an XML Document

Figure 13.11: call_stylesheet.xml (end_species.css)

Figure 13.12: call_stylesheet_xml.xsl is the document shown. It is used to transform panthers.xml into call_stylesheet_xml.xml which calls end_species.css for formatting information.

186 Calling a Style Sheet for an HTML Document callstyles_html.xsl, (tigers_rhinos.xml, callstyles_html.html, callstyles.css)
187 Using Internal Style Sheets callstyles_html_internal.xsl
188 Applying Styles Locally local_styles.xsl (local_styles.html)

Chapter 14, Layout with CSS

The files in this section are in the css_layout folder. The XML files formatted by the CSS examples on pages 190-197 are identical to panthers.xml, except for the call to the individual stylesheet. The XML files used from pages 198-205 are identical to panthersblurb.xml--except for the call to the individual stylesheet--(which only differs from panthers.xml in the addition of an extra sidebar (blurb) paragraph.) Finally, the examples in the last two sections (pages 206-9) are identical to the pantherlist.xml file--again, except for the call to the individual stylesheet.

Page Section Code
190 Defining Elements as Block-Level or Inline

block.css (block.xml)

191 Hiding Elements Completely hide.css (hide.xml)
192 Offsetting Elements In the Natural Flow relative.css (relative.xml)
193 Positioning Elements Absolutely absolute.css (absolute.xml)
194 Setting the Height or Width for an Element heightwidth.css (heightwidth.xml)
195 Setting the Border border.css (border.xml)
196 Adding Padding Around an Element padding.css (padding.xml)
197 Setting the Margins around an Element margin.css (margin.xml)
198 Wrapping Text around Elements float.css (float.xml)
199 Stopping Text Wrap clear.css (clear.xml)
200 Changing the Foreground Color foreground.css (foreground.xml)
201 Changing the Background background.css (background.xml)
202 Positioning Elements in 3D zindex.css (zindex.xml)
203 Aligning Elements Vertically verticalalign.css (verticalalign.xml)
204 Determining Where Overflow Should Go overflow.css (overflow.xml)
205 Clipping an Element clip.css (clip.xml)
206-207 Setting List Properties lists.css (pantherlist.xml)
208 Specifying Page Breaks pagebreak.css (pagebreak.xml)

Chapter 15, Formatting Text with CSS

The files in this section are in the css_text folder. All of the XML files are identical to panthers_layout.xml, except for the call to the individual stylesheet. Indeed panthers_layout.xml is simply the same panthersblurb.xml file we saw in the last chapter with a call to the layout.css stylesheet which includes all the layout information we added in Chapter 14. Note also that you'll need the embedded font files for all the examples from page 211 on, and that the embedded fonts will only be visible in IE5 for Windows.

Page Section Code
210 Choosing a Font Family

font-family.css (font-family.xml)

211 Embedding Fonts on a Page embedfonts.css (embedfonts.xml)
212 Creating Italics italic.css (italic.xml)
213 Applying Bold Formatting bold.css (bold.xml)
214 Setting the Font Size font-size.css (font-size.xml)
215 Setting the Line Height line-height.css (line-height.xml)
216 Setting All Font Values at Once font.css (font.xml)
217 Setting the Text Color color.css (color.xml)
218 Changing the Text¹s Background background.css (background.xml)
219 Controlling Spacing spacing.css (spacing.xml)
220 Aligning Text text-align.css (text-align.xml)
221 Underlining Text underline.css (underline.xml)
222 Changing the Text Case transform.css (transform.xml)

Part 6: XLink and XPointer

Chapter 16, XLink and XPointer

Since none of the examples in this chapter can really be tested yet (except for well-formedness, but that's pretty boring), I haven't taken the trouble of posting the code. If you really want a copy of it, just let me know. I'd be happy to give it to you (or post it, if a lot of you want it). Once browsers can deal with XLink and XPointer, I will make these examples more real and then post the code so you can see how they work for yourselves. Comments?


Appendix A: XHTML

You can find these files in the xhtml folder.

Page Section Code
240 How Does a Browser Know? Figures A.1 and A.3: xhtml.xml
Figure A.2:xhtml.html
241 Writing XHTML xhtml.xml
242 Declaring a DTD for XHTM xhtml_dtd.xml

Appendix B: XML Tools

These are the most up-to-date links to the tools listed.

Page Section Code
244 Validating XML Files against a DTD Brown University's Scholarly Technology Group's XML Validator
245 Validating XML with a Schema XML Schema Validator from the W3C
246 Transforming XML with an XSLT Processor

Michael Kay's Saxon

J. David Eisenberg's MacTransform

Appendices C and D: Special Symbols and Colors in Hex

You can find links to these tables in the Extras section of my HTML4 Web site. Click on Useful Files to find the appropriate table.


You can find the complete index online!

©Copyright 2000 by Elizabeth Castro. All rights reserved.
Please don't copy this page. Instead, link to it! Thanks.