Skip to content

Instantly share code, notes, and snippets.

@netojoaobatista
Created February 19, 2013 22:03
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save netojoaobatista/4990541 to your computer and use it in GitHub Desktop.
Save netojoaobatista/4990541 to your computer and use it in GitHub Desktop.
Simple RSS2JSON using XSLT
#!/bin/sh
curl -s "http://news.php.net/group.php?group=php.doc&format=rss" |xsltproc rss2json.xsl /dev/stdin |json_reformat
<?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:template match="channel">
<xsl:text>{"channel":{</xsl:text>
<xsl:text>"title":"</xsl:text><xsl:value-of select="./title" /><xsl:text>",</xsl:text>
<xsl:text>"description":"</xsl:text><xsl:value-of select="description" /><xsl:text>",</xsl:text>
<xsl:text>"items":[</xsl:text>
<xsl:for-each select="./item">
<xsl:if test="position() != 1"><xsl:text>,</xsl:text></xsl:if>
<xsl:apply-templates select="." />
</xsl:for-each>
<xsl:text>]</xsl:text>
<xsl:text>}}</xsl:text>
</xsl:template>
<xsl:template match="item">
<xsl:text>{</xsl:text>
<xsl:text>"link":"</xsl:text><xsl:value-of select="./link" /><xsl:text>",</xsl:text>
<xsl:text>"title":"</xsl:text><xsl:value-of select="./title" /><xsl:text>",</xsl:text>
<xsl:text>"pubDate":"</xsl:text><xsl:value-of select="pubDate" /><xsl:text>"</xsl:text>
<xsl:text>}</xsl:text>
</xsl:template>
<xsl:template match="/">
<xsl:text>{"rss":</xsl:text>
<xsl:apply-templates select=".//channel" />
<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