Sunday, 4 March 2012

command line xslt transforms

Just putting this here because I was asked to. How to transform a file using XSLT on the command line.

Here is a really simple xsl stylesheet to remove some elements from an xml file:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" indent="yes" omit-xml-declaration="no"/>
 <xsl:template match="node()|@*">
  <xsl:copy>
   <xsl:apply-templates select="node()|@*"/>
  </xsl:copy>
 </xsl:template>
 <xsl:template match="book[@name='X']"/>
</xsl:stylesheet>
</xsl:stylesheet>

Here is an xml file:

<?xml version="1.0" encoding="UTF-8"?>
<a>
<book name="X">
<title>A book</title>
</book>
<book name="Y">
<title>Another book</title>
</book>
</a>
Here is a command:

xsltproc x.xsl x.xml

And here is the output;

<?xml version="1.0"?>
<a>
<book name="Y">
<title>Another book</title>
</book>
</a>

No comments:

Post a Comment