Guest User

xml2json

a guest
Feb 25th, 2014
185
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 14.78 KB | None | 0 0
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <xsl:stylesheet version="1.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
  3.  
  4.   <xsl:output method="text" />
  5.  
  6.  
  7.   <xsl:variable name="quot">"</xsl:variable>
  8. <xsl:variable name="t_attPrefix"></xsl:variable>
  9. <xsl:variable name="t_attSuffix"></xsl:variable>
  10. <xsl:variable name="t_txtPrefix">value</xsl:variable>
  11. <xsl:variable name="t_txtSuffix"></xsl:variable>
  12. <xsl:variable name="t_encaseForArray">false</xsl:variable>
  13. <xsl:variable name="t_flattenSimpleElements">true</xsl:variable>
  14. <xsl:variable name="t_flattenSimpleCollectionsToArrays">true</xsl:variable>
  15. <xsl:variable name="t_dropRoot">false</xsl:variable>
  16. <xsl:variable name="t_elementAppendForUnique">Node</xsl:variable>
  17. <xsl:variable name="t_item"><from>'</from><to>\'</to></xsl:variable>
  18. <xsl:variable name="t_escapeFrom">'</xsl:variable>
  19. <xsl:variable name="t_escapeTo">\'</xsl:variable>
  20.  
  21.   <xsl:param name="encaseObject" select="$quot" />
  22.   <xsl:param name="encaseString" select="$quot" />
  23.   <xsl:param name="attPrefix" select="$t_attPrefix" />
  24.   <xsl:param name="attSuffix" select="$t_attSuffix" />
  25.   <xsl:param name="txtPrefix" select="$t_txtPrefix" />
  26.   <xsl:param name="txtSuffix" select="$t_txtSuffix" />
  27.  
  28.   <xsl:param name="encaseForArray" select="$t_encaseForArray='true'"/>
  29.   <xsl:param name="flattenSimpleElements" select="$t_flattenSimpleElements='true'"/>
  30.   <xsl:param name="flattenSimpleCollectionsToArrays" select="$t_flattenSimpleCollectionsToArrays='true'"/>
  31.   <xsl:param name="dropRoot" select="$t_dropRoot='true'"/>
  32.   <xsl:param name="elementAppendForUnique" select="t_elementAppendForUnique" />
  33.  
  34.   <xsl:param name="cln">:</xsl:param>
  35.  
  36.   <xsl:template match="/">
  37.    
  38.     <!-- Build the Initial JSON -->
  39.     <xsl:variable name="initial_JSON">
  40.       <xsl:apply-templates select="current()/child::*" mode="build" >
  41.         <xsl:with-param name="path" select="'/'"/>
  42.       </xsl:apply-templates>
  43.     </xsl:variable>
  44.  
  45.    
  46.     <xsl:variable name="JSON">
  47.       <xsl:choose>
  48.         <xsl:when test="$dropRoot">
  49.           <xsl:variable name="rootNode" select="name(/*[1])"/>
  50.           <xsl:value-of select="substring-after($initial_JSON,concat($rootNode,$cln))"/>
  51.         </xsl:when>
  52.         <xsl:otherwise>
  53.           <xsl:value-of select="concat('{',$initial_JSON,'}')"/>
  54.         </xsl:otherwise>
  55.       </xsl:choose>
  56.     </xsl:variable>
  57.        
  58.     <xsl:if test="$encaseForArray=true()">[</xsl:if>
  59.     <xsl:value-of select="$JSON"/>
  60.     <xsl:if test="$encaseForArray=true()">]</xsl:if>
  61.  
  62.     <!--
  63.    PROCESSING COMPLETED.....
  64.    -->
  65.   </xsl:template>
  66.  
  67.   <xsl:template match="*" mode="build">
  68.  
  69.     <xsl:param name="path"/>
  70.     <xsl:variable name="nName" select="name(.)"/>
  71.     <xsl:variable name="iPreceding" select="count(preceding-sibling::*[name()=$nName])"/>
  72.     <xsl:variable name="iFollowing" select="count(following-sibling::*[name()=$nName])"/>
  73.  
  74.     <xsl:variable name="nameSuffix">
  75.       <xsl:if test="count(parent::*[name(@*)=$nName]) &gt; 0">
  76.         <xsl:value-of select="$elementAppendForUnique"/>
  77.       </xsl:if>
  78.     </xsl:variable>
  79.    
  80.     <xsl:choose>
  81.      <xsl:when test="$iPreceding = 0 and $iFollowing &gt; 0">
  82.  
  83.         <xsl:value-of select="concat($encaseObject,$nName,$nameSuffix,$encaseObject,$cln,'[')"/>
  84.  
  85.         <!--
  86.        enumerate through same named nodes
  87.        -->
  88.         <xsl:for-each select="../*[name()=$nName]">
  89.  
  90.           <xsl:variable name="nonSimpleChildrenCount">
  91.             <xsl:call-template name="support-count-nonsimple-children">
  92.               <xsl:with-param name="startNode" select="current()"/>
  93.             </xsl:call-template>
  94.           </xsl:variable>
  95.  
  96.           <xsl:variable name="isSimple" select="number($nonSimpleChildrenCount)=0"/>
  97.  
  98.           <xsl:variable name="json_content">
  99.             <xsl:apply-templates select="current()" mode="process-node">
  100.               <xsl:with-param name="path" select="$path"/>
  101.               <xsl:with-param name="isSimpleNode" select="$isSimple"/>
  102.               <xsl:with-param name="isCollection" select="true()"/>
  103.             </xsl:apply-templates>
  104.           </xsl:variable>
  105.  
  106.           <xsl:value-of select="$json_content"/>
  107.  
  108.           <!--
  109.          if there are children then do an iterative call
  110.          -->
  111.           <xsl:if test="child::*">
  112.             <xsl:if test="string-length($json_content) &gt; 1">
  113.               <xsl:text>,</xsl:text>
  114.             </xsl:if>
  115.             <xsl:apply-templates select="current()/*" mode="build">
  116.               <xsl:with-param name="path" select="concat($path,name(),'/')"/>
  117.             </xsl:apply-templates>
  118.           </xsl:if>
  119.  
  120.           <xsl:if test="($isSimple=false() and $flattenSimpleElements=true()) or $flattenSimpleElements=false()">
  121.             <xsl:text>}</xsl:text>
  122.           </xsl:if>
  123.  
  124.           <xsl:if test="position()!=last()">
  125.             <xsl:text>,</xsl:text>
  126.           </xsl:if>
  127.  
  128.         </xsl:for-each>
  129.  
  130.         <xsl:text>]</xsl:text>
  131.  
  132.         <xsl:if test="following-sibling::*[name()!=$nName]">
  133.           <xsl:text>,</xsl:text>
  134.         </xsl:if>
  135.       </xsl:when>
  136.  
  137.       <!--
  138.      TYPE 2: SINGLETON OBJECTS
  139.      The current Node is a singleton with no siblings
  140.      -->
  141.       <xsl:when test="$iPreceding = 0 and $iFollowing = 0">
  142.  
  143.         <!-- get the simple status of the element -->
  144.         <xsl:variable name="nonSimpleChildrenCount">
  145.           <xsl:call-template name="support-count-nonsimple-children">
  146.             <xsl:with-param name="startNode" select="current()"/>
  147.           </xsl:call-template>
  148.         </xsl:variable>
  149.         <xsl:variable name="isSimple" select="number($nonSimpleChildrenCount)=0"/>
  150.  
  151.         <xsl:variable name="json_content">
  152.           <xsl:apply-templates select="current()" mode="process-node">
  153.             <xsl:with-param name="path" select="$path"/>
  154.             <xsl:with-param name="isSimple" select="$isSimple"/>
  155.             <xsl:with-param name="isCollection" select="false()"/>
  156.           </xsl:apply-templates>
  157.         </xsl:variable>
  158.  
  159.         <xsl:value-of select="concat($encaseObject, $nName, $nameSuffix, $encaseObject, $cln)"/>
  160.         <xsl:value-of select="$json_content"/>
  161.  
  162.         <xsl:if test="child::*">
  163.           <xsl:if test="string-length($json_content) &gt; 1">
  164.             <xsl:text>,</xsl:text>
  165.           </xsl:if>
  166.           <xsl:apply-templates select="current()/*" mode="build">
  167.             <xsl:with-param name="path" select="concat($path,$nName,'/')"/>
  168.           </xsl:apply-templates>
  169.         </xsl:if>
  170.  
  171.         <xsl:if test="($isSimple=false() and $flattenSimpleElements=true()) or $flattenSimpleElements=false()">
  172.           <xsl:text>}</xsl:text>
  173.         </xsl:if>
  174.  
  175.         <xsl:if test="following-sibling::*">
  176.           <xsl:text>,</xsl:text>
  177.         </xsl:if>
  178.  
  179.       </xsl:when>
  180.  
  181.     </xsl:choose>
  182.  
  183.   </xsl:template>
  184.  
  185.  
  186.   <xsl:template match="*" mode="process-node">
  187.    
  188.     <xsl:param name="path" />
  189.     <xsl:param name="isSimple"/>
  190.     <xsl:param name="isCollection"/>
  191.  
  192.     <xsl:if test="($isSimple=false() and $flattenSimpleElements=true()) or $flattenSimpleElements=false()">
  193.       <xsl:text>{</xsl:text>
  194.     </xsl:if>
  195.  
  196.     <xsl:apply-templates select="@*" mode="process-attributes">
  197.       <xsl:with-param name="path" select="concat($path,name(),'/')"/>
  198.     </xsl:apply-templates>
  199.  
  200.     <xsl:if test="@* and string-length(text())!=0">
  201.       <xsl:text>,</xsl:text>
  202.     </xsl:if>
  203.    
  204.     <xsl:apply-templates select="." mode="process-element">
  205.       <xsl:with-param name="path" select="concat($path,name(),'/')"/>
  206.       <xsl:with-param name="isSimple" select="$isSimple"/>
  207.       <xsl:with-param name="isCollection" select="$isCollection"/>
  208.     </xsl:apply-templates>
  209.    
  210.   </xsl:template>
  211.  
  212.   <xsl:template match="@*" mode="process-attributes">
  213.     <xsl:param name="path"/>
  214.     <xsl:param name="isSimple"/>
  215.  
  216.     <xsl:variable name="cleaned">
  217.       <xsl:call-template name="process-string-content">
  218.         <xsl:with-param name="valueToProcess" select="normalize-space(.)"/>
  219.         <xsl:with-param name="path" select="concat($path,'@',name(),'/')"/>
  220.       </xsl:call-template>
  221.     </xsl:variable>
  222.     <xsl:value-of select="concat($encaseObject,$attPrefix,name(),$attSuffix,$encaseObject,$cln,$cleaned)"/>
  223.     <xsl:if test="position()!=last()">
  224.       <xsl:text>,</xsl:text>
  225.     </xsl:if>
  226.   </xsl:template>
  227.  
  228.   <xsl:template match="*" mode="process-element">
  229.     <xsl:param name="path"/>
  230.     <xsl:param name="isSimple"/>
  231.     <xsl:param name="isCollection"/>
  232.  
  233.     <xsl:variable name="value" select="normalize-space(text())"/>
  234.     <xsl:if test="string-length($value)!=0">
  235.       <xsl:variable name="cleaned">
  236.         <xsl:call-template name="process-string-content">
  237.           <xsl:with-param name="valueToProcess" select="$value"/>
  238.           <xsl:with-param name="path" select="$path"/>
  239.         </xsl:call-template>
  240.       </xsl:variable>
  241.  
  242.       <xsl:choose>
  243.         <xsl:when test="$isSimple=true() and $flattenSimpleElements=true()">
  244.           <xsl:value-of select="$cleaned"/>
  245.         </xsl:when>
  246.         <xsl:when test="$isCollection=true() and $flattenSimpleCollectionsToArrays=true() and $flattenSimpleElements=true()">
  247.           <xsl:value-of select="$cleaned"/>
  248.         </xsl:when>
  249.         <xsl:otherwise>
  250.           <xsl:value-of select="concat($encaseObject,$txtPrefix,$txtSuffix,$encaseObject,$cln,$cleaned)"/>
  251.         </xsl:otherwise>
  252.       </xsl:choose>
  253.  
  254.     </xsl:if>
  255.  
  256.   </xsl:template>
  257.  
  258.   <xsl:template name="process-string-content">
  259.  
  260.     <xsl:param name="path"/>
  261.     <xsl:param name="valueToProcess"/>
  262.    
  263.     <xsl:variable name="value">
  264.       <xsl:call-template name="support-escape-characters">
  265.         <xsl:with-param name="cleanIt" select="$valueToProcess"/>
  266.       </xsl:call-template>
  267.     </xsl:variable>    
  268.    
  269.     <xsl:variable name="nPtrs" select="a"/>
  270.     <xsl:variable name="pathStrip" select="substring($path,1,string-length($path) -1)"/>
  271.  
  272.     <xsl:variable name="datatype">
  273.       <xsl:choose>
  274.         <xsl:when test="$nPtrs/pointer[text()=$pathStrip and @match='exact']">
  275.           <xsl:value-of select="$nPtrs/pointer[text()=$pathStrip and @match='exact']/@type"/>
  276.         </xsl:when>
  277.         <xsl:when test="$nPtrs/pointer[contains($pathStrip,text()) and @match='any']">
  278.           <xsl:value-of select="$nPtrs/pointer[contains($pathStrip,text()) and @match='any']/@type"/>
  279.         </xsl:when>
  280.         <xsl:when test="string(number($value))!='NaN'">
  281.           <xsl:text>number</xsl:text>
  282.         </xsl:when>
  283.         <xsl:when test="translate($value,'true','TRUE')='TRUE' or translate($value,'false','FALSE')='FALSE'">
  284.           <xsl:text>boolean</xsl:text>
  285.         </xsl:when>
  286.         <xsl:otherwise>
  287.           <xsl:text>string</xsl:text>
  288.         </xsl:otherwise>
  289.       </xsl:choose>
  290.     </xsl:variable>
  291.  
  292.     <xsl:choose>
  293.      
  294.       <xsl:when test="$datatype='native'">
  295.         <xsl:if test="string-length($value)=0">
  296.           <xsl:text>{}</xsl:text>
  297.         </xsl:if>
  298.         <xsl:value-of select="$value"/>
  299.       </xsl:when>
  300.      
  301.       <xsl:when test="$datatype='number'">
  302.         <xsl:if test="string-length($value)=0">
  303.           <xsl:text>null</xsl:text>
  304.         </xsl:if>
  305.         <xsl:value-of select="$value"/>
  306.       </xsl:when>
  307.      
  308.       <xsl:when test="$datatype='boolean'">
  309.         <xsl:choose>
  310.           <xsl:when test="translate($value,'TRUE','true')='true' or $value='1'">
  311.             <xsl:text>true</xsl:text>
  312.           </xsl:when>
  313.           <xsl:otherwise>
  314.             <xsl:text>false</xsl:text>
  315.           </xsl:otherwise>
  316.         </xsl:choose>
  317.       </xsl:when>
  318.      
  319.       <!-- assumes a format of yyyy-mm-ddThh:mm:ss -->
  320.       <xsl:when test="$datatype='date'">
  321.         <xsl:value-of select="concat('new Date(',substring($value,1,4),',',number(substring($value,6,2))-1,',',substring($value,9,2),')')"/>
  322.       </xsl:when>
  323.  
  324.       <!-- assumes a format of yyyy-mm-ddThh:mm:ss -->
  325.       <xsl:when test="$datatype='datetime'">
  326.         <xsl:value-of select="concat('new Date(',substring($value,1,4),',',number(substring($value,6,2))-1,',',substring($value,9,2),substring($value,12,2),',',substring($value,15,2),',',substring($value,18,2),')')"/>
  327.       </xsl:when>
  328.  
  329.       <xsl:when test="$datatype='string'">
  330.         <xsl:value-of select="concat($encaseString,$value,$encaseString)"/>
  331.       </xsl:when>
  332.  
  333.     </xsl:choose>    
  334.    
  335.   </xsl:template>
  336.  
  337.  
  338.   <xsl:template name="support-count-nonsimple-children">
  339.     <xsl:param name="startNode"/>
  340.  
  341.     <xsl:variable name="total">
  342.       <xsl:text>0</xsl:text>
  343.       <xsl:for-each select="$startNode">
  344.         <xsl:variable name="nName" select="name($startNode)"/>
  345.         <xsl:value-of select="number(count(current()/*)) +
  346.                              number(count(current()[@*])) +
  347.                              number(count(preceding-sibling::*[name()=$nName])) +
  348.                              number(count(following-sibling::*[name()=$nName]))"/>
  349.       </xsl:for-each>
  350.     </xsl:variable>
  351.  
  352.     <xsl:value-of select="number($total)"/>
  353.   </xsl:template>
  354.  
  355.   <!--
  356.  ///////////////////////////////////////////////////////////////////////////////////////////
  357.  SUPPORT: ESCAPE CHARACTERS
  358.  
  359.  Applies Character escaping base on the escape nodes in the config file
  360.  ///////////////////////////////////////////////////////////////////////////////////////////
  361.  -->
  362.   <xsl:template name="support-escape-characters">
  363.    
  364.     <xsl:param name="cleanIt"/>
  365.     <xsl:param name="cleaned"/>
  366.     <xsl:param name="nodePos" select="1"/>
  367.  
  368.     <xsl:variable name="escapeFrom" select="$t_escapeFrom"/>
  369.     <xsl:variable name="escapeTo" select="$t_escapeTo"/>
  370.  
  371.     <xsl:choose>
  372.       <xsl:when test="string-length(substring-before($cleanIt,$escapeFrom))!=0 or starts-with($cleanIt,$escapeFrom)">
  373.         <xsl:variable name="left" select="substring-before($cleanIt,$escapeFrom)"/>
  374.         <xsl:call-template name="support-escape-characters">
  375.           <xsl:with-param name="cleanIt" select="substring($cleanIt,number(string-length($left)+string-length($escapeFrom)+1),string-length($cleanIt))"/>
  376.           <xsl:with-param name="cleaned" select="concat($cleaned,$left,$escapeTo)"/>
  377.           <xsl:with-param name="nodePos" select="$nodePos"/>
  378.         </xsl:call-template>
  379.       </xsl:when>
  380.       <xsl:otherwise>
  381.         <xsl:choose>
  382.           <xsl:when test="number($nodePos) &lt; count($t_item)">
  383.             <xsl:call-template name="support-escape-characters">
  384.               <xsl:with-param name="cleanIt" select="concat($cleaned,$cleanIt)"/>
  385.               <xsl:with-param name="nodePos" select="number($nodePos) + 1"/>
  386.             </xsl:call-template>
  387.           </xsl:when>
  388.           <xsl:otherwise>
  389.             <xsl:value-of select="concat($cleaned,$cleanIt)"/>
  390.           </xsl:otherwise>
  391.         </xsl:choose>
  392.       </xsl:otherwise>
  393.     </xsl:choose>
  394.    
  395.   </xsl:template>
  396.  
  397. </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment