XQuery:更新插入属性失败

XQuery: update insert attribute failed

test.xml:

<?xml version="1.0" encoding="UTF-8"?>
<breakfast_menu>
<food>
  <name>French Toast aaa</name>
  <price>.95</price>
  <description>Our famous Belgian Waffles with plenty of real maple syrup</description>
  <calories>650</calories>
</food>
<food>
  <name>French Toast</name>
  <price>.50</price>
  <description>Thick slices made from our homemade sourdough bread</description>
  <calories>600</calories>
</food>
<food>
  <name>Homestyle Breakfast</name>
  <price>.95</price>
  <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description>
  <calories>950</calories>
</food>
</breakfast_menu>

test.xqy:

for $x in doc('test.xml')//*
return update insert attribute id {'abcd'} into $x

我为每个 XML 标记添加了一个新属性。 xqy 文件非常简单。我得到了:

[XPST0003] Unexpected end of query: 'insert attribut...'.

有什么帮助吗?

您遇到了两个问题:

  • 误用 update 声明和
  • 缺少 node 关键字。

BaseX 专用 update statement is only meant to be used with the copy/modify construct;你在这里不需要它。然后,用于插入任何类型节点的运算符始终是 insert node $node [positional clause] into $target 和可选的 [positional clause]。除了节点变量 $node,您当然也可以使用节点构造函数,例如 attribute id {'abcd'}.

正确的查询是:

for $x in doc('test.xml')//*
return insert node attribute id {'abcd'} into $x