开发者

how to use a element variable in xpath of an xquery

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 err开发者_C百科or 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


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 


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}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜