使用 XQuery Update 删除节点

发布于 2024-09-24 16:58:12 字数 1258 浏览 0 评论 0原文

我正在尝试使用 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 技术交流群。

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

发布评论

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

评论(2

∞梦里开花 2024-10-01 16:58:12

您将 $feed 声明为 xs:anyAtomicType,但您正在设置它并将其用作 node()

我实际上对查询的编译感到惊讶。尝试删除 xs:anyAtomicType 或将其替换为 element()

此外,您只希望 @src 选择您的子节点,而不是 @libx:src。所以

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 := doc($doc_name)/atom:feed;
let $parent_entry := $feed/atom:entry[atom:id=$parent_id]
return delete node $parent_entry//libx:entry[@src=$child_id]

You are declaring $feed as xs:anyAtomicType, but you are setting it and using it as a node().

I'm actually suprised the query compiles. Try removing the xs:anyAtomicType or replacing it with element().

Also you only want @src to select your child node, not @libx:src. So

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 := doc($doc_name)/atom:feed;
let $parent_entry := $feed/atom:entry[atom:id=$parent_id]
return delete node $parent_entry//libx:entry[@src=$child_id]
暮色兮凉城 2024-10-01 16:58:12

不知道输出或错误是什么,我猜测 $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 the declare 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 of declare variable $parent_entry....

xquery is a pain, isn't it?

-tjw

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