0

I want to perform dynamic combination (AND OR) search on the basis of the provided parameter by the user.

Search Combination Example:

( (title = "United States" or isbn = "2345371242192") and author ="Jhon" )

In the above query each parameter will look on their XPATH e.g. (item/tigroup/title, item/isbn), XPATH not provided by the user, i have to generate XPATH dynamically with search combination

How Combination query can be formed dynamically to pass it to the BaseX?

User can perform any kind of AND OR search, their can be multiple AND OR criteria

Any suggestions much appreciated

  • In what way does the user provide the parameters to XQuery, are the declared as external variables? Is the input XML format known/fixed? Where do the XPath expressions like `item/tigroup/title` come from? – Martin Honnen Nov 12 '22 at 09:52
  • @MartinHonnen The parameter will come from front-end where user will choose the filed and their value with the AND OR combination, i am asking from front-end to provide ( (title = "United States" or isbn = "2345371242192") and author ="Jhon" ) and i am thinking to concatenate the corresponding XPATH on they fly and send this to basesx db in the predicate – Dharmendra Kumar Singh Nov 12 '22 at 11:46
  • e.g. db:open('test')/*[ ( ( $title = item/tigroup/title or $isbn= item/isbn ) and normalize-space(string-join((item/author)[1]/*:personname//text()) ) = $Author )]/base-uri() is is possible to achieve this way – Dharmendra Kumar Singh Nov 12 '22 at 11:56
  • How should i ask Combination (AND OR) parameter from front-end so that i could apply in the predicate by concatenating XPATH as i have mentioned in the above comment. – Dharmendra Kumar Singh Nov 12 '22 at 12:07

1 Answers1

0

With xquery:eval, strings can be evaluated as XQuery expression (see the documentation for more examples):

declare variable $QUERY := 'text()';

db:open('db')//*[xquery:eval($QUERY, map { '': . })]

Please note that it’s very risky to evaluate arbitrary strings as XQuery code. If the string contains user input, malicious strings may be passed on that do unexpected things. In the example above, a malicious string could be a file operation (e.g., file:delete(.)), or a query that runs very long and blocks your system.

Christian Grün
  • 6,012
  • 18
  • 34
  • Can You please suggest better way to achieve this – Dharmendra Kumar Singh Nov 13 '22 at 13:28
  • ...what exactly? – Christian Grün Nov 13 '22 at 19:18
  • I doubt there is a simple answer to your requirement. We’d at need to know more about how your frontend generates the query string. The mailing list may be a better place to dive into this. – Christian Grün Nov 14 '22 at 10:37
  • as i mentioned front-end will generate the query e.g.( (title = "United States" or isbn = "2345371242192") and author ="Jhon" ) well grouping with AND OR operator, so if you look the parameter e.g.(title, isbn author) each parameter will look into some specific XPATH to compare value then the result of query will return the URI of the document. It is the REST API and GET method, so my Question is here that, is this right way generating combination query to pass to BaseX so that i can achieve the Combination (AND OR) search. – Dharmendra Kumar Singh Nov 14 '22 at 11:27
  • Is there any other way to generate the combination (AND OR) query so that i can directly Pass it to BaseX ? – Dharmendra Kumar Singh Nov 14 '22 at 11:29
  • As i have mentioned this is REST GET call then i if use rest:query-param for the parameter then how can i manage each parameter ($title, $isbn, $author) in the combination(AND OR) search. – Dharmendra Kumar Singh Nov 14 '22 at 12:04
  • If you need further assistance, you’ll probably have to describe details on the construction of nested AND/OR combinations in more detail, e.g. via the mailing list. – Christian Grün Nov 14 '22 at 15:30
  • i have sent this on mailing list BaseX-talk – Dharmendra Kumar Singh Nov 14 '22 at 16:17