W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

19
WRITING XPATH QUERIES SD2520 Databases using XML and JQuery

Transcript of W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

Page 1: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

WRITING XPATH QUERIES

SD2520Databases using XML and JQuery

Page 2: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

XPATH PATTERNS AND EXPRESSIONS

When you create a template, you use a pattern to specify the nodes that the template can be applied to.

When you apply a template, you use an expression to specify the node set that should be processed.

You write both patterns and expressions using XML Path Language (XPath) syntax.

Page 3: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

XPATH

XPath is a language for selecting nodes and node sets by specifying their location paths in the XML document

You can also use XPath in other XSLT instructions to further process given node sets to return values instead of nodes.

XPath has built-in functions to do math, process strings, and test conditions in an XML document.

Page 4: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

NOTE:

Like XSLT, XQuery uses XPath expressions.

The most current version of the language is XPath 2.0.

However, because version 1.0 is still more widely used, will cover XPath 1.0. in two chapters

Page 5: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

LOCATING NODES

At the foundation of the XPath language is the ability to use location paths to refer to a node or node set.

A node is an individual piece of the XML document (such as an element, an attribute, or some text content).

A location path uses relationships to describe the location of a node or set of nodes relative to a given node.

When translating location paths, XPath considers all XML documents as tree structures. They are considered node trees, which are a hierarchical structure of nodes (see Figures 10-1 and 10-2 next).

Page 6: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

Figure 10.1Figure 10.2

Page 7: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

THE XML NODE TREE

In an XML node tree, everything in the tree is a node, and every node is in some way related to another.

At the top of the node tree is the root node. The root node, or document node, can have

any number of child nodes. To these child nodes, the root node is a

parent node. These child nodes can have any number of

child nodes themselves, and so on, and so on.

Page 8: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

THE XML NODE TREE

Child nodes with the same parent are called sibling nodes.

Descendant nodes are a node’s child nodes, its children’s child nodes, and so forth.

Ancestor nodes are a node’s parent node, grandparent nodes, etc.

Through XPath location paths, you can access any of these nodes from any other simply by knowing the re-lationship between the two.

Page 9: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

LOCATION PATHS There are two kinds of location paths: relative

location paths and absolute location paths. A relative location path consists of a sequence of

location steps separated by / (a forward slash). Each step selects a node or node set relative to the

current node. Then, each node in that set is used as the current node for the following step, and so on.

An absolute location path consists of / (a forward slash), optionally followed by a relative location path.

A / by itself selects the root node of the XML document. If it is followed by a relative location path, then the location path is a relative location path starting at the root node.

Page 10: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

USING LOCATED NODES Often, when using location paths, you will be using

the located node or node set as a container of other elements to process. Other times, you will want to know the node’s value.

In XPath, there are seven different node types: root nodes (of which there is always exactly one) element nodes text nodes attribute nodes comment nodes processing instruction nodes namespace nodes.

For each node type, there is a way of retrieving its value. For some, the value is part of the node itself; for others, the value is based on the value of its descendant nodes.

Page 11: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

* * IMPORTANT TIP * *

The XPath language syntax was inspired in part by the common “path/file” file system.

The current node is the element, or node, that is currently being processed.

The context node is where the XPath location path address starts.

In most circumstances, these terms are interchangeable, so the term current node is used throughout the book.

Page 12: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

DETERMINING THE CURRENT NODE As the XSLT processor goes through your style

sheet, it works on one node at a time. It is through the use of the

xsl:template xsl:apply-templates xsl:for-each elements that it knows which parts of your XML document to

process and when. When developing an XSLT style sheet, you will

often specify what to process next with respect to what is being processed now.

The node currently being processed is called the current node.

Before you can refer from the current node, you will need to know how to identify it (Figure 10-3 next Slide).

Page 13: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

FIGURE 10-3

At point 1, the current node is / (the root node) as specified by the xsl:template match=”/” instruction.

When the processor reaches point 1a, the current node becomes the first wonder element in ancient_wonders and the processing jumps to point 2 where that wonder element is processed according to the xsl:template match=”wonder” instruction.

Page 14: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

FIGURE 10-3

Then, when the processor reaches point 2a, the name element with a language attribute not equal to English becomes the current node

and the processing jumps to point 3, where that name element becomes the current node and is processed according

to the xsl:template match=”name[@language!=’English’]” instruction.

Page 15: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

FIGURE 10-3

When this instruction is complete,

the processor “returns” from the name template applied in point 2a.

The current node then becomes the wonder element once again, until the processor finishes the wonder template

and returns to the root template.

Page 16: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

FIGURE 10-3

After this first wonder element is processed,

the second wonder element becomes the current node,

and so on until all the wonder elements have been processed

(and taken their turn as the current node)

Page 17: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

TO DETERMINE THE CURRENT NODE:

By default, the current node is the one that is specified by the current template.

In other words, the current node is identified by the template’s match attribute.

If there is an xsl:apply-templates instruction, the current node becomes the node that is matched by the corresponding template (that is, the one specified in the match attribute of the xsl:template instruction).

When the processor “returns” from that xsl:template, the current node reverts back to one from the original template’s match attribute.

Page 18: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

* * TIPS * *

The xsl:apply-templates instruction may process more than one node in the case where the select expression returns a node set.

You won’t always want to select the entire node set.

To get a subset of the current node, you can add a test called a predicate.

You can also use . (the current node shortcut) in a predicate to refer to the context node.

The context node is the node that is being tested by the predicate.

Page 19: W RITING XP ATH Q UERIES SD2520 Databases using XML and JQuery.

REFERRING TO THE CURRENT NODE

If you’re currently processing the node that you want to use in a select attribute, there’s a shortcut you can use. Instead of referencing the current node using a location path from the root node, it’s much easier to use the current node shortcut (Figure 10-4)