Case-Insensitive XPath contains() Function
In XPath, the contains() function is case-sensitive. However, there are ways to work around this limitation.
Method 1: Translate Characters
This method involves translating characters to lowercase or uppercase before checking for the substring:
/html/body//text()[ contains( translate(., 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), 'test' ) ]
This converts all uppercase letters in the text to lowercase before checking for the substring "test."
Method 2: Dynamic XPath Expression
Using a scripting language like JavaScript, you can construct a dynamic XPath expression that handles case insensitivity:
function xpathPrepare(xpath, searchString) {
return xpath.replace("$u", searchString.toUpperCase())
.replace("$l", searchString.toLowerCase())
.replace("$s", searchString.toLowerCase());
}
xp = xpathPrepare("//text()[contains(translate(., '$u', '$l'), '$s')]", "Test");
This replaces placeholders in the XPath expression with uppercase, lowercase, and lowercase versions of the search string.
Additional Considerations
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3