Riju Mahna
Riju Mahna

Reputation: 6926

AEM XPATH Query issue with single quote

My application runs a query created using querybuilder API but finally, its converted into an XPath query and executed. Everything works well except when there is a quote symbol in the text. I ma using custom predicates to search the text. Example: predicateMap.put("0_group.2_group.title_customcase.title_fulltext", searchText);

I tried this in the native interface at http://localhost:4502/crx/explorer/ui/search.jsp and it fails there as well.

Search for: don't
Search in: /content/geometrixx/en
XPath Query generated: /jcr:root/content/geometrixx/en//*[jcr:contains(., 'don't')] order by @jcr:score descending

Exception (similar to my server logs):

Error: javax.jcr.query.InvalidQueryException: java.text.ParseException:

Is there any workaround for this? I did find some similar questions on SoF but not the exact thing. The querydebug page is able to search this text but it just seems to replace it with 2 quotes:

/jcr:root/content/geometrixx/en//*[jcr:contains(., 'don''t')]

So I am confused why such behavior withing AEM itself.

Upvotes: 0

Views: 970

Answers (1)

Pooja Chauhan
Pooja Chauhan

Reputation: 450

There are always two escape mechanisms you have to keep in mind:

1) escaping a character within the contains statement. e.g. the spec says you can search for a phrase using double quotes: "foo bar". that means if you want to use a double quote as literal an not a delimiter for a phrase you have to escape it.

2) escaping any string literal appropriately in XPath or SQL. E.g. in XPath the string literal must not be written as: 'don"t'

for your query that means you have to write: /jcr:root/content/geometrixx/en//*[jcr:contains(., 'don\"t')] order by @jcr:score descending

OR

you can use double quot /jcr:root/content/geometrixx/en//*[jcr:contains(., "don't")] order by @jcr:score descending

Upvotes: 1

Related Questions