1 (edited by BDeed 02-09-2014 16:19:35)

Topic: Consuming REST response (VB.NET application)

I doubt anyone other than me is actually trying to do this but just in case!

I am converting the Gateway REST response to an XML then transforming the XML using an XSL (transformation stylesheet).
I've just succeed in putting together a stylesheet that successfully converts the REST XML response into a csv, it should be usable by anyone and it's just so damn beautiful i had to share it!

You'll notice i am currently omitting the site key and taxon authority fields. This is because they are not always returned with a record and it was leading to file misalignment. Once i figure out how to include the node even when absent i'll post an update.

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" method="text" encoding="UTF-8" />

<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="/*"> <!-- add headers to sheet -->
<xsl:for-each select="*[1]/*[not(self::siteKey)][not(self::pTaxonAuthority)]">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>"</xsl:text>
<xsl:if test="position() != last()">
    <xsl:value-of select="','"/>
</xsl:if>
</xsl:for-each>
<xsl:text>&#10;</xsl:text>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="TaxonObservation"> <!-- add values under headers -->
  <xsl:for-each select="*[not(self::siteKey)][not(self::pTaxonAuthority)]">
  <xsl:text>"</xsl:text>
   <xsl:value-of select="."/>
   <xsl:text>"</xsl:text>
   <xsl:if test="position() != last()">
    <xsl:value-of select="','"/>
   </xsl:if>
  </xsl:for-each>
  <xsl:text>&#10;</xsl:text>
</xsl:template>

<xsl:template match="@siteKey" />
<xsl:template match="@pTaxonAuthority" />

</xsl:stylesheet>
Natural History & Biodiversity Data Enthusiast