Is it possible to make a link in an xquery file (.xq) a variable?
For example, I have the following link and I need to turn this into a variable so that I can use it with a for-contains code.
{ for $x in doc("myfile.xml")/portion/food
return <li><a href="#link"></a>{data($x)}</li>}</ul>
{ for $y in doc("myfile.xml")/portion[contains(., <THIS IS WHERE THE LINK VARIABLE SHOULD GO>)]
I know to set a variable is as follows, but havent been able to put the 2 together to make it work
declare variable $hello := ("Hello World");
Your question combines different points that should be handled separately. If i understand correctly, you want to display a query result in a web browser, and you want to manipulate the DOM of the result page after it was created, on user interaction. The first part (display in browser) isn’t the problem, i think.
Since the browser just displays the query result that’s serialized into a file (in the same way a simple text editor would do), there’s basically no “contact” or “communication” between browser and XQuery engine that would allow for re-evaluating any user interaction. So, your question basically asks how to create such a way of re-evaluating an XQuery based on a custom parameter.
The eXist-db‘s documentation has an interesting yet simple example, a number-guessing game that requires server-side evaluation of user input and modifying the page content on each guess. It’s completely implemented in XQuery, using eXist-dbs’s session handling and XQuery Servlet capabilities.
It should be quite easy to adapt the number guessing example to your needs. You don’t even need to track the guesses count, so it isn’t required to create a session on the server. It’s sufficient to send the selected item’s
idwith the request (as the example demonstrates with the guessed number, using a plain HTML form), and to evaluate that request property using an XQuery function. In the example, thelocal:guessfunction is responsible for this task, and it creates the appropriate HTML output, too.Such a solution depends on the XQuery engine’s capabilities. Maybe a “standalone” solution would be possible using Javascript, but that seems to be beyond the scope of the question.