I am getting Invalid Xpath Exception for following code.
current.Name = current.Name.replace("'", "\'");
System.out.println(current.Name );
String xp1 = "//page[@name='"+current.Name+"']" ;
Element n = (Element) oDocument.selectSingleNode(xp1+"/Body/contents");
Exception occurs when the string in current.name has an apostrophe in it
current.name: "Répartition par secteur d'activité"
Error Message
You can escape the quote by doubling it:
current.Name = current.Name.replace("'", "''");
EDIT:
For Xpath 1.0, you could try the following:
String xp1 = "//page[@name=\""+current.Name+"\"]" ;
I.e. Use double quotes instead of single quotes to delimit the name (though that means you won't be able to search for strings with a double quote.
Note also, for the second solution you won't need to replace the quotes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With