[英]how to replace the value of an attribute using xquery
I have an xml document as follows 我有一个xml文件如下
<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". 我试图写一个xquery 1)找到具有给定userid的用户 - 索尼2)将给定用户的“test”属性的值更改为“newvalue”。
I am trying the following: (where doc_name = name of the xml document, userid = sony) 我正在尝试以下方法:(其中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
I see the following error: [XUTY0008] Single element, text, attribute, comment or pi expected as replace target. 我看到以下错误:[XUTY0008]预期单个元素,文本,属性,注释或pi作为替换目标。
How do I fix this? 我该如何解决?
You wrote: 你写了:
let $old := $users_doc/user[userid=$userid]/@trusted="oldvalue"
So, $old holds the result for that node set comparison. 因此,$ old保存该节点集比较的结果。
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. 编辑 :我认为在这种情况下使用replace value of
更好。
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. $old
的类型是xs:boolean - 不是节点类型。 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 . BTW:标准XQuery中没有replace
运算符 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.