Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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" /> 
like image 519
Grundlefleck Avatar asked Nov 26 '08 19:11

Grundlefleck


People also ask

Can you use CSS with XSLT?

An XSLT style sheet can emit HTML <STYLE> elements, including CSS specifications, directly into the HTML that results from the XSLT transformation. This option works best when the number of CSS rules is small and easily managed.

How does an XSLT processor use an XSLT stylesheet with an XML document?

XSLT Processor takes the XSLT stylesheet and applies the transformation rules on the target XML document and then it generates a formatted document in the form of XML, HTML, or text format. This formatted document is then utilized by XSLT formatter to generate the actual output which is to be displayed to the end-user.

How do you link an XSLT stylesheet to an XML document?

Link the XSL Style Sheet to the XML DocumentAdd the XSL style sheet reference to your XML document ("cdcatalog. xml"): <? xml version="1.0" encoding="UTF-8"?>

Which XSL element is used to extract information from XML document?

XSLT <xsl:value-of> Element.


2 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.

like image 102
Dimitre Novatchev Avatar answered Oct 10 '22 09:10

Dimitre Novatchev


The answer by @Dimitre Novatchev is certainly both correct and elegant, but there's a generalization (that the OP didn't ask about): what if the element you want to filter also has child elements or text that you want to keep?

I believe this minor variation covers that 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> 

The match condition can be complicated to specify other attributes, etc., and you can use multiple such templates if you're dropping other things.

So this input:

<?xml version="1.0" encoding="UTF-8"?> <mydocument>     <p>Here's 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 output:

<?xml version="1.0" encoding="UTF-8"?><mydocument>     <p>Here's 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> 

Credit to XSLT Cookbook.

like image 35
Sboisen Avatar answered Oct 10 '22 08:10

Sboisen