1
votes

Using XQuery (BaseX implementation), I need to process a set of XML records that may contain pairs of curly brackets within element text (e.g., <a>Abc {de} fg</a>). Right now, I am preprocessing the records outside of XQuery to ensure that these characters have been escaped as double brackets (<a>Abc {{de}} fg</a>).

Is there a standard way to escape these characters within XQuery in order to prevent them from being executed as an expression?

1
If you have an XML source, don't you parse that with doc or collection where it then shouldn't matter that there are curly braces? After all, they only have a special meaning in XQuery code, but not in XML input. - Martin Honnen
You're right, of course. My confusion came from trying to work with XQuery from a Python client library. - tat

1 Answers

1
votes

If you want to embed arbitrary UTF-8 encoded XML input in your query, you can use the XQuery 3.1 string constructor and fn:parse-xml:

let $string := ``[<a>Abc {de} fg</a>]``
return fn:parse-xml($string)

The only thing you should ensure is that your XML string does not contain the characters ]``.

In general, the safest way is to save your input in external files (or in a database) and address it via fn:doc, fn:collection, fetch:xml, db:open, etc.