XML study (Lesson 3: Going back in the XML with XPATH (part II))

The set (Set of) Nodes returned by XPath are called Collection. Usually in programming, from "Collection" is used to refer to a set of objects of type. In turn we can pass through (iterate through) the objects in a Collection, but not guaranteed their order, ie any object encountered before or after any object.

In contrast, in the XPath standard, as a Collection is returned by an XPath Query (question), it retains the order of their rank and Nodes in XML documents. That is, if the XPath returns a branch of the nodes except the nodes are not eligible, the remaining node held in position on a branch.

Because the attributes of an Element unordered, so they can lie in a jumbled Collection.

Indexing a Collection

A Collection of Nodes is seen as an Array. Want to talk directly to a node in the Collection, you can use an index within brackets. Index is the first node is 1.

Brackets ([]) is higher Precedence (calculated ago) slash mark (/) or two slash marks (//). Here are two examples:

author[1]Element first author.
author[firstname][3]Element Element author a firstname third child.

Relationship (Axes)

Location using a path to a Axis specify the relationship between the Nodes are selected for the context node. The following is a list full of axes:

ancestor::The ancestors of the context node. The ancestors of the context node consists of father, grandfather, great-grandfather. Etc., so ancestor:: axis always including the root node, unless the context node is the root node.
ancestor-or-self::The main context node and its ancestors. The ancestor-or-self:: axis always including the root node.
attribute::The Attributes of the context node. If context is not an Element node is sure axis will be empty.
child::Children of the context node. A child is any node that lies just below the context node in the tree. However, attribute or namespace nodes are not considered children of the context node.
descendant::Descendants of the context node. Children and grandchildren are children,grandchildren, scattered,. etc., so descendant:: axis never contains attribute ornamespace nodes.
following::All nodes appear after the context node in the tree, not including children, attribute nodes, or namespace nodes.
following-sibling::The namespace nodes of the context node. Each namespace has a namespace nodein the scope (scope) of the context node.
If context is not an Element node, the Axis will be empty.
namespace::The namespace nodes of the context node. Each namespace has a namespace node in the scope (scope) of the context node.
If context is not an Element node, the Axis will be empty.
parent::Context node's parent node, if it is his father. Parent node is located just above the context node in the tree node.
preceding::All nodes appear before the context node in the tree, regardless of the ancestral nodes, attribute nodes, or namespace nodes. One way to identify đi trước:: axis is all nodes have finished completely before starting context node.
preceding-sibling::All nodes him (in front), context node. đi trước-sibling:: axis refers to only the child Nodes, Node of the same father, lying on the tree before the context node.
Nếu context node là Attribute hay Namespace thìpreceding-sibling:: axis sẽ trống rỗng.
self::Is the context node.

Refine (Filters)

As we saw above, to limit the Nodes removed only a satisfactory condition, we add a filter (filter) to Collection. Filter Claus just like he is a WHERE Clause in SQL language of the database.

If a Collection is located between a filter, it would result Collection returns TRUE if at least one node and FALSE if the Collection is empty (empty). For example expression author / degree means that function to transform Collection Boolean value will be TRUE if the author has promised an Element Element child named degree.

Filters always determined by its context. In other words, the expression book [author] Element means for each book found, it will try to see if the name contains an Element no author. Likewise, book [author = 'Brown'] means that for each Element book found, it will try to see if an Element contains the author name with no value in Brown.

We can use dot (.) To explore current context node. For example, book [. = 'Dreams'] means that for each Element book found in the current context, it will be tried if there is no value in Dreams. Here are a few examples:

book[excerpt]All Element book contains at least one excerpt Element.
book[excerpt]/titleAll Element Element title is in the book that contains at least one excerpt Element.
book[excerpt]/author[degree]All author Element Element contains at least one degree and are in the book Elements Element contains at least one excerpt.
book[author/degree]All Element book contains at least one author Element Element degree with at least onechild..
book[excerpt][title]All Element book contains at least one and at least one excerpt Element title Element.


To compare two objects we use in the XPath sign (=) for equal and (! =) For not equal. All Element and Attributes is the string, but is Typecast (seen as) those numbers when compared.

author[lastname = "Smith"]All author Element Element contains at least one value in the LastName Smith.
author[lastname[1] = "Smith"]All Element Element LastName author has the first value by Smith.
author/degree[@from != "Harvard"]All degree Element, an Element is the author, and have an attribute with a value from zero to as "Harvard."
author[lastname = /editor/lastname]All contain an author Element Element Element LastName LastName equal to theElement is the root of the editor.
author[. = "John Hamilton"]All Element author has string value is John Hamilton.

Operator Union | (assembled)

XPath language support Operator Union, like Logical OR (either). Here are a few examples:

firstname | lastnameAll Element FirstName and LastName in the current context.
(bookstore/book | bookstore/magazine)All Element book or magazine as a child Element bookstore.
book | book/authorAll Element Element book or author is the book the Elements
(book | magazine)/priceElement is the price of any book or Element Element magazine.

Try Node (Node Type Tests)

To select the types other than Node Element node, use Node-Type Test. The purpose of the Node-Type test is used to indicate an unusual choice. For example, descendant:: text () gives us all the text nodes are descendants of the context node, although that kind of context node descendants of the node is Element.There are four types of Node-Type tests as listed below.

Node typeReturnsExample
comment()me how node.following::comment () selects all comment nodes appear after the context node.
node()all nodes.preceding::node () selects all nodes before the context node.
processing-instruction()any processing instruction node.self:: processing instruction () selects all processing instruction nodes in the contextnode.
text()all text node.child:: text () selects all text nodes as children of the context node.

Test Processing Instruction Node target type

A node test can pick any kind of processing instruction, ie the selected target (target). The syntax of a test such as:

The following example returns the node test all processing instruction nodes are referredto an XSL stylesheet in the document:

Add a Location Path Examples

./authorAll Element author in the current context.
Expresion this expression is equivalent to the next row.
authorAll Element author of the current context.
/bookstoreDocument (Root) Element name of this bookstore.
//authorAll Element author in the document.
book[/bookstore/@specialty = @style]Element book has all the style Attribute with value equal to the value of AttributeSpecialty bookstore Document Element of the document
author/firstnameAll children of the Elements Element firstname author
bookstore//titleAll Element title one or more lower level, ie the descendants of, Element bookstore. Note that this expression differs from expression in a design.
bookstore/*/titleAll Element title I of the bookstore.
bookstore//book/excerpt//emphAll Element emph anywhere below is the excerpt of the book elements, element anywhere in the bookstore.
.//titleAll Element title one or more lower than the current context node.
author/*All Element is the author of the child elements.
book/*/lastnameElement All elements LastName is the nephew of the book.
* / *All Element child node of current context.
*[@specialty]All you have Attribute Element Specialty..
@styleStyle attribute of the current context node.
price/@exchangeAttribute exchange price of the Elements in the current context, ie the price of the currentcontext node Elements
price/@exchange/totalReturns an empty node set, because there is no Element Attributes children. This expression is acceptable in the grammar of the XML Path Language, but not really valid.
book[@style]Element book has all the attributes in the current context node style.
Note the part in square brackets is the condition of the book Element
book/@styleStyle attributes of any Element booktrong current context node.
There are no conditions as found above. Attribute or Element to say to the right post.
@ *All attributes of the current context node.
The first author element node in the current context.
author[firstname][3]Tuesday Element author has a child Element firstname.
my:bookElement namespace from my book.
my:*All Element in my namespace..


Post a Comment