123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134 |
- <?xml version="1.0" encoding="UTF-8"?>
- <!--
- Licensed Materials - Property of IBM
- IBM Cognos Products: HTS
- (C) Copyright IBM Corp. 2005, 2010
- US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
- -->
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xts="http://developer.cognos.com/schemas/xts/" xmlns:xtsext="xalan://com.cognos.xts.ext.XTSExt">
- <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
- <xsl:template name="convertToJSON">
- <xsl:param name="taskInfo"/>
- <xsl:param name="isCurrentNodeArrayItem" select="false()"/>
- <xsl:if test="$taskInfo!=''">
- <xsl:for-each select="$taskInfo/*">
- <xsl:variable name="children" select="./*"/>
- <xsl:variable name="hasChildren" select="count($children) > 0"/>
- <xsl:variable name="firstChild" select="local-name($children[1])"/>
- <xsl:variable name="arrayItems">
- <xsl:value-of select="count($children[local-name(.)=$firstChild])=count($children) and count($children) > 1"/>
- </xsl:variable>
- <xsl:choose>
- <xsl:when test="$hasChildren='true'">
- <xsl:call-template name="makeJSONNameObjectPair">
- <xsl:with-param name="value" select="."/>
- <xsl:with-param name="isArray" select="$arrayItems"/>
- <xsl:with-param name="isCurrentNodeArrayItem" select="$isCurrentNodeArrayItem"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:call-template name="makeJSONNameStringPair">
- <xsl:with-param name="value" select="."/>
- <xsl:with-param name="isCurrentNodeArrayItem" select="$isCurrentNodeArrayItem"/>
- </xsl:call-template>
- </xsl:otherwise>
- </xsl:choose>
- <xsl:if test="$isCurrentNodeArrayItem='true' and position()=1">
- <xsl:text>}</xsl:text>
- </xsl:if>
- <xsl:if test="position() > 1 and position() != last() and $isCurrentNodeArrayItem='true'">
- <xsl:text>}</xsl:text>
- </xsl:if>
- <xsl:if test="position() != last()">
- <xsl:text>,</xsl:text>
- </xsl:if>
- </xsl:for-each>
- </xsl:if>
- </xsl:template>
- <xsl:template name="makeJSONNameObjectPair">
- <xsl:param name="value"/>
- <xsl:param name="isArray"/>
- <xsl:param name="isCurrentNodeArrayItem"/>
- <!-- inside an array each item starts with '{' -->
- <xsl:if test="position() > 1 and $isCurrentNodeArrayItem=true()">
- <xsl:text>{</xsl:text>
- </xsl:if>
- <xsl:value-of select="local-name($value)"/>
- <xsl:text>:</xsl:text>
- <!-- a JSON array starts with '[' -->
- <xsl:if test="$isArray='true'">
- <xsl:text>[</xsl:text>
- </xsl:if>
- <!-- start of JSON object -->
- <xsl:text>{</xsl:text>
- <xsl:call-template name="convertToJSON">
- <xsl:with-param name="taskInfo" select="$value"/>
- <xsl:with-param name="isCurrentNodeArrayItem" select="$isArray='true'"/>
- </xsl:call-template>
- <!-- end of JSON object -->
- <xsl:text>}</xsl:text>
- <!-- mark end of array -->
- <xsl:if test="$isArray='true'">
- <xsl:text>]</xsl:text>
- </xsl:if>
- </xsl:template>
- <xsl:template name="makeJSONNameStringPair">
- <xsl:param name="value"/>
- <xsl:param name="isCurrentNodeArrayItem"/>
- <xsl:if test="position() > 1 and $isCurrentNodeArrayItem=true()">
- <xsl:text>{</xsl:text>
- </xsl:if>
- <xsl:value-of select="local-name($value)"/>
- <xsl:text>:</xsl:text>
- <xsl:text>'</xsl:text>
- <!-- javscript encode the contents -->
- <xsl:value-of select="xtsext:javascriptencode($value)"/>
- <xsl:text>'</xsl:text>
- </xsl:template>
- <xsl:template name="makeJSONObject">
- <xsl:param name="name"/>
- <xsl:param name="value"/>
- <xsl:if test="count($value/*) > 0">
- <xsl:text>{</xsl:text>
- </xsl:if>
- <xsl:value-of select="$name"/>
- <xsl:text>:</xsl:text>
- <xsl:call-template name="makeJSONValue">
- <xsl:with-param name="value" select="$value/*"/>
- </xsl:call-template>
- <xsl:if test="count($value/*) > 0">
- <xsl:text>}</xsl:text>
- </xsl:if>
- </xsl:template>
- <xsl:template name="makeJSONValue">
- <xsl:param name="value"/>
- <!-- this node has children so make another JSON object-->
- <xsl:choose>
- <xsl:when test="count($value/*) > 0">
- <xsl:call-template name="makeJSONObject">
- <xsl:with-param name="name" select="local-name($value)"/>
- <xsl:with-param name="value" select="$value/*"/>
- </xsl:call-template>
- </xsl:when>
- <!-- encode a string -->
- <xsl:otherwise>
- <xsl:text>'</xsl:text>
- <!-- javscript encode the contents -->
- <xsl:value-of select="xtsext:javascriptencode(string(.))"/>
- <xsl:text>'</xsl:text>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
- <xsl:template name="makeJSONArray">
- <xsl:param name="value"/>
- <xsl:text>[</xsl:text>
- <xsl:call-template name="makeJSONObject">
- <xsl:with-param name="name" select="local-name($value)"/>
- <xsl:with-param name="value" select="$value"/>
- </xsl:call-template>
- <xsl:text>]</xsl:text>
- </xsl:template>
- </xsl:stylesheet>
|