changeset 11:14162f9133a1

working on xml to json transform
author cin
date Sun, 08 Apr 2018 23:30:11 +0300
parents 19a8a71eaa54
children 191b81b2052b
files data/jsondata.xml xslt/json.xsl
diffstat 2 files changed, 74 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/data/jsondata.xml	Sun Apr 08 23:30:11 2018 +0300
@@ -0,0 +1,12 @@
+<?xml version="1.0" encoding="utf-8"?>
+<person>
+	<firstName>Harry</firstName>
+	<lastName>Busy</lastName>
+	<age>23</age>
+	<address>
+		<street>1st avenue</street>
+	</address>
+	<address>
+		<street>2nd avenue</street>
+	</address>
+</person>
\ No newline at end of file
--- a/xslt/json.xsl	Mon Mar 12 02:58:34 2018 +0300
+++ b/xslt/json.xsl	Sun Apr 08 23:30:11 2018 +0300
@@ -1,38 +1,72 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <xsl:stylesheet version="1.0"
-	xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
-	<xsl:output method="text"/>
-	
-	<xsl:template match="/">
-		<xsl:apply-templates mode="json-array" />
+	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
+	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
+	<xsl:output method="text" />
+
+	<xsl:template match="*[count(*) = 0]">
+		<xsl:apply-templates mode="json-value" />
+	</xsl:template>
+
+	<xsl:template match="*[count(*) > 0]">
+		<xsl:text>{ </xsl:text>
+		<xsl:apply-templates mode="json-object-member"/>
+		<xsl:text> }</xsl:text>
 	</xsl:template>
 	
-	<xsl:template match="*" mode="json-array">
-		[
-			<xsl:for-each select="*">
-				<xsl:apply-templates select="." mode="json-value"/>
-				<xsl:if test="position() != last()">,</xsl:if>
-			</xsl:for-each>
-		]
+	<xsl:template match="text()"/>
+
+	<!-- handle json-object -->
+	
+	<xsl:template name="write-object">
+	
+	</xsl:template>
+
+	<xsl:template match="*" mode="json-object-member">
+		<xsl:call-template name="write-json-string">
+			<xsl:with-param name="text" select="local-name(.)"/>
+		</xsl:call-template>
+		<xsl:text> : </xsl:text>
+		<xsl:apply-templates mode="json-value"/>
+		<xsl:if test="position() != last()">
+			<xsl:text>, </xsl:text>
+		</xsl:if>
 	</xsl:template>
 	
-	<xsl:template match="*[count(*) != 0]" mode="json-array-item">
-		"<xsl:value-of select="name()"/> : <xsl:apply-templates mode="json-value"/>"
-		<xsl:if test="position() != last()">,</xsl:if>
+	<!-- handle json-value -->
+
+	<xsl:template match="*[@xsi:nil = 'true']">
+		<xsl:text>null</xsl:text>
+	</xsl:template>
+
+	<xsl:template match="text()[. = 'true']" mode="json-value">
+		<xsl:text>true</xsl:text>
+	</xsl:template>
+
+	<xsl:template match="text()[. = 'false']"
+		mode="json-value">
+		<xsl:text>false</xsl:text>
+	</xsl:template>
+
+	<xsl:template match="text()[string(number(.)) != 'NaN']"
+		mode="json-value">
+		<xsl:value-of select="number(.)" />
+	</xsl:template>
+
+	<xsl:template match="text()" mode="json-value">
+		<xsl:call-template name="write-json-string">
+			<xsl:with-param name="text" select="."/>
+		</xsl:call-template>
+	</xsl:template>
+
+	<xsl:template match="*" mode="json-value">
+		<xsl:apply-templates select="."/>
 	</xsl:template>
 	
-	<xsl:template match="*" mode="json-value">
-		<xsl:choose>
-			<xsl:when test="count(*) != 0">{ <xsl:apply-templates mode="json-named-value" /> }</xsl:when>
-			<xsl:when test="text() = 'true'">true</xsl:when>
-			<xsl:when test="text() = 'false'">false</xsl:when>
-			<xsl:when test="text() = 'null'">null</xsl:when>
-			<xsl:when test="string(number(.)) != 'NaN'"><xsl:value-of select="number(.)"/></xsl:when>
-			<xsl:otherwise>"<xsl:value-of select="text()"/>"</xsl:otherwise>
-		</xsl:choose>
+	<!-- template traits -->
+	<xsl:template name="write-json-string">
+		<xsl:param name="text"/>
+		<xsl:value-of select="concat('&quot;', $text,'&quot;')" />
 	</xsl:template>
-	
-	<xsl:template match="*" mode="json-named-value">
-		<xsl:value-of select="name()"/> : <xsl:apply-templates select="." mode="json-value"/>
-	</xsl:template>	
+
 </xsl:stylesheet>
\ No newline at end of file