tag:blogger.com,1999:blog-53351193587644047222023-11-15T08:18:04.919-08:00DevCenterAmbrish Kumarhttp://www.blogger.com/profile/06271104579199984342noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-5335119358764404722.post-8474879563753589602011-07-14T22:50:00.000-07:002011-07-19T03:02:52.513-07:00XPath<span style="font-weight: bold;"></span>What is XPath?<br />
<br />
• XPath is a syntax for defining parts of an XML document<br />
• XPath uses path expressions to navigate in XML documents<br />
• XPath contains a library of standard functions<br />
• XPath is a major element in XSLT<br />
• XPath uses a compact , non-XML syntax to facilitate use of XPath within URIs and XML attribute values.<br />
In XPath, there are seven kinds of nodes: element, attribute, text, namespace, processing-instruction, comment, and document nodes.<br />
<b>Selecting nodes</b><br />
XPath uses path expressions to select nodes in an XML document. The node is selected by following a path or steps. The most useful path expressions are listed below....<br />
<table border="1"><tbody>
<tr> <th>Expression</th> <th>Description</th> </tr>
<tr> <td>nodename</td> <td>Selects all child nodes of the named node</td> </tr>
<tr> <td>/</td> <td>Selects from the root node</td> </tr>
<tr> <td>//</td> <td>Selects nodes in the document from the current node that match the selection no matter where they are</td> </tr>
<tr> <td>.</td> <td>Selects the current node</td> </tr>
<tr> <td>..</td> <td>Selects the parent of the current node</td> </tr>
<tr> <td>@</td> <td>Selects attributes</td> </tr>
</tbody></table><br />
AAA -- selects all the child nodes of thr AAA element <br />
<br />
<b>XPath as filesystem addressing:-</b><br />
The basic XPath syntax is similar to filesystem addressing. If the path starts with the slash / , then it represents an absolute path to the required element. <br />
/xxx - select the root element xxx<br />
<br />
AAA/xxx -- selects all the xxx elements that are children of AAA<br />
<br />
<b>Start with //</b><br />
If the path starts with // then all elements in the document which fulfill following criteria are selected.<br />
//BBB -- Selects all BBB elements no matter where they are in the document<br />
DDD//BBB -- Selects all BBB elements that are descendant of the DDD element, no matter where they are under the DDD element<br />
<br />
<b>Attributes are specified by @ prefix.</b><br />
//@aks -- Selects all attributes that are named aks<br />
//BBB[@id] -- Select BBB elements which have attribute id <br />
<br />
<b>Selecting unknown nodes nodes</b><br />
XPath wildcards can be used to select unknown XML elements.<br />
<table border="1"><tbody>
<tr> <th>Wildcard</th> <th>Description</th> </tr>
<tr> <td>*</td> <td>matches any element node</td> </tr>
<tr> <td>@*</td> <td>Matches any attribute node</td> </tr>
<tr> <td>node()</td> <td>Matches any node of any kind</td> </tr>
<tr> <td>.</td> <td>Selects the current node</td> </tr>
</tbody></table><br />
/title/* -- Selects all the child nodes of the title element<br />
//* -- Selects all elements in the document<br />
//title[@*] -- Selects all title elements which have any attribute<br />
//title[not(@*)] -- Select title elements without an attribute<br />
<br />
<b>Selecting Several Paths</b><br />
By using the | operator in an XPath expression you can select several paths.<br />
pathexpr1 | pathexpr2 <br />
pathexpr1 or pathexpr2<br />
<b>Location Path Expression</b><br />
A location path can be absolute or relative.<br />
An absolute location path starts with a slash ( / ) and a relative location path does not. In both cases the location path consists of one or more steps, each separated by a slash:<br />
An absolute location path:<br />
<br />
/step/step/...<br />
A relative location path:<br />
<br />
step/step/... <br />
Each step is evaluated against the nodes in the current node-set.<br />
A step consists of:<br />
• an axis (defines the tree-relationship between the selected nodes and the current node)<br />
• a node-test (identifies a node within an axis)<br />
• zero or more predicates (to further refine the selected node-set)<br />
<b>Predicates</b><br />
Predicates are used to find a specific node or a node that contains a specific value.<br />
Predicates are always embedded in square brackets.<br />
<br />
In the table below we have listed some path expressions with predicates and the result of the expressions<br />
<div><table border="1"><tbody>
<tr> <th>Path Expression</th> <th>Result</th> </tr>
<tr> <td>/bookstore/book[1]</td> <td>Selects the first book element that is the child of the bookstore element.</td> </tr>
<tr> <td>/bookstore/book[last()]</td> <td>Selects the last book element that is the child of the bookstore element</td> </tr>
<tr> <td>/bookstore/book[last()-1]</td> <td>Selects the last but one book element that is the child of the bookstore element</td> </tr>
<tr> <td>/bookstore/book[position()<3]</td> <td>Selects the first two book elements that are children of the bookstore element</td> </tr>
<tr> <td>//title[@lang]</td> <td>Selects all the title elements that have an attribute named lang</td> </tr>
<tr> <td>//title[@lang='eng']</td> <td>Selects all the title elements that have an attribute named lang with a value of 'eng'</td> </tr>
<tr> <td>/bookstore/book[price>35.00]</td> <td>Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00</td> </tr>
<tr> <td>/bookstore/book[price>35.00]/title</td> <td>Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00</td> </tr>
</tbody></table></div>Ambrish Kumarhttp://www.blogger.com/profile/06271104579199984342noreply@blogger.com0