Advertisement
rplantiko

Convert ABAP data to JSON

Sep 12th, 2011
1,386
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 4.91 KB | None | 0 0
  1. <!-- Generates JSON data from ABAP data, see my blog:
  2.     http://ruediger-plantiko.blogspot.com/2011/09/ein-json-builder-in-abap.html -->
  3. <xsl:transform version="1.0"
  4.  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  5.  xmlns:sap="http://www.sap.com/sapxsl"
  6.  xmlns:asx="http://www.sap.com/abapxml">
  7.  
  8.   <xsl:strip-space elements="*"/>
  9.  
  10.   <xsl:template match="/">
  11.     <xsl:apply-templates select="/asx:abap/asx:values"/>
  12.   </xsl:template>
  13.  
  14.   <xsl:template match="DATA_ROOT">
  15.     <xsl:call-template name="getData"/>
  16.   </xsl:template>
  17.  
  18.   <xsl:template name="getData">
  19.     <asx:abap>
  20.       <asx:values>
  21.         <JSON>
  22.           <xsl:call-template name="showData">
  23.             <xsl:with-param name="data" select="."/>
  24.           </xsl:call-template>
  25.         </JSON>
  26.       </asx:values>
  27.     </asx:abap>
  28.   </xsl:template>
  29.  
  30.   <xsl:template name="showData">
  31.     <xsl:param name="data"/>
  32.     <xsl:variable name="type" select="$data/TYPE"/>
  33.     <xsl:for-each select="$data/DATA">
  34.       <xsl:choose>
  35.         <xsl:when test="$type = 'N'">
  36.           <xsl:call-template name="simpleValue"/>
  37.         </xsl:when>
  38.         <xsl:when test="$type = 'S'">
  39.           <xsl:call-template name="stringValue"/>
  40.         </xsl:when>
  41.         <xsl:when test="$type = 'B'">
  42.           <xsl:call-template name="booleanValue"/>
  43.         </xsl:when>
  44.         <xsl:when test="$type = 'h'">
  45.           <xsl:call-template name="showHash"/>
  46.         </xsl:when>
  47.         <xsl:when test="$type = 'a'">
  48.           <xsl:call-template name="showArray"/>
  49.         </xsl:when>
  50.       </xsl:choose>
  51.     </xsl:for-each>
  52.   </xsl:template>
  53.  
  54.   <xsl:template name="showHash">
  55.     <xsl:variable name="id" select="substring(@href,2)"/>
  56.     <xsl:text>{</xsl:text>
  57.     <xsl:for-each select="/asx:abap/asx:heap/*[@id = $id]/*">
  58.       <xsl:if test="position() > 1">,</xsl:if>
  59.       <xsl:call-template name="showHashRow"/>
  60.     </xsl:for-each>
  61.     <xsl:text>}</xsl:text>
  62.   </xsl:template>
  63.  
  64.   <xsl:template name="showHashRow">
  65.     <xsl:text>"</xsl:text><xsl:value-of select="KEY"/><xsl:text>":</xsl:text>
  66.     <xsl:call-template name="showData">
  67.       <xsl:with-param name="data" select="."/>
  68.     </xsl:call-template>
  69.   </xsl:template>
  70.  
  71.  
  72.   <xsl:template name="showArray">
  73.     <xsl:variable name="id" select="substring(@href,2)"/>
  74.     <xsl:text>[</xsl:text>
  75.     <xsl:for-each select="/asx:abap/asx:heap/*[@id = $id]/*">
  76.       <xsl:if test="position() > 1">,</xsl:if>
  77.       <xsl:call-template name="showData">
  78.         <xsl:with-param name="data" select="."/>
  79.       </xsl:call-template>
  80.     </xsl:for-each>
  81.     <xsl:text>]</xsl:text>
  82.   </xsl:template>
  83.  
  84.   <xsl:template name="stringValue">
  85.     <xsl:text>"</xsl:text><xsl:call-template name="string-replace-all">
  86.       <xsl:with-param name="text">
  87.         <xsl:call-template name="string-replace-all">
  88.           <xsl:with-param name="text">
  89.             <xsl:call-template name="simpleValue"/>
  90.           </xsl:with-param>
  91.           <xsl:with-param name="replace">\</xsl:with-param>
  92.           <xsl:with-param name="by">\\</xsl:with-param>
  93.         </xsl:call-template>
  94.       </xsl:with-param>
  95.       <xsl:with-param name="replace">"</xsl:with-param>
  96.       <xsl:with-param name="by">\"</xsl:with-param>
  97.     </xsl:call-template><xsl:text>"</xsl:text>
  98.   </xsl:template>
  99.  
  100.   <xsl:template name="simpleValue">
  101.     <xsl:choose>
  102.       <xsl:when test="@href">
  103.         <xsl:call-template name="getReference">
  104.           <xsl:with-param name="href" select="./@href"/>
  105.         </xsl:call-template>
  106.       </xsl:when>
  107.       <xsl:otherwise><xsl:value-of select="."/></xsl:otherwise>
  108.     </xsl:choose>
  109.   </xsl:template>
  110.  
  111.   <xsl:template name="booleanValue">
  112.     <xsl:variable name="value">
  113.       <xsl:call-template name="simpleValue"/>
  114.     </xsl:variable>
  115.     <xsl:choose>
  116.       <xsl:when test="contains( $value, 'X')">true</xsl:when>
  117.       <xsl:otherwise>false</xsl:otherwise>
  118.     </xsl:choose>
  119.   </xsl:template>
  120.  
  121.   <xsl:template name="getReference">
  122.     <xsl:param name="href"/>
  123.     <xsl:value-of select="/asx:abap/asx:heap/*[@id = substring($href,2)]"/>
  124.   </xsl:template>
  125.  
  126. <!-- Template dankend übernommen aus http://geekswithblogs.net/Erik/archive/2008/04/01/120915.aspx -->
  127.   <xsl:template name="string-replace-all">
  128.     <xsl:param name="text" />
  129.     <xsl:param name="replace" />
  130.     <xsl:param name="by"/>
  131.     <xsl:choose>
  132.       <xsl:when test="contains($text, $replace)">
  133.         <xsl:value-of select="substring-before($text,$replace)" />
  134.         <xsl:value-of select="$by" />
  135.         <xsl:call-template name="string-replace-all">
  136.           <xsl:with-param name="text"
  137.          select="substring-after($text,$replace)" />
  138.           <xsl:with-param name="replace" select="$replace" />
  139.           <xsl:with-param name="by" select="$by" />
  140.         </xsl:call-template>
  141.       </xsl:when>
  142.       <xsl:otherwise>
  143.         <xsl:value-of select="$text" />
  144.       </xsl:otherwise>
  145.     </xsl:choose>
  146.   </xsl:template>
  147.  
  148. </xsl:transform>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement