Re: Q: pattern selection for outside the context node

Subject: Re: Q: pattern selection for outside the context node
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Thu, 08 Jul 1999 18:24:49 -0700
At 99/07/08 16:30 -0700, Aaron Fischer wrote:
>Hey everyone.  I have a data-driven XSL transformation script.  In other
>words, I have a whole bunch of templates, one for every kind of element in
>each level of the XML tree, with each template calling
><xsl:apply-templates/> on its children.
>My problem is, within certain templates, I want to reference data in
>certain elements located elsewhere in the XML tree.  However, when this
>template is called upon, the context node is such that the element I want
>to reference is NOT a descendant of the context node;  i.e. it's somewhere
>else above, in the XML tree;  and at the moment it stands as inaccessible
>Is there a pattern syntax for starting at the root or going above the
>context node, up into the XML tree?  

What you've asked for in your simple question is the subject of an entire
tutorial topic ... so I'll only give an outline here.  Fortunately, the
XSLT 19990421 working draft has good examples.

You have your choice of traversing the source tree from where you are, or
from the root of the tree, or from an arbitrary node identified by the
node's unique identifier (assigned to element nodes by an attribute of type
ID), or from an arbitrary node based on key values you identify in the

>Every pattern selection I've tried is
>always evaluated from the context node, which is the very thing I want to
>bypass.  Is there a way to directly assign the value of the context node?

I think that question is mis-worded, as one doesn't "assign" the value of
the context node.  The stylesheet asks the XSLT engine to process a given
context node list and the engine then processes each node of the context
node list in a particular order (typically document order, though some
requests are reverse document order).

Where you say:


You are actually saying:

   <xsl:apply-templates select="from-children(*)"/>

Which can also be abbreviated by saying:

   <xsl:apply-templates select="*"/>

Each of which is giving the XSL engine a context node list of all children
and then asking the engine to apply the template for each member of the
context node list.  This is indicated by the "from-children" axis of the
select pattern.

According to 6.1.1 there are 12 axes you can choose from when building a

 [5] AxisIdentifier ::=  'from-ancestors'
                       | 'from-ancestors-or-self'
                       | 'from-attributes'
                       | 'from-children'
                       | 'from-descendants'
                       | 'from-descendants-or-self'
                       | 'from-following'
                       | 'from-following-siblings'
                       | 'from-parent'
                       | 'from-preceding'
                       | 'from-preceding-siblings'
                       | 'from-self'

And according to 6.1.4 there are abbreviations available to be used to
address 5 of the axes:

    .       refers to   from-self
    @name   refers to   from-attributes   (name of "*" means "all")
    name    refers to   from-children     (name of "*" means "all")
    ..      refers to   from-parent
    //      refers to   from-descendants
When the pattern starts with "/", the orientation is from the root of the

The pattern can be comprised of a number of location steps, each step
delimited by "/".

The function id() described in 6.2.2 allows you to, in a location step,
refer to a node or nodes with unique identifiers.

The function key() is similar to id() in that it allows you to refer to a
node or nodes elsewhere in the document, but it does so without the
reference needing to be a node with a unique identifier.  There is a short
description of the power of the key() function in the free resources
section of our web site.

There are numerous examples in section 6 of the 19990421 working draft
XSLT, as well as in available tutorial material that has been cited in
earlier messages.

There is a lot for you to work with.  I hope this has helped.

......... Ken

G. Ken Holman                    mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.   
Box 266, Kars, Ontario CANADA K0A-2E0   +1(613)489-0999   (Fax:-0995)
Website:  XSL/XML/DSSSL/SGML services, training, libraries, products.
Publications:   Introduction to XSLT (3rd Edition) ISBN 1-894049-00-4
Next instructor-led training:   MS'99 1999-08-16  MT'99 1999-12-05/06

 XSL-List info and archive:

Current Thread