Created
June 1, 2012 11:49
-
-
Save elmimmo/2851536 to your computer and use it in GitHub Desktop.
XSLT: from intermediate XML format to JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<!-- Stylesheet for converting between an intermediate format and JSON --> | |
<!-- Copyright 2006 Theo Hultberg, all rights reserved --> | |
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" | |
version="1.0"> | |
<xsl:output method="text" | |
encoding="UTF-8" | |
/> | |
<xsl:variable name="nl"><xsl:text>
</xsl:text></xsl:variable> | |
<xsl:variable name="tab"><xsl:text>	</xsl:text></xsl:variable> | |
<xsl:variable name="quote"><xsl:text>"</xsl:text></xsl:variable> | |
<xsl:template match="/"> | |
<xsl:apply-templates/> | |
</xsl:template> | |
<xsl:template match="object"> | |
<xsl:if test="./@name"> | |
<xsl:text>"</xsl:text> | |
<xsl:value-of select="@name"/> | |
<xsl:text>": </xsl:text> | |
</xsl:if> | |
<xsl:text>{</xsl:text> | |
<xsl:apply-templates/> | |
<xsl:text>}</xsl:text> | |
<xsl:if test="position() < last() - 1"> | |
<!-- TODO: find out why is the -1 necessary? --> | |
<xsl:text>,</xsl:text> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="array"> | |
<xsl:if test="@name"> | |
<xsl:text>"</xsl:text> | |
<xsl:value-of select="@name"/> | |
<xsl:text>": </xsl:text> | |
</xsl:if> | |
<xsl:text>[</xsl:text> | |
<xsl:apply-templates/> | |
<xsl:text>]</xsl:text> | |
<xsl:if test="position() < last() - 1"> | |
<!-- TODO: find out why is the -1 necessary? --> | |
<xsl:text>,</xsl:text> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="string|date"> | |
<xsl:variable name="text"> | |
<xsl:call-template name="escape-linebreaks"> | |
<xsl:with-param name="string"> | |
<xsl:call-template name="escape-quotes"> | |
<xsl:with-param name="string"> | |
<xsl:value-of select="."/> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:variable> | |
<xsl:if test="@name"> | |
<xsl:text>"</xsl:text> | |
<xsl:value-of select="@name"/> | |
<xsl:text>": </xsl:text> | |
</xsl:if> | |
<xsl:text>"</xsl:text> | |
<xsl:value-of select="$text"/> | |
<xsl:text>"</xsl:text> | |
<xsl:if test="position() < last() - 1"> | |
<!-- TODO: find out why is the -1 necessary? --> | |
<xsl:text>,</xsl:text> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="integer|real"> | |
<xsl:if test="@name"> | |
<xsl:text>"</xsl:text> | |
<xsl:value-of select="@name"/> | |
<xsl:text>": </xsl:text> | |
</xsl:if> | |
<xsl:value-of select="."/> | |
<xsl:if test="position() < last() - 1"> | |
<!-- TODO: find out why is the -1 necessary? --> | |
<xsl:text>,</xsl:text> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template match="property[@type='bool']"> | |
<xsl:if test="@name"> | |
<xsl:text>"</xsl:text> | |
<xsl:value-of select="@name"/> | |
<xsl:text>": </xsl:text> | |
</xsl:if> | |
<xsl:value-of select="."/> | |
<xsl:if test="position() < last() - 1"> | |
<!-- TODO: find out why is the -1 necessary? --> | |
<xsl:text>,</xsl:text> | |
</xsl:if> | |
</xsl:template> | |
<xsl:template name="escape-linebreaks"> | |
<xsl:param name="string"/> | |
<xsl:call-template name="replace-all"> | |
<xsl:with-param name="string"> | |
<xsl:value-of select="$string"/> | |
</xsl:with-param> | |
<xsl:with-param name="old"> | |
<xsl:value-of select="$nl"/> | |
</xsl:with-param> | |
<xsl:with-param name="new"> | |
<xsl:text>\n</xsl:text> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:template> | |
<xsl:template name="escape-quotes"> | |
<xsl:param name="string"/> | |
<xsl:call-template name="replace-all"> | |
<xsl:with-param name="string"> | |
<xsl:value-of select="$string"/> | |
</xsl:with-param> | |
<xsl:with-param name="old"> | |
<xsl:value-of select="$quote"/> | |
</xsl:with-param> | |
<xsl:with-param name="new"> | |
<xsl:text>\</xsl:text> | |
<xsl:value-of select="$quote"/> | |
</xsl:with-param> | |
</xsl:call-template> | |
</xsl:template> | |
<xsl:template name="replace-all"> | |
<xsl:param name="string"/> | |
<xsl:param name="old"/> | |
<xsl:param name="new"/> | |
<xsl:choose> | |
<xsl:when test='contains($string, $old)'> | |
<xsl:value-of select="substring-before($string, $old)" /> | |
<xsl:value-of select="$new"/> | |
<xsl:call-template name="replace-all"> | |
<xsl:with-param name="string" select="substring-after($string, $old)" /> | |
<xsl:with-param name="old" select="$old" /> | |
<xsl:with-param name="new" select="$new" /> | |
</xsl:call-template> | |
</xsl:when> | |
<xsl:otherwise> | |
<xsl:value-of select="$string" /> | |
</xsl:otherwise> | |
</xsl:choose> | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Original was from XSL, plists and a bit of JSON