- Sams Teach Yourself XML in 21 Days, Third Edition
- Table of Contents
- About the Author
- Acknowledgments
- We Want to Hear from You!
- Introduction
- Part I: At a Glance
- Day 1. Welcome to XML
- All About Markup Languages
- All About XML
- Looking at XML in a Browser
- Working with XML Data Yourself
- Structuring Your Data
- Creating Well-Formed XML Documents
- Creating Valid XML Documents
- How XML Is Used in the Real World
- Online XML Resources
- Summary
- Q&A
- Workshop
- Day 2. Creating XML Documents
- Choosing an XML Editor
- Using XML Browsers
- Using XML Validators
- Creating XML Documents Piece by Piece
- Creating Prologs
- Creating an XML Declaration
- Creating XML Comments
- Creating Processing Instructions
- Creating Tags and Elements
- Creating CDATA Sections
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 3. Creating Well-Formed XML Documents
- What Makes an XML Document Well-Formed?
- Creating an Example XML Document
- Understanding the Well-Formedness Constraints
- Using XML Namespaces
- Understanding XML Infosets
- Understanding Canonical XML
- Summary
- Q&A
- Workshop
- Day 4. Creating Valid XML Documents: DTDs
- All About DTDs
- Validating a Document by Using a DTD
- Creating Element Content Models
- Commenting a DTD
- Supporting External DTDs
- Handling Namespaces in DTDs
- Summary
- Q&A
- Workshop
- Declaring Attributes in DTDs
- Day 5. Handling Attributes and Entities in DTDs
- Specifying Default Values
- Specifying Attribute Types
- Handling Entities
- Summary
- Q&A
- Workshop
- Day 6. Creating Valid XML Documents: XML Schemas
- Using XML Schema Tools
- Creating XML Schemas
- Dissecting an XML Schema
- The Built-in XML Schema Elements
- Creating Elements and Types
- Specifying a Number of Elements
- Specifying Element Default Values
- Creating Attributes
- Summary
- Q&A
- Workshop
- Day 7. Creating Types in XML Schemas
- Restricting Simple Types by Using XML Schema Facets
- Creating XML Schema Choices
- Using Anonymous Type Definitions
- Declaring Empty Elements
- Declaring Mixed-Content Elements
- Grouping Elements Together
- Grouping Attributes Together
- Declaring all Groups
- Handling Namespaces in Schemas
- Annotating an XML Schema
- Summary
- Q&A
- Workshop
- Part I. In Review
- Well-Formed Documents
- Valid Documents
- Part II: At a Glance
- Day 8. Formatting XML by Using Cascading Style Sheets
- Our Sample XML Document
- Introducing CSS
- Connecting CSS Style Sheets and XML Documents
- Creating Style Sheet Selectors
- Using Inline Styles
- Creating Style Rule Specifications in Style Sheets
- Summary
- Q&A
- Workshop
- Day 9. Formatting XML by Using XSLT
- Introducing XSLT
- Transforming XML by Using XSLT
- Writing XSLT Style Sheets
- Using <xsl:apply-templates>
- Using <xsl:value-of> and <xsl:for-each>
- Matching Nodes by Using the match Attribute
- Working with the select Attribute and XPath
- Using <xsl:copy>
- Using <xsl:if>
- Using <xsl:choose>
- Specifying the Output Document Type
- Summary
- Q&A
- Workshop
- Day 10. Working with XSL Formatting Objects
- Introducing XSL-FO
- Using XSL-FO
- Using XSL Formatting Objects and Properties
- Building an XSL-FO Document
- Handling Inline Formatting
- Formatting Lists
- Formatting Tables
- Summary
- Q&A
- Workshop
- Part II. In Review
- Using CSS
- Using XSLT
- Using XSL-FO
- Part III: At a Glance
- Day 11. Extending HTML with XHTML
- Why XHTML?
- Writing XHTML Documents
- Validating XHTML Documents
- The Basic XHTML Elements
- Organizing Text
- Formatting Text
- Selecting Fonts: <font>
- Comments: <!-->
- Summary
- Q&A
- Workshop
- Day 12. Putting XHTML to Work
- Creating Hyperlinks: <a>
- Linking to Other Documents: <link>
- Handling Images: <img>
- Creating Frame Documents: <frameset>
- Creating Frames: <frame>
- Creating Embedded Style Sheets: <style>
- Formatting Tables: <table>
- Creating Table Rows: <tr>
- Formatting Table Headers: <th>
- Formatting Table Data: <td>
- Extending XHTML
- Summary
- Q&A
- Workshop
- Day 13. Creating Graphics and Multimedia: SVG and SMIL
- Introducing SVG
- Creating an SVG Document
- Creating Rectangles
- Adobe's SVG Viewer
- Using CSS Styles
- Creating Circles
- Creating Ellipses
- Creating Lines
- Creating Polylines
- Creating Polygons
- Creating Text
- Creating Gradients
- Creating Paths
- Creating Text Paths
- Creating Groups and Transformations
- Creating Animation
- Creating Links
- Creating Scripts
- Embedding SVG in HTML
- Introducing SMIL
- Summary
- Q&A
- Workshop
- Day 14. Handling XLinks, XPointers, and XForms
- Introducing XLinks
- Beyond Simple XLinks
- Introducing XPointers
- Introducing XBase
- Introducing XForms
- Summary
- Workshop
- Part III. In Review
- Part IV: At a Glance
- Day 15. Using JavaScript and XML
- Introducing the W3C DOM
- Introducing the DOM Objects
- Working with the XML DOM in JavaScript
- Searching for Elements by Name
- Reading Attribute Values
- Getting All XML Data from a Document
- Validating XML Documents by Using DTDs
- Summary
- Q&A
- Workshop
- Day 16. Using Java and .NET: DOM
- Using Java to Read XML Data
- Finding Elements by Name
- Creating an XML Browser by Using Java
- Navigating Through XML Documents
- Writing XML by Using Java
- Summary
- Q&A
- Workshop
- Day 17. Using Java and .NET: SAX
- An Overview of SAX
- Using SAX
- Using SAX to Find Elements by Name
- Creating an XML Browser by Using Java and SAX
- Navigating Through XML Documents by Using SAX
- Writing XML by Using Java and SAX
- Summary
- Q&A
- Workshop
- Day 18. Working with SOAP and RDF
- Introducing SOAP
- A SOAP Example in .NET
- A SOAP Example in Java
- Introducing RDF
- Summary
- Q&A
- Workshop
- Part IV. In Review
- Part V: At a Glance
- Day 19. Handling XML Data Binding
- Introducing DSOs
- Binding HTML Elements to HTML Data
- Binding HTML Elements to XML Data
- Binding HTML Tables to XML Data
- Accessing Individual Data Fields
- Binding HTML Elements to XML Data by Using the XML DSO
- Binding HTML Tables to XML Data by Using the XML DSO
- Searching XML Data by Using a DSO and JavaScript
- Handling Hierarchical XML Data
- Summary
- Q&A
- Workshop
- Day 20. Working with XML and Databases
- XML, Databases, and ASP
- Storing Databases as XML
- Using XPath with a Database
- Introducing XQuery
- Summary
- Q&A
- Workshop
- Day 21. Handling XML in .NET
- Creating and Editing an XML Document in .NET
- From XML to Databases and Back
- Reading and Writing XML in .NET Code
- Using XML Controls to Display Formatted XML
- Creating XML Web Services
- Summary
- Q&A
- Workshop
- Part V. In Review
- Appendix A. Quiz Answers
- Quiz Answers for Day 1
- Quiz Answers for Day 2
- Quiz Answers for Day 3
- Quiz Answers for Day 4
- Quiz Answers for Day 5
- Quiz Answers for Day 6
- Quiz Answers for Day 7
- Quiz Answers for Day 8
- Quiz Answers for Day 9
- Quiz Answers for Day 10
- Quiz Answers for Day 11
- Quiz Answers for Day 12
- Quiz Answers for Day 13
- Quiz Answers for Day 14
- Quiz Answers for Day 15
- Quiz Answers for Day 16
- Quiz Answers for Day 17
- Quiz Answers for Day 18
- Quiz Answers for Day 19
- Quiz Answers for Day 20
- Quiz Answers for Day 21
Creating Elements and Types
To declare elements, you use the <xsd:element> element in XML schemas. When you declare an element, you can specify its type. For example, the element named <comment> in the sample XML document you've been working with just contains text:
<comment>Good risk</comment>
You can declare the <comment> element by using <xsd:element> and giving it the name "comment" and the type xsd:string, like this:
<xsd:element name="comment" type="xsd:string"/>
That was easy enough. In this case, you could use a type that is built into XML schemas, xsd:string. However, you can also define our own types. In fact, there are two types that you can create—simple and complex types.
To create simple types, you use the <xsd:simpleType> element, and to create complex types, you use the <xsd:complexType> element. After you create our own types, you can declare elements by using those types.
Elements that enclose child elements or have attributes are complex types. Elements that enclose only simple data, such as numbers, strings, or dates, but do not have any child elements are simple types. In other words, complex types can have internal structure, such as child elements and attributes, but simple types cannot. For example, attributes are always simple types because attribute values cannot have any internal structure (such as child elements).
The distinction between simple and complex types is an important one because you declare simple and complex types differently. You declare complex types ourselves, and the XML schema specification comes with many simple types already declared, as you'll see. You can also declare our own simple types, and you'll see how to do that as well.
Using Simple Types
You've already seen that you can use built-in XML schema types when declaring elements, like this:
<xsd:element name="comment" type="xsd:string"/>
Table 6.3 lists the built-in simple types for XML schemas. Note that if you use the xsd namespace prefix in an XML schema, as is usual, you reference the simple types in the schema as xsd:anyURI, xsd:base64Binary, and so on. Probably the most-used built-in simple type is xsd:string, which is simply a string of text. Other common types are xsd:int for integers, and xsd:date for dates. Note also how detailed these types are compared to DTDs; remember that with DTDs, you can't define data types.
Table 6.3. Simple Types Built into XML Schema
Simple Type |
Examples |
anyURI |
http://www.xmlpowercorp.com |
base64Binary |
GpM6 |
boolean |
true, false, 1, 0 |
byte |
-1, 200 |
date |
2005-03-02 |
dateTime |
2005-03-02T10:14:00.000-05:00 |
decimal |
1.23456, -1.23456, 200000.00 |
double |
12345, 12.345E-6, 3.1415926 |
duration |
P2Y1M3DT10H40M21.7S |
ENTITIES |
(XML entities) |
ENTITY |
(An XML entity) |
float |
12345, 12.345E-6, 3.1415926 |
gDay |
---31 |
gMonth |
--02-- |
gMonthDay |
--04-31 |
gYear |
2005 |
gYearMonth |
2005-07 |
hexBinary |
0EE6 |
ID |
(XML ID) |
IDREF |
(XML ID REF) |
IDREFS |
(XML ID REFS) |
int |
1, 2, -3, 7654321 |
integer |
-12345, -100, 1000 |
language |
en-US, de, fr, jp |
long |
-1234, 12345678901234 |
Name |
Edward |
NCName |
Thomas |
negativeInteger |
-1, -12, -12345 |
NMTOKEN |
CA |
NMTOKENS |
CA NJ, PA NY MA |
nonNegativeInteger |
0, 1, 2, 12345 |
nonPositiveInteger |
-12, -33, 0 |
normalizedString |
Welcome to XML Schemas |
NOTATION |
(XML NOTATION) |
positiveInteger |
1, 2, 3, 123456 |
QName |
xml21:Name |
short |
-1, 2, 245 |
string |
Welcome to XML Schemas |
time |
10:22:00.000 |
token |
Hello |
unsignedByte |
0, 88, 127 |
unsignedInt |
0, 1234 |
unsignedLong |
0, 126789675 |
unsignedShort |
0, 123 |
Usually, when you want to use a simple data type, you use one of the ones listed in Table 6.3. However, you can also use the <xsd:simpleType> element to declare our own simple types, as you're going to see tomorrow. If you can't give simple types child elements or attributes and you have a whole selection of predefined simple types to choose from, as shown in Table 6.3, what's the point of declaring our own simple types? Among other things, declaring our own simple types lets you restrict the values those types can take.
For example, take a look at the <term> element in the mortgage XML document. This element gives the term (that is, the length) of the mortgage in years, and you want that number to be 30 or less, so you can use a <xsd:simpleType> element, enclosing a <xsd:restriction> element, to restrict the possible values that can be used in the <term> element. Inside the <xsd:restriction> element, you can use the <xsd:maxInclusive> element to restrict possible values in the <term> element to 30 years or less, like this:
<xsd:element name="term"> <xsd:simpleType> <xsd:restriction base="xsd:integer"> <xsd:maxInclusive value="30"/> </xsd:restriction> </xsd:simpleType>
You'll hear more about this kind of restriction, as well as create our own simple types, tomorrow. at the following section explores how to create our own complex types, which is a far more common thing to do than creating our own simple types.
Using Complex Types
You create new complex types by using the <xsd:complexType> element in schemas. A complex type definition can contain element declarations, references to other elements, and attribute declarations. You declare elements by using the <xsd:element> element and attributes by using the <xsd:attribute> element. Like DTDs, element declarations specify the syntax of an element; however, in XML schemas, element and attribute declarations can also specify the element or attribute type.
Here's an example. In the XML document, the document element, which is called <document>, contains <comment>, <mortgagee>, <mortgages>, and <bank> elements, in that order:
<document> <comment> . . . </comment> <mortgagee> . . . </mortgagee> <mortgages> . . . </mortgages> <bank> . . . </bank> </document>
The <document> element can contain other elements, so you need to declare it by using a complex type, which you can call documentType in our XML schema. In the XML schema, you can declare the <document> element to be of the documentType type:
<xsd:element name="document" type="documentType"/>
You create the documentType type by using an <xsd:complexType> element. In this case, you want to indicate that the subelements in <document> will be <comment>, <mortgagee>, <mortgages>, and <bank> elements, in that order, so you use the <xsd:sequence> element (tomorrow you'll see how you can use <xsd:all>, which enables elements to appear in any order and in any sequence):
<xsd:element name="document" type="documentType"/> <xsd:complexType name="documentType"> <xsd:sequence> <xsd:element ref="comment" minOccurs="1"/> <xsd:element name="mortgagee" type="recordType"/> <xsd:element name="mortgages" type="mortgagesType"/> <xsd:element name="bank" type="recordType"/> </xsd:sequence> </xsd:complexType>
The other types you see in this declaration—such as recordType, which is the type of the <mortgagee> and <bank> elements—are also complex types. Here's how recordType is declared:
<xsd:complexType name="recordType"> <xsd:sequence> <xsd:element name="name" type="xsd:string"/> <xsd:element name="location" type="xsd:string"/> <xsd:element name="city" type="xsd:string"/> <xsd:element name="state" type="xsd:string"/> </xsd:sequence> </xsd:complexType>
Note that the elements in this declaration, such as <name>, <location>, and others, are all of the simple type, xsd:string, which is built in to the XML schema specification, so this is all you need to do to declare them.
Using the new type recordType, you can create <bank> elements, like this:
<bank phone="888.555.8888"> <name>XML Bank</name> <location>12 Schema Place</location> <city>New York</city> <state>NY</state> </bank>
That's how it works: You create a type in an XML schema and then you can declare elements of that type. If you want to use a complex type, you have to create it, and you do that by using the <xsd:complexType> element.
One way of declaring elements, as you've seen today, is to specify the element's name and type, as in the following example, which declares the <comment> element:
<xsd:element name="comment" type="xsd:string"/>
Now that you've declared <comment>, what if you want to use this element in several places in an XML document? Say, for example, that you want to use <comment> elements in various places throughout a document, like this:
<customer>
<comment>No more credit on this one.</comment> </customer> <supplier> <comment>Always delivers late.</comment> </supplier>
In this case, you can use the ref attribute of the <xsd:element> element to indicate that the element you want to use at a particular location has already been declared. You can see an example in the following XML schema for the <comment> element:
<xsd:complexType name="documentType"> <xsd:sequence> <xsd:element ref="comment" minOccurs="1"/> <xsd:element name="mortgagee" type="recordType"/> <xsd:element name="mortgages" type="mortgagesType"/> <xsd:element name="bank" type="recordType"/> </xsd:sequence> <xsd:attribute name="documentDate" type="xsd:date"/> </xsd:complexType> . . . <xsd:element name="comment" type="xsd:string"/>
Using the ref attribute lets you make use of an element that has already been declared. Note, however, that you can't just include any element by reference—the element you refer to must have been declared globally, which means that it is itself not part of any other complex type. (The other option is to declare elements inside other declarations, which is declaring them locally.) A global element or attribute declaration appears as an immediate child element of the <xsd:schema> element, and when you declare an element or attribute globally, it can be used in any complex type. Using the ref attribute in this way is a powerful technique.
Note also the other attribute here besides ref—minOccurs. This attribute lets you specify how many of a certain element you'll allow at a specific location. The following section takes a look at that.