xsl-list
[Top] [All Lists]

Re: [xsl] Variations in XML to CSV

2011-04-21 18:07:06
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

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>