An XSLT to turn XML into CSV
1 2 3 4 5 6 7 8 9 10 11 12
<root> <element> <foo>1</foo> <bar> <baz>2</baz> <fooz>3</fooz> </bar> </element> <element> <foo>1</foo> </element> </root>
1 2 3 4 5 6 7 8 9 10
<root> <element> <foo>1</foo> <bar.baz>2</bar.baz> <bar.fooz>3</bar.fooz> </element> <element> <foo>1</foo> </element> </root>
considering I am interested in converting each 'element' into a CSV line.
The 'namespaced' element names are required, because they serve as the CSV column headers and they are required to be unique (which, for our case, is guaranteed by this approach).
The other subproblem is converting XML to CSV, of which the main challenge was making sure the last element is not followed by a comma.
In the end, I came up with the templates below to print the values of the 'childless' elements.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
<xsl:template match="//element"> <xsl:apply-templates select="*" /> <xsl:text>
</xsl:text> </xsl:template> <xsl:template match="//element//*"> <xsl:choose> <xsl:when test="count(child::*) > 0"> <xsl:apply-templates select="*" /> </xsl:when> <xsl:otherwise> <xsl:text>"</xsl:text> <xsl:value-of select="."/> <xsl:text>"</xsl:text> </xsl:otherwise> </xsl:choose> <xsl:if test="position() != last()"> <xsl:text>,</xsl:text> </xsl:if> </xsl:template>
The upper template applies the lower template to the child nodes of element nodes called 'element', one 'element' at a time. The lower template determines whether the node has any child elements. If it doesn't, it prints the node. Otherwise, it recursively applies this template to the child nodes that were present. Finally, a comma is placed for each element that isn't the last in the node-set. What's a bit tricky here is that no comma is placed for the last grandchild of an 'element', so you might expect those to be missing, but that one is provided by the comma after the element itself.
1) They have been in business for quite a while, started exporting data in CSV format long ago (when XML was not mainstream yet) and still have to support customers that haven't switched (we are talking business to business here) or
2) They are dealing with new customers that wish to receive data in CSV format, because they can not (or do not want to) handle XML
Comments are closed