jackrabbit-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Joshua Levy <l...@csl.sri.com>
Subject Re: Quote escaping in jcr:contains searchexp
Date Tue, 19 Sep 2006 16:34:28 GMT
For the record, the answer to my first question is that 
that there are two layers of escaping, firstly for
jcr:contains and secondly for XPath, so a query with
single quotes looks like:
  //element(*, my:record)[jcr:contains(.,'Love\''s Labour\''s Lost')]

I wanted to do a jcr:contains query based on input, exposing
some features (such as term exclusion with -) to end users
but with no risk of syntax errors.  In case they're of use,
are couple simple encoding functions that accomplish this
are below.

I'd still be interested in any suggestions re the general
question of libraries to assist with building XPath syntax,


    * Convert a string to an XPath 2.0 string literal, suitable for inclusion in
    * a query. See JSR-170 spec v1.0, Sec.
    * @param str
    *           Any string.
    * @return A valid XPath 2.0 string literal, including enclosing quotes.
   public static String stringToXPathLiteral(String str) {
      // Single quotes needed for jcr:contains()
      return "'" + str.replaceAll("'", "''") + "'";

    * Convert a string to a JCR search expression literal, suitable for use in
    * jcr:contains() (inside XPath queries). The characters - and " have special
    * meaning, and may be escaped with a backslash to obtain their literal
    * value. See JSR-170 spec v1.0, Sec.
    * @param str
    *           Any string.
    * @return A valid XPath 2.0 string literal suitable for use in
    *         jcr:contains(), including enclosing quotes.
   public static String stringToJCRSearchExp(String str) {
      // Escape ' and \ everywhere, preceding them with \ except when \ appears
      // in one of the combinations \" or \-
      return stringToXPathLiteral(str.replaceAll("\\\\(?![-\"])", "\\\\\\\\").replaceAll("'",

>I would be keen to hear about this.
>Is there or should there be a utility class to escape things correctly for
>XPath? Kind of like URLEncoder but for xpath?
>On 9/17/06, Joshua Levy <levy@csl.sri.com> wrote:
>> Hi,
>> I'm trying to properly escape characters in an XPath
>> query using jcr:contains.  Based on Sec of
>> the JCR spec it seemed like one should use single
>> quotes and backslash escape some characters (", ', -, \).
>> Queries like this work for me:
>>   //element(*, my:record)[jcr:contains(.,'blah\-blah')] order by
>> jcr:score() descending
>>   //element(*, my:record)[jcr:contains(.,'blah\"blah')] order by
>> jcr:score() descending
>> But
>>   //element(*, my:record)[jcr:contains(.,'blah\'blah')] order by
>> jcr:score() descending
>> throws an exception (below).
>> Am I not doing the escaping correctly, or is it a bug?
>> Also, this brings up broader question:  Are there any
>> libraries people use to help formulate (JCR) XPath queries,
>> that assist with such syntax details?  It seems rather
>> error-prone to be constructing them by hand, particularly
>> with character escaping (more than one kind), etc.
>> Many thanks!
>> Joshua

View raw message