json-converter.xslt 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. Licensed Materials - Property of IBM
  4. IBM Cognos Products: HTS
  5. (C) Copyright IBM Corp. 2005, 2010
  6. US Government Users Restricted Rights - Use, duplication or disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
  7. -->
  8. <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">
  9. <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
  10. <xsl:template name="convertToJSON">
  11. <xsl:param name="taskInfo"/>
  12. <xsl:param name="isCurrentNodeArrayItem" select="false()"/>
  13. <xsl:if test="$taskInfo!=''">
  14. <xsl:for-each select="$taskInfo/*">
  15. <xsl:variable name="children" select="./*"/>
  16. <xsl:variable name="hasChildren" select="count($children) &gt; 0"/>
  17. <xsl:variable name="firstChild" select="local-name($children[1])"/>
  18. <xsl:variable name="arrayItems">
  19. <xsl:value-of select="count($children[local-name(.)=$firstChild])=count($children) and count($children) &gt; 1"/>
  20. </xsl:variable>
  21. <xsl:choose>
  22. <xsl:when test="$hasChildren='true'">
  23. <xsl:call-template name="makeJSONNameObjectPair">
  24. <xsl:with-param name="value" select="."/>
  25. <xsl:with-param name="isArray" select="$arrayItems"/>
  26. <xsl:with-param name="isCurrentNodeArrayItem" select="$isCurrentNodeArrayItem"/>
  27. </xsl:call-template>
  28. </xsl:when>
  29. <xsl:otherwise>
  30. <xsl:call-template name="makeJSONNameStringPair">
  31. <xsl:with-param name="value" select="."/>
  32. <xsl:with-param name="isCurrentNodeArrayItem" select="$isCurrentNodeArrayItem"/>
  33. </xsl:call-template>
  34. </xsl:otherwise>
  35. </xsl:choose>
  36. <xsl:if test="$isCurrentNodeArrayItem='true' and position()=1">
  37. <xsl:text>}</xsl:text>
  38. </xsl:if>
  39. <xsl:if test="position() &gt; 1 and position() != last() and $isCurrentNodeArrayItem='true'">
  40. <xsl:text>}</xsl:text>
  41. </xsl:if>
  42. <xsl:if test="position() != last()">
  43. <xsl:text>,</xsl:text>
  44. </xsl:if>
  45. </xsl:for-each>
  46. </xsl:if>
  47. </xsl:template>
  48. <xsl:template name="makeJSONNameObjectPair">
  49. <xsl:param name="value"/>
  50. <xsl:param name="isArray"/>
  51. <xsl:param name="isCurrentNodeArrayItem"/>
  52. <!-- inside an array each item starts with '{' -->
  53. <xsl:if test="position() &gt; 1 and $isCurrentNodeArrayItem=true()">
  54. <xsl:text>{</xsl:text>
  55. </xsl:if>
  56. <xsl:value-of select="local-name($value)"/>
  57. <xsl:text>:</xsl:text>
  58. <!-- a JSON array starts with '[' -->
  59. <xsl:if test="$isArray='true'">
  60. <xsl:text>[</xsl:text>
  61. </xsl:if>
  62. <!-- start of JSON object -->
  63. <xsl:text>{</xsl:text>
  64. <xsl:call-template name="convertToJSON">
  65. <xsl:with-param name="taskInfo" select="$value"/>
  66. <xsl:with-param name="isCurrentNodeArrayItem" select="$isArray='true'"/>
  67. </xsl:call-template>
  68. <!-- end of JSON object -->
  69. <xsl:text>}</xsl:text>
  70. <!-- mark end of array -->
  71. <xsl:if test="$isArray='true'">
  72. <xsl:text>]</xsl:text>
  73. </xsl:if>
  74. </xsl:template>
  75. <xsl:template name="makeJSONNameStringPair">
  76. <xsl:param name="value"/>
  77. <xsl:param name="isCurrentNodeArrayItem"/>
  78. <xsl:if test="position() &gt; 1 and $isCurrentNodeArrayItem=true()">
  79. <xsl:text>{</xsl:text>
  80. </xsl:if>
  81. <xsl:value-of select="local-name($value)"/>
  82. <xsl:text>:</xsl:text>
  83. <xsl:text>'</xsl:text>
  84. <!-- javscript encode the contents -->
  85. <xsl:value-of select="xtsext:javascriptencode($value)"/>
  86. <xsl:text>'</xsl:text>
  87. </xsl:template>
  88. <xsl:template name="makeJSONObject">
  89. <xsl:param name="name"/>
  90. <xsl:param name="value"/>
  91. <xsl:if test="count($value/*) &gt; 0">
  92. <xsl:text>{</xsl:text>
  93. </xsl:if>
  94. <xsl:value-of select="$name"/>
  95. <xsl:text>:</xsl:text>
  96. <xsl:call-template name="makeJSONValue">
  97. <xsl:with-param name="value" select="$value/*"/>
  98. </xsl:call-template>
  99. <xsl:if test="count($value/*) &gt; 0">
  100. <xsl:text>}</xsl:text>
  101. </xsl:if>
  102. </xsl:template>
  103. <xsl:template name="makeJSONValue">
  104. <xsl:param name="value"/>
  105. <!-- this node has children so make another JSON object-->
  106. <xsl:choose>
  107. <xsl:when test="count($value/*) &gt; 0">
  108. <xsl:call-template name="makeJSONObject">
  109. <xsl:with-param name="name" select="local-name($value)"/>
  110. <xsl:with-param name="value" select="$value/*"/>
  111. </xsl:call-template>
  112. </xsl:when>
  113. <!-- encode a string -->
  114. <xsl:otherwise>
  115. <xsl:text>'</xsl:text>
  116. <!-- javscript encode the contents -->
  117. <xsl:value-of select="xtsext:javascriptencode(string(.))"/>
  118. <xsl:text>'</xsl:text>
  119. </xsl:otherwise>
  120. </xsl:choose>
  121. </xsl:template>
  122. <xsl:template name="makeJSONArray">
  123. <xsl:param name="value"/>
  124. <xsl:text>[</xsl:text>
  125. <xsl:call-template name="makeJSONObject">
  126. <xsl:with-param name="name" select="local-name($value)"/>
  127. <xsl:with-param name="value" select="$value"/>
  128. </xsl:call-template>
  129. <xsl:text>]</xsl:text>
  130. </xsl:template>
  131. </xsl:stylesheet>