由于名称中存在撇号,无效的XPath表达式异常 [英] Invalid XPath expression exception due to presence of apostrophe in name

查看:122
本文介绍了由于名称中存在撇号,无效的XPath表达式异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下代码的无效Xpath异常。

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");

当current.name中的字符串中包含撇号时发生异常

Exception occurs when the string in current.name has an apostrophe in it

current.name:Répartitionparsecteur'activité

current.name: "Répartition par secteur d'activité"

错误信息

推荐答案

你可以通过加倍来逃避报价:

You can escape the quote by doubling it:

current.Name = current.Name.replace("'", "''");

编辑:

对于Xpath 1.0,您可以尝试以下方法:

For Xpath 1.0, you could try the following:

String xp1 = "//page[@name=\""+current.Name+"\"]" ;

I.e。使用双引号而不是单引号来分隔名称(尽管这意味着您将无法使用双引号搜索字符串。

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.

这篇关于由于名称中存在撇号,无效的XPath表达式异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆