CS835 - Data and Document Representation & Processing

Lecture 4 Hypermedia II Linking - Xpath, Xmlbase, Xinclude, XLink and XPointer


Reference: The XML Revolution Technologies for the future Web



XInclude - combining XML documents

To enhance reuse and modularity, a technique for constructing new XML documents from existing ones is desirable.

XInclude provides a simple inclusion mechanism.

Why yet another specification?

Application conformance: Think of XML as if Namespaces, XInclude, and XML Base were parts of the basic XML specification. (Caveat: the latter two are not widely implemented yet.)


An XInclude example

A document containing:

<foo xmlns:xi="http://www.w3.org/2001/XInclude">
  <xi:include href="somewhere.xml"/>

where somewhere.xml contains:


is equivalent to:

<foo xmlns:xi="http://www.w3.org/2001/XInclude">

XInclude details

How is the included resource denoted?

Other issues:

Many XInclude processors support only whole-document URIs, not full XPointer.

XML Base

A URI identifies a resource:

Inspired by the <base href="..."> mechanism in HTML, XML Base provides a uniform way of resolving relative URIs.

In the following example:

<... xml:base="http://www.daimi.au.dk/">
  <... href="~mis/mn/index.html" .../>

the value of href attribute can be interpreted as the absolute URI http://www.daimi.au.dk/~mis/mn/index.html.

Examples of applications:

Future XML parsers will support Namespaces, XInclude, and XML Base.


Three layers:

These technologies are standardized but not all widely implemented yet.


Problems with HTML links

The HTML link model:

Construction of a hyperlink:

Problems when using the HTML model for general XML:

The usual point: generic solutions allow generic tools!


The XLink linking model

Basic XLink terminology:

Link: explicit relationship between two or more resources.
Linking element: an XML element that asserts the existence and describes the characteristics of a link.
Locator: an identification of a remote resource that is participating in the link.

One linking element defines a set of traversable arcs between some resources.

A local resource comes from the linking element's own content.

Outbound: the source is a local resource
Inbound: the destination is a local resource
Third-party: none of the resources are local

Third-party links can be used to construct shared link bases for browsers.


An example

A linking element defining a third-party "extended" link involving two remote resources:

 <mylink xmlns:xlink="http://www.w3.org/1999/xlink" xlink:type="extended">
 <myresource xlink:type="locator" 
xlink:href="students.xml#Fred" xlink:label="student"/>
 <myresource xlink:type="locator" 
xlink:href="teachers.xml#Joe" xlink:label="teacher"/>
 <myarc xlink:type="arc" 
xlink:from="student" xlink:to="teacher"/>

A powerful example application of general XLinks:

Using third-party links and a smart browser, a group of people can annotate Web pages with "post-it notes" for discussion - without having write access to the pages. They simply need to agree on a set of URIs to XLink link bases defining the annotations. The smart XLink-aware browser lets them select parts of the Web pages (as XPointer ranges), comment the parts by creating XLinks to a small XHTML documents, view each other's comments, place comments on comments, and perhaps also aid in structuring the comments.



Linking elements

- defining links

All elements with XLink information contain an xlink:type attribute.

(Note the confusing terminology: a resource is defined either by a "resource" element or by a "locator" element.)

XPointer is described later - just think of XPointer expression as URIs for now...



- link semantics

Arcs can be annotated with abstract behavior information using the following attributes:

xlink:show - what happens when the link is activated?

Possible values:


insert the presentation of the target resource (the one at the end of the arc) in place of the source resource (the one at the beginning of the arc, where traversal was initiated) (example: as images in HTML)


display the target resource some other place without affecting the presentation of the source resource (example: as target="_blank" in an HTML link)


replace the presentation of the resource containing the source with a presentation of the destination (example: as normal HTML links)


behavior specified elsewhere


no behavior is specified


xlink:actuate - when is the link activated?

Possible values:


traverse the link immediately when recognized (example: as HTML images)


traverse when explicitly requested (example: as normal HTML links)


behavior specified elsewhere


no behavior is specified

Note: these notions of link behavior are rather abstract and do not make sense for all applications.

Semantic attributes:  describe the meaning of link resources and arcs


provide human readable descriptions (also available as xlink:type="title" to allow markup)

xlink:role and xlink:arcrole

URI references to descriptions



Simple vs. Extended links

- for compatibility and simplicity

Two kinds of links:

Convenient shorthand notation for simple links:

 <mylink xlink:type="simple" xlink:href="..." xlink:show="..." .../>

is equivalent to:

 <mylink xlink:type="extended">
 <myresource xlink:type="resource" 
 <myresource xlink:type="locator" 
xlink:role="remote" xlink:href="..."/>
 <myarc xlink:type="arc" 
xlink:from="local" xlink:to="remote" xlink:show="..." .../>

Many XLink properties (e.g. xlink:type and xlink:show) can conveniently be specified as defaults in the schema definition!

When should I use XLink? Tim Berners-Lee: only for hypertext linking (Not everybody agree...)



HLink vs. XLink

Why is XHTML not using XLink?

The problem:

When integrating XLink in a host language, the use of the XLink namespace makes a mess.

HLink: a recent alternative to XLink

Example HLink: Definition of the link semantics of <a href="..."> elements in XHTML.

<hlink namespace="http://www.w3.org/1999/xhtml"

13 September 2002: W3C's HTML Working Group publishes HLink draft for intended use in XHTML 2.0

24 September 2002: W3C's Technical Architecture Group rejects HLink in favor of XLink for the design of XHTML 2.0



XPointer: Why, what, and how?

Example of an XPointer:

 / \
 | \ /|
 | ---------------------------- |
 \ XPointer expression /
 \ /
 XPointer fragment identifier

(points to the first five section elements in the article root element.)

In HTML, fragment identifiers may denote anchor IDs - XPointer generalizes that.


XPointer vs. XPath

XPointer is based upon XPath:


XPointer fragment identifiers

An XPointer fragment identifier (the substring to the right of # in the URI) is either

    xpointer(...) xpointer(...) ...

containing a list (typically of length 1) of XPointer expressions.

Each expression is evaluated in turn, and the first where evaluation succeeds is used. (This allows alternative pointers to be specified thereby increasing robustness.)

Recently, the XPointer spec has been split into four (tiny) parts:

Next: We will now look into XPath and then later describe what additional features XPointer adds to XPath...



XPath: Location paths

XPath is a declarative language for:

The central construct is the location path, which is a sequence of location steps separated by /, e.g.:

 child::section[position()<6] / descendant::cite / attribute::href

selects all href attributes in cite elements in the first 5 sections of an article document.

A context consists of:

Initial context: defined externally (e.g. by XPointer, XSLT, or XQuery).
Location paths can be prefixed with / to use the document root as initial context node!

Note: in the XPath data model, the XML document tree has a special root node above the root element.

There is a strong analogy to directory paths (in UNIX). As an example, the directory path /*/d/*.txt selects a set of files, and the location path /*/d/*[@ext="txt"] select a set of XML elements


Location steps

A single location step has the form

    axis :: node-test [ predicate ]

The candidates that survive the filtration constitute the result.

This structure of location steps makes implementation rather easy and efficient, since the complex predicates are only evaluated on relatively few nodes.

The example from before:

 child::section[position()<6] / descendant::cite / attribute::href

selects all href attributes in cite elements in the first 5 sections of an article document.




Available axes:


the children of the context node


all descendants (children, childrens children, ...)


the parent (empty if at the root)


all ancestors from the parent to the root


siblings to the right


siblings to the left


all following nodes in the document, excluding descendants


all preceding nodes in the document, excluding ancestors


the attributes of the context node


namespace declarations in the context node


the context node itself


the union of descendant and self


the union of ancestor and self

Note that attributes and namespace declarations are considered a special kind of nodes here.

Some of these axes assume a document ordering of the tree nodes. The ordering is the left-to-right preorder traversal of the document tree - which is the same as the order in the textual representation.

The resulting sets are ordered intuitively, either forward (in document order) or reverse (reverse document order).
For instance, following is a forward axis, and ancestor is a reverse axis.

(Frustratingly, each technology uses a slightly different tree model...)



Node tests

Testing by node type:


chardata nodes


comment nodes


processing instruction nodes


all nodes (not including attributes and namespace declarations)

Testing by node name:



nodes with that name



any node

Warning: There is a bug in the XPath 1.0 spec! Default namespaces are required to be handled incorrectly, so, if using Namespaces together with XPath (or XSLT), all elements must have an explicit prefix.



- expressions coerced to type boolean

A predicate filters a node-set by evaluating the predicate expression on each node in the set with


 child::section[position()<6] / descendant::cite[attribute::href="there"]

selects all cite elements with href="there" attributes in the first 5 sections of an article document.

(Compare with the earlier example.)

The XPath predicate language is very large, but these are the essential ones to know

An entire location path may be used as a predicate

This is very useful to look ahead:




Available types:

An expression can be:

Coercion may occur at function arguments and when expressions are used as predicates.

Variables and functions are evaluated using the context.



Core function library

Node-set functions:


returns the context size


returns the context position


number of nodes in node-set


string representation of first node in node-set



String functions:



type cast to string

concat(string, string, ...)


string concatenation




Boolean functions:



type cast to boolean



boolean negation




Number functions:



type cast to number



sum of number value of each node in node-set




- see the XPath specification for the complete list.




Syntactic sugar: convenient notation for common situations

Normal syntax



nothing   (so child is the default axis)






.   (useful because location paths starting with / begin evaluation at the root)





selects all href attributes in descendants of the context node.

Furthermore, the coercion rules often allow compact notation, e.g.


refers to the third foo child element of the context node (because 3 is coerced to position()=3).



XPath visualization

Using Explorer 6 (or an updated version of Explorer 5) it is easy to experiment with XPath expressions.

The XPath Visualizer provides an interactive XPath evaluator that additionally visualizes the resulting node set (online installation).

This tool is implemented as an ordinary HTML page that makes heavy use of XSLT and JavaScript.



XPath examples

The following XPath expressions point to sets of nodes in the recipe collection:

"The amounts of flour being used":



"The ingredients of which half a cup are used":

//ingredient[@amount='0.5' and @unit='cup']/@name

grated Parmesan cheese
shredded mozzarella cheese
orange juice

"The second step in preparing stock for Cailles en Sarcophages":


When the liquid is relatively clear, add the carrots, celery, whole onion, 
bay leaf, parsley, peppercorns and salt. Reduce the heat, cover and let 
simmer at least 2 hours to make a hearty stock.



XPath 2.0

- currently a Working Draft, developed to capture the common subset of XSLT 2.0 and XQuery 1.0

Major changes from 1.0:



XPointer, Part II - how XPointer uses XPath


XPointer: Context initialization

An XPointer is basically an XPath expression occurring in a URI.

When evaluated, the initial context is defined as follows:

xmlns(myprefix=http://mynamespace.org) xpointer(...)

Warning: several levels of character escaping occur when using XPointer in XML documents



Extra XPointer features

XPointer provides a more fine-grained addressing than XPath.


get location of element containing current XPointer


get location where user initiated link traversal


get start point of location set


find matching substrings





selects the locations right before the first character of all character data nodes in the document.


 /section[1] / range-to(/section[3])

selects everything from the beginning of the first section to the end of the third.



Selected links: