xml2json.xslt 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--
  3. +========================================================================+
  4. | IBM Confidential
  5. | OCO Source Materials
  6. | IBM Cognos Products: BUX
  7. | (C) Copyright IBM Corp. 2009, 2013
  8. |
  9. | The source code for this program is not published or otherwise
  10. | divested of its trade secrets, irrespective of what has been deposited
  11. | with the U.S. Copyright Office.
  12. +========================================================================+
  13. -->
  14. <xsl:stylesheet version="1.0"
  15. xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  16. xmlns:helper="com.cognos.portal.utils.EncodingUtils"
  17. extension-element-prefixes="helper">
  18. <xsl:output method="text" version="1.0" encoding="UTF-8" indent="no"/>
  19. <xsl:param name="arrays" select="'|application|propertyGroup|property|defaultValue|choice|feature|'"/>
  20. <xsl:param name="prefixes" select="'atom'"/>
  21. <xsl:param name="exclude-prefixes" select="concat(' ', $prefixes, ' ')"/>
  22. <xsl:param name="include-lang" select="true()"/>
  23. <xsl:param name="tagsAsStrings" select="''"/>
  24. <xsl:template match="/">
  25. <xsl:apply-templates select="*|@*" mode="json"/>
  26. </xsl:template>
  27. <xsl:template match="*|@*|text()" mode="json">
  28. <xsl:variable name="this-node" select="local-name()"/>
  29. <xsl:variable name="this-ns" select="namespace-uri()"/>
  30. <xsl:variable name="precedings" select="preceding-sibling::*[local-name() = $this-node and namespace-uri() = $this-ns]"/>
  31. <xsl:variable name="followings" select="following-sibling::*[local-name() = $this-node and namespace-uri() = $this-ns]"/>
  32. <xsl:variable name="isArray" select="contains($arrays , concat(concat('|', local-name()), '|') )"/>
  33. <xsl:if test="position() &gt; 1">,</xsl:if>
  34. <xsl:if test="not($precedings)">
  35. <xsl:if test="position() = 1">
  36. <xsl:text>{</xsl:text>
  37. </xsl:if>
  38. <xsl:text>&quot;</xsl:text>
  39. <xsl:choose>
  40. <xsl:when test="name() != '' and not(contains($exclude-prefixes, concat(' ', substring-before(name(), ':'), ' ')))">
  41. <xsl:value-of select="translate(name(),':','$')"/>
  42. </xsl:when>
  43. <xsl:when test="local-name() != ''">
  44. <xsl:value-of select="local-name()"/>
  45. </xsl:when>
  46. <xsl:otherwise>_text</xsl:otherwise>
  47. </xsl:choose>
  48. <xsl:if test="$followings or $isArray">
  49. <xsl:text>List</xsl:text>
  50. </xsl:if>
  51. <xsl:text>&quot;:</xsl:text>
  52. <xsl:if test="$followings or $isArray">
  53. <xsl:text>[</xsl:text>
  54. </xsl:if>
  55. </xsl:if>
  56. <xsl:choose>
  57. <xsl:when test="*">
  58. <xsl:apply-templates select="*|@*" mode="json"/>
  59. </xsl:when>
  60. <xsl:when test="@*[$include-lang or namespace-uri()!='http://www.w3.org/XML/1998/namespace']">
  61. <xsl:apply-templates select="@*|text()" mode="json"/>
  62. </xsl:when>
  63. <xsl:otherwise>
  64. <xsl:value-of select="concat('&quot;', helper:jsonencode(string(.)), '&quot;')" disable-output-escaping="yes"/>
  65. <!--
  66. <xsl:choose>
  67. <xsl:when test="not(contains($tagsAsStrings,concat(',', local-name() ,','))) and (.='true' or .='false' or string(number(.))!='NaN')">
  68. <xsl:value-of select="."/>
  69. </xsl:when>
  70. <xsl:otherwise>
  71. <xsl:value-of select="concat('&quot;', helper:jsonencode(string(.)), '&quot;')" disable-output-escaping="yes"/>
  72. </xsl:otherwise>
  73. </xsl:choose>
  74. -->
  75. </xsl:otherwise>
  76. </xsl:choose>
  77. <xsl:if test="not($followings)">
  78. <xsl:if test="$precedings or $isArray">
  79. <xsl:text>]</xsl:text>
  80. </xsl:if>
  81. <xsl:if test="position() = last()">
  82. <xsl:text>}</xsl:text>
  83. </xsl:if>
  84. </xsl:if>
  85. </xsl:template>
  86. <xsl:template match="text()"/>
  87. </xsl:stylesheet>
  88. <!-- $Header: //bux/main/prod/plugins/com.ibm.cognos.bux.utils/data/asJSON_compact.xslt#4 $ -->
  89. <!-- $DateTime: 2010/07/21 13:51:32 $ -->
  90. <!-- $Change: 12887 $ -->