Can XSLT render the content: &lt;p&gt as html <p> rather than text?

M

mark4asp

I have an element, report which contains tags which have been
transformed. E.g. <p> is &lt;p&gt

<myXml>
<report>This text has html tags in it.&lt;p&gt which but <> has been
changed to &lt;&gt</report>
</myXml>

I there a way that the XSLT transformation can render the content as
html rather than text?



PS: I tried to cludge this by getting the xmlDOM, then using javascript
to get the the <report> text but this just gives inconsistent results
(sometimes the <report> displays and other times I get an error telling
me that the expected item is null.

var reportTxt = getNodeText(oXmlDom.getElementsByTagName('report'));

a watch on oXmlDom.getElementsByTagName('report') shows the item is
there.

a watch on reportTxt shows no item (= null).

It's not the getNodeText() because when I do:

var reportNode = oXmlDom.getElementsByTagName('report');

I get the same, a watch on oXmlDom.getElementsByTagName('report') shows
the node is there; a watch on reportNode shows no node present.

This is just a crazy error which I can't debug.

On balance, I prefer the XSLT transform if possible.
 
M

Martin Honnen

mark4asp said:
I have an element, report which contains tags which have been
transformed. E.g. <p> is &lt;p&gt

<myXml>
<report>This text has html tags in it.&lt;p&gt which but <> has been
changed to &lt;&gt</report>
</myXml>

I there a way that the XSLT transformation can render the content as
html rather than text?

Well the proper way to solve that is at the source, instead of escaping
the HTML markup you should insert well-formed XHTML markup, then your
XSLT stylesheet can easily copy it to the output or transform it to HTML
if needed.
If you can't do that then one approach is disable-output-escaping e.g.
<xsl:template match="report">
<div>
<xsl:value-of select="." disable-output-escaping="yes"/>
</div>
</xsl:template>
However disable-output-escaping is an optional feature that is not
supported by all XSLT processors respectively is not supported with all
XSLT uses (for instance when chaining XSLT tranformations). So you will
have to check whether it works with your XSLT processor in the setting
you use it with.

Other than that you are left with processing and parsing the text with
the escaped markup and create elements as needed, a task that is rather
difficult with the restricted string processing that XSLT/XPath 1.0 offer.
 
M

mark4asp

Martin said:
Well the proper way to solve that is at the source, instead of
escaping the HTML markup you should insert well-formed XHTML markup,
then your XSLT stylesheet can easily copy it to the output or
transform it to HTML if needed. If you can't do that then one
approach is disable-output-escaping e.g. <xsl:template
match="report"> <div> <xsl:value-of select="."
disable-output-escaping="yes"/> </div> </xsl:template>
However disable-output-escaping is an optional feature that is not
supported by all XSLT processors respectively is not supported with
all XSLT uses (for instance when chaining XSLT tranformations). So
you will have to check whether it works with your XSLT processor in
the setting you use it with.

Other than that you are left with processing and parsing the text
with the escaped markup and create elements as needed, a task that is
rather difficult with the restricted string processing that
XSLT/XPath 1.0 offer.

Are you saying that the problem here is that the escaped html is not
xhtml valid? I can see that it would be a problem as xslt is designed
to give proper xhtml.

Given that I can't make this escaped html valid without a big effort
(manually editing >> 500 reports); are you recommending me to give up
using xslt?

I don't understand this bit:
instead of escaping the HTML markup

I'm not doing that. It's what Sql Server or .net does. Whatever method
I use to create xml I get the escaped html. I hope you're not going to
tell me to bypass both and create my own method.
you should insert well-formed XHTML markup

This stuff is entered into the database via a cms. The users of the cms
don't understand the concept of 'well-formed XHTML' - they are not
techies. Worse still, some of this is scraped from various sites;
generally when the report is an OJEC tender. The cms users will just
cut and paste the relevant part of the OJEC/OJEU report directly into
the cms.

I have a huge problem creating valid xhtml markup. I'm not authoring it.

Thanks for your suggestion. I shall look into it.
 
M

mark4asp

Martin said:
<xsl:template match="report">
<div>
<xsl:value-of select="." disable-output-escaping="yes"/>
</div>
</xsl:template>

Thanks again. This is working in IE6 on my PC, so I hope it continues
to work with the other browsers; now that the trickiest one is OK.

However. I guess it depends on what the client has installed... It will
be a bit hit and miss.
 
M

Martin Honnen

mark4asp said:
Thanks again. This is working in IE6 on my PC, so I hope it continues
to work with the other browsers; now that the trickiest one is OK.

No, if you rely on the browser to perform the XSLT transformation then
you should not rely on disable-output-escaping. Firefox for instance
does not support it at all
<URL:http://developer.mozilla.org/en/doc...zilla_FAQ#Can_I_do_disable-output-escaping.3F>
as it does not serialize the transformation result.
If you want to rely on disable-output-escaping then perform the XSLT
transformation on the server where you control the XSLT processor.
 
M

mark4asp

Martin said:
No, if you rely on the browser to perform the XSLT transformation
then you should not rely on disable-output-escaping. Firefox for
instance does not support it at all <url:
http://developer.mozilla.org/en/docs/XSL_Transformations_in_Mozilla_FA
Q#Can_I_do_disable-output-escaping.3F> as it does not serialize the
transformation result. If you want to rely on
disable-output-escaping then perform the XSLT transformation on the
server where you control the XSLT processor.

Point taken, but the page is working OK with IE 6 sp2 and IE7. I am
only required to get this website working with those browsers. It
doesn't work with FF, Opera or Safari. I'll change it to a server XSLT
when I have time, as I try to follow some kind of standard though my
boss just hates the mention of the word.
 
D

dnovatchev

mark4asp said:
I have an element, report which contains tags which have been
transformed. E.g. <p> is &lt;p&gt

<myXml>
<report>This text has html tags in it.&lt;p&gt which but <> has been
changed to &lt;&gt</report>
</myXml>

I there a way that the XSLT transformation can render the content as
html rather than text?

First of all, the text above is not a well-formed xml document.

Should be something like this:

<myXml>
<report>This text has html tags in it.&lt;p&gt; which but &lt;&gt; has
been
changed to &amp;lt;&amp;gt;</report>
</myXml>

Using an XSLT transformation like the one below:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput omit-xml-declaration="yes" indent="yes"
cdata-section-elements="report"
/>

<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

one gets this result:

<myXml>

<report><![CDATA[This text has html tags in it.<p> which but <> has
been
changed to &lt;&gt;]]></report>

</myXml>

which may be quite close to what you want.

Do note, that although the markup seems "restored", the contents of
"report"
is still a text node (contained in a CDATA section).

Cheers,
Dimitre Novatchev
 
A

Andy Dingley

Well the proper way to solve that is at the source, instead of escaping
the HTML markup you should insert well-formed XHTML markup,

The problem is to convey HTML, and almost certainly to convey HTML
fragments rather than well-formed documents (i.e. no single root
element). Switching to XHTML is an inappropriate fix for the first,
and unworkable for the second.
 
A

Andy Dingley

<myXml>
<report>This text has html tags in it.&lt;p&gt which but <> has been
changed to &lt;&gt</report>
</myXml>

Firstly, "<>" just isn't well-formed XML. You aren't going to get far
trying to process that.

Secondly, this is a common problem in the RSS world (for carrying
embedded fragments of HTML). There's code out there to solve it.

In general, this isn't an "XSLT" class of problem. It's basically
about string slicing on the contents of a text node, not the usual
"element-tree and XPath" world that XSLT works with. You _can_ solve
it (painfully) in XSLT, but it's generally easier to run a different
language over the text nodes, not try to use XSLT/XPath. As you appear
to be using DOM methods to access elements, I guess you're halfway
there already.

If you do try it in XSLT (and it's possible, just painful) then you're
looking at recursive algorithms for parsing the string content and
<xsl:element> to generate the content (the easy bit!).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top