javascripthtmlxsltxformsxsltforms

XSLTForms: Convert Text to HTML - add to DOM - deal with bad input?


Is there some way I can get XSLTForms to convert CDATA into HTML nodes which can then output to the DOM?

Any tips appreciated - one requirement - the data cannot be guaranteed to be valid HTML, so the solution will need to fall-back gracefully (and just display the text as-is - which is what I'm currently doing).

XFORM:

<?xml-stylesheet href="xsltforms/xsltforms.xsl" type="text/xsl"?>
<?xsltforms-options debug="no"?>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ev="http://www.w3.org/2001/xml-events"
      xmlns:xf="http://www.w3.org/2002/xforms"
      >
<head>

<xf:model>
    <xf:instance src="data.xml"/>
</xf:model>

</head>
<body>

<h1> What's on Tonight </h1>

<xf:repeat ref="records/record">
    <details>
        <summary>
            <xf:output ref="title"/>
        </summary>
        <p>
            <xf:output ref="description"/>
        </p>
    </details>
</xf:repeat>

</body>
</html>

DATA:

<data xmlns="">
    <records>
        <record>
            <title><![CDATA[Macbeth]]></title>
            <description><![CDATA[A <b>bold</b> interpretation of the classic play]]></description>
        </record>
        <record>
            <title><![CDATA[Malformed]]></title>
            <description><![CDATA[Distopian Sci-Fi Thriller, set in a post-XML world where elements are left <unclosed>]]></description>
        </record>
    </records>
</data>

Current output:

current output

Desired output:

Desired output


Solution

  • Instead of using output/@ref, you should use output/@value and output/@mediatype:

    <details>
        <summary>
            <xf:output value="title"/>
        </summary>
        <p>
            <xf:output value="description" mediatype="text/html"/>
        </p>
    </details>