简体   繁体   English

VTD-XML和Xpath 2.0转义字符串

[英]VTD-XML and Xpath 2.0 escaping string

When I try to running the following xpath expression in Java using VTD-XML I get an unexpected error. 当我尝试使用VTD-XML在Java中运行以下xpath表达式时,出现意外错误。

Code: 码:

..
AutoPilot ap = new AutoPilot(vn);
ap.selectXPath("/a//b[text() = 'apple''banana']");

Error: 错误:

Syntax error after or around the end of ==> /a//b[text() = 'apple'
Caused by: com.ximpleware.XPathParseException: XPath Syntax error: #29
    at com.ximpleware.xpath.parser.unrecovered_syntax_error(parser.java:492)
    at java_cup.runtime.lr_parser.parse(lr_parser.java:601)
    at com.ximpleware.AutoPilot.selectXPath(AutoPilot.java:809)

Is this not a bug? 这不是错误吗? I was under the impression that escaping single quotes in XPath 2.0 was acceptable? 我的印象是,在XPath 2.0中转义单引号是可以接受的吗? When I try running the xpath query in XML Spy with the same document it runs fine. 当我尝试使用同一文档在XML Spy中运行xpath查询时,它运行良好。

由于XPath还支持".."字符串,其中可以包含未转义的'-s,因此您可以使用:

   ap.selectXPath("/a//b[text() = \"apple'banana\"]");

Escaping is a matter that is entirely left to the host language that uses XPath to query data. 转义是完全由使用XPath查询数据的主机语言来解决的问题。 The escaping rules of that higher-level language usually also apply to XPath expressions used therein. 该高级语言的转义规则通常也适用于其中使用的XPath表达式。

I quote from Michael Kay's XPath 2.0 Programmer's Reference: 我引用了Michael Kay的XPath 2.0程序员参考:

Similarly, when XPath expressions are written within character strings in a host language such as Java, you will need to use the escaping conventions of that language: for example, [...] a quotation mark as \\" . 类似地,当XPath表达式以诸如Java之类的宿主语言写在字符串中时,您将需要使用该语言的转义约定:例如,引号为\\"

In other words, '' does escape a single quote, in XSLT 2.0 . 换句话说, 在XSLT 2.0中'' 确实转义了单引号。 In Java, the single quote must be escaped as \\' I guess. 在Java中,单引号必须转义为\\'我想。

ap.selectXPath("/a//b[text() = 'apple\'banana']");

Unfortunately it looks like escaping isn't an option, I had to write a custom function based on the following: 不幸的是,看起来转义不是一个选择,我不得不根据以下内容编写一个自定义函数:

XQuery looking for text with 'single' quote XQuery寻找带有“单”引号的文本

It was written in javascript so I converted it to Java: 它是用JavaScript编写的,因此我将其转换为Java:

private static String cleanStringForXPath(String dirtyString)
    {
        Pattern pattern = Pattern.compile("([^'\"]+|['\"])");
        Matcher matcher = pattern.matcher(dirtyString);

        int count = 0;
        StringBuilder sb = new StringBuilder();

        while(matcher.find()) {
            String part = matcher.group(1);
            if(part.equals("'")) {
                sb.append("\"'\"");
            } else if(part.equals("\"")) {
                sb.append("'\"'");
            } else { 
                sb.append("'" + part + "'");
            }
            sb.append(",");
            count++;
        }

    String result = sb.length() > 0 ? sb.substring(0, sb.length() - 1): "";
    return (count > 1) ? "concat(" + result + ")" : result;
}

I tested this function and it seems to resolve my problem. 我测试了此功能,看来可以解决我的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM