xmlstarlet:过滤掉具有属性的元素 [英] xmlstarlet: filter out element with attribute

查看:33
本文介绍了xmlstarlet:过滤掉具有属性的元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何过滤掉某些类型的元素,而这些元素没有具有魔法值的属性并保留文档的其余部分?所有这些都使用 xmlstarlet?

How to filter out elements of certain type which to not have an attribute with a magic value and retain the rest of the document? All this using xmlstarlet?

我要说的是:

cat << EOF > database.xml
<?xml version="1.0"?>
<database>

    <some name="A" />
    <some name="B" />
    <some name="C" />
    <text>this is some text to be applied...</text>
    <project>
        <test deeper="structure"/>
    </project>

</database>
EOF

xmlstarlet sel -t -m "*" -c "*[not(self::some[@name != 'A'])]" database.xml

收益

<some name="A"/><text>this is some text to be applied...</text><project>
        <test deeper="structure"/>
    </project>

但这隐藏了我宝贵的 tag.除了缩进,这不是问题......并且当 不是<的直接后代时不起作用/code>,例如 的子项.

But this hides my precious <database> tag. Besides the indentation, which is not a problem... And doesn't work when <some> are not a direct descendant of <database>, childs of <project> for example.

我想得到的是数据库,但所有<some>都被删除了除了名为A的那个:

What I want to get is the database as it is, but all <some> removed except the one named A:

<database>

    <some name="A" />


    <text>this is some text to be applied...</text>
    <project>
        <test deeper="structure"/>
    </project>

</database>

问候

推荐答案

不幸的是,xmlstarlet 的 sel 不支持 apply-templates,但您可以使用 ed 命令:

Unfortunately, xmlstarlet's sel doesn't support apply-templates, but you can use the ed command for this:

xmlstarlet ed -d '/database//some[@name != "A"]' input.xml

这篇关于xmlstarlet:过滤掉具有属性的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆