Parts of a Location Step


A location step includes, at most, three kinds of details: an axis specification, a node test, and predicates.

An axis specification includes a range of nodes for subsequent consideration. The most common axis specification is child, whose use means that the location step accesses children of the context. Our examples used the child specification, but child is the default and so was not required in our syntax. If you assign an axis specification explicitly, you must follow it with two colons (for example, child::).

By varying the axis specification

  • you can indicate how to select nodes in a given location step; for example, you can include all descendant nodes rather than stepping down the tree one generation at a time

  • you can indicate in which direction to seek nodes for selection; for example, you can include nodes that are siblings rather than children or descendants

A node test is a criterion to compare against each of the nodes included in an axis specification. That test is either a node name to be matched (as in our prior examples) or a preset value.

Predicates are optional and further restrict the set of nodes that are selected.

Axis Specifications

This section lists the thirteen axis specifications. As you read through the list, note the following distinction. The forward axes are the axis specifications that assign context-position numbers to nodes in XML-source order. The reverse axes are the axis specifications that assign context-position numbers to nodes in reverse XML-source order.

The XML processor always returns nodes in XML-source order, regardless of whether you use a forward or reverse axis. Only the context-position number that is assigned to each node is affected by the axis direction, as we'll show later.

The axis specifications are as follows:

  • ancestor is a reverse axis that includes the parent of the context node, the parent of the parents, and so on, up the tree.

  • ancestor-or-self is a reverse axis that includes the context node from the previous location step, along with (as you'd guess) the parent of the context node, the parent of the parents, and so on, up the tree.

  • attribute is described later.

  • child is a forward axis that includes the children of the context node and is the default axis specification.

  • descendant is a forward axis that includes the children of the context node, the children of those children, and so on, down the tree.

  • descendant-or-self is a forward axis that includes the context node from the previous location step, along with (right!) the children of the context node, the children of the children, and so on, down the tree.

  • following is a forward axis that includes all nodes subsequent to the context node, including subsequent siblings but excluding children and other descendants.

  • following-sibling is a forward axis that includes all the next siblings (the first through the last) of the context node.

  • namespace is described later.

  • parent is a reverse axis that includes the parent of the context node; however, the context position is 1 at most.

  • preceding is a reverse axis and includes all prior nodes, including prior siblings and text nodes, but excluding parents and other ancestors.

  • preceding-sibling is a reverse axis and includes all previous siblings (the first through the last) of the context node.

  • self includes the context node. The axis is considered forward, but the context position is 1 at most.

In general, you can restrict the selection of nodes by using attribute values in predicates. Only two axis specifications, however, actually include attribute nodes or (what is similar) namespace nodes.

The attribute axis specification includes attribute nodes. This specification selects nodes only if the context node is an element node. In the XPath model:

  • The parent of an attribute node is an element node and is accessible through the parent or ancestor axis specification. The converse relationship is not true, however: an attribute node is not a child of an element node. You can access an attribute node in two ways: by the attribute axis specification when the context node is an element, and by the self axis specification when the context node is an attribute.

  • A namespace URI in a declaration such as xmlns = 'www.ibm.com' or xmlns:tns = 'www.ibm.com' is available through the namespace axis specification but not through the attribute axis specification.

  • The order of attribute nodes within an element node can vary from one XPath processor to another. (In general in XML, attributes are unordered.)

The namespace axis specification includes namespace nodes. In XPath 2.0, this rarely used specification is deprecated (is made a target for future removal from the language). Therefore, you may want to skim or skip the next paragraph.

The namespace specification selects nodes only if the context node is an element node. The string value of a namespace node is a namespace URI. In the XPath model:

  • Each element node includes a namespace node that represents the XML system namespace. (The XML system namespace allows use of attributes such as xml:lang, which identifies a human language.) The string value of the namespace node is http://www.w3.org/XML/1998/namespace.

  • Each element node also includes a node for each namespace that is in scope for the XML element. Consider the following XML document:

     <?xml version="1.0" encoding="ISO-8859-1"?> <other:Insured xlmns="defaultNamespace" xmlns:other="otherNamespace">    <CarPolicy/> </other:Insured> 

    The related CarPolicy node includes namespace nodes whose string values are defaultNamespace and otherNamespace.

  • The parent of a namespace node is an element node. The converse relationship is not in effect, however: a namespace node is not a child of an element node. You can access a namespace node in two ways: by the namespace axis specification when the context node is an element, and by the self axis specification when the context node is a namespace node.

  • The order of namespace nodes within an element node can vary from one XPath processor to another.

You can categorize axis specifications in a different way:

  • Six axis specifications provide access vertically, up and down the node tree: ancestor, ancestor-or-self, child, descendant, descendant-or-self, and parent. In relation to BPEL, you'll primarily use child and, on occasion, descendant.

  • Six axis specifications provide access horizontally, across the node tree: attribute, following, following-sibling, namespace, preceding, and preceding-sibling. In relation to BPEL, you'll probably use only attribute and on occasion, following and following-sibling.

    The attribute and namespace specifications do not concern the relationship of one element node to another but let you access nodes derived from details that are internal to an XML element.

  • The self specification stands alone and is used on occasion in BPEL.

Node Tests

As mentioned, a node test is a criterion to compare against each of the nodes included in an axis specification. Several preset node tests are available:

  • *

  • comment()

  • text()

  • node()

  • processing-instruction()

*

The asterisk (*) node test retains all the element nodes and no others in a given axis specification, except in two instances. In the case of the attribute specification, the asterisk retains all the attribute nodes and no others. In the case of the namespace specification, the asterisk retains all the namespace nodes and no others.

The asterisk provides a way to include a node without referencing a name. In relation to our main example, the following expression accesses the second CarPolicy node.

 /Insured/*[2] 




SOA for the Business Developer. Concepts, BPEL, and SCA
SOA for the Business Developer: Concepts, BPEL, and SCA (Business Developers series)
ISBN: 1583470654
EAN: 2147483647
Year: 2004
Pages: 157
Authors: Ben Margolis

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