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

Discussion in 'XML' started by mark4asp, Mar 13, 2008.

  1. mark4asp

    mark4asp Guest

    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.
     
    mark4asp, Mar 13, 2008
    #1
    1. Advertising

  2. Re: Can XSLT render the content: &lt;p&gt as html <p> rather thantext?

    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/
     
    Martin Honnen, Mar 13, 2008
    #2
    1. Advertising

  3. mark4asp

    mark4asp Guest

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

    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.
     
    mark4asp, Mar 13, 2008
    #3
  4. mark4asp

    mark4asp Guest

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

    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.
     
    mark4asp, Mar 13, 2008
    #4
  5. Re: Can XSLT render the content: &lt;p&gt as html <p> rather thantext?

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



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Mar 13, 2008
    #5
  6. mark4asp

    mark4asp Guest

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

    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/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.
     
    mark4asp, Mar 13, 2008
    #6
  7. Re: Can XSLT render the content: &lt;p&gt as html <p> rather thantext?

    "mark4asp" <mark4asp@gmail.com> wrote in message
    news:47d928eb$0$5619$c3e8da3@news.astraweb.com...
    >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
     
    dnovatchev@gmail.com, Mar 15, 2008
    #7
  8. mark4asp

    Andy Dingley Guest

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

    On 13 Mar, 13:38, Martin Honnen <mahotr...@yahoo.de> 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.
     
    Andy Dingley, Mar 17, 2008
    #8
  9. mark4asp

    Andy Dingley Guest

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

    On 13 Mar, 13:15, "mark4asp" <mark4...@gmail.com> 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!).
     
    Andy Dingley, Mar 17, 2008
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. darrel
    Replies:
    4
    Views:
    15,835
    darrel
    May 4, 2004
  2. Iain
    Replies:
    0
    Views:
    339
  3. chr158ooth
    Replies:
    0
    Views:
    876
    chr158ooth
    Sep 16, 2011
  4. Surendra

    Dropdown control that displays images rather than text?

    Surendra, Mar 28, 2006, in forum: ASP .Net Web Controls
    Replies:
    1
    Views:
    137
  5. david@scene-double.co.uk

    ASP to display image rather than text path ??

    david@scene-double.co.uk, Aug 16, 2005, in forum: ASP General
    Replies:
    2
    Views:
    205
    Bob Barrows [MVP]
    Aug 16, 2005
Loading...

Share This Page