From: http://blog.csdn.net/oldjavaman/
---------------------------
XPath is an XML query statement, which is similar to the SQL role. The XML below is used as an example to describe how to parse XPath.
<? XML version = "1.0" encoding = "ISO-8859-1"?> <Catalog> <CD Country = "USA"> <title> empire burlesque </title> <artist> Bob Dylan </artist> <price> 10.90 </price> </CD> <CD Country = "UK"> <title> hide your heart </title> <artist> Bonnie Taylor </artist> <price> 9.90 </price> </Cd> <CD Country = "USA"> <title> Greatest Hits </title> <artist> Dolly Parton </artist> <price> 9.90 </price> </Cd> </ catalog>
Locate metric point
XML is the structure of structured data, similar to the structure of the Internal data library of the Case System, and XPath is also similar to the path naming method of the Case System. However, XPath is a pattern. You can select all nodes that match a pattern in the XML example. For example, to select all the price elements in CD under catalog, you can use:
/CATALOG/CD/price
If the start of xpath is a diagonal line (/), it indicates the opposite path. If the start is two diagonal lines (//), it indicates that all elements in the file that match the pattern will be selected, even different segments in the topology will be selected. The following method selects all the elements named cd in the file (any member in the file will be selected ):
// CD
Select unknown elements
You can use wildcards (*) to select unknown elements. The following method selects all child elements of/CATALOG/CD:
/CATALOG/CD /*
The following method selects all the child elements of the catalog, including the elements with price as the child element.
/CATALOG/*/price
The following statement Selects all the elements named Price with two parent nodes.
/*/Price
The following selection method selects all elements in the file.
//*
It should be noted that the XPath method must start with two oblique headers (//) to access non-linear elements (//), if you want to access an unknown element, you can use the asterisk (*). A star token can only represent an element with an unknown name. It cannot represent an element with an unknown sequence.
Select Branch
You can select a branch if you use the middle-range rule. The following method extracts the first element named CD from the child element of catalog. The definition of xpath does not contain 0th elements.
/CATALOG/CD [1]
The following method selects the last CD element in the catalog: (xpathj does not define the first () function, the first element can be extracted using [1] of the preceding example.
/CATALOG/CD [last ()]
The following method selects all/CATALOG/CD elements containing the price sub-element.
/CATALOG/CD [price]
The following sort method selects all/CATALOG/CD elements whose price value is equal to 10.90.
/CATALOG/CD [price = 10.90]
The following statement selects the price element value, which is equal to the price element of all/CATALOG/CD elements of 10.90.
/CATALOG/CD [price = 10.90]/price
Select more than one path
Use or to calculate the RMB (|) to select more than one path. For example:
/CATALOG/CD/Title | CATALOG/CD/artist
Select all titles and artist elements.
// Title | // artist
Select all titles, artist, and price
// Title | // artist | // price
Optional
In XPath, you can also choose between elements. All features start. For example, in the selection file, all the features called country are as follows:
// @ Country
Select all CD elements containing the country nature:
// CD [@ Country]
The following selection method selects all CD elements with attention
// CD [@ *]
The following statement selects the CD element whose country encoding is UK.
// CD [@ Country = 'U']