如何使用 xquery 替换属性的值

发布于 2024-10-02 13:27:19 字数 744 浏览 0 评论 0原文

我有一个 xml 文档,如下所示,

<users>
  <user test="oldvalue">
   <userid>sony</userid>
   <name>vijay</name>
  </user>
</users>

我正在尝试编写一个 xquery 1) 查找具有给定 userid 的用户 - sony 2) 将给定用户的“test”属性的值更改为“newvalue”。

我正在尝试以下操作:(其中 doc_name = xml 文档的名称,userid = sony)

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

let $old := $users_doc/user[userid=$userid]/@test="oldvalue"
let $new := $users_doc/user[userid=$userid]/@test="newvalue"

return replace node $old with $new

我看到以下错误: [XUTY0008] 单个元素、文本、属性、注释或 pi 需要作为替换目标。

我该如何解决这个问题?

I have an xml document as follows

<users>
  <user test="oldvalue">
   <userid>sony</userid>
   <name>vijay</name>
  </user>
</users>

I am trying to write an xquery to
1) find the user with the given userid - sony
2) change the value of the "test" attribute of the given user to "newvalue".

I am trying the following: (where doc_name = name of the xml document, userid = sony)

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

let $old := $users_doc/user[userid=$userid]/@test="oldvalue"
let $new := $users_doc/user[userid=$userid]/@test="newvalue"

return replace node $old with $new

I see the following error:
[XUTY0008] Single element, text, attribute, comment or pi expected as replace target.

How do I fix this?

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

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

发布评论

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

评论(2

趴在窗边数星星i 2024-10-09 13:27:19

您写道:

let $old := $users_doc/user[userid=$userid]/@trusted="oldvalue" 

因此, $old 保存该节点集比较的结果。

您需要:

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

    let $old := $users_doc/user[userid=$userid]/@test
    let $new := 'newvalue' 

return replace value of node $old with $new 

编辑:我认为在这种情况下最好使用替换值

You wrote:

let $old := $users_doc/user[userid=$userid]/@trusted="oldvalue" 

So, $old holds the result for that node set comparison.

You need:

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

    let $old := $users_doc/user[userid=$userid]/@test
    let $new := 'newvalue' 

return replace value of node $old with $new 

Edit: I think is better to use replace value of in this case.

空心空情空意 2024-10-09 13:27:19
let $old := $users_doc/user[userid=$userid]/@test="oldvalue"

让 $new := $users_doc/user[userid=$userid]/@test="newvalue"

返回用 $new 替换节点 $old

$old 的类型是 xs:boolean ——不是节点类型。因此,您收到的错误消息是正确的。

您想要

let $old := $users_doc/user[userid=$userid]/@test[.="oldvalue"]

顺便说一句:标准 XQuery 中没有replace 运算符

let $old := $users_doc/user[userid=$userid]/@test="oldvalue"

let $new := $users_doc/user[userid=$userid]/@test="newvalue"

return replace node $old with $new

The type of $old is xs:boolean -- not a node type. Therefore, the error message you get is correct.

You want:

let $old := $users_doc/user[userid=$userid]/@test[.="oldvalue"]

BTW: There is no replace operator in the standard XQuery.

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