如何在 xquery 的 xpath 中使用元素变量

发布于 2024-10-01 22:44:40 字数 957 浏览 0 评论 0原文

我正在使用以下 xml 文档

<users>
  <user>
    <userid>sony</userid>
    <password>sonypass</password>
  </user>
</users>

我正在尝试编写一个常见的 xquery 通过传递变量来更新用户信息 - a) doc_name (xml 文档的名称) b)userid (要更新信息的用户的 userid) c) update_node(要更新的元素,例如密码) d) new_value(要更新的元素的新值,例如sonynewpass)。我不确定如何使用 xpath 中的元素变量来执行此操作。我尝试了以下操作:

declare variable $doc_name as xs:string external;
declare variable $userid as xs:string external;
declare variable $update_node as xs:element() external;
declare variable $new_value as xs:string external;
let $users_doc := doc($doc_name)/users

let $old_node := $users_doc/user[userid=$userid]/{$update_node}

return replace value of node $old_node with $new_value

但是,此查询在第 7 行中抛出 [XPST0003] Expecting location path error (let $old_node := $users_doc/user[userid=$userid]/{$update_node})

有没有办法使用xpath 中的元素变量?

谢谢, 索尼

I am using the following xml document

<users>
  <user>
    <userid>sony</userid>
    <password>sonypass</password>
  </user>
</users>

I am trying to write a common xquery to update the user information by passing the variables - a) doc_name (name of the xml document) b)userid (userid of the user whose info is to be updated) c) update_node (element to be updated e.g. password) d) new_value (new value of the element to be updated e.g. sonynewpass). I am not sure how to use the element variable in the xpath to do this. I tried the following:

declare variable $doc_name as xs:string external;
declare variable $userid as xs:string external;
declare variable $update_node as xs:element() external;
declare variable $new_value as xs:string external;
let $users_doc := doc($doc_name)/users

let $old_node := $users_doc/user[userid=$userid]/{$update_node}

return replace value of node $old_node with $new_value

But, this query is throwing a [XPST0003] Expecting location path error in line 7 (let $old_node := $users_doc/user[userid=$userid]/{$update_node})

Is there a way to use an element variable in xpath ?

Thanks,
Sony

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽容 2024-10-08 22:44:40

您可以传递节点名称:

declare variable $doc_name as xs:string external; 
declare variable $userid as xs:string external; 
declare variable $update_node external; 
declare variable $new_value as xs:string external; 
let $users_doc := doc($doc_name)/users 

let $old_node := $users_doc/user[userid=$userid]/*[name()=$update_node] 

return replace value of node $old_node with $new_value 

You can pass the node name:

declare variable $doc_name as xs:string external; 
declare variable $userid as xs:string external; 
declare variable $update_node external; 
declare variable $new_value as xs:string external; 
let $users_doc := doc($doc_name)/users 

let $old_node := $users_doc/user[userid=$userid]/*[name()=$update_node] 

return replace value of node $old_node with $new_value 
思念绕指尖 2024-10-08 22:44:40

我认为你需要改变

let $old_node := $users_doc/user[userid=$userid]/{$update_node}

线路

let $old_node := $users_doc[userid=$userid]/{$update_node}

I think you need to change the line

let $old_node := $users_doc/user[userid=$userid]/{$update_node}

for

let $old_node := $users_doc[userid=$userid]/{$update_node}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文