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>
4
  • 1
    Do you get that expected output by any HTML parser? Commented Mar 11, 2020 at 10:20
  • 1
    As the double quote serves as a delimiter for an attribute value any double quote in an attribute value needs to be escaped, in both XML and HTML. Try your "expected output" at validator.w3.org/nu/#textarea and you will see that it doesn't pass any HTML validation. Commented Mar 11, 2020 at 11:12
  • Yes @martin honnen I do understand, it but is there any way to handle it as a JSON text? :) Like storing the attribute value in a variable and pass them in attribute!!! Commented Mar 11, 2020 at 11:33
  • It is not clear what you want to achieve, first of all, even {["name" : "A", "owner" : "XXX", ]} isn't JSON Commented Mar 11, 2020 at 11:56

1 Answer 1

1

Your expected output is not well-formed HTML and can't be handled by any HTML parser, even if it's very lenient. How is it supposed to tell which of the double-quotation marks in the @json attribute represents the end of the attribute value? So you need to change your expectations for the output.

The actual output, in which the quotation marks have been escaped, should work fine.

Except that it's not actually valid JSON: within an object "{...}" you need keyword-value pairs, not a bare value.

Sign up to request clarification or add additional context in comments.

4 Comments

I have updated the code with proper JSON output. [I was just curious to try the new things], the JSON attributes can be used by an application to process the span tags, of course, there are other ways to do this but I was wondering how it will work if JSON is used.
You've still got the problem with double-quotes. How is the HTML parser supposed to know when it sees json="{" that the second " doesn't signal the end of the attribute value?
Yes, you are correct, HTML parser won't accept this, I'm a beginner in this one, just thought there must be a way to preserve them using some techniques or tricks.
There is such a technique: you escape the quotes using "&#34;", as in the output that you aren't enamoured with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.