Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?xml version="1.0"?>
- <xsl:stylesheet version="1.0"
- xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
- <xsl:output method="text"/>
- <xsl:template match = "/">{
- <xsl:apply-templates select="*"/>}
- </xsl:template>
- <!--object or element property-->
- <xsl:template match="*">
- "<xsl:value-of select="name()"/>" : <xsl:call-template name="properties">
- <xsl:with-param name="parent" select="'Yes'"></xsl:with-param>
- </xsl:call-template>
- </xsl:template>
- <!--array element to match all array element which has multiple children-->
- <xsl:template match="*" mode="ArrayElement">
- <xsl:call-template name="properties"/>
- </xsl:template>
- <!--object properties-->
- <xsl:template name="properties">
- <xsl:param name="parent"></xsl:param>
- <xsl:variable name="child" select="name(*[1])"/>
- <xsl:choose>
- <xsl:when test="not(*|@*)"> <!--?-->
- <xsl:choose>
- <xsl:when test="$parent='Yes'">
- <xsl:text>"</xsl:text>
- <xsl:value-of select="."/>
- <xsl:text>"</xsl:text>
- </xsl:when>
- <xsl:otherwise>
- "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>" <!--?-->
- </xsl:otherwise>
- </xsl:choose>
- </xsl:when>
- <xsl:when test="count(*[name()=$child])>1">{
- "<xsl:value-of select="$child"/>":[
- <xsl:apply-templates select="*" mode="ArrayElement"/>]
- }
- </xsl:when>
- <xsl:otherwise>{
- <xsl:for-each select="@*">
- <xsl:choose>
- <xsl:when test="position()=last()">
- "@<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
- </xsl:when>
- <xsl:when test="(./*) and not(position()=last())">
- "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
- </xsl:when>
- <xsl:when test="./*"> <!--check if current element has child elements-->
- "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
- </xsl:when>
- <xsl:when test="(position()=last()) and not(./*)">
- "@<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
- </xsl:when>
- <xsl:otherwise>
- "@<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>",
- </xsl:otherwise>
- </xsl:choose>
- <!--"VALUE" : "<xsl:value-of select=".."/>"-->
- </xsl:for-each>
- <!--<xsl:for-each select="@node()">
- <xsl:if test="./*">
- "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
- </xsl:if>
- </xsl:for-each>-->
- <xsl:apply-templates select="*"/>
- }</xsl:otherwise>
- </xsl:choose>
- <xsl:if test="following-sibling::*">,</xsl:if>
- </xsl:template>
- <!--attribute property--> <!--not used??-->
- <!-- <xsl:template match="@*">
- <xsl:choose>
- <xsl:when test="count(parent::*/@*) < 2">
- "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>"
- </xsl:when>
- <xsl:otherwise>
- "<xsl:value-of select="name()"/>" : "<xsl:value-of select="."/>,
- </xsl:otherwise>
- </xsl:choose>
- </xsl:template>-->
- </xsl:stylesheet>
Add Comment
Please, Sign In to add comment