使用 XQuery Update 删除节点
我正在尝试使用 xquery 从父节点中删除子节点。比如说,我的 xml 中有一个如下所示的条目:
<entry>
<id>36</id>
<title>Engineering Village Author Tool</title>
<updated>2009-09-30T12:55:42Z</updated>
<libx:libapp>
<libx:entry xmlns:libx="http://libx.org/xml/libx2" src="37"/>
</libx:libapp>
</entry>
如何删除子节点
<libx:entry xmlns:libx="http://libx.org/xml/libx2" src="37"/>
我正在使用以下 xquery 代码:
declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $child_id as xs:string external;
declare variable $parent_id as xs:string external;
declare variable $doc_name as xs:string external;
declare variable $feed as xs:anyAtomicType := doc($doc_name)/atom:feed;
let $parent_entry := $feed/atom:entry[atom:id=$parent_id]
return delete node $parent_entry//libx:entry[@libx:src=$child_id]
此处传递的变量的值是: 孩子 ID = 37 父 ID = 36 doc_name = 正在使用的文档的名称
我猜测使用命名空间的方式或我在 xquery 行中使用的 xpath 表达式有问题:
return delete node $parent_entry//libx:entry[@libx:src=$child_id]
请帮我解决这个问题。
I am trying to remove a child node from a parent node using xquery. Say, I have an entry shown below in my xml:
<entry>
<id>36</id>
<title>Engineering Village Author Tool</title>
<updated>2009-09-30T12:55:42Z</updated>
<libx:libapp>
<libx:entry xmlns:libx="http://libx.org/xml/libx2" src="37"/>
</libx:libapp>
</entry>
How do I delete the child node
<libx:entry xmlns:libx="http://libx.org/xml/libx2" src="37"/>
I am using the following xquery code:
declare namespace libx='http://libx.org/xml/libx2';
declare namespace atom='http://www.w3.org/2005/Atom';
declare variable $child_id as xs:string external;
declare variable $parent_id as xs:string external;
declare variable $doc_name as xs:string external;
declare variable $feed as xs:anyAtomicType := doc($doc_name)/atom:feed;
let $parent_entry := $feed/atom:entry[atom:id=$parent_id]
return delete node $parent_entry//libx:entry[@libx:src=$child_id]
The values of the variables passed here are:
child_id = 37
parent_id = 36
doc_name = name of the document being used
I am guessing something is wrong either with the way am using namespaces or the xpath expression i am using in my xquery at line :
return delete node $parent_entry//libx:entry[@libx:src=$child_id]
Please help me fix this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您将
$feed 声明为 xs:anyAtomicType
,但您正在设置它并将其用作node()
。我实际上对查询的编译感到惊讶。尝试删除
xs:anyAtomicType
或将其替换为element()
。此外,您只希望
@src
选择您的子节点,而不是@libx:src
。所以You are declaring
$feed as xs:anyAtomicType
, but you are setting it and using it as anode()
.I'm actually suprised the query compiles. Try removing the
xs:anyAtomicType
or replacing it withelement()
.Also you only want
@src
to select your child node, not@libx:src
. So不知道输出或错误是什么,我猜测 $parent_node 没有正确设置;也就是说,查询
$feed/atom:entry[atom:id=$parent_id]
不会返回任何内容。我会尝试$feed/entry[id=$parent_id]
来获得乐趣。另外,请确保 $feed 也设置正确,并尝试从declare variable $feed
语句中删除“atom:”。至于打印语句(大概是为了调试),这可能特定于您正在使用的数据库供应商。如果您仍在使用 BaseX,请从命令行运行此查询并添加“-v”作为输出。现在我再次查看该代码,我想知道为什么我使用
let $parent_entry...
而不是declare variable $parent_entry...
。xquery 很痛苦,不是吗?
-tjw
Not knowing what the output or error is, I would guess that $parent_node isn't being set properly; that is, the query
$feed/atom:entry[atom:id=$parent_id]
isn't returning anything. I would try$feed/entry[id=$parent_id]
for fun. Also, make sure $feed is getting set correctly as well, and also try removing the "atom:" from thedeclare variable $feed
statement.As for a print statement (presumably to debug), that is probably specific to the vendor of database you are using. If you are still using BaseX, run this query from the command line and add "-v" for output. Now that I look at that code again, I wonder why I used
let $parent_entry...
instead ofdeclare variable $parent_entry...
.xquery is a pain, isn't it?
-tjw