Re: [xsl] Variations in XML to CSV

Subject: Re: [xsl] Variations in XML to CSV
From: Markus Flatscher <markus.flatscher@xxxxxxxxx>
Date: Thu, 21 Apr 2011 19:17:55 -0400
Bridger,

if you come across this again, David Lee's xmlsh (www.xmlsh.org) might
be worth a look. It comes with a command-line xml2csv command:

http://www.xmlsh.org/CommandXml2csv

Markus

On Thu, Apr 21, 2011 at 7:06 PM, Bridger Dyson-Smith
<bdysonsmith@xxxxxxxxx> wrote:
> Hi all,
> thanks for the feedback. I clearly don't know enough about XSLT to use
> it exclusively to solve my problem(s); however, I was able to use some
> simple templates to extract parts of the tree in the right order.
>
> xml:
> <root>
>  <metadata>
>   <title>Alpha</title>
>   <subject>Sinister</subject>
>   <creator>Beta</creator>
>   <subject>Gamma</subject>
>   <subject>Delta</subject>
>   <subject>Epsilon</subject>
>   <date>2011-04-19</date>
>   <relation>0012_0001</relation>
>  </metadata>
>  <metadata>
>   <title>Zeta</title>
>   <creator>Eta</creator>
>   <creator>Theta</creator>
>   <contributor>Dexter</contributor>
>   <contributor>Iota</contributor>
>   <subject>Kappa</subject>
>   <subject>Lambda</subject>
>   <date>2011-04-19</date>
> <relation>0012_0002</relation>
>  </metadata>
> </root>
>
> xslt:
> <?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";
>    exclude-result-prefixes="xs"
>    version="2.0">
>
>    <xsl:strip-space elements="*"/>
>    <xsl:output method="text"/>
>
>    <xsl:template match="/">
>        <xsl:apply-templates/>
>    </xsl:template>
>
>    <xsl:template match="metadata">
>        <xsl:for-each select=".">
>            <xsl:value-of select="normalize-space(title)"
> disable-output-escaping="yes"/><xsl:text>&#x9;<xsl:text>
>        </xsl:for-each>
>    </xsl:template>
> </xsl:stylesheet>
>
> I took the output, dropped it into vim and did a line-by-line find &
> replace (:%s/\t/\r/g) to columnize the data. I then went back to my
> XSLT and added successive templates (commenting out the earlier
> templates) to pull the remaining data.
>
> example:
> <xsl:template match="metadata">
>        <xsl:value-of select="normalize-space(relation)"
> disable-output-escaping="yes"/><xsl:text>&#x9;</xsl:text>
>        <xsl:for-each select="subject">
>            <xsl:value-of select="normalize-space(.)"
> disable-output-escaping="yes"/><xsl:text>&#x9;</xsl:text>
>        </xsl:for-each>
>    </xsl:template>
>
> I had consistent points that I could use (values from relation), so
> again in vim :%s/\t0012_0/\r0012_0/g columnized the values and allowed
> me to import into an OpenOffice document.
>
> It was, all-in-all, a not very elegant solution but it worked. Thanks
> again for the suggestions and feedback!
> Cheers,
> Bridger

Current Thread