Hi all,
I have some trouble with the correct formulation of a transform. I'm generating CSV files. I can easily generate the following csv:
"version","","stuff",
"version1version2","annotation1annotation2","yadda",
However, I would like for the different instances of subfields to be comma-separated within their string, as follows:
"version","","stuff",
"version1,version2","annotation1,annotation2","yadda",
My input looks something like
<?xml version="1.0" encoding="UTF-8"?>
<collection>
<record>
<datafield tag="020">
<subfield code="a">version</subfield>
</datafield>
<datafield tag="040">
<subfield code="b">stuff</subfield>
</datafield>
</record>
<record>
<datafield tag="020">
<subfield code="a">version1</subfield>
<subfield code="9">annotation1</subfield>
</datafield>
<datafield tag="020">
<subfield code="a">version2</subfield>
<subfield code="9">annotation2</subfield>
</datafield>
<datafield tag="040">
<subfield code="b">yadda</subfield>
</datafield>
</record>
</collection>
Using the following xsl (and xsltproc)
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:apply-templates select="collection/record"/>
</xsl:template>
<xsl:template match="record">
<xsl:text>"</xsl:text>
<xsl:apply-templates select="datafield[@tag='020']/subfield[@code='a']"/>
<xsl:text>",</xsl:text>
<xsl:text>"</xsl:text>
<xsl:apply-templates select="datafield[@tag='020']/subfield[@code='9']"/>
<xsl:text>",</xsl:text>
<xsl:text>"</xsl:text>
<xsl:apply-templates select="datafield[@tag='040']/subfield[@code='b']"/>
<xsl:text>",</xsl:text>
<xsl:text>
</xsl:text>
</xsl:template>
I would guess that some combination of following-sibling:: or not(position()=last()) with call-template is going to be involved, but I haven't hit on a working solution yet. Any help?
I'm not looking for a generic XML-to-csv transform - anything geared to this particular dataset is fine.