Friday, August 24, 2012

xpath usage \ шпаргалка по работае с xpath


// 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 thetranslate() 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










Source: http://www.exampledepot.com/egs/org.w3c.dom/xpath_GetElemByText.html