Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0" encoding="utf-8"?>
- <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
- <xsl:output method="text" />
- <xsl:variable name="quot">"</xsl:variable>
- <xsl:variable name="t_attPrefix"></xsl:variable>
- <xsl:variable name="t_attSuffix"></xsl:variable>
- <xsl:variable name="t_txtPrefix">value</xsl:variable>
- <xsl:variable name="t_txtSuffix"></xsl:variable>
- <xsl:variable name="t_encaseForArray">false</xsl:variable>
- <xsl:variable name="t_flattenSimpleElements">true</xsl:variable>
- <xsl:variable name="t_flattenSimpleCollectionsToArrays">true</xsl:variable>
- <xsl:variable name="t_dropRoot">false</xsl:variable>
- <xsl:variable name="t_elementAppendForUnique">Node</xsl:variable>
- <xsl:variable name="t_item"><from>'</from><to>\'</to></xsl:variable>
- <xsl:variable name="t_escapeFrom">'</xsl:variable>
- <xsl:variable name="t_escapeTo">\'</xsl:variable>
- <xsl:param name="encaseObject" select="$quot" />
- <xsl:param name="encaseString" select="$quot" />
- <xsl:param name="attPrefix" select="$t_attPrefix" />
- <xsl:param name="attSuffix" select="$t_attSuffix" />
- <xsl:param name="txtPrefix" select="$t_txtPrefix" />
- <xsl:param name="txtSuffix" select="$t_txtSuffix" />
- <xsl:param name="encaseForArray" select="$t_encaseForArray='true'"/>
- <xsl:param name="flattenSimpleElements" select="$t_flattenSimpleElements='true'"/>
- <xsl:param name="flattenSimpleCollectionsToArrays" select="$t_flattenSimpleCollectionsToArrays='true'"/>
- <xsl:param name="dropRoot" select="$t_dropRoot='true'"/>
- <xsl:param name="elementAppendForUnique" select="t_elementAppendForUnique" />
- <xsl:param name="cln">:</xsl:param>
- <xsl:template match="/">
- <!-- Build the Initial JSON -->
- <xsl:variable name="initial_JSON">
- <xsl:apply-templates select="current()/child::*" mode="build" >
- <xsl:with-param name="path" select="'/'"/>
- </xsl:apply-templates>
- </xsl:variable>
- <xsl:variable name="JSON">
- <xsl:choose>
- <xsl:when test="$dropRoot">
- <xsl:variable name="rootNode" select="name(/*[1])"/>
- <xsl:value-of select="substring-after($initial_JSON,concat($rootNode,$cln))"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="concat('{',$initial_JSON,'}')"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:variable>
- <xsl:if test="$encaseForArray=true()">[</xsl:if>
- <xsl:value-of select="$JSON"/>
- <xsl:if test="$encaseForArray=true()">]</xsl:if>
- <!--
- PROCESSING COMPLETED.....
- -->
- </xsl:template>
- <xsl:template match="*" mode="build">
- <xsl:param name="path"/>
- <xsl:variable name="nName" select="name(.)"/>
- <xsl:variable name="iPreceding" select="count(preceding-sibling::*[name()=$nName])"/>
- <xsl:variable name="iFollowing" select="count(following-sibling::*[name()=$nName])"/>
- <xsl:variable name="nameSuffix">
- <xsl:if test="count(parent::*[name(@*)=$nName]) > 0">
- <xsl:value-of select="$elementAppendForUnique"/>
- </xsl:if>
- </xsl:variable>
- <xsl:choose>
- <xsl:when test="$iPreceding = 0 and $iFollowing > 0">
- <xsl:value-of select="concat($encaseObject,$nName,$nameSuffix,$encaseObject,$cln,'[')"/>
- <!--
- enumerate through same named nodes
- -->
- <xsl:for-each select="../*[name()=$nName]">
- <xsl:variable name="nonSimpleChildrenCount">
- <xsl:call-template name="support-count-nonsimple-children">
- <xsl:with-param name="startNode" select="current()"/>
- </xsl:call-template>
- </xsl:variable>
- <xsl:variable name="isSimple" select="number($nonSimpleChildrenCount)=0"/>
- <xsl:variable name="json_content">
- <xsl:apply-templates select="current()" mode="process-node">
- <xsl:with-param name="path" select="$path"/>
- <xsl:with-param name="isSimpleNode" select="$isSimple"/>
- <xsl:with-param name="isCollection" select="true()"/>
- </xsl:apply-templates>
- </xsl:variable>
- <xsl:value-of select="$json_content"/>
- <!--
- if there are children then do an iterative call
- -->
- <xsl:if test="child::*">
- <xsl:if test="string-length($json_content) > 1">
- <xsl:text>,</xsl:text>
- </xsl:if>
- <xsl:apply-templates select="current()/*" mode="build">
- <xsl:with-param name="path" select="concat($path,name(),'/')"/>
- </xsl:apply-templates>
- </xsl:if>
- <xsl:if test="($isSimple=false() and $flattenSimpleElements=true()) or $flattenSimpleElements=false()">
- <xsl:text>}</xsl:text>
- </xsl:if>
- <xsl:if test="position()!=last()">
- <xsl:text>,</xsl:text>
- </xsl:if>
- </xsl:for-each>
- <xsl:text>]</xsl:text>
- <xsl:if test="following-sibling::*[name()!=$nName]">
- <xsl:text>,</xsl:text>
- </xsl:if>
- </xsl:when>
- <!--
- TYPE 2: SINGLETON OBJECTS
- The current Node is a singleton with no siblings
- -->
- <xsl:when test="$iPreceding = 0 and $iFollowing = 0">
- <!-- get the simple status of the element -->
- <xsl:variable name="nonSimpleChildrenCount">
- <xsl:call-template name="support-count-nonsimple-children">
- <xsl:with-param name="startNode" select="current()"/>
- </xsl:call-template>
- </xsl:variable>
- <xsl:variable name="isSimple" select="number($nonSimpleChildrenCount)=0"/>
- <xsl:variable name="json_content">
- <xsl:apply-templates select="current()" mode="process-node">
- <xsl:with-param name="path" select="$path"/>
- <xsl:with-param name="isSimple" select="$isSimple"/>
- <xsl:with-param name="isCollection" select="false()"/>
- </xsl:apply-templates>
- </xsl:variable>
- <xsl:value-of select="concat($encaseObject, $nName, $nameSuffix, $encaseObject, $cln)"/>
- <xsl:value-of select="$json_content"/>
- <xsl:if test="child::*">
- <xsl:if test="string-length($json_content) > 1">
- <xsl:text>,</xsl:text>
- </xsl:if>
- <xsl:apply-templates select="current()/*" mode="build">
- <xsl:with-param name="path" select="concat($path,$nName,'/')"/>
- </xsl:apply-templates>
- </xsl:if>
- <xsl:if test="($isSimple=false() and $flattenSimpleElements=true()) or $flattenSimpleElements=false()">
- <xsl:text>}</xsl:text>
- </xsl:if>
- <xsl:if test="following-sibling::*">
- <xsl:text>,</xsl:text>
- </xsl:if>
- </xsl:when>
- </xsl:choose>
- </xsl:template>
- <xsl:template match="*" mode="process-node">
- <xsl:param name="path" />
- <xsl:param name="isSimple"/>
- <xsl:param name="isCollection"/>
- <xsl:if test="($isSimple=false() and $flattenSimpleElements=true()) or $flattenSimpleElements=false()">
- <xsl:text>{</xsl:text>
- </xsl:if>
- <xsl:apply-templates select="@*" mode="process-attributes">
- <xsl:with-param name="path" select="concat($path,name(),'/')"/>
- </xsl:apply-templates>
- <xsl:if test="@* and string-length(text())!=0">
- <xsl:text>,</xsl:text>
- </xsl:if>
- <xsl:apply-templates select="." mode="process-element">
- <xsl:with-param name="path" select="concat($path,name(),'/')"/>
- <xsl:with-param name="isSimple" select="$isSimple"/>
- <xsl:with-param name="isCollection" select="$isCollection"/>
- </xsl:apply-templates>
- </xsl:template>
- <xsl:template match="@*" mode="process-attributes">
- <xsl:param name="path"/>
- <xsl:param name="isSimple"/>
- <xsl:variable name="cleaned">
- <xsl:call-template name="process-string-content">
- <xsl:with-param name="valueToProcess" select="normalize-space(.)"/>
- <xsl:with-param name="path" select="concat($path,'@',name(),'/')"/>
- </xsl:call-template>
- </xsl:variable>
- <xsl:value-of select="concat($encaseObject,$attPrefix,name(),$attSuffix,$encaseObject,$cln,$cleaned)"/>
- <xsl:if test="position()!=last()">
- <xsl:text>,</xsl:text>
- </xsl:if>
- </xsl:template>
- <xsl:template match="*" mode="process-element">
- <xsl:param name="path"/>
- <xsl:param name="isSimple"/>
- <xsl:param name="isCollection"/>
- <xsl:variable name="value" select="normalize-space(text())"/>
- <xsl:if test="string-length($value)!=0">
- <xsl:variable name="cleaned">
- <xsl:call-template name="process-string-content">
- <xsl:with-param name="valueToProcess" select="$value"/>
- <xsl:with-param name="path" select="$path"/>
- </xsl:call-template>
- </xsl:variable>
- <xsl:choose>
- <xsl:when test="$isSimple=true() and $flattenSimpleElements=true()">
- <xsl:value-of select="$cleaned"/>
- </xsl:when>
- <xsl:when test="$isCollection=true() and $flattenSimpleCollectionsToArrays=true() and $flattenSimpleElements=true()">
- <xsl:value-of select="$cleaned"/>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="concat($encaseObject,$txtPrefix,$txtSuffix,$encaseObject,$cln,$cleaned)"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:if>
- </xsl:template>
- <xsl:template name="process-string-content">
- <xsl:param name="path"/>
- <xsl:param name="valueToProcess"/>
- <xsl:variable name="value">
- <xsl:call-template name="support-escape-characters">
- <xsl:with-param name="cleanIt" select="$valueToProcess"/>
- </xsl:call-template>
- </xsl:variable>
- <xsl:variable name="nPtrs" select="a"/>
- <xsl:variable name="pathStrip" select="substring($path,1,string-length($path) -1)"/>
- <xsl:variable name="datatype">
- <xsl:choose>
- <xsl:when test="$nPtrs/pointer[text()=$pathStrip and @match='exact']">
- <xsl:value-of select="$nPtrs/pointer[text()=$pathStrip and @match='exact']/@type"/>
- </xsl:when>
- <xsl:when test="$nPtrs/pointer[contains($pathStrip,text()) and @match='any']">
- <xsl:value-of select="$nPtrs/pointer[contains($pathStrip,text()) and @match='any']/@type"/>
- </xsl:when>
- <xsl:when test="string(number($value))!='NaN'">
- <xsl:text>number</xsl:text>
- </xsl:when>
- <xsl:when test="translate($value,'true','TRUE')='TRUE' or translate($value,'false','FALSE')='FALSE'">
- <xsl:text>boolean</xsl:text>
- </xsl:when>
- <xsl:otherwise>
- <xsl:text>string</xsl:text>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:variable>
- <xsl:choose>
- <xsl:when test="$datatype='native'">
- <xsl:if test="string-length($value)=0">
- <xsl:text>{}</xsl:text>
- </xsl:if>
- <xsl:value-of select="$value"/>
- </xsl:when>
- <xsl:when test="$datatype='number'">
- <xsl:if test="string-length($value)=0">
- <xsl:text>null</xsl:text>
- </xsl:if>
- <xsl:value-of select="$value"/>
- </xsl:when>
- <xsl:when test="$datatype='boolean'">
- <xsl:choose>
- <xsl:when test="translate($value,'TRUE','true')='true' or $value='1'">
- <xsl:text>true</xsl:text>
- </xsl:when>
- <xsl:otherwise>
- <xsl:text>false</xsl:text>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- <!-- assumes a format of yyyy-mm-ddThh:mm:ss -->
- <xsl:when test="$datatype='date'">
- <xsl:value-of select="concat('new Date(',substring($value,1,4),',',number(substring($value,6,2))-1,',',substring($value,9,2),')')"/>
- </xsl:when>
- <!-- assumes a format of yyyy-mm-ddThh:mm:ss -->
- <xsl:when test="$datatype='datetime'">
- <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),')')"/>
- </xsl:when>
- <xsl:when test="$datatype='string'">
- <xsl:value-of select="concat($encaseString,$value,$encaseString)"/>
- </xsl:when>
- </xsl:choose>
- </xsl:template>
- <xsl:template name="support-count-nonsimple-children">
- <xsl:param name="startNode"/>
- <xsl:variable name="total">
- <xsl:text>0</xsl:text>
- <xsl:for-each select="$startNode">
- <xsl:variable name="nName" select="name($startNode)"/>
- <xsl:value-of select="number(count(current()/*)) +
- number(count(current()[@*])) +
- number(count(preceding-sibling::*[name()=$nName])) +
- number(count(following-sibling::*[name()=$nName]))"/>
- </xsl:for-each>
- </xsl:variable>
- <xsl:value-of select="number($total)"/>
- </xsl:template>
- <!--
- ///////////////////////////////////////////////////////////////////////////////////////////
- SUPPORT: ESCAPE CHARACTERS
- Applies Character escaping base on the escape nodes in the config file
- ///////////////////////////////////////////////////////////////////////////////////////////
- -->
- <xsl:template name="support-escape-characters">
- <xsl:param name="cleanIt"/>
- <xsl:param name="cleaned"/>
- <xsl:param name="nodePos" select="1"/>
- <xsl:variable name="escapeFrom" select="$t_escapeFrom"/>
- <xsl:variable name="escapeTo" select="$t_escapeTo"/>
- <xsl:choose>
- <xsl:when test="string-length(substring-before($cleanIt,$escapeFrom))!=0 or starts-with($cleanIt,$escapeFrom)">
- <xsl:variable name="left" select="substring-before($cleanIt,$escapeFrom)"/>
- <xsl:call-template name="support-escape-characters">
- <xsl:with-param name="cleanIt" select="substring($cleanIt,number(string-length($left)+string-length($escapeFrom)+1),string-length($cleanIt))"/>
- <xsl:with-param name="cleaned" select="concat($cleaned,$left,$escapeTo)"/>
- <xsl:with-param name="nodePos" select="$nodePos"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:choose>
- <xsl:when test="number($nodePos) < count($t_item)">
- <xsl:call-template name="support-escape-characters">
- <xsl:with-param name="cleanIt" select="concat($cleaned,$cleanIt)"/>
- <xsl:with-param name="nodePos" select="number($nodePos) + 1"/>
- </xsl:call-template>
- </xsl:when>
- <xsl:otherwise>
- <xsl:value-of select="concat($cleaned,$cleanIt)"/>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>
- </xsl:stylesheet>
Advertisement
Add Comment
Please, Sign In to add comment