Binding a variable

Monday, 28 October 2019 · 3 messages · last reply 29 Oct 2019
BE
Ben Engbers28 Oct 2019 13:20 #
Hi, When experimenting with my RBaseX-package (I had hoped to submit it to CRAN today), I use the following pattern: 1 Define a query 2 Create a query-object 3 Bind variables (optional) 4 Execute the query When used this pattern on the following query, everything functions as expected: declare variable $name external; for $i in 1 to 3 return element { $name } { $i } The following query is also functioning:
paste("declare variable $greet external;",
      "declare variable $friend external;",
      "declare variable $into external;",
      "let $greet := 'Greetings, my '",
      "let $friend := 'friend'",
      "for $i in 1 to 3",
      "let $friend_num := $greet || $friend || $i",
      "return insert nodes element { $friend } { $friend_num }",
      "into root",
      sep = " ")

When I modify
  "into root" to "into $p"
and bind $p to root
  Bind(query_obj, "$p", "root")
I get the following error: [XPST0008] Undeclared variable $p The Bind-function returns with code \00, indicating that it is executed without errors. Does this mean that there is a bug in my code or am I violating XQuery-syntax? Ben Engbers
CG
Christian Grün29 Oct 2019 03:18 #
Hi Ben, If you...
Quoted text · Ben Engbers · 2 lines
> and bind $p to root > Bind(query_obj, "$p", "root")
…you’ll need to add another external variable declaration in your query: "declare variable $p external;" ... Please note, in addition, that your query won’t be executable as you are trying to assign a dynamic path expression (e.g., 'root') to your query. If you need to build dynamic query strings, you’ll have to modify your original query string and send the result to the server. Hope this helps, Christian
Quoted text · Ben Engbers · 33 lines
> When used this pattern on the following query, everything functions as > expected: > declare variable $name external; for $i in 1 to 3 return element { $name > } { $i } > > The following query is also functioning: > paste("declare variable $greet external;", > "declare variable $friend external;", > "declare variable $into external;", > "let $greet := 'Greetings, my '", > "let $friend := 'friend'", > "for $i in 1 to 3", > "let $friend_num := $greet || $friend || $i", > "return insert nodes element { $friend } { $friend_num }", > "into root", > sep = " ") > > When I modify > "into root" to "into $p" > and bind $p to root > Bind(query_obj, "$p", "root") > > I get the following error: > > [XPST0008] Undeclared variable $p > > The Bind-function returns with code \00, indicating that it is executed > without errors. > > Does this mean that there is a bug in my code or am I violating > XQuery-syntax? > > Ben Engbers
BE
Ben Engbers29 Oct 2019 10:05 #
Hi Christian, Thanks for the explanation. I am glad to learn that - at least for this moment - I don't have to change my code :-) Ben
Quoted text · Christian Grün · 19 lines
> Hi Ben, > > If you... > >> and bind $p to root >> Bind(query_obj, "$p", "root") > > …you’ll need to add another external variable declaration in your query: > > "declare variable $p external;" > ... > > Please note, in addition, that your query won’t be executable as you > are trying to assign a dynamic path expression (e.g., 'root') to your > query. If you need to build dynamic query strings, you’ll have to > modify your original query string and send the result to the server. > > Hope this helps, > Christian

⚡Generated with XQuery