jbrehr
jbrehr

Reputation: 815

XSLT convert text/special characters to HTML

In my XML file I have the following node with text content that is HTML encoded for reserved characters:

<seg>&lt;div style="position:relative;padding-bottom:56.25%;padding-top:10px;height:0;overflow:hidden;"&gt;&lt;iframe src="https://www.example.com/video/fmsh/embed.1/subject.27453" style="position:absolute;top:0;left:0;width:100%;height: 100%;" width="550" height="306" frameborder="0" allowfullscreen="" scrolling="no"&gt;&lt;/iframe&gt;&lt;/div&gt;</seg>

I want to process the content of <seg> back into HTML using XSLT 3.0 (under eXist-db) during the transformation process. In Xquery (also under eXist-db) I can use util:parse() to do this, but I have been unable to identify the same functionality in XSLT. I feel as though xsl:result-document should be the tool but I haven't come close to even getting it to output a useable error to find a solution, so I'm not sure that is even the correct approach.

Thanks in advance.

Edit: Adding two attempts:

1.

<xsl:template match="seg">
    <xsl:value-of select="." disable-output-escaping="yes"/>
</xsl:template>

outputs:

<?javax.xml.transform.disable-output-escaping?>&lt;div style="position:relative;padding-bottom:56.25%;padding-top:10px;height:0;overflow:hidden;"&gt;&lt;iframe src="https://www.example.com/video/fmsh/embed.1/subject.27453" style="position:absolute;top:0;left:0;width:100%;height: 100%;" width="550" height="306" frameborder="0" allowfullscreen scrolling="no"&gt;&lt;/iframe&gt;&lt;/div&gt;<?javax.xml.transform.enable-output-escaping?>

2.

<xsl:template match="seg">
    <xsl:value-of select="parse-xml-fragment(./text())"/>
</xsl:template>

outputs: nothing

Upvotes: 1

Views: 497

Answers (1)

zx485
zx485

Reputation: 29022

To transform the content of your <seg> element to HTML, you can simply use the (optional) attribute disable-output-escaping of xsl:value-of:

<xsl:value-of select="seg" disable-output-escaping="yes" />

It is not supported by all XSLT processors, so try it out.
The output of this is HTML:

<div style="position:relative;padding-bottom:56.25%;padding-top:10px;height:0;overflow:hidden;"><iframe src="https://www.example.com/video/fmsh/embed.1/subject.27453?width=100%&height=100%" style="position:absolute;top:0;left:0;width:100%;height: 100%;" width="550" height="306" frameborder="0" allowfullscreen scrolling="no"></iframe></div>

You cannot easily process this output further with an XSLT processor, because it's not well-formed XML (it's HTML):

  1. There is an unescaped & in the @src attribute
  2. The HTML attribute allowfullscreen has no value (not acceptable in XML)

But if you really want to process it further, treat the result as a string, replace the problematic aspects with the fn:replace() function, and then use the fn:parse-xml-fragment() function to output conforming XML.

Upvotes: 1

Related Questions