How to remove elements from xml using xslt with list of styles and xsltproc?

I have many XML files that have something like a form:

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

What I want to remove from the file.

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

At this point in the script I already have a list of files containing the item I want to delete, so one file can be used as a parameter.




EDIT: The question was initially absent in the intent.

What I'm trying to achieve is to remove the entire Element element, where (fruit == "apple" && animal == "cat"). In the same document there are many elements called "Element", I want them to remain. So

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

It would be:

 <Element fruit="orange" animal="dog" /> <Element fruit="pear" animal="wild three eyed mongoose of kentucky" /> 
+66
Nov 26 '08 at 19:24
source share
2 answers

Using one of the most fundamental XSLT design patterns: “Overriding identity transformation ”, you can simply 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>

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

When this conversion is applied to the following XML source 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 desired result is obtained:

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

Additional snippets of usage code and redefinition of the identity template can be found here .

+128
Nov 26 '08 at 20:44
source share

@Dimitre Novatchev's answer is certainly correct and elegant, but there is a generalization (which the OP did not ask about): what if the element you want to filter also has children or text that you want to keep?

I believe this minor option covers this case:

 <?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="2.0"> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> <!-- drop DropMe elements, keeping child text and elements --> <xsl:template match="DropMe"> <xsl:apply-templates/> </xsl:template> </xsl:stylesheet> 

A matching condition can be difficult to specify other attributes, etc., and you can use several such patterns if you drop other things.

So this entry:

 <?xml version="1.0" encoding="UTF-8"?> <mydocument> <p>Here text to keep</p> <p><DropMe>Keep this text but not the element</DropMe>; and keep what follows.</p> <p><DropMe>Also keep this text and <b>this child element</b> too</DropMe>, along with what follows.</p> </mydocument> 

produces this conclusion:

 <?xml version="1.0" encoding="UTF-8"?><mydocument> <p>Here text to keep</p> <p>Keep this text but not the element; and keep what follows.</p> <p>Also keep this text and <b>this child element</b> too, along with what follows.</p> </mydocument> 

Thanks to the XSLT Cookbook .

+1
Mar 08 '19 at 21:53
source share



All Articles