- 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
Introducing XLinks
Where would the Web be without hyperlinks? You find them all over. Here's an example of an HTML hyperlink:
<A HREF = "http://www.XMLPowerCorp.com/reviews.html#insurance"> Health Insurance <A>
This hyperlink appears as the text "Health Insurance" in an HTML document, and when you click it, the browser navigates to (the fictitious) http://www.XMLPowerCorp.com/reviews.html#insurance. XLinks work in much the same way as this, except they're the XML version of the familiar HTML construct.
The XLink 1.0 specification is a W3C recommendation that was released June 27, 2001. (The most current version of this recommendation is at http://www.w3.org/TR/xlink.) You use XLinks to link one document to another or to link one location inside a document to another.
You saw one of the relatively few software implementations of XLinks in SMIL yesterday, when in the process of creating a text path, you defined a path named path1 and used an XLink to refer to that path:
<path id="path1" style="fill:none" d="M100,160 C100,60 250,60 250,140 S400,260 400,160"/> <text x="40" y="100" font-family="sans-serif" font-size="16pt"> <textPath xlink:href="#path1" xmlns:xlink="http://www.w3.org/1999/xlink"> . . . </textPath> </text>
This a very simple example of an XLink; it simply defines a namespace called xlink with the URI http://www.w3.org/1999/xlink. Then it uses an xlink:href attribute to connect to the path1 path. This example works in the Adobe SVG plug-in, as you saw yesterday, but it's a very simple version of an XLink. Here's a more standard example, which converts the HTML <A> hyperlink from earlier today into an XLink:
<link xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href = "http://www.XMLPowerCorp.com/insurance.xml"> Health Insurance </link>
This is a simple XLink, which is the closest you can get to a standard HTML hyperlink by using XLink. In this example you set the xlink:type attribute to "simple" and the xlink:show attribute to "new"; this means the browser should open the linked-to document in a new window and set the xlink:href attribute to the URI of the new document. (Bear in mind that the form of XML URIs isn't settled yet either and may grow more complex and comprehensive in time.)
XLinks can go far beyond the simple. In fact, the XLink conception is very ambitious. You might want to link to not one but multiple documents, for example. Or you might want to link to a set of documents and have the link know which document comes next and which document was the previous document in the set. Or you might want a link to be able to search for more recent resources or set up an abstract series of paths that a link could follow, depending on the user's context. XLinks can do all that—theoretically.
Here are a few of the current implementations of XLinks:
- X2X (http://www.empolis.co.uk/products/prod_X2X.asp), from empolis GmbH, is an XML XLink engine. It allows linking between documents and information resources.
- Fujitsu XLink Processor (http://www.labs.fujitsu.com/free/xlip/en/index.html), developed by Fujitsu Laboratories, Ltd., is an implementation of XLink and XPointer.
- xlinkit.com (http://www.xlinkit.com) is a lightweight application service that provides rule-based XLink generation.
- Mozilla (http://www.mozilla.org) is a browser that supports simple XLinks.
- Amaya (http://www.w3c.org/Amaya), the W3C test browser, supports simple XLinks.
Listing 14.1 puts a health insurance example to work in an XML document. This example is written for XLink-aware browsers such as the W3C's Amaya browser, and it also includes some JavaScript (for example, the onClick attribute in this example), which is not XML but is honored by browsers such as Internet Explorer, enabling a simple XLink in that browser.
Example 14.1. Emulating XLinks (ch14_01.xml)
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="ch14_02.css"?> <insurance> <title> Supporting XLinks </title> Looking for <link xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href = "http://www.w3c.org" onClick="location.href='http://www.w3c.org'"> health insurance </link>? </insurance>
You can also use a CSS, ch14_02.css, to make the XLink look like a standard HTML hyperlink—blue, underlined, and when the mouse cursor moves across it, it turns to a hand. (However, note that in the Amaya browser, the cursor does not change.)
Example 14.2. An XLink Style Sheet (ch14_02.css)
link {color: #0000FF; text-decoration: underline; cursor: hand} title {font-size: 24pt}
Figure 14.1 shows the results of Listing 14.2 in the Amaya browser. This is a true XLink in a true XLink-enabled browser. When you double-click the XLink, the browser navigates to the target document, www.w3c.org.
Figure 14.1 A simple XLink in the Amaya browser.
This XLink example also works in Internet Explorer, thanks to Internet Explorer's support of the non-XML onClick attribute (see Figure 14.2). When you click the XLink, the browser navigates to the target document.
Figure 14.2 A simple XLink in Internet Explorer.
As this simple example shows, you don't need an <A> element to create an XLink. In fact, any XML element will do. You create an XLink by using attributes, not by using a particular element. You use the xlink:type attribute to create an XLink, and you set it to one of the allowable types of XLinks: "simple", "extended", "locator", "arc", "resource", "title", or "none". Here are the available XLink attributes:
- xlink:arcrole — Contains the link's role in an arc, which can support multiple resources and various traversal paths. (We'll talk more about arcs later today.)
- xlink:actuate — Specifies when the link should be traversed. You can set this attribute to "onLoad", "onRequest", "other", "none", or other values supported by the software you're using.
- xlink:from — Defines the starting resources in an arc.
- xlink:href — Acts as the locator attribute. This attribute contains the data that allows an XLink to find a remote resource.
- xlink:label — Contains a human-readable label for the link.
- xlink:role — Describes a remote resource in a machine-readable fashion.
- xlink:show — Specifies how to display the linked-to resource. Possible values are "new" (open a new display space), "replace" (replace the currently displayed data), "embed" (embed the new resource in the current one), "other" (leave the show function up to the displaying software), and "none" (don't show the resource).
- xlink:title — Describes a remote resource in a human-readable way.
- xlink:to — Defines a target or ending resource in an arc.
- xlink:type — Sets the type of the XLink. This is a required attribute, and the possible values are "simple", "extended", "locator", "arc", "resource", "title", and "none".
You don't use all these XLink attributes in the same XLink. Which attributes you use depends on the type of link you're creating, as given by the xlink:type attribute. Table 14.1 shows which attributes are required for each type of link.
Table 14.1. Required and Optional Attributes by xlink:type
simple |
extended |
locator |
arc |
resource |
title |
|
actuate |
Optional |
N/A |
N/A |
Optional |
N/A |
N/A |
arcrole |
Optional |
N/A |
N/A |
Optional |
N/A |
N/A |
from |
N/A |
N/A |
N/A |
Optional |
N/A |
N/A |
href |
Optional |
N/A |
Required |
N/A |
N/A |
N/A |
label |
N/A |
N/A |
Optional |
N/A |
optional |
N/A |
role |
Optional |
Optional |
Optional |
Optional |
Optional |
N/A |
show |
Optional |
N/A |
N/A |
Optional |
N/A |
N/A |
title |
Optional |
Optional |
Optional |
Optional |
Optional |
N/A |
to |
N/A |
N/A |
N/A |
Optional |
N/A |
N/A |
type |
Required |
Required |
Required |
Required |
Required |
Required |
The XLink attributes specify what kind of XLink you're creating. Let's take a closer look at some of them now, starting with xlink:type.
Using xlink:type
The xlink:type attribute is the most important XLink attribute because it sets the type of XLink you're creating. Here are the possible values:
- simple — Is used to create a simple link.
- extended — Is used to create an extended link.
- locator — Is used to create a locator link that points to a resource.
- arc — Is used to create an arc with multiple resources and various traversal paths.
- resource — Is used to create a resource link, which indicates a specific resource.
- title — Is used to create a title link and can hold the location of element markup for further information on a title (such as with international versions).
You've already seen how to create simple links, which are the most common type of XLinks:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="ch14_02.css"?> <insurance> <title> Supporting XLinks </title> Looking for <link xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href = "http://www.w3c.org" onClick="location.href='http://www.w3c.org'"> health insurance </link>? </insurance>
You'll also see other types of XLinks today.
Using xlink:href
The xlink:href attribute is called the locator attribute. You use it to give the URI of a remote resource. You've already put this attribute to work today:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="ch14_02.css"?> <insurance> <title> Supporting XLinks </title> Looking for <link xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href = "http://www.w3c.org" onClick="location.href='http://www.w3c.org'"> health insurance </link>? </insurance>
When you work with simple URLs, the values you can assign to this attribute are fairly simple. But when you work with general URIs, which can include XPointers, things can get pretty complex, as you'll see later today.
Using xlink:show
The XLink xlink:show attribute specifies how to show the linked resource. The xlink:show attribute has five predefined values:
- embed — Embeds the linked-to resource in the current resource.
- new — Opens a new display area, such as a new window, to display the new resource.
- none — Does not show the resource.
- other — Indicates a setting other than those that are predefined.
- replace — Replaces the current resource, usually in the same window.
Besides these values, you can also specify your own values as well, as long as the software you're using supports those values. You've put the xlink:show attribute to work already today, to indicate that a new window should be opened when the link is activated:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="ch14_02.css"?> <insurance> <title> Supporting XLinks </title> Looking for <link xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:href = "http://www.w3c.org" onClick="location.href='http://www.w3c.org'"> health insurance </link>? </insurance>
Using xlink:actuate
The xlink:actuate attribute specifies when a link should be traversed. The xlink:actuate attribute has these predefined values:
- onRequest — Means that the link should be traversed on the user's request.
- onLoad — Means that the link should be traversed when the resource is loaded.
- other — Specifies a custom preference.
- none — Specifies that there should be no actuation.
You can also set your own values for xlink:actuate (as long as your XML application understands them).
Using xlink:role and xlink:title
You can use the xlink:role and xlink:title attributes to describe a remote resource. Here's what using these attributes might look like:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/css" href="ch14_02.css"?> <insurance> <title> Supporting XLinks </title> Looking for <link xmlns:xlink = "http://www.w3.org/1999/xlink" xlink:type = "simple" xlink:show = "new" xlink:role = "insurance " xlink:title = "Health Insurance Data" xlink:href = "http://www.w3c.org" onClick="location.href='http://www.w3c.org'"> health insurance </link>? </insurance>
The xlink:title attribute is designed to be read by people, whereas the xlink:role attribute is designed to be read by software. A link's role indicates the category of a link; in this example, that's "insurance ".
Using xlink:arcrole and xlink:label
The xlink:label attribute contains a human-readable label for an XLink. The xlink:arcrole attribute works with XLink arcs, which are sets of links that can contain multiple resources and various traversal paths. Each XLink can be a member of various arcs and can have different roles in each. For example, a person might be a supervisor in one arc but an employee in another. We'll take a closer look at arcs a little later today.