本文介绍了如何使用 Augeas 更新现有的或创建新的 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于以下 XML:

<properties>
  <entry key="foo">bar</entry>
</properties>

我可以使用以下 augeas 命令更新带有属性foo"的退出条目:

I can update exiting entry with attribute "foo" with the following augeas command:

set /files/test.xml/properties/entry[#attribute/key='foo']/#text bar2

如果没有输入属性的现有条目,是否有 augeas 命令创建一个新节点(具有键属性),如果输入属性已经存在条目,则更新现有节点?我尝试了以下方法:

Is there augeas command(s) to create a new node (with key attribute) if there is no existing entry with the input attribute, and update existing if entry already exists with the input attribute? I tried the following:

set /files/test.xml/properties/entry[#attribute/key='hello']/#text world

但这只会导致以下结果,没有属性:

But this only results in the following, without attribute:

<properties>
  <entry key="foo">bar2</entry>
  <entry>world</entry>
</properties>

推荐答案

/files/test.xml/properties/entry[#attribute/key='hello']/#text 没有匹配任何节点,因此 Augeas 创建一个新节点.如果您想更新这两个值.

/files/test.xml/properties/entry[#attribute/key='hello']/#text doesn't match any node, so Augeas creates a new node. If you want to update both values.

显然,您只想保留一个 entry 节点并设置其 text 和 key 属性:

Apparently, you want to keep only one entry node and set both its text and key attribute:

defnode entry /files/test.xml/properties/entry[#attribute/key="foo"]
set $entry/#attribute/key 'hello'
set $entry/#text 'world'

这篇关于如何使用 Augeas 更新现有的或创建新的 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 03:54