Xml for xpath operations

Source: Internet
Author: User

1. Get Document (many methods)
SAXReader saxReader = new SAXReader ();
Document document = saxReader. read (FileUtil. getFileInputStream (fileName ));

2. query Element
String xpath = "/composites/composite [@ type = 'ondelete']"; // query the composite of the attribute type = 'ondelete'
List <Element> composites = document. selectNodes (xpath );

3. xpath syntax

Select Node
XPath uses path expressions to select nodes in XML documents. Nodes are selected by following the path or step.

The most useful path expressions are listed below:
Expression description
Nodename: Select All subnodes of this node
/Select from the root node
// Select the nodes in the document from the current node that matches the selected node, regardless of their location
. Select the current node
.. Select the parent node of the current node
@ Select attributes

Instance
In the following table, we have listed some path expressions and expression results:

Path expression result
Bookstore Selects all subnodes of the bookstore Element
/Bookstore select the root element bookstore

Note: If the path starts with a forward slash (/), the path always represents the absolute path to an element!

Bookstore/book Selects all the book elements that belong to the sub-elements of bookstore.
// Book Selects all the child elements of the book, regardless of their location in the document.
Bookstore // book Selects all the book elements belonging to the descendants of the bookstore element, regardless of their location under the bookstore.
// @ Lang select all attributes named Lang.

Predicates)
It is used to find a specific node or a node that contains a specified value.

The predicates are embedded in square brackets.

Instance
In the following table, we list some path expressions with predicates and the results of the expressions:

Path expression result
/Bookstore/book [1] select the first book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()] select the last book element that belongs to the bookstore sub-element.
/Bookstore/book [last ()-1] select the last and second book elements belonging to the bookstore sub-elements.
/Bookstore/book [position () <3] select the first two bookstore sub-elements.
// Title [@ Lang] Selects all the title elements with the attribute Lang.
// Title [@ lang = 'eng'] Selects all title elements and these elements have the lang attribute with the value of ENG.
/Bookstore/book [price> 35.00] selects the book element of all bookstore elements, and the value of the price element must be greater than 35.00.
/Bookstore/book [price> 35.00]/title: select the title element of the book element in all bookstore elements, and the value of the price element must be greater than 35.00.

Select unknown Node
The XPath wildcard can be used to select unknown XML elements.

Wildcard description
* Match any element node
@ * Match any attribute node
Node () matches any type of node

Instance
In the following table, we list some path expressions and the results of these expressions:

Path expression result
/Bookstore/* select all subnodes of the bookstore Element
// * Select all elements in the document
// Title [@ *] Selects all the title elements with attributes.

Select several paths
You can select several paths by using the "|" operator in the path expression.

Instance
In the following table, we list some path expressions and the results of these expressions:

Path expression result
// Book/title | // book/price select the title and price of all book elements.
// Title | // price select the title and price elements in all documents.
/Bookstore/book/title | // price select the title element of all the book elements that belong to the bookstore element, and all the price elements in the document.

XPath axis
The axis defines a node set relative to the current node.

Axis name result
Ancestor Selects all the ancestors of the current node (parent, grandfather, etc)
Ancestor-or-self Selects all the ancestors of the current node (parent, grandfather, etc.) and the current node itself
Attribute Selects all attributes of the current node
Child Selects all child elements of the current node.
Descendant Selects all descendant elements (child, sun, etc.) of the current node ).
Descendant-or-self Selects all child elements (child, sun, etc.) of the current node and the current node itself.
Following Selects all nodes after the end label of the current node in the document.
Namespace: Select All namespace nodes of the current node
Parent selects the parent node of the current node.
Preceding Selects all nodes before the start label of the current node in the document.
Preceding-sibling Selects all peer nodes before the current node.
Self selects the current node.

Location Path expression
The location path can be absolute or relative.

The absolute path starts with a forward slash (/), but the relative path does not. In either case, the path contains one or more steps, and each step is separated by a slash:

Absolute path:
/Step/... relative path:
Step/... each step is calculated based on the nodes in the current node set.

Steps include:
Axis)
Define the tree relationship between the selected node and the current node
Node-test)
Recognizes nodes inside a certain axis
Zero or more predicates (predicate)
Further refine the selected node set
Step Syntax:
Axis name: node test [predicate] instance
Example result
Child: book Selects all the book nodes that belong to the child elements of the current node.
Attribute: lang selects the lang attribute of the current node.
Child: * select all child elements of the current node.
Attribute: * select all attributes of the current node.
Child: text () select all text subnodes of the current node
Child: node () selects all child nodes of the current node
Descendant: book Selects all book descendants of the current node
Ancestor: book Selects all the books of the current node.
Ancestor-or-self: book Selects all the book predecessors of the current node and the current node (if this node is a book node)
Child: */child: price: select all the price grandchildren of the current node.

XPath Operators
Operators available in XPath expressions are listed below:

Operator description instance Return Value
| Calculate two node sets // book | // cd returns all node sets with book and ck Elements
+ Addition 6 + 4 10
-Subtraction 6-4 2
* Multiplication 6*4 24
Div Division 8 div 4 2
= Equal to price = 9.80 if price is 9.80, true is returned.

If the price is 9.90, return fasle.

! = Not equal to price! = 9.80 if the price is 9.90, true is returned.

If the price is 9.80, return fasle.

<Less than price <9.80 if price is 9.00, true is returned.

If the price is 9.90, return fasle.

<= Less than or equal to price <= 9.80 if price is 9.00, true is returned.

If the price is 9.90, return fasle.

> Greater than price> 9.80 if price is 9.90, true is returned.

If the price is 9.80, return fasle.

>=Greater than or equal to price >=9.80 if price is 9.90, true is returned.

If the price is 9.70, return fasle.

Or price = 9.80 or price = 9.70 if the price is 9.80, true is returned.

If the price is 9.50, return fasle.

And price> 9.00 and price <9.90 if the price is 9.80, true is returned.

If the price is 8.50, return fasle.

Mod calculates the remainder of division 5 mod 2 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.