Search code examples
xmlxpathxmlstarlet

xmlstarlet: filter out element with attribute


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?

What I have to far is:

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

and

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

yields

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

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.

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>

Greetings


Solution

  • 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