XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (516 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
4.67Mb size Format: txt, pdf, ePub

The use of positional predicates with the attribute axis doesn't make much sense, because the order of attributes is undefined (though I did see one stylesheet that was using
@*[1]
to match the first attribute, and
@*
—which has lower priority—to match the others, which is perfectly legitimate so long as you realize that it's unpredictable which of the attributes will be the first). In the following description, I'll assume that you're using the child axis.

If there is only one predicate in the
PatternStep
, or if this predicate is the first, then:

  • last()
    is the number of siblings of the node being tested that satisfy the
    NodeTest
    (including the node itself). For example, if we are testing a

    element against the pattern
    para [last() = 1]
    , then
    last()
    is the number of

    elements that are children of the parent of the

    element being tested. This pattern will match any

    element that is the only

    child of its parent.
  • position()
    is the position of the node being tested among these siblings, taking them in document order and counting from one. So
    para[1]
    , which means
    para[position() = 1]
    , will match any

    element that is the first

    child of its parent element, in document order.

Note that it is the position of the node relative to its siblings that counts, not the position in the sequence you are processing the nodes. For example, suppose you want to process all the

elements in a document, in alphabetical order. You can write:


     


Then suppose you have the following two template rules:







The first template rule will be used for any

that is the first

child of its parent. Not, as you might expect, the first

in alphabetical order, nor even the first

element in the document. If you want to apply different processing to the

that is first in alphabetical order, the way to do it is as follows:


  

    

       …

    

    

       …

    

  


This is because the context position within the body of the template rule is the position of the node in the list of nodes being processed, whereas the result for deciding whether a node matches a pattern is the same regardless of the processing context.

If there are several predicates in the
PatternStep
, then
position()
and
last()
in predicates after the first apply to the nodes that survived the previous predicates. So
speech[speaker= ’Hamlet’] [1]
matches a

element that is the first

element among its siblings, counting only those

elements in which one of the

s is Hamlet.

Other books

Bliss by Clem, Bill
If I Were Your Woman by Donna Hill
Kowloon Tong by Paul Theroux
Demons of Desire by Debra Dunbar
Can't Get Enough by Tenille Brown
Miss Dimple Disappears by Mignon F. Ballard
La meta by Eliyahu M. Goldratt
Honor Thyself by Danielle Steel
Cries of Penance by Roxy Harte