Linking-Based Standards

With the birth of the Web came the need for and dependency on linking. Whether it was for being able to link to internal locations within a document, or to link to new documents altogether, the need for links was apparent. These are just a few of the reasons the <a> (anchor) element is so widely used and, frankly, a requirement for any Web-published document. But linking for HTML is not the only document type that needs this functionality. XML documents also need the ability to link to other resources. Additionally, efforts should be made to prevent broken links.

In this section we will introduce you to several efforts that revolve around linking, such as XLink and XPointer. As you will see, dramatic improvements are being made and the world of linking, as we currently know it, is changing.

The XML Linking Language

The XML Linking Language (XLink) builds upon the basic unidirectional <a> element links we are used to in HTML and XHTML. It not only defines and provides this familiar functionality, it also provides for more complex linking structures, including

  • Asserting linking relationships between two or more documents
  • Associating metadata with links
  • Expressing links that reside in a location separate from the linked resource

The XLink language itself consists of several attributes within the XLink namespace that define how the link is to be handled and how it works. The type attribute, which is one of the most important, defines the type of link. Table A-1 lists the possible types of links.

Table A-1 Types of XLink Links

Type Description

arc

Provides traversal rules among the link's participating resources.

extended

Offers full XLink functionality.

locator

Address of the remote resource participating in the link.

none

Has no specific XLink meaning.

resource

Supplies local resources that participate in the link.

simple

Offers shorthand syntax for the common outbound link between two participating resources (like how the <a> element currently works).

title

Provides a human-readable title for a link.

The Mozilla browser was one of the first user agents to support XLink, and it provides the ability to perform some functions using the simple links as defined by the XLink specification. To demonstrate, xlink.xml in Listing A-8 contains three links using the XLink language. If you load this XML document into the browser, you will be able to click on the links to access the pages.

Listing A-8 xlink.xml: An XLink example which can be run in the Mozilla browser.

 <?xml version="1.0"?> <body xmlns:xlink="http://www.w3.org/1999/xlink"> <para>Here is a list of my favorite links... <li xlink:type="simple" xlink:href="http://www.msn.com">Personal</li> <li xlink:type="simple"  xlink:href="http://msdn.microsoft.com">Work</li> <li xlink:type="simple"  xlink:href="http://www.microsoft.com">Industry</li> </para> </body> 

The XML Path Language

The XML Path Language (XPath), which is used to address parts of an XML document, represents shared syntax and semantics between XSLT and XPointer. XPath models a document as a tree of nodes, which includes element, attribute, and text nodes. It then provides the grammar for accessing the leaves of these nodes.

For instance, the syntax child::p selects the <p> element children of the context node. If you wanted all element children you would use child::*. You can even describe more complex locations such as the first <p> child of the context node with child::p[position( )=1]. Better yet, let's say you want to find the first one with the id attribute set to description. This would be accomplished with child::p[attribute::id=description][position( )=1].

This representation is referred to as the Location Path of the element instance. The child keyword, in these examples, is the axis of the XPath statement. Other axes are included in Table A-2.

Table A-2 Axes in XPath

Key Description

ancestor

Contains the ancestors of the context node. This refers to the parent of the context node and the parent's parent and so on.

ancestor-or-self

Contains the context node and the ancestors of the context node.

attribute

Contains the attributes of the context node.

child

Contains the children of the context node.

descendant

Contains the descendants of the context node. This refers to a child or a child of a child and so on of the context node.

descendant-or-self

Contains the context node and the descendants of the context node.

following

Contains all nodes in the same document as the context node that are after the context node in document order, excluding any descendants or attribute and namespace nodes.

following-sibling

Contains all the following siblings of the context node.

namespace

Contains the namespace nodes of the context node.

parent

Contains the parent of the context node, if one exists.

preceding

Contains all nodes in the same document as the context node that are before the context node in document order, excluding any descendants or attribute and namespace nodes.

preceding-sibling

Contains all the preceding siblings of the context node.

self

Contains just the context node itself.

In addition to identifying portions of an XML document, XPath also has the ability to perform base string, numeric, and boolean manipulation. As the world of linking within XML documents (especially on the Web) begins to demand more out of user agents, XPath will play a major role in defining and describing documents for linking purposes.

You can find more information on XPath at http://www.w3.org/TR/xpath.

The XML Pointer Language

The XML Pointer Language (XPointer) is based on XPath and supports the addressing of internal parts of an XML document. This allows you to evaluate a document's structure and choose internal subsets based on criteria such as element types, attribute values, character content, and relative position. This is accomplished by defining extensions to XPath.

Using XHTML as an example, if you wanted to find the element with the id attribute value set to booktitle, you would use the following syntax:

 xpointer(id("booktitle")) 

This might, syntactically, look similar to XPath, as you would expect. But an extension to XPath in XPointer also allows you to specify a range. Maybe you want to locate everything from the title (id=booktitle) of the book to the end (id=bookend). You could do this with:

 xpointer(id("booktitle")/range-to(id("bookend"))) 

XPointer, which is currently at a Candidate Recommendation state at the W3C, offers flexibility over the standard <a> element usage common in HTML and XHTML. However, because of the immaturity of this specification effort, we recommend you wait before implementing this in any of your projects. Once it has reached a Proposed Recommendation state, although not complete, you should be safe to use it as you see fit.

The XML Base

Linking, as we have seen, is an important piece of functionality. It allows us to build virtual documents and information repositories by linking from document to outside resources. But often, as we have commonly seen in HTML and XHTML, links are not absolute URIs, but are relative to the resource.

Rendering user agents, especially off line browsers, might need to know the absolute URIs for their processing. To handle this type of situation Web developers use the XHTML <base> element, which can specify the base URI for any relative URIs a given document might contain. The need for this is no different in XML. In response and in addition to the other efforts the XLink Working Group is working on, XML Base was born.

XML Base is a short specification whose sole purpose is to enable XML developers using links the ability to specify base URIs. This is accomplished by including an xml:base attribute in the root element with a value equal to the base URI. Additionally, you can specify this attribute in child elements if you: (a) do not want to apply the base URI to all elements in a document, or (b) want to override the base URI for a specific element and any child elements it has.

As an example, let's take a look at sample-base.xml in Listing A-9. Here we define a base URI of http://www.microsoft.com/employees/ for the entire document, but we override it for the list of sites we have included.

Listing A-9 sample-base.xml: A simple demonstration of defining a base URI for a whole document, and then overriding it for specific elements and their children.

 <?xml version="1.0"?> <user xml:base="http://www.microsoft.com/employees/" xmlns:xlink="http://www.w3.org/1999/xlink"> <name>R. Allen Wyke</name> <phone>919.555.1212</phone> <!-- base URI applies to the following 3 child elements --> <sites xml:base="/www/"> <li xlink:type="simple" xlink:href="pesonal.xml">Personal</li> <li xlink:type="simple"  xlink:href="work.xml">Work</li> <li xlink:type="simple"  xlink:href="industry.xml">Industry</li> </sites> <!-- base URI defined in root element applies here --> <img src="/books/4/456/1/html/2/photo.gif" alt="My Photo" width="40" height="40" /> </user> 

This is all the XML Base specification is: the xml:base attribute. If you want to learn more about this standard and how user agents are supposed to conform, please see http://www.w3.org/TR/xmlbase.



XML Programming
XML Programming Bible
ISBN: 0764538292
EAN: 2147483647
Year: 2002
Pages: 134

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net