1

这与我之前使用 Augeas 更新 xml的帖子有关。我手动调整了格式,并能够查看在 augtool 中如何添加和解析新行和选项卡。我尝试添加这些新行,但无法在项目节点和子项目节点之间插入每个#text行。是否可以通过为items/#text设置新变量来添加新的行和选项卡?

使用 Augeas 插入这些字符是否可行?

        <FileTypes>
        <items>
        <item value="video/*"/>
        <item value="audio/*"/>
        <item value="application/rar"/>
        </items>
        </FileTypes>      
/files/opt/webapp/config.xml/File/FileTypes
/files/opt/webapp/config.xml/File/FileTypes/#text = "\n"
/files/opt/webapp/config.xml/File/FileTypes/items
/files/opt/webapp/config.xml/File/FileTypes/items/#text[1] = "\n\t\t"
/files/opt/webapp/config.xml/File/FileTypes/items/item[1] = "#empty"
/files/opt/webapp/config.xml/File/FileTypes/items/item[1]/#attribute
/files/opt/webapp/config.xml/File/FileTypes/items/item[1]/#attribute/value = "video/*"
/files/opt/webapp/config.xml/File/FileTypes/items/#text[2] = "\t\t"
/files/opt/webapp/config.xml/File/FileTypes/items/item[2] = "#empty"
/files/opt/webapp/config.xml/File/FileTypes/items/item[2]/#attribute
/files/opt/webapp/config.xml/File/FileTypes/items/item[2]/#attribute/value = "audio/*"
/files/opt/webapp/config.xml/File/FileTypes/items/#text[3] = "\t\t"
/files/opt/webapp/config.xml/File/FileTypes/items/item[3] = "#empty"
/files/opt/webapp/config.xml/File/FileTypes/items/item[3]/#attribute
/files/opt/webapp/config.xml/File/FileTypes/items/item[3]/#attribute/value = "application/rar"

我在下面有这样的东西来插入一个新行,但它似乎没有工作,只添加了一个文字换行符和制表符。

set /files/opt/webapp/config.xml/File/FileTypes/items/#text \n\t\t'

这将导致更糟糕的情况。

        <FileTypes>
        <items>\n\t\t<item value="video/*"/>
<item value="audio/*"/>
<item value="application/rar"/>
</items>
        </FileTypes>      
4

1 回答 1

1

我想出了一个优雅的幂等解决方案,以确保您的<item>节点按一个列表缩进。这里有一个建议:

# define $audio (see previous question)
defnode audio item[#attribute/value="audio/*"] "#empty"

# define $noindent as $audio if the immediate previous node is not a `#text` one
defvar noindent $audio[preceding-sibling::*[1][label()!="#text"]]

# insert a #text element before $noindent. Will fail silently if $noindent doesn't match anything, making the change idempotent
ins #text before $noindent

# Set the value of the next #text node, which we are sure exists now
set #text[following-sibling::*[1][#attribute/value="audio/*"]] "\t"

一般来说,#text工作正常,你的问题很可能是对价值的错误引用。

于 2020-08-06T19:59:21.760 回答