diff xslt/json-person.xsl @ 13:197a850b1f6f default tip

working version of xml2json transformation
author cin
date Mon, 09 Apr 2018 16:27:26 +0300
parents 191b81b2052b
children
line wrap: on
line diff
--- a/xslt/json-person.xsl	Mon Apr 09 06:43:46 2018 +0300
+++ b/xslt/json-person.xsl	Mon Apr 09 16:27:26 2018 +0300
@@ -6,26 +6,47 @@
 	<xsl:output method="text" />
 	
 	<xsl:template match="firstName | lastName" mode="json-member-value">
-		<xsl:call-template name="write-value">
-			<xsl:with-param name="value" select="key('members',concat(generate-id(..),local-name(.)))[last()]"/>
+		<xsl:param name="values" select="."/>
+		<xsl:call-template name="write-array">
+			<xsl:with-param name="values" select="$values"/>
 		</xsl:call-template>
 	</xsl:template>
 	
-	<xsl:template match="address" mode="json-member-value">
-		<xsl:call-template name="write-array"/>
+	<xsl:template match="address | work | home" mode="json-object">
+		<xsl:call-template name="write-member-string">
+			<xsl:with-param name="name" select="'_type'"/>
+			<xsl:with-param name="value" select="local-name(.)"/>
+		</xsl:call-template>
+		
+		<xsl:variable name="members" select="@order | *"/>
+		<xsl:if test="$members">
+			<xsl:call-template name="write-separator"/>
+			<xsl:call-template name="write-members">
+				<xsl:with-param name="members" select="$members"/>
+			</xsl:call-template>
+		</xsl:if>
 	</xsl:template>
 	
-	<xsl:template match="work | home">
-		<address>
-			<_type><xsl:value-of select="local-name(.)"/></_type>
-			<xsl:copy-of select="*"/>
-		</address>
+	<xsl:template match="person" mode="json-object">
+		<xsl:variable name="address" select="home | work | address"/>
+		<xsl:variable name="members" select="*[not(self::home | self::work | self::address)]"/>
+		
+		<xsl:if test="$address">
+			<xsl:call-template name="write-member-array">
+				<xsl:with-param name="name" select="'address'"/>
+				<xsl:with-param name="values" select="$address"/>
+			</xsl:call-template>
+		</xsl:if>
+		
+		<xsl:if test="count($address) > 0 and count($members) > 0">
+			<xsl:call-template name="write-separator"/>
+		</xsl:if>
+		
+		<xsl:call-template name="write-members">
+			<xsl:with-param name="members" select="$members"/>
+		</xsl:call-template>
+		
 	</xsl:template>
 	
-	<xsl:template match="@*">
-		<xsl:element name="{local-name(.)}" >
-			<xsl:value-of select="."/>
-		</xsl:element>
-	</xsl:template>
 	
 </xsl:stylesheet>
\ No newline at end of file