XPath is the XML query language, and the role of SQL is very similar. The following XML is an example, introducing the syntax of XPath.
Xml Version = "1.0" encoding = "ISO-8859-1"?>
cd>
cd>
cd>
catalog>
The positioning node XML is a tree structure, the structure of the data clip in the similar archive system, XPath is similar to the path naming method of the archive system. However, XPath is a pattern that can be selected in the XML file, and the path is in line with all nodes of a mode. For example, all Price elements can be used in Catalog under Catalog.
/ catalog / cd / price
If the beginning of XPath is a slash (/) representing this is an absolute path. If the beginning is two oblique lines (//) indicates that all elements in the file are selected, even if they are in the tree, different hierarchies are also selected. The following grammar will select all elements called CD in the file (any level in the tree will be selected):
// CD
Select Unknown Elements Use an asterisk (Wildcards, *) to select unknown elements. Below this grammar selection / Catalog / CD all child elements:
/ catalog / cd / *
The following grammar will elect the child elements of all Catalog, which contains the elements of the PRICE as a child.
/ catalog / * / price
The following grammar selection has two parent nodes called all elements of the Price.
/ * / * / price
The following grammar will select all the elements in the file.
// *
It is to be noted that if you want to access an elements that don't hierarchically, the XPath syntax must begin with two slashes (//), you want to access unknown elements to use an asterisk (*), as an asterisk can only represent unknown elements, Do not represent an unknown level of elements.
Select branches to use brackets to select branches. The following syntax removes the first element called the CD from the child elements of Catalog. There is no 0 element in the definition of XPath.
/ Catalog / CD [1]
The following syntax is selected in the last CD element in Catalog: (Xpathj does not define the first element in the first () of [1] with the previous example.
/ Catalog / CD [Last ()]
The following syntax elects all / Catalog / CD elements containing the PRICE child elements.
/ catalog / cd [price]
The following syntax elects the value of the price element equal to 10.90 / Catalog / CD element
/catalog/cd[proice=10.90]
The following syntax elects the value of the Price element equal to all / catalog / cd elements of 10.90 / Catalog/cd[Price = 10.90]/price
Select one or more paths to use the OR operational element (|) to select more than one path. E.g:
/ catalog / cd / title | Catalog / CD / Artist
Select all Title and Artist elements
// Title | // Artist
Select all Title and Artist and Price elements
// Title | // Artist | // Price
Select Properties In XPath, in addition to selecting an element, you can also select attributes. Attributes are out of @. For example, select all properties called Country in the file:
@ County
Select all CD elements containing this property:
// CD [@country]
The following syntax selection all CD elements containing properties
// CD [@ *]
The following syntax selects a CD element with a country property value UK
// CD [@ country = 'uk']