Skip to content

Instantly share code, notes, and snippets.

@bzerangue
Forked from netsensei/nasa.xsl
Created July 21, 2018 13:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save bzerangue/37813f803a0320077ee4501e4dfc7e68 to your computer and use it in GitHub Desktop.
Save bzerangue/37813f803a0320077ee4501e4dfc7e68 to your computer and use it in GitHub Desktop.
Converts XML output of the US GSA Social Media API to CSV - http://registry.usa.gov/accounts.xml?agency_id=nasa
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:variable name="delimiter" select="','" />
<!-- define an array containing the fields we are interested in -->
<xsl:variable name="fieldArray">
<field>service_id</field>
<field>account</field>
<field>service_url</field>
<field>verified</field>
<field>details_url</field>
</xsl:variable>
<xsl:param name="fields" select="document('')/*/xsl:variable[@name='fieldArray']/*" />
<xsl:template match="/">
<!-- output the header row -->
<xsl:for-each select="$fields">
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:value-of select="." />
</xsl:for-each>
<!-- output newline -->
<xsl:text>&#xa;</xsl:text>
<xsl:apply-templates select="result/accounts/account"/>
</xsl:template>
<xsl:template match="account">
<xsl:variable name="currNode" select="." />
<!-- output the data row -->
<!-- loop over the field names and find the value of each one in the xml -->
<!-- output the data row -->
<!-- loop over the field names and find the value of each one in the xml -->
<xsl:for-each select="$fields">
<xsl:if test="position() != 1">
<xsl:value-of select="$delimiter"/>
</xsl:if>
<xsl:text>"</xsl:text>
<xsl:value-of select="$currNode/*[name() = current()]" />
<xsl:text>"</xsl:text>
</xsl:for-each>
<!-- output newline -->
<xsl:text>&#xa;</xsl:text>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment