Skip to content

Instantly share code, notes, and snippets.

@dtan
Created March 30, 2011 13:05
Show Gist options
  • Save dtan/894341 to your computer and use it in GitHub Desktop.
Save dtan/894341 to your computer and use it in GitHub Desktop.
Convert XML to JSON output
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:json="http://json.org/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- this is in the context of Symphony CMS, hence ../utilities -->
<xsl:import href="../utilities/xml2json.xsl"/>
<xsl:output omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="data">
<xsl:call-template name="json">
<!-- select in this case the xpath to the node you want to jsonify -->
<xsl:with-param name="xml" select="projects/entry" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!--
Example call
<xsl:call-template name="string-replace">
<xsl:with-param name="haystack" select="item" />
<xsl:with-param name="search" select="'ab'" />
<xsl:with-param name="replace" select="'zz'" />
</xsl:call-template>
-->
<xsl:template name="string-replace">
<xsl:param name="haystack" />
<xsl:param name="search" />
<xsl:param name="replace" select="''" />
<xsl:choose>
<xsl:when test="contains($haystack, $search)">
<xsl:value-of select="substring-before($haystack, $search)" />
<xsl:value-of select="$replace" />
<xsl:call-template name="string-replace">
<xsl:with-param name="haystack" select="substring-after($haystack, $search)" />
<xsl:with-param name="search" select="$search" />
<xsl:with-param name="replace" select="$replace" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$haystack" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<?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">
<!-- this is in the context of Symphony CMS, hence ../utilities -->
<xsl:import href="../utilities/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:apply-templates select="exsl:node-set($xml)" mode="json" />
}
</xsl:template>
<xsl:template match="*" mode="json">
<xsl:variable name="prev-sib" select="preceding-sibling::*[1]" />
<xsl:variable name="next-sib" select="following-sibling::*[1]" />
<xsl:variable name="prev-sib-name" select="name($prev-sib)" />
<xsl:variable name="next-sib-name" select="name($next-sib)" />
<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)" />
<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:variable name="prev-sib" select="preceding-sibling::*[1]" />
<xsl:variable name="next-sib" select="following-sibling::*[1]" />
<xsl:choose>
<xsl:when test="$prev-sib and not($next-sib)"> "_value":</xsl:when>
<xsl:when test="not($prev-sib) and not($next-sib) and $is-string">"</xsl:when>
</xsl:choose>
<xsl:choose>
<xsl:when test="$prev-sib and not($next-sib)">
<xsl:if test="$is-string">"</xsl:if>
<xsl:call-template name="escape-string">
<xsl:with-param name="og-str" select="." />
</xsl:call-template>
<xsl:if test="$is-string">"</xsl:if>
</xsl:when>
<xsl:when test="not($prev-sib) and not($next-sib)">
<xsl:call-template name="escape-string">
<xsl:with-param name="og-str" select="." />
</xsl:call-template>
</xsl:when>
</xsl:choose>
<xsl:if test="not($prev-sib) and not($next-sib) and $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="escape-string">
<xsl:with-param name="og-str" select="." />
</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:template name="escape-string">
<!--
replace end of line in strings (\n), otherwise will throw error
end of line: &#10;
\: &#92;
-->
<xsl:param name="og-str" />
<xsl:param name="prev" select="''" />
<xsl:param name="next" select="''" />
<xsl:variable name="tmp-str" select="translate($og-str, '&#92;&#34;', '&#34;')" />
<xsl:variable name="new-tmp-str">
<xsl:call-template name="string-replace"><!-- Escape Quotes -->
<xsl:with-param name="haystack" select="$tmp-str" />
<xsl:with-param name="search" select="'&#34;'" />
<xsl:with-param name="replace" select="'&#92;&#34;'" />
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="string-replace"><!-- Escape newlines -->
<xsl:with-param name="haystack" select="$new-tmp-str" />
<xsl:with-param name="search" select="'&#10;'" />
<xsl:with-param name="replace" select="'&#92;n'" />
</xsl:call-template>
</xsl:template>
</xsl:stylesheet>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment