265 <h3>4. XPath Context</h3>
266
267 <p>XPath location paths may be relative to a particular node in the
268 document, known as the <code>context</code>. A context consists of:
269 <ul>
270 <li>a node (the context node)</li>
271 <li>a pair of non-zero positive integers (the context position and the context size)</li>
272 <li>a set of variable bindings</li>
273 <li>a function library</li>
274 <li>the set of namespace declarations in scope for the expression</li>
275 </ul>
276
277 <p>
278 It is an XML document tree represented as a hierarchy of nodes, a
279 {@link org.w3c.dom.Node} for example, in the JDK implementation.
280
281 <a name="XPath.Use"></a>
282 <h3>5. Using the XPath API</h3>
283
284 Consider the following XML document:
285 <p>
286 <blockquote>
287 <pre>
288 <widgets>
289 <widget>
290 <manufacturer/>
291 <dimensions/>
292 </widget>
293 </widgets>
294 </pre>
295 </blockquote>
296
297 <p>
298 The <code><widget></code> element can be selected with the following process:
299
300 <blockquote>
301 <pre>
302 // parse the XML as a W3C Document
303 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
304 Document document = builder.parse(new File("/widgets.xml"));
305
349 In the above cases, the type of the expected results are known. In case where
350 the result type is unknown or any type, the {@link javax.xml.xpath.XPathEvaluationResult}
351 may be used to determine the return type. The following code demonstrates the usage:
352 <blockquote>
353 <pre>
354 XPathEvaluationResult<?> result = xpath.evaluateExpression(expression, document);
355 switch (result.type()) {
356 case NODESET:
357 XPathNodes nodes = (XPathNodes)result.value();
358 ...
359 break;
360 }
361 </pre>
362 </blockquote>
363
364 <p>
365 The XPath 1.0 Number data type is defined as a double. However, the XPath
366 specification also provides functions that returns Integer type. To facilitate
367 such operations, the XPath API allows Integer and Long to be used in
368 {@code evaluateExpression} method such as the following code:
369 <p>
370 <blockquote>
371 <pre>
372 int count = xpath.evaluate("count(/widgets/widget)", document, Integer.class);
373 </pre>
374 </blockquote>
375
376 @since 1.5
377
378 </body>
379 </html>
|
265 <h3>4. XPath Context</h3>
266
267 <p>XPath location paths may be relative to a particular node in the
268 document, known as the <code>context</code>. A context consists of:
269 <ul>
270 <li>a node (the context node)</li>
271 <li>a pair of non-zero positive integers (the context position and the context size)</li>
272 <li>a set of variable bindings</li>
273 <li>a function library</li>
274 <li>the set of namespace declarations in scope for the expression</li>
275 </ul>
276
277 <p>
278 It is an XML document tree represented as a hierarchy of nodes, a
279 {@link org.w3c.dom.Node} for example, in the JDK implementation.
280
281 <a name="XPath.Use"></a>
282 <h3>5. Using the XPath API</h3>
283
284 Consider the following XML document:
285 <blockquote>
286 <pre>
287 <widgets>
288 <widget>
289 <manufacturer/>
290 <dimensions/>
291 </widget>
292 </widgets>
293 </pre>
294 </blockquote>
295
296 <p>
297 The <code><widget></code> element can be selected with the following process:
298
299 <blockquote>
300 <pre>
301 // parse the XML as a W3C Document
302 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
303 Document document = builder.parse(new File("/widgets.xml"));
304
348 In the above cases, the type of the expected results are known. In case where
349 the result type is unknown or any type, the {@link javax.xml.xpath.XPathEvaluationResult}
350 may be used to determine the return type. The following code demonstrates the usage:
351 <blockquote>
352 <pre>
353 XPathEvaluationResult<?> result = xpath.evaluateExpression(expression, document);
354 switch (result.type()) {
355 case NODESET:
356 XPathNodes nodes = (XPathNodes)result.value();
357 ...
358 break;
359 }
360 </pre>
361 </blockquote>
362
363 <p>
364 The XPath 1.0 Number data type is defined as a double. However, the XPath
365 specification also provides functions that returns Integer type. To facilitate
366 such operations, the XPath API allows Integer and Long to be used in
367 {@code evaluateExpression} method such as the following code:
368 <blockquote>
369 <pre>
370 int count = xpath.evaluate("count(/widgets/widget)", document, Integer.class);
371 </pre>
372 </blockquote>
373
374 @since 1.5
375
376 </body>
377 </html>
|