|
Home > Online Product Documentation > Table of Contents > Specifying the Nodes to Evaluate Specifying the Nodes to Evaluate
Consider the
This section discusses the following topics: Understanding XPath Processor TermsTo use the context operators, it is important to understand the following terms: Axis
An
axis specifies a list of nodes in relation to the context node. For example, the
Context NodeA context node is the node the XPath processor is currently looking at. The context node changes as the XPath processor evaluates a query. If you pass a document to the XPath processor, the root node is the initial context node. If you pass a node to the XPath processor, the node that you pass is the initial context node. During evaluation of a query, the initial context node is also the current node. Context Node SetA context node set is a set of nodes that the XPath processor evaluates. Current NodeCurrent node is the node that the XPath processor is looking at when it begins evaluation of a query. In other words, the current node is the first context node that the XPath processor uses when it starts to execute the query. During evaluation of a query, the current node does not change. If you pass a document to the XPath processor, the root node is the current node. If you pass a node to the XPath processor, that node is the current node. Document ElementThe document element is the element in a document that contains all other elements. The document element is an immediate child of the root node. When you obtain the document element of a document, you obtain all marked-up text in that document. Filter
A
filter in a query specifies a restriction on the set of nodes to be returned. For example, the filter in the following query restricts the result set to
Location Path ExpressionA location path expression is an XPath expression. It has the following format:
Location StepAn XPath expression consists of one or more location steps. A location step has the following format:
Node Test
You apply a
node test to a list of nodes. A node test returns nodes of a particular type or nodes with a particular name. For example, a node test might return all
Root Node
The
root node is the root of the tree. It does not occur anywhere else in the tree. The document element node for a document is a child of the root node. The root node also has as children processing instructions and
Starting at the Context Node
Following is a query that looks for all child
This query is simply the name of the element you want to search for. If the context node is any one of the
About Root Nodes and Document ElementsA root node is the topmost node in the tree that represents the contents of an XML document. The root node can contain comments, a declaration, and processing instructions, as well as the document element. The document element is the element that contains all other elements; that is, the document element contains elements that are in the document but that are not immediate children of the root node. Starting at the Root Node
To specify that the XPath processor should start at the root node when it evaluates nodes for a query, insert a forward slash (
In an XML document, there is no text that corresponds to the root node. Externally, a root node is really a concept. Internally, there are data structures that represent this concept, but there is no text that you can point to and call a root node. The following query instructs the XPath processor to start at the root node, as indicated by the forward slash at the beginning of the query.
This query searches the children of the root node for a
The following query returns the entire document, starting with the root node. As you can see, the entire query is just a forward slash:
This query returns everything - comments, declarations, processing instructions, the document element, and any elements, attributes, comments, and processing instructions that the document element contains. Descending Along BranchesSometimes you want the XPath processor to evaluate all nodes that are descendants of a node and not just the immediate children of that node. This amounts to operating on a branch of the tree that forms the document.
To specify the evaluation of descendants that starts at the root node, insert two forward slashes (
To specify the evaluation of descendants that starts at the context node, insert a dot and two forward slashes (
Following is a query that finds all
Suppose the context node is the first
At the beginning of a query,
The
This query searches the current context for
For the sample
The first query returns all
Tip:
Explicitly Specifying the Current Context
If you want to explicitly specify the current context node, place a dot and a forward slash (
Remember, if you specify the name of an element as a complete query (for example,
You can also specify the dot notation (
In this example, the XPath processor finds all
Specifying Children or Descendants of Parent NodesSometimes you want a query to return information about a sibling of the context node. One way to obtain a sibling is to define a query that navigates up to the parent and then down to the sibling.
For example, suppose the context node is the first
The double dot (
Now suppose that the context node is still the first
The double dot notation need not appear at the beginning of a query. It can appear anywhere in a query string, just like the dot notation. Examples of XPath Expression ResultsTable 61 provides examples of XPath expression results:
Syntax for Specifying an Axis in a QueryThe previous sections provide examples of XPath expression syntax that uses abbreviations. This section introduces you to the axis syntax that many of the abbreviations represent. For a list of XPath abbreviations, see XPath Abbreviations Quick Reference. You can use axis syntax to specify a location path in a query. An axis specifies the tree relationship between the nodes selected by an expression and the context node. The syntax for specifying an axis in a query is as follows:
The axis names are defined in Supported Axes. A node test is a simple expression that tests for a specified node type or node name. For example:
XPath 2.0 adds additional tests, such as In addition, you can follow the node test with any number of filters. Supported AxesThe XPath processor supports all XPath axes: About the child Axis
The
If the context node is the
About the descendant Axis
The
If the context node is the
About the parent Axis
The
If the first
Note that dot dot
About the ancestor Axis
The
If the context node is the first
About the following-sibling Axis
The
If the context node is the first
About the preceding-sibling Axis
The
If the context node is the third
About the following Axis
The
The
The following example selects the
If the context node is the first
About the preceding Axis
The
The
The following example selects the
If the third
About the attribute Axis
The
If the context node is the second
About the namespace Axis
The
If more than one declaration defines the same prefix, the resulting node set includes only the definition that is closest to the context node.
If the context node is not an element, the
For example, if an element is in the scope of three namespace declarations, its
About the self Axis
The
Note that dot (
About the descendant-or-self Axis
The
If the context node is the
Note that
About the ancestor-or-self Axis
The
If the context node is the
Axes That Represent the Whole XML DocumentThe following group of axes represent an entire XML document: There is no overlap among these axes, as shown in the following figure:
|
Support for DataDirect SequeLink
Learn about Stylus Studio's support for DataDirect SequeLink, an end-to-end database middleware solution that provides a universal thin client for ODBC, JDBC, ADO or .NET, providing flexibility for both client- and server-based deployments.
Writing Custom XML Adapters
There are cases where the input format is too complicated for Convert to XML, or both reading and writing of the file format is needed. This section will take you through the process of writing a medium-sized adapter in Java.
Stylus Studio Blogs
Blogging is the hottest fad on the Internet and Stylus Studio has jumped on the bandwagon. Bookmark these XML Blogs to keep up with all the latest XML buzz in the internet blogosphere. Includes RSS and Atom Feeds!
Learn SQL/XML
Learn how to access and update relational databases using industry-standard SQL/XML technologies along with Stylus Studio's DB-to-XML editor and File Explorer technologies.