Guest User

xml2json_xsl

a guest
Sep 3rd, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XML 3.83 KB | None | 0 0
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0"
  3.                xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.     <xsl:output method="text"/>
  5.  
  6.  
  7.     <xsl:template match = "/">{
  8.             <xsl:apply-templates select="*"/>}
  9.     </xsl:template>
  10.  
  11.     <!--object or element property-->
  12.     <xsl:template match="*">
  13.         "<xsl:value-of select="name()"/>" : <xsl:call-template name="properties">
  14.         <xsl:with-param name="parent" select="'Yes'"></xsl:with-param>
  15.     </xsl:call-template>
  16.     </xsl:template>
  17.  
  18.     <!--array element to match all array element which has multiple children-->
  19.         <xsl:template match="*" mode="ArrayElement">
  20.             <xsl:call-template name="properties"/>
  21.         </xsl:template>
  22.  
  23.     <!--object properties-->
  24.     <xsl:template name="properties">
  25.         <xsl:param name="parent"></xsl:param>
  26.         <xsl:variable name="child" select="name(*[1])"/>
  27.         <xsl:choose>
  28.             <xsl:when test="not(*|@*)"> <!--?-->
  29.                 <xsl:choose>
  30.                     <xsl:when test="$parent='Yes'">
  31.                         <xsl:text>&quot;</xsl:text>
  32.                         <xsl:value-of select="."/>
  33.                         <xsl:text>&quot;</xsl:text>
  34.                     </xsl:when>
  35.                     <xsl:otherwise>
  36.                         "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>" <!--?-->
  37.                     </xsl:otherwise>
  38.                 </xsl:choose>
  39.             </xsl:when>
  40.  
  41.             <xsl:when test="count(*[name()=$child])>1">{
  42.                 "<xsl:value-of select="$child"/>":[
  43.                 <xsl:apply-templates select="*" mode="ArrayElement"/>]
  44.                 }
  45.             </xsl:when>
  46.  
  47.             <xsl:otherwise>{
  48.                 <xsl:for-each select="@*">
  49.                     <xsl:choose>
  50.                         <xsl:when test="position()=last()">
  51.                             "@<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
  52.                         </xsl:when>
  53.                         <xsl:when test="(./*) and not(position()=last())">
  54.                             "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
  55.                         </xsl:when>
  56.                         <xsl:when test="./*"> <!--check if current element has child elements-->
  57.                             "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
  58.                         </xsl:when>
  59.                         <xsl:when test="(position()=last()) and not(./*)">
  60.                             "@<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
  61.                         </xsl:when>
  62.                         <xsl:otherwise>
  63.                             "@<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
  64.                         </xsl:otherwise>
  65.                     </xsl:choose>
  66.                     <!--"VALUE" : "<xsl:value-of select=".."/>"-->
  67.                 </xsl:for-each>
  68.  
  69.                 <!--<xsl:for-each select="@node()">
  70.                    <xsl:if test="./*">
  71.                        "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
  72.                    </xsl:if>
  73.                </xsl:for-each>-->
  74.                 <xsl:apply-templates select="*"/>
  75.                 }</xsl:otherwise>
  76.         </xsl:choose>
  77.         <xsl:if test="following-sibling::*">,</xsl:if>
  78.     </xsl:template>
  79.  
  80.  
  81.     <!--attribute property--> <!--not used??-->
  82.   <!-- <xsl:template match="@*">
  83.        <xsl:choose>
  84.            <xsl:when test="count(parent::*/@*) &lt; 2">
  85.                "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
  86.            </xsl:when>
  87.            <xsl:otherwise>
  88.                "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>,
  89.            </xsl:otherwise>
  90.        </xsl:choose>
  91.    </xsl:template>-->
  92. </xsl:stylesheet>
  93.  
  94.  
  95.  
  96.  
  97.  
Add Comment
Please, Sign In to add comment