json-to-xml.xsl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!--<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  3. xmlns:fix_se_defaultdb_schema="http://biuro.biall-net.pl/xmlschema_procesy5/WPS_Functions/fix_se_defaultdb_schema/fix_se_defaultdb_schema.xsd"
  4. xmlns:system_cache__appinfo="http://biuro.biall-net.pl/xmlschema_procesy5/default_db_xml_cache/appinfo.xsd"
  5. >
  6. <xsl:output indent="yes"/>
  7. <xsl:strip-space elements="*"/>
  8. <xsl:template match="fix_se_defaultdb_schema:fix_sql_ID_KORESP_PROJ_errors.json">
  9. <xsl:copy>
  10. <xsl:copy-of select="@*"/>
  11. <xsl:call-template name="json2xml">
  12. <xsl:with-param name="text" select="."/>
  13. </xsl:call-template>
  14. </xsl:copy>
  15. </xsl:template>
  16. <xsl:template name="json2xml">
  17. <xsl:param name="text"/>
  18. <xsl:variable name="mode0">
  19. <xsl:variable name="regexps" select="'//(.*?)\n', '/\*(.*?)\*/', '(''|&quot;)(([^\\]|\\[\\&quot;''/btnvfr])*?)\3', '(-?\d+(\.\d+([eE][+-]?\d+)?|[eE][+-]?\d+))', '(-?[1-9]\d*)', '(-?0[0-7]+)', '(-?0x[0-9a-fA-F]+)', '([:,\{\}\[\]])', '(true|false)', '(null)'"/>
  20. <xsl:analyze-string select="$text" regex="{string-join($regexps,'|')}" flags="s">
  21. <xsl:matching-substring>
  22. <xsl:choose>
  23. <!-\- single line comment -\->
  24. <xsl:when test="regex-group(1)">
  25. <xsl:comment>
  26. <xsl:value-of select="regex-group(1)"/>
  27. </xsl:comment>
  28. <xsl:text>&#10;</xsl:text>
  29. </xsl:when>
  30. <!-\- multi line comment -\->
  31. <xsl:when test="regex-group(2)">
  32. <xsl:comment>
  33. <xsl:value-of select="regex-group(2)"/>
  34. </xsl:comment>
  35. </xsl:when>
  36. <!-\- string -\->
  37. <xsl:when test="regex-group(3)">
  38. <string>
  39. <xsl:analyze-string select="regex-group(4)" regex="\\([\\&quot;'/btnvfr])" flags="s">
  40. <xsl:matching-substring>
  41. <xsl:variable name="s" select="regex-group(1)"/>
  42. <xsl:choose>
  43. <xsl:when test="$s=('\', '&quot;', '''', '/')">
  44. <xsl:value-of select="regex-group(1)"/>
  45. </xsl:when>
  46. <xsl:when test="$s='b'">
  47. <!-\-xsl:text>&#8;</xsl:text-\->
  48. <xsl:message select="'escape sequense \b is not supported by XML'"/>
  49. <xsl:text>\b</xsl:text>
  50. </xsl:when>
  51. <xsl:when test="$s='t'">
  52. <xsl:text>&#9;</xsl:text>
  53. </xsl:when>
  54. <xsl:when test="$s='n'">
  55. <xsl:text>&#10;</xsl:text>
  56. </xsl:when>
  57. <xsl:when test="$s='v'">
  58. <!-\-xsl:text>&#11;</xsl:text-\->
  59. <xsl:message select="'escape sequence \v is not supported by XML'"/>
  60. <xsl:text>\v</xsl:text>
  61. </xsl:when>
  62. <xsl:when test="$s='f'">
  63. <!-\-xsl:text>&#12;</xsl:text-\->
  64. <xsl:message select="'escape sequence \f is not supported by XML'"/>
  65. <xsl:text>\f</xsl:text>
  66. </xsl:when>
  67. <xsl:when test="$s='r'">
  68. <xsl:text>&#13;</xsl:text>
  69. </xsl:when>
  70. <xsl:otherwise>
  71. <xsl:message terminate="yes" select="'internal error'"/>
  72. </xsl:otherwise>
  73. </xsl:choose>
  74. </xsl:matching-substring>
  75. <xsl:non-matching-substring>
  76. <xsl:value-of select="."/>
  77. </xsl:non-matching-substring>
  78. </xsl:analyze-string>
  79. </string>
  80. </xsl:when>
  81. <!-\- double -\->
  82. <xsl:when test="regex-group(6)">
  83. <double>
  84. <xsl:value-of select="regex-group(6)"/>
  85. </double>
  86. </xsl:when>
  87. <!-\- integer -\->
  88. <xsl:when test="regex-group(9)">
  89. <integer>
  90. <xsl:value-of select="regex-group(9)"/>
  91. </integer>
  92. </xsl:when>
  93. <!-\- octal -\->
  94. <xsl:when test="regex-group(10)">
  95. <integer>
  96. <xsl:value-of xmlns:Integer="java:java.lang.Integer" select="Integer:parseInt(regex-group(10), 8)"/>
  97. </integer>
  98. </xsl:when>
  99. <!-\- hex -\->
  100. <xsl:when test="regex-group(11)">
  101. <integer>
  102. <xsl:value-of xmlns:Integer="java:java.lang.Integer" select="Integer:parseInt(replace(regex-group(11), '0x', ''), 16)"/>
  103. </integer>
  104. </xsl:when>
  105. <!-\- symbol -\->
  106. <xsl:when test="regex-group(12)">
  107. <symbol>
  108. <xsl:value-of select="regex-group(12)"/>
  109. </symbol>
  110. </xsl:when>
  111. <!-\- boolean -\->
  112. <xsl:when test="regex-group(13)">
  113. <boolean>
  114. <xsl:value-of select="regex-group(13)"/>
  115. </boolean>
  116. </xsl:when>
  117. <!-\- null -\->
  118. <xsl:when test="regex-group(14)">
  119. <null />
  120. </xsl:when>
  121. <xsl:otherwise>
  122. <xsl:message terminate="yes" select="'internal error'"/>
  123. </xsl:otherwise>
  124. </xsl:choose>
  125. </xsl:matching-substring>
  126. <xsl:non-matching-substring>
  127. <xsl:if test="normalize-space()!=''">
  128. <xsl:message select="concat('unknown token: ', .)"/>
  129. <xsl:value-of select="."/>
  130. </xsl:if>
  131. </xsl:non-matching-substring>
  132. </xsl:analyze-string>
  133. </xsl:variable>
  134. <xsl:variable name="mode1">
  135. <xsl:apply-templates mode="json2xml1" select="$mode0/node()[1]"/>
  136. </xsl:variable>
  137. <xsl:variable name="mode2">
  138. <xsl:apply-templates mode="json2xml2" select="$mode1"/>
  139. </xsl:variable>
  140. <xsl:variable name="mode3">
  141. <xsl:apply-templates mode="json2xml3" select="$mode2"/>
  142. </xsl:variable>
  143. <xsl:copy-of select="$mode3"/> <!-\- change $mode3 to $mode[0-2] for easy debug -\->
  144. </xsl:template>
  145. <!-\- json2xml1 mode: group content between {} and [] into object and array elements -\->
  146. <xsl:template mode="json2xml1" match="node()" priority="-9">
  147. <xsl:copy-of select="."/>
  148. <xsl:apply-templates mode="json2xml1" select="following-sibling::node()[1]"/>
  149. </xsl:template>
  150. <xsl:template mode="json2xml1" match="symbol[.=('}',']')]"/>
  151. <xsl:template mode="json2xml1" match="symbol[.=('{','[')]">
  152. <xsl:element name="{if (.='{') then 'object' else 'array'}">
  153. <xsl:apply-templates mode="json2xml1" select="following-sibling::node()[1]"/>
  154. </xsl:element>
  155. <xsl:variable name="level" select="count(preceding-sibling::symbol[.=('{','[')])-count(preceding-sibling::symbol[.=('}',']')])+1"/>
  156. <xsl:variable name="ender"
  157. select="following-sibling::symbol[.=('}',']') and count(preceding-sibling::symbol[.=('{','[')])-count(preceding-sibling::symbol[.=('}',']')])=$level][1]"/>
  158. <xsl:apply-templates mode="json2xml1" select="$ender/following-sibling::node()[1]"/>
  159. </xsl:template>
  160. <!-\- json2xml2 mode: group <string>:<string|integer|double|object|array> into field element -\->
  161. <xsl:template priority="-9" mode="json2xml2" match="@*|node()">
  162. <xsl:copy>
  163. <xsl:apply-templates mode="json2xml2" select="@*|node()"/>
  164. </xsl:copy>
  165. </xsl:template>
  166. <xsl:template mode="json2xml2"
  167. match="string[following-sibling::*[1]/self::symbol[.=':'] and following-sibling::*[2]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null)]"/>
  168. <xsl:template mode="json2xml2"
  169. match="symbol[.=':'][preceding-sibling::*[1]/self::string and following-sibling::*[1]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null)]">
  170. <field name="{preceding-sibling::*[1]}">
  171. <xsl:for-each select="following-sibling::*[1]">
  172. <xsl:copy>
  173. <xsl:apply-templates mode="json2xml2" select="@*|node()"/>
  174. </xsl:copy>
  175. </xsl:for-each>
  176. </field>
  177. </xsl:template>
  178. <xsl:template mode="json2xml2"
  179. match="*[self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null][preceding-sibling::*[2]/self::string and preceding-sibling::*[1]/self::symbol[.=':']]"/>
  180. <!-\- json2xml3 mode: drop comma between consecutive field and object elements -\->
  181. <xsl:template priority="-9" mode="json2xml3" match="@*|node()">
  182. <xsl:copy>
  183. <xsl:apply-templates mode="json2xml3" select="@*|node()"/>
  184. </xsl:copy>
  185. </xsl:template>
  186. <xsl:template mode="json2xml3" match="object/symbol[.=','][preceding-sibling::*[1]/self::field and following-sibling::*[1]/self::field]"/>
  187. <xsl:template mode="json2xml3" match="array/symbol[.=','][preceding-sibling::*[1]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null) and following-sibling::*[1]/(self::string|self::integer|self::double|self::boolean|self::object|self::array|self::null)]"/>
  188. </xsl:stylesheet>-->
  189. <!--<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  190. xmlns:xs="http://www.w3.org/2001/XMLSchema"
  191. exclude-result-prefixes="xs"
  192. xmlns:system_cache__appinfo="http://biuro.biall-net.pl/xmlschema_procesy5/default_db_xml_cache/appinfo.xsd"
  193. version="2.0">
  194. <xsl:output indent="yes"/>
  195. <xsl:strip-space elements="*"/>
  196. <xsl:template name="system_cache__appinfo:json-to-xml" match="contains( local-name(),'.json')">
  197. <system_cache__appinfo:json-to-xml>
  198. <xsl:attribute name="system_cache__appinfo:name" select="name()"/>
  199. <xsl:for-each select="tokenize( text(),'{')">
  200. <root>
  201. </root>
  202. </xsl:for-each>
  203. </system_cache__appinfo:json-to-xml>
  204. </xsl:template>
  205. </xsl:stylesheet>-->