Learn XPath

XPath is a finding information in an XML document language, a navigation in the XML language used in the document. XPath can be used to traverse the elements and attributes in an XML document. The W3C XSLT XPath standard primary elements, and XQuery and XPointer are simultaneously built on XPath expression. Therefore, an understanding of XPath is the basis of many advanced XML applications.

  • XPath uses path expressions to navigate in XML documents
  • XPath contains a standard library, XPath contains over 100 built-in functions. These functions for string values, numeric, date and time comparison, node and QName processing, the processing sequence, the logical values and the like.
  • XPath is a major element in XSLT
  • XPath is a W3C standard

In XPath, there are seven types of nodes: element, attribute, text, namespaces, processing instructions, comments and document node (or root).

XPath uses path expressions to select nodes in an XML document or set of nodes. Along the path through the node (path) or step (steps) to select the.

The most useful path expressions:

expression description
nodename Select all the child nodes of this node.
/ Choose from the root node.
// Select the document matches the selected node from the current node, regardless of their location.
. Select the current node.
.. Select the parent of the current node.
@ Select Properties.

 

Predicate (Predicates)

Predicate is used to find a specific node or a node that contains the value specified. Predicate is embedded in square brackets.

Some paths with a predicate expression, and expression of the results:

Path expression result
/bookstore/book[1] Select an element belonging to the first sub bookstore book element.
/bookstore/book[last()] Select the sub-elements belonging to the bookstore last book element.
/bookstore/book[last()-1] Select the sub-elements belonging to the reciprocal bookstore's second book element.
/bookstore/book[position()<3] Select book element belonging to two sub-elements of the bookstore element foremost.
//title[@lang] Select all of lang has a property named title element.
// title [@ lang = 'What'] Select all the title elements, and these elements have a lang attribute value of eng.
/bookstore/book[price>35.00] Select all the book elements bookstore element, and wherein the price element value must be greater than 35.00.
/bookstore/book[price>35.00]/title

Select all the title elements of the book element bookstore element, and wherein the value of the price element must be greater than 35.00.

 

XPath wildcards can be used to select unknown XML elements.

Tsuhaifu description
* Matches any element node.
@* Matches any attribute node.
node() Match any type of node.

Select several paths: by using the path expression "|" operator, you can select several paths.

Path expression result
//book/title | //book/price Select the book title and price elements of all the elements.
//title | //price Select all title and price elements in the document.
/bookstore/book/title | //price Select book element of the bookstore element belonging to all the title elements, as well as document all the price elements.

XPath axis: the axis defined with respect to the set of nodes of the current node.

Axis name result
ancestor Select all ancestors (parent, grandparent, etc.) of the current node.
ancestor-or-self Select all ancestors (parent, grandparent, etc.) of the current node and the current node itself.
attribute Select all the attributes of the current node.
child Select all the child elements of the current node.
descendant Select all descendants of the current node elements (son, Sun, etc.).
descendant-or-self Select all descendants of the current node elements (son, Sun, etc.) as well as the current node itself.
following Select all nodes in the document after the closing tag of the current node.
namespace Select all the namespace of the current node.
parent Select the parent of the current node.
preceding Select all the nodes in the document before the start tag of the current node.
preceding-sibling Select all siblings before the current node.
self Select the current node.

Step (step) include:

  • Tree defining a relationship between the selected node and the current node: a shaft (axis)
  • Test nodes (node-test): identifying an internal node shaft
  • Zero or more predicates (predicate): deeper refine the set of nodes selected

Step syntax:              axis :: node name test [predicate]

example result
child::book Select all book nodes belonging to the current node's child elements.
attribute :: long Select lang attribute of the current node.
child::* Select all the child elements of the current node.
attribute::* Select all the attributes of the current node.
child::text() Select all text child nodes of the current node.
child::node() Select all child nodes of the current node.
descendant::book Select all book descendants of the current node.
ancestor::book Select all book ancestors of the current node.
ancestor-or-self::book Select all book ancestors of the current node and the current node (if the node is a book node)
child::*/child::price Select all price grandson of the current node.

XPath operators : The following lists the available operators in XPath expression:

Operators description Examples return value
| 计算两个节点集 //book | //cd 返回所有拥有 book 和 cd 元素的节点集
+ 加法 6 + 4 10
- 减法 6 - 4 2
* 乘法 6 * 4 24
div 除法 8 div 4 2
= 等于 price=9.80

如果 price 是 9.80,则返回 true。

如果 price 是 9.90,则返回 false。

!= 不等于 price!=9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 false。

< 小于 price<9.80

如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 false。

<= 小于或等于 price<=9.80

如果 price 是 9.00,则返回 true。

如果 price 是 9.90,则返回 false。

> 大于 price>9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.80,则返回 false。

>= 大于或等于 price>=9.80

如果 price 是 9.90,则返回 true。

如果 price 是 9.70,则返回 false。

or price=9.80 or price=9.70

如果 price 是 9.80,则返回 true。

如果 price 是 9.50,则返回 false。

and price>9.00 and price<9.90

如果 price 是 9.80,则返回 true。

如果 price 是 8.50,则返回 false。

mod 计算除法的余数 5 mod 2

实例

XML 文件

<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book>
<book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book>
<book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book>
<book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book>
</bookstore>

HTML文件

<html>
<body>
<script type="text/javascript">
function loadXMLDoc(dname)
{
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET",dname,false);
xhttp.send("");
return xhttp.responseXML;
}

xml=loadXMLDoc("/example/xmle/books.xml");
path="/bookstore/book/title"
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);
for (i=0;i<nodes.length;i++)
  {
  document.write(nodes[i].childNodes[0].nodeValue);
  document.write("<br />");
  }
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE, null);
var result=nodes.iterateNext();
while (result)
  {
  document.write(result.childNodes[0].nodeValue);
  document.write("<br />");
  result=nodes.iterateNext();
  }
}
</script>
</body>
</html>

输出

Harry Potter
Everyday Italian
Learning XML
XQuery Kick Start

参考资料:

转载于:https://www.cnblogs.com/JoannaQ/archive/2013/04/24/3039333.html

Guess you like

Origin blog.csdn.net/weixin_34037173/article/details/93056251