<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    
    <xsl:output method="text"/>
    
    <xsl:variable name="quot">"</xsl:variable>
    <xsl:variable name="escapedQuot">\\"</xsl:variable>
    
    <xsl:template match="/">       
            <xsl:apply-templates/>        
    </xsl:template>
    
    <xsl:template match="*">
        {
         "name":"<xsl:value-of select="name(.)"/>"
          <xsl:apply-templates select="text()[normalize-space(.)!='']"/>
        <xsl:variable name="children"><xsl:for-each select="@*|*">,<xsl:apply-templates select="."/></xsl:for-each></xsl:variable>
        <xsl:variable name="children2" select="normalize-space($children)"/>
        <xsl:if test="$children2">,"children": [ <xsl:value-of select="if (starts-with($children2,',')) then substring($children2,2) else $children2"/> ]</xsl:if>
        }
    </xsl:template>
    
    <xsl:template match="text()">,"content":"<xsl:value-of select="normalize-space(replace(., $quot, $escapedQuot))"/>"</xsl:template>
    
    <xsl:template match="@*">{"name":"@<xsl:value-of select='local-name(.)'/>","content":"<xsl:value-of select="."/>"}
    </xsl:template>
</xsl:stylesheet>