[英]How to use XQuery to Update an Attribute in BaseX XML Database?
I am using BaseX to store XML data with multiple item
nodes in the following format: 我正在使用BaseX以以下格式存储具有多个
item
节点的XML数据:
<root>
<item id="Root" parent_id="0" type="folder">
<content>
<name>Root</name>
</content>
</item>
<item id="One" parent_id="Root" type="file">
<content>
<name>First File in the Tree</name>
</content>
</item>
<item id="Two" parent_id="Root" type="file">
<content>
<name>Second File in the Tree</name>
</content>
</item>
</root>
I am trying to update the parent_id
on one of the 'item' nodes to another item
node's id
attribute. 我正在尝试将其中一个“项目”节点上的
parent_id
更新为另一个item
节点的id
属性。
The tree before running the XQuery expression: 运行XQuery表达式之前的树:
Root
根
∟ First File in the Tree∟树中的第一个文件
∟ Second File in the Tree∟树中的第二个文件
I am trying to figure out the correct XQuery expression to change the parent of one of the item
s from root
to "one" item
, so that my tree now looks like this: 我试图找出正确的XQuery表达式,将
item
之一的父item
从root
更改为“ one” item
,这样我的树现在看起来像这样:
Expected result tree: 预期结果树:
Root
根
∟ First File in the Tree∟树中的第一个文件
∟ Second File in the Tree∟树中的第二个文件
Since the attribute parent_id
controls this, I am looking to update exactly that. 由于属性
parent_id
控制了此操作,因此我希望对此进行更新。
If all you want to do is to update the @parent_id
attribute, use a simple updating query like 如果您要做的只是更新
@parent_id
属性,请使用简单的更新查询,例如
replace value of node //item[@id='Two']/@parent_id with 'One'
But: this wouldn't actually change the XML tree , but only the one you're representing in XML (although I believe that this is what your question was about). 但是:这实际上并不会改变XML树 ,而只会改变您在XML中表示的 树 (尽管我相信这就是您的问题所在)。 A more XML-like approach to store the tree would be directly mapping the tree you want to have to XML:
一种更类似于XML的存储树的方法是将想要的树直接映射到XML:
<root>
<item id="Root" type="folder">
<content>
<name>Root</name>
</content>
<item id="One" type="file">
<content>
<name>First File in the Tree</name>
</content>
<item id="Two" type="file">
<content>
<name>Second File in the Tree</name>
</content>
</item>
</item>
</item>
</root>
Or even more concise, holding the same information: 甚至更简洁,持有相同的信息:
<folder id="Root" name="Root">
<file id="One" name="First File in the Tree">
<file id="Two" name="Second File in the Tree" />
</file>
</folder>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.