Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > Can XSLT render the content: &lt;p&gt as html <p> rather than text?

Reply
Thread Tools

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

 
 
mark4asp
Guest
Posts: n/a
 
      03-13-2008
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.
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-13-2008
mark4asp wrote:
> 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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
mark4asp
Guest
Posts: n/a
 
      03-13-2008
Martin Honnen wrote:

> mark4asp wrote:
> > 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.


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.


 
Reply With Quote
 
mark4asp
Guest
Posts: n/a
 
      03-13-2008
Martin Honnen wrote:

> <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.
 
Reply With Quote
 
Martin Honnen
Guest
Posts: n/a
 
      03-13-2008
mark4asp wrote:
> Martin Honnen wrote:
>
>> <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.


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_FAQ#Can_I_do_disabl e-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.



--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
mark4asp
Guest
Posts: n/a
 
      03-13-2008
Martin Honnen wrote:

> mark4asp wrote:
> > Martin Honnen wrote:
> >
> >> <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.

>
> 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..._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.

 
Reply With Quote
 
dnovatchev@gmail.com
Guest
Posts: n/a
 
      03-15-2008
"mark4asp" <(E-Mail Removed)> wrote in message
news:47d928eb$0$5619$(E-Mail Removed)...
>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"
xmlnssl="http://www.w3.org/1999/XSL/Transform">
<xslutput 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

 
Reply With Quote
 
Andy Dingley
Guest
Posts: n/a
 
      03-17-2008
On 13 Mar, 13:38, Martin Honnen <(E-Mail Removed)> wrote:

> 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.

 
Reply With Quote
 
Andy Dingley
Guest
Posts: n/a
 
      03-17-2008
On 13 Mar, 13:15, "mark4asp" <(E-Mail Removed)> wrote:

> <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!).
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
identify transform using XSL:FO rather than XSLT chr158ooth XML 0 09-16-2011 01:23 PM
parse HTML by class rather than tag lorean2007@yahoo.fr Python 1 02-23-2007 08:34 AM
Can I set a dropDownList to the TEXT rather than index? darrel ASP .Net 4 05-04-2004 04:10 PM
Q:Add AutoPostBack flag by Code rather than HTML? Sky ASP .Net 1 01-29-2004 06:49 AM
Problems retrieving items from a list using a reference rather than an integer - can you help? Rogue9 Python 9 07-18-2003 07:13 AM



Advertisments