Search code examples
javaxmlxpathvtd-xml

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.

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? When I try running the xpath query in XML Spy with the same document it runs fine.


Solution

  • 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

    It was written in javascript so I converted it to 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.