Skip to content

Instantly share code, notes, and snippets.

@dtan
Created July 4, 2010 22:08
Show Gist options
  • Save dtan/463801 to your computer and use it in GitHub Desktop.
Save dtan/463801 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>
this may be useful as well (setting header-type to 'json'):
http://symphony-cms.com/discuss/thread/26893/1/#position-7
-->
<xsl:template name="json">
<xsl:param name="xml" />
<xsl:param name="depth" select="1" />
<xsl:if test="$depth = 1">{</xsl:if>
<xsl:apply-templates select="exsl:node-set($xml)" mode="json" />
<xsl:if test="$depth = 1">}</xsl:if>
</xsl:template>
<xsl:template match="*" mode="json">
<xsl:variable name="prev-sib-name" select="name(preceding-sibling::*[1])" />
<xsl:variable name="next-sib-name" select="name(following-sibling::*[1])" />
<xsl:variable name="prev-sibling" select="name() = $prev-sib-name" />
<xsl:variable name="next-sibling" select="name() = $next-sib-name" />
<xsl:variable name="start-array" select="not($prev-sibling) and $next-sibling" />
<xsl:variable name="end-array" select="$prev-sibling and not($next-sibling)" />
<xsl:variable name="has-siblings" select="$prev-sibling or $next-sibling" />
<xsl:variable name="has-children" select="child::*" />
<xsl:variable name="has-attributes" select="@*" />
<xsl:variable name="is-node" select="$has-siblings or not($has-siblings)" />
<!-- Name -->
<xsl:if test="($is-node and not($prev-sibling)) or $start-array">"<xsl:value-of select="name(.)" />": </xsl:if>
<!-- Array -->
<xsl:if test="$start-array">[</xsl:if> <!-- and position() = 1 -->
<!-- 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="$end-array">]</xsl:if><!-- /Array and position() = last() -->
<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