Created
April 26, 2016 14:56
-
-
Save simonellistonball/c9f3d665acf8452ac72f21d182ac8447 to your computer and use it in GitHub Desktop.
XML 2 JSON XSLT
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"?> | |
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> | |
<xsl:output method="text"/> | |
<xsl:template match="/">{ | |
<xsl:apply-templates select="*"/>} | |
</xsl:template> | |
<!-- Object or Element Property--> | |
<xsl:template match="*"> | |
"<xsl:value-of select="name()"/>" : <xsl:call-template name="Properties"/> | |
</xsl:template> | |
<!-- Array Element --> | |
<xsl:template match="*" mode="ArrayElement"> | |
<xsl:call-template name="Properties"/> | |
</xsl:template> | |
<!-- Object Properties --> | |
<xsl:template name="Properties"> | |
<xsl:variable name="childName" select="name(*[1])"/> | |
<xsl:choose> | |
<xsl:when test="not(*|@*)">"<xsl:value-of select="."/>"</xsl:when> | |
<xsl:when test="count(*[name()=$childName]) > 1">{ "<xsl:value-of select="$childName"/>" :[<xsl:apply-templates select="*" | |
mode="ArrayElement"/>] }</xsl:when> | |
<xsl:otherwise>{ | |
<xsl:apply-templates select="@*"/> | |
<xsl:apply-templates select="*"/> | |
}</xsl:otherwise> | |
</xsl:choose> | |
<xsl:if test="following-sibling::*">,</xsl:if> | |
</xsl:template> | |
<!-- Attribute Property --> | |
<xsl:template match="@*">"<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>", | |
</xsl:template> | |
</xsl:stylesheet> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment