![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e1076. Finding Elements by Content in a DOM Document Using XPathXPath is an expression language for selecting nodes in an XML file. See e1074 Finding Elements by Absolute Location in a DOM Document Using XPath for common XPath expression for selecting elements. This example adds to those examples by demonstrating the ability to select elements based on their content. In an element such as This example demonstrates some common uses of expressions that use element content; for more information on XPath, see the specification at http://www.w3c.org/TR/xpath. In the example, the result of an XPath expression is shown next to the expression; the numbers are ids of elements in the sample file shown at the end of the example. // Get all elements that equal the string cat String xpath = "//*[.='cat']"; // 2 6 // Get all elements that equal the string dog xpath = "//*[.='dog']"; // (none) // Note that element #3 does not match because its // content isXPath 1.0 does not support case-insensitive matches. However, a simple case-insensitive match can be done using the translate()
function, which converts a string by mapping one character into another:
// Get all elements that contain the string cat, ignoring case xpath = "//*[contains(translate(.,'abcdefghijklmnopqrstuvwxyz'," + " 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'),'CAT')]"; // 1 2 4 5 6 7To execute an XPath expression, see e1074 Finding Elements by Absolute Location in a DOM Document Using XPath. Here is the sample XML file used in the example: <?xml version="1.0" encoding="UTF-8"?> <root id="1"> <elem1 id="2">cat</elem1> <elem1 id="3"> dog </elem1> <elem1 id="4"> cat <elem2 id="5"> <elem3 id="6">cat</elem3> </elem2> dog </elem1> <elem1 id="7">Cat</elem1> </root>
e1075. Selecting from a Set of Child Elements in a DOM Document Using XPath e1077. Finding Elements by Id in a DOM Document Using XPath e1078. Finding Elements by Attributes in a DOM Document Using XPath
© 2002 Addison-Wesley. |