Quantcast
Channel: Active questions tagged html - Stack Overflow
Viewing all articles
Browse latest Browse all 74765

Handling Json text in xslt for Xml to html transformation

$
0
0

I'm trying to transform an XML to HTML and some HTML element needs to hold JSON text as attribute value transformed from the XML. but when transformed, I am unable to get the proper output for JSON in attribute, Help me on this confused due to the " which is a double quote

XML:

<?xml version="1.0" encoding="UTF-8"?>
<main>
    <sub id="1" name="A" owner="XXX">text</sub>
    <sub id="2" name="B" owner="yyy">text</sub>
</main>

XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
  <html>
  <body>
    <p>
    <xsl:for-each select="main/sub">
        <span>
            <xsl:attribute name="json">
                <xsl:text>{"properties" : [ {</xsl:text>
                <xsl:for-each select="./@*">                    
                    <xsl:if test="name() = 'id'"><xsl:text>"id" : "</xsl:text><xsl:value-of select="." /><xsl:text>",</xsl:text></xsl:if>
                    <xsl:if test="name() = 'name'"><xsl:text>"name" : "</xsl:text><xsl:value-of select="." /><xsl:text>",</xsl:text></xsl:if>
                    <xsl:if test="name() = 'owner'"><xsl:text>"owner" : "</xsl:text><xsl:value-of select="." /><xsl:text>"</xsl:text></xsl:if>
                </xsl:for-each>
                <xsl:text>} ] }</xsl:text>
            </xsl:attribute>            
        </span>
    </xsl:for-each>
   </p>
  </body>
  </html>
</xsl:template>
</xsl:stylesheet>

Actual Output:

<html>
   <body>
      <p><span json="{&#34;properties&#34; : [ {&#34;id&#34; : &#34;1&#34;,&#34;name&#34; : &#34;A&#34;,&#34;owner&#34; : &#34;XXX&#34;} ] }"></span><span json="{&#34;properties&#34; : [ {&#34;id&#34; : &#34;2&#34;,&#34;name&#34; : &#34;B&#34;,&#34;owner&#34; : &#34;yyy&#34;} ] }"></span></p>
   </body>
</html>

Expected Output

<html>
   <body>
      <p><span json="{"properties" : [ {"id" : "1","name" : "A","owner" : "XXX"} ] }"></span><span json="{"properties" : [ {"id" : "2","name" : "B","owner" : "yyy"} ] }"></span></p>
   </body>
</html>

Viewing all articles
Browse latest Browse all 74765

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>