In a given XML file, I'm trying to search for the presence of a string using XPath
in Java. However, even though the string is there, my output is always coming up as a No. Hoping someone here can point out what I may be doing wrong?
XML File:
<article>
<body>
<section>
<h1>intro1</h1>
<region>introd1</region>
<region>introd2</region>
</section>
<section>
<h1 class="pass">1 task objectives</h1>
<region>object1</region>
<region>object2</region>
</section>
<section>
<h1 class="pass">1 task objectives</h1>
<region>object1</region>
<region>This is the Perfect Word I am looking for</region>
</section>
</body>
</article>
Within Java, I'm trying to check for the presence of the word "perfect"
like this:
expr = xpath.compile("//article//body//section//region[contains(.,'perfect')]");
object result = expr.evaluate(doc,XPathConstants.NODESET);
NodeList nodes = (NodeList)result;
if (nodes.getLength() > 0) {
System.out.println("Found");
// do other stuff here
} else {
System.out.println("Not found");
}
When I run this, the output is always "Not Found"
. Any idea what I am doing wrong?
XML/XPath are case-sensitive, your XPath should be
To make case-insensitive, use this
I tested this...
Against
Using...
Which outputted...