Presentation is loading. Please wait.

Presentation is loading. Please wait.

SD2520 Databases using XML and JQuery

Similar presentations


Presentation on theme: "SD2520 Databases using XML and JQuery"— Presentation transcript:

1 SD2520 Databases using XML and JQuery
Writing XPath Queries SD2520 Databases using XML and JQuery

2 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.

3 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.

4 Like XSLT, XQuery uses XPath expressions.
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

5 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).

6 Figure 10.1 Figure 10.2

7 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.

8 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.

9 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.

10 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.

11 * * 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.

12 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).

13 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.

14 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=” instruction.

15 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.

16 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)

17 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.

18 * * 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.

19 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)


Download ppt "SD2520 Databases using XML and JQuery"

Similar presentations


Ads by Google