How to remove elements from xml using xslt with stylesheet and xsltproc?

I have a lot of XML files which have something of the form:

< Element  
    fruit="apple"  
    animal="cat" 
/>

Which I want to be removed from the file.

Using an XSLT stylesheet and the Linux command-line utility xsltproc, how could I do this?

By this point in the script I already have the list of files containing the element I wish to remove, so the single file can be used as a parameter.


EDIT: the question was originally lacking in intention.

What I am trying to achieve is to remove the entire element "Element" where (fruit=="apple" && animal=="cat"). In the same document there are many elements named "Element", I wish for these to remain. So

< Element  
    fruit="orange"  
    animal="dog" 
/>
< Element  
    fruit="apple"  
    animal="cat" 
/>  
< Element  
    fruit="pear"  
    animal="wild three eyed mongoose of kentucky" 
/>

Would become:

< Element  
    fruit="orange"  
    animal="dog" 
/>
< Element  
    fruit="pear"  
    animal="wild three eyed mongoose of kentucky" 
/>

Answers


Using one of the most fundamental XSLT design patterns: "Overriding the identity transformation" one will just write the following:

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

 <xsl:output omit-xml-declaration="yes"/>

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

    <xsl:template match="Element[@fruit='apple' and @animal='cat']"/>
</xsl:stylesheet>

Do note how the second template overrides the identity (1st) template only for elements named "Element" that have an attribute "fruit" with value "apple" and attribute "animal" with value "cat". This template has empty body, which means that the matched element is simply ignored (nothing is produced when it is matched).

When this transformation is applied on the following source XML document:

<doc>... 
    <Element name="same">foo</Element>...
    <Element fruit="apple" animal="cat" />
    <Element fruit="pear" animal="cat" />
    <Element name="same">baz</Element>...
    <Element name="same">foobar</Element>...
</doc>

the wanted result is produced:

<doc>... 
    <Element name="same">foo</Element>...
    <Element fruit="pear" animal="cat"/>
    <Element name="same">baz</Element>...
    <Element name="same">foobar</Element>...
</doc>

More code snippets of using and overriding the identity template can be found here.


Need Your Help

Class creating by copying doesnt work

java reflection asmx

I am following the exmaple in the ASM 4.0 manual, in the section which explains the visitor there is an exmaple of copying the class using a code similar to this:

jQuery click event on <a> fires twice

jquery asp.net-mvc-4

I have a click event that is being called twice and I can't figure out why.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.