Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@tts
Last active December 15, 2015 13:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tts/5268693 to your computer and use it in GitHub Desktop.
Save tts/5268693 to your computer and use it in GitHub Desktop.
Transform a CSV file to XML
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema"
version="2.0">
<!--
csv2xml.xsl
Trasnform a CSV file to XML
CSV can have a header line. Subsequent lines: year\ttitle\tcites\tdoi\tschool\n
java -jar saxon9he.jar -it:main -o:pubs.xml -s:pubs.csv -xsl:csv2xml.xsl
Tuija Sonkkila 28.3.2013
-->
<xsl:param name="input-uri" as="xs:string"/>
<xsl:output method="xml" indent="yes"/>
<xsl:template name="main">
<xsl:variable name="in" select="unparsed-text($input-uri, 'iso-8859-1')"/>
<pubs>
<xsl:analyze-string select="$in" regex="\n">
<xsl:non-matching-substring>
<pub>
<xsl:analyze-string select="." regex='^([^py\t]+)\t([^\t]+)\t([^\t]+)\t([^\t]+)\t(.*)$'>
<xsl:matching-substring>
<year><xsl:value-of select="regex-group(1)"/></year>
<title><xsl:value-of select="regex-group(2)"/></title>
<cites><xsl:value-of select="regex-group(3)"/></cites>
<doi><xsl:value-of select="regex-group(4)"/></doi>
<school><xsl:value-of select="regex-group(5)"/></school>
</xsl:matching-substring>
</xsl:analyze-string>
</pub>
</xsl:non-matching-substring>
</xsl:analyze-string>
</pubs>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment