Created
September 26, 2015 14:46
-
-
Save netsensei/4ae7b712e5058c5cb312 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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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>
</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>
</xsl:text> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment