- 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
Matching Nodes by Using the match Attribute
When you create an XSLT template, you need to specify what you want the template to match, and you do that with the match attribute. Knowing how to match a node or nodes by using this attribute becomes very important, and there's a whole new syntax here. You'll get the details on what values you can assign to the match attribute first because when you know that, you know how to create XSLT templates. Let's start by looking at how to match the root node.
Handling the Root Node
As you've seen today, you can match the root node, which corresponds to the very beginning of the document, by assigning a forward slash (/) to the match attribute in an <xsl:template> element:
<xsl:template match="/"> <xsl:apply-templates/> </xsl:template>
Note that XSLT processors start off at the root node level automatically and start searching from that point, so this template is unnecessary. (In fact, you could say that this template is invoked by default in all XSLT processors.)
Handling Elements
As you've seen today, you can match elements simply by using their names:
<xsl:template match="states"> <HTML> <xsl:apply-templates/> </HTML> </xsl:template>
You can use the / operator to separate element names when you want to refer to a child of a particular node. For example, say you want to create a rule that applies only to <name> elements that are children of <state> elements. In that case, you can match to the expression "state/name". For example, the following rule will surround the text of such elements in a <P> element:
<xsl:template match="state/name"> <P><xsl:value-of select="."/></P> </xsl:template>
Note the expression "." here. You use "." with the select attribute to specify the current node, as you'll see later today, in the section "Working with the select Attribute and XPath."
You can also use * character as a wildcard; it can stand for any element (* can match only elements). For example, you could use the following to match any child element of the node from which you start searching:
<xsl:template match="*">
The following rule applies to all <name> elements that are grandchildren of <state> elements:
<xsl:template match="state/*/name"> <P><xsl:value-of select="."/></P> </xsl:template>
Now you've used "state/name" to match all <name> elements that are direct children of <state> elements, and you've used "state/*/name" to match all <name> elements that are grandchildren of <state> elements. You could also perform both of these matches in an easier way—by simply using the expression "state//name", which matches all <name> elements that are inside <state> elements, no matter how many levels deep (these elements are called descendants of the <state> element). Here's how you do it:
<xsl:template match="states//name"> <P><xsl:value-of select="."/></P> </xsl:template>
Handling Attributes
You can handle attributes very much like you handle elements. All that's different is that you have to preface the attribute name with @. For example, say that you want to recover the value of the units attributes in the <population> and <area> elements of the XML example:
<?xml version="1.0" encoding ="UTF-8"?> <states> <state> <name>California</name> <population units="people">33871648</population><!--2000 census--> <capital>Sacramento</capital> <bird>Quail</bird> <flower>Golden Poppy</flower> <area units="square miles">155959</area> </state> . . .
To get the values of the units attribute, you simply need to refer to it as @units. For example, here's how you might get the population value (using <xsl:value-of select="."/>), insert a space (with the <xsl:text> element, like this: <xsl:text> </xsl:text>), and then add the units for this element (using <xsl:value-of select="@units"/>):
<xsl:template match="population"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="@units"/> </xsl:template>
You can see this at work in Listing 9.10, which reads the data in the XML document and displays it in an HTML table—including the units for various values, as applicable.
Example 9.10. A Sample XSL Style Sheet That Has Multiple Matches (ch09_10.xsl)
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/states"> <HTML> <HEAD> <TITLE> State Data </TITLE> </HEAD> <BODY> <H1> State Data </H1> <TABLE BORDER="1"> <TR> <TD>Name</TD> <TD>Population</TD> <TD>Capital</TD> <TD>Bird</TD> <TD>Flower</TD> <TD>Area</TD> </TR> <xsl:apply-templates/> </TABLE> </BODY> </HTML> </xsl:template> <xsl:template match="state"> <TR> <TD><xsl:value-of select="name"/></TD> <TD><xsl:apply-templates select="population"/></TD> <TD><xsl:apply-templates select="capital"/></TD> <TD><xsl:apply-templates select="bird"/></TD> <TD><xsl:apply-templates select="flower"/></TD> <TD><xsl:apply-templates select="area"/></TD> </TR> </xsl:template> <xsl:template match="population"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="@units"/> </xsl:template> <xsl:template match="capital"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="bird"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="flower"> <xsl:value-of select="."/> </xsl:template> <xsl:template match="area"> <xsl:value-of select="."/> <xsl:text> </xsl:text> <xsl:value-of select="@units"/> </xsl:template> </xsl:stylesheet>
Here's the HTML you get, including the HTML table:
<HTML> <HEAD> <META http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE> State Data </TITLE> </HEAD> <BODY> <H1> State Data </H1> <TABLE BORDER="1"> <TR> <TD>Name</TD> <TD>Population</TD> <TD>Capital</TD> <TD>Bird</TD> <TD>Flower</TD> <TD>Area</TD> </TR> <TR> <TD>California</TD> <TD>33871648 people</TD> <TD>Sacramento</TD> <TD>Quail</TD> <TD>Golden Poppy</TD> <TD>155959 square miles</TD> </TR> <TR> <TD>Massachusetts</TD> <TD>6349097 people</TD> <TD>Boston</TD> <TD>Chickadee</TD> <TD>Mayflower</TD> <TD>7840 square miles</TD> </TR> <TR> <TD>New York</TD> <TD>18976457 people</TD> <TD>Albany</TD> <TD>Bluebird</TD> <TD>Rose</TD> <TD>47214 square miles</TD> </TR> </TABLE> </BODY> </HTML>
Figure 9.4 shows this result in Internet Explorer.

Figure 9.4 Reading attribute values.
There's another thing you need to know about attributes: You can use the @* wildcard to select all attributes of an element. For example, "state/@*" would select any attributes of a <state> element.
Handling ID Attributes
If you have given elements an ID attribute and have declared that attribute in a DTD or XML schema, you can match those elements by using the id() expression. For example, here's how you might match elements with the ID value Steven:
<xsl:template match="id('Steven')"> <xsl:value-of select="."/> </xsl:template>
Handling Processing Instructions
You can match processing instructions by using the XPath expression processing-instruction(). Here's an example:
<xsl:template match="/processing-instruction()"> <P> Matched a processing instruction. </P> </xsl:template>
You can specify what processing instruction you want to match if you list its name in the parentheses here. The following example matches the processing instruction <?xml-stylesheet?>:
<xsl:template match="/processing-instruction(xml-stylesheet)"> <P> Matched an xml-stylesheet processing instruction. </P> </xsl:template>
Handling Multiple Matches
You can catch more than one match by using one match attribute if you use the Or operator, |. For example, say that you want to display the values of the <bird> and <flower> elements in bold, using the HTML <B> tag. To do that, you can use a single template to match both elements, as shown in Listing 9.11.
Example 9.11. An XSL Style Sheet That Uses Multiple Matches (ch09_11.xsl)
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="states"> <HTML> <BODY> <xsl:apply-templates/> </BODY> </HTML> </xsl:template> <xsl:template match="state"> <P> <xsl:apply-templates/> </P> </xsl:template> <xsl:template match="bird | flower"> <B> <xsl:apply-templates/> </B> </xsl:template> </xsl:stylesheet>
Here are the results:
<HTML> <BODY> <P> California 33871648 Sacramento <B>Quail</B> <B>Golden Poppy</B> 155959 </P> <P> Massachusetts 6349097 Boston <B>Chickadee</B> <B>Mayflower</B> 7840 </P> <P> New York 18976457 Albany <B>Bluebird</B> <B>Rose</B> 47214 </P> </BODY> </HTML>
Note that the <bird> and <flower> values are enclosed in HTML <B> elements to make them bold. Also note that the text from elements that didn't even have a template is inserted into the result document as well. The reason for this is that there is a default template built in to XSLT for elements, and it just inserts their values into the resulting document. When you used <xsl:apply-templates> on the child elements of the <states> element, the default template was automatically used for elements, without any explicit template. You'll see the default rules for XSLT later today.
Matching Using XPath Expressions
So far today you've been matching expressions such as states and @units by assigning those values to match attributes. The expressions you've seen have been a subset of the complete XPath syntax (we'll talk more about XPath later today). But oddly enough, you can actually use the full XPath syntax in a value you assign to a match attribute if you use it in a node test. To create a node test, you use the [] operator to test whether a certain condition is true. For example, you can test the value of an element, whether an element has a particular child or attribute, and even the position of a node in the document.
For example, here's how you could match <state> elements that have child <flower> elements:
<xsl:template match = "state[flower]">
Here's how you could match any element that has a <flower> or <bird> child element:
<xsl:template match = "*[flower | bird]">
Here's how you could match any element that has a units attribute:
<xsl:template match="*[@units]">
These expressions inside the [ and ] are full XPath expressions, so it's time to start taking a look at XPath. Up to this point, you've taken a look at the kinds of expressions you can use with the <xsl:template> element's match attribute. The select attribute, which can be used in the <xsl:apply-templates>, <xsl:value-of>, <xsl:for-each>, <xsl:copy-of>, and <xsl:sort> XSLT elements, however, may be assigned a full XPath expression.