Skip to content

Instantly share code, notes, and snippets.

@makenosound
Created September 21, 2009 07:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save makenosound/190115 to your computer and use it in GitHub Desktop.
Save makenosound/190115 to your computer and use it in GitHub Desktop.
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
extension-element-prefixes="exsl">
<xsl:import href="../../utilities/generic/string-replace.xsl" />
<xsl:strip-space elements="* | @*"/>
<!--
Example call
<xsl:call-template name="json">
<xsl:with-param name="xml" select="path/to/node"/>
</xsl:call-template>
-->
<xsl:template name="json"><xsl:param name="xml" />{<xsl:apply-templates select="exsl:node-set($xml)" mode="json" />}</xsl:template>
<xsl:template match="*" mode="json">
<xsl:variable name="has-siblings" select="name(.) = name(preceding-sibling::*) or name(.) = name(following-sibling::*)" />
<xsl:variable name="has-children" select="child::*" />
<xsl:variable name="has-attributes" select="@*" />
<xsl:variable name="is-node" select="$has-siblings and position() = 1 or not($has-siblings)" />
<!-- Name -->
<xsl:if test="$is-node">"<xsl:value-of select="name(.)" />": </xsl:if>
<!-- Array -->
<xsl:if test="$has-siblings and position() = 1">[</xsl:if>
<!-- Match Attributes and/or Children -->
<xsl:choose>
<!-- Attributes AND Children -->
<xsl:when test="$has-attributes and $has-children">{ "_attributes" : {<xsl:apply-templates select="@*" mode="json"/>}, <xsl:apply-templates select="* | text()" mode="json"/> }</xsl:when>
<!-- Attributes only -->
<xsl:when test="$has-attributes">{ "_attributes" : {<xsl:apply-templates select="@*" mode="json"/>}, "_value" : <xsl:apply-templates select="* | text()" mode="json"/><xsl:if test=". = ''">null</xsl:if>}</xsl:when>
<!-- Children only -->
<xsl:when test="$has-children">{ <xsl:apply-templates select="* | text()" mode="json"/> }</xsl:when>
<!-- Otherwise recursive -->
<xsl:otherwise>
<xsl:apply-templates select="* | text()" mode="json"/>
<xsl:if test=". = ''">null</xsl:if>
</xsl:otherwise>
</xsl:choose>
<xsl:if test="$has-siblings and position() = last()">]</xsl:if><!-- /Array -->
<xsl:if test="position() != last()">,</xsl:if><!-- Separator -->
</xsl:template>
<!-- Match text() -->
<xsl:template match="text()" mode="json">
<xsl:variable name="is-string" select="string(number(.)) = 'NaN' and . != 'true' and . != 'false'" />
<xsl:if test="$is-string">"</xsl:if>
<xsl:call-template name="string-replace"><!-- Escape Quotes -->
<xsl:with-param name="haystack" select="." />
<xsl:with-param name="search" select="'&#34;'" />
<xsl:with-param name="replace" select="'&#92;&#34;'" />
</xsl:call-template>
<xsl:if test="$is-string">"</xsl:if>
</xsl:template>
<!-- Match all attributes -->
<xsl:template match="@*" mode="json">
<xsl:text>"</xsl:text>
<xsl:value-of select="name()"/>
<xsl:text>" : </xsl:text>
<xsl:variable name="is-string" select="string(number(.)) = 'NaN' and . != 'true' and . != 'false'" />
<xsl:if test="$is-string">"</xsl:if>
<xsl:call-template name="string-replace"><!-- Escape Quotes -->
<xsl:with-param name="haystack" select="." />
<xsl:with-param name="search" select="'&#34;'" />
<xsl:with-param name="replace" select="'&#92;&#34;'" />
</xsl:call-template>
<xsl:if test="$is-string">"</xsl:if>
<xsl:if test=". = ''">null</xsl:if>
<xsl:if test="position() != last()">,</xsl:if>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment