Expand my Community achievements bar.

Dive into Adobe Summit 2024! Explore curated list of AEM sessions & labs, register, connect with experts, ask questions, engage, and share insights. Don't miss the excitement.

& charter while querying CQ DAM with xpath

Avatar

Level 2

Hi All,

While querying a legacy CQ DAM for its assets using xpath, we are getting a "javax.jcr.query.InvalidQueryException" due to the presence of & character in the file name. Is there a cleaner way to escape & character from our query? Any pointer would be helpful.

java.lang.Exception: javax.jcr.query.InvalidQueryException: Lexical error at line 1, column 127.  Encountered: "&" (38), after : "" for statement: for $v in /jcr:root/content/dam/acme//*[@fileReference='/content/dam/ackme/img/abc's Tattoo/cde-&-fgh-abc's-Tattoo-1.jpg'] return $v

 

Thanks in advance

3 Replies

Avatar

Level 10

If you're building a path from user-supplied names, you need to escape illegal JCR characters (eg "item:1" becomes "item%3A1"):

String name = "/foo/" + Text.escapeIllegalJcrChars(name);

If you want to use paths in XPath queries, though, you need to escape according to ISO9075 rules (eg "1hr0" becomes "_x0031_hr0"):

String query = "/jcr:root" + ISO9075.encodePath(node.getPath()) + "/" + ISO9075.encode(name);

If this is a full text search or search then use following:

String searchTerm = "/content/dam/ackme/img/abc's Tattoo/cde-&-fgh-abc's-Tattoo-1.jpg"; String q = "/jcr:root/foo/element(*, foo)" + "[jcr:contains(@fileReference, '" + Text.escapeIllegalXpathSearchChars(searchTerm).replaceAll("'", "''") + "')]"

Read these for escaping a name in search:

 http://www.day.com/specs/jcr/1.0/6.6.4.9_Escaping.html
  http://www.day.com/specs/jcr/1.0/6.4.3_Escaping_of_Names.html

Avatar

Level 2

Amit_Kumar wrote...

If you're building a path from user-supplied names, you need to escape illegal JCR characters (eg "item:1" becomes "item%3A1"):

  1. String name = "/foo/" + Text.escapeIllegalJcrChars(name);

If you want to use paths in XPath queries, though, you need to escape according to ISO9075 rules (eg "1hr0" becomes "_x0031_hr0"):

 
  1. String query = "/jcr:root" + ISO9075.encodePath(node.getPath()) + "/" + ISO9075.encode(name);

If this is a full text search or search then use following:

 
  1. String searchTerm = "/content/dam/ackme/img/abc's Tattoo/cde-&-fgh-abc's-Tattoo-1.jpg";
  2. String q = "/jcr:root/foo/element(*, foo)" +
  3. "[jcr:contains(@fileReference, '" + Text.escapeIllegalXpathSearchChars(searchTerm).replaceAll("'", "''") + "')]"

Read these for escaping a name in search:

 http://www.day.com/specs/jcr/1.0/6.6.4.9_Escaping.html
  http://www.day.com/specs/jcr/1.0/6.4.3_Escaping_of_Names.html

 

Thanks for the quick response.

But I'm a bit confused. In the sample given what id "foo" stand for.

If I'm searching the said image from /content/dam/acme can I modify the query like

String q = "/jcr:root/content/dam/ackme/element(*, /content/dam/ackme)" + "[jcr:contains(@fileReference, '" + Text.escapeIllegalXpathSearchChars(searchTerm).replaceAll("'", "''") + "')]"

Avatar

Level 10
  1. foo/element was a sample nothing more
  2. In your case this will be changed to 
  3. String q = "/jcr:root/content/dam/ackme//*" + "[jcr:contains(@fileReference, '" + Text.escapeIllegalXpathSearchChars(searchTerm).replaceAll("'", "''") + "')]"