Finding Elements by Content in a DOM Document Using XPath

XPath is an expression language for selecting nodes in an XML file. See 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 <A>cat</A>, the content is simply "cat". However, in the case of an element that contains other elements, the content is the concatenation of the content of all subelements. For example, in the XML fragment <A>cat<B>,<C>dog,</C>and</B>rat</A>, the content of A is "cat,dog,andrat".

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 is " dog " rather than "dog"

// Get all elements that contain the string cat
xpath = "//*[contains(.,'cat')]";                    // 1 2 4 5 6

// Get all elem3 elements that contain the string cat
xpath = "//elem3[contains(.,'cat')]";                // 6

// Get all elements that contain the string cat,
// ignoring the contents of any subelements
xpath = "//*[contains(child::text(),'cat')]";        // 2 4 6

// Get all elements without subelements and whose contents contains the string cat
xpath = "//*[count(*)=0 and contains(.,'cat')]";     // 2 6
XPath 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 7
To execute an XPath expression, see 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>

Comments

5 Mar 2010 - 4:21am by Anonymous (not verified)

In IE 8 this method of using path is not working, why is that?

16 Feb 2011 - 11:26pm by Anonymous (not verified)

because path only works in IE9, please update your windows explorer.

1 Jan 2012 - 3:19pm by Anonymous (not verified)

LMFAO yet another unsatisfied IE customer...
@developers - stop developing exception code just for IE because it's lame and doesn't work. If it don't work leave it. Not your problem.

12 Apr 2012 - 9:14pm by Huh (not verified)

So saying 'Not my problem' to a client who is wondering why 25% of visitors cant reach their website is how you conduct business?

IE is lame but its widely still used, so unless you are comfortable losing business just to demonstrate how cutting edge you are, IE 7,8 and even 6 are still in play.

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.