I found myself asking why XML formater to CSV (or what ever) should be so hard? So I started to explore XSLT stylesheets and all the bad things seems to fade a way and good things are here again!
Problem
Problem was to remove some nodes from XML-tree. There isn’t ‘good’ way to do this on XSLT (if there is please tell me!). but the XML dummy code looks like this:
<?xml version="1.0"?>
<root>
<node copy="0" id="1">
<child>Child 1</child>
</node>
<node copy="0" id="2">
<child>Child 2</child>
</node>
<node copy="0" id="3">
<child>Child 3</child>
</node>
<node copy="0" id="4">
<child>Child 4</child>
</node>
<node copy="1" id="5">
<child>Child 5</child>
</node>
<node copy="0" id="6">
<child>Child 6</child>
</node>
<node copy="1" id="7">
<child>Child 7</child>
</node>
<node copy="0" id="8">
<child>Child 8</child>
</node>
<node copy="0" id="9">
<child>Child 9</child>
</node>
<node copy="0" id="10">
<child>Child 10</child>
</node>
</root>
and XSLT file for copying only copy=”1″ looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<root>
<xsl:for-each select="/root/node">
<xsl:if test="@copy='1'">
<xsl:text> </xsl:text>
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
So its so short. Then just launch it with
xsltproc some.xsl some.xml
and there should be correct output