简体   繁体   中英

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

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

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

Write an XSLT stylesheet doing

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:template match="@* | node()">
  <xsl:copy>
    <xsl:apply-templates select="@* | node()"/>
  </xsl:copy>
</xsl:template>

<xsl:template match="some[@name != 'A']"/>

</xsl:stylesheet>

then call xmlstarlet to apply that stylesheet to your input XML: xmlstarlet tr sheet.xsl input.xml .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM