Skip to content

Instantly share code, notes, and snippets.

@vicmortelmans
Created December 9, 2011 21:39
Show Gist options
  • Save vicmortelmans/1453419 to your computer and use it in GitHub Desktop.
Save vicmortelmans/1453419 to your computer and use it in GitHub Desktop.
XSLT to turn YQL Open Table "csv" output with <row>s and <col>s into semantically structured XML based on title row
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<rows>
<xsl:apply-templates select="//row"/>
</rows>
</xsl:template>
<xsl:template match="row[position() &gt; 1]">
<xsl:element name="row">
<xsl:apply-templates mode="col"/>
</xsl:element>
</xsl:template>
<xsl:template match="row|diagnostics"/>
<xsl:template match="*" mode="col">
<xsl:variable name="position">
<xsl:value-of select="position()"/>
</xsl:variable>
<xsl:variable name="header">
<xsl:value-of select="../../row[1]/*[name(.) = name(current())]"/>
</xsl:variable>
<xsl:if test="$header != ''">
<xsl:element name="{translate($header,' ','_')}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment