我可以在与XQJ绑定的变量中让XQuery评估{$ expression}吗? [英] Can I make XQuery evaluate { $expression } within variables bound with XQJ?

查看:84
本文介绍了我可以在与XQJ绑定的变量中让XQuery评估{$ expression}吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要模拟XQuery Update中的自动增量值,在第一次运行<root count="0"/>的情况下,以下操作可以正常工作:

To mimic an auto-increment value in XQuery Update, the following works fine, assuming <root count="0"/> when running this for the first time:

let $count := /root/@count
return (
  insert node <node id='{ $count }'/> into /root,
  replace value of node $count with $count + 1  
)

...很好地屈服:

<root count="1">
  <node id="0">
</root>

但是,我想在Java代码中定义节点,然后

However, I'd like to define the node in my Java code, and then bind that as an org.w3c.dom.Node or Document, or even String. Like:

String expr =
     " declare variable $n external; "
   + " let $count := /root/@count; "
   + " return ( "
   + "   insert node $n into /root, "
   + "   replace value of node $count with $count + 1 "
   + " ) ";
XQConnection xqc = ...;
XQPreparedExpression xqp = xqc.prepareExpression(expr);
// org.w3c.dom.Node node is <node id='{ $count }'/>
xqp.bindNode(new QName("n"), node, null);
xqp.executeQuery();

但是,这只是在属性中留下了文本 { $count }.将节点绑定为xs:string值具有相同的效果.

However, this just leaves me the text { $count } in the attribute. Binding the node as an xs:string value has the same effect.

当然,这是防止"XQuery注入"的好方法.还有:是否有任何方法可以使XQuery Update进程成为我在变量本身中包含的括起来的表达式?

Of course, this is a nice protection against "XQuery injection". Still then: is there any way to make XQuery Update process an enclosed expression I have in the variables themselves?

(也非常欢迎在XQuery中使用自动增量值的任何其他聪明的主意,但请参见

(Any other smart ideas to use auto-increment values in XQuery are very welcome too, but then see Auto increment with XQuery Update?)

推荐答案

说到注入...为什么不只是将节点作为字符串传递并使用basex:eval()?

Speaking of injection...why not just pass the node as a string and use basex:eval()?

String node = "<node id='{ $count }'/>";
String expr =
   ...
   + "   insert node xquery:eval($n) into /root, "
   ...

上方,xquery: 指的是BaseX模块.

这篇关于我可以在与XQJ绑定的变量中让XQuery评估{$ expression}吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆