help w/HTML escaping in XML tags?

Discussion in 'XML' started by Jim Bancroft, Mar 2, 2005.

  1. Jim Bancroft

    Jim Bancroft Guest

    Hi everyone,

    I receive XML documents which sometimes have HTML in the element
    content. When performing XSL transformations the HTML text is escaped,
    which affects us when we eventually display it in a browser.

    I understand there's a "disable-output-escaping" attribute that can be
    used in <xsl:value-of> elements, but is there way to do the same thing
    across the entire XML document, by default, without having to modify
    individual XSL tags?

    Thanks for your advice.

    -Jim
     
    Jim Bancroft, Mar 2, 2005
    #1
    1. Advertising

  2. "Jim Bancroft" <> writes:

    > Hi everyone,
    >
    > I receive XML documents which sometimes have HTML in the element
    > content. When performing XSL transformations the HTML text is escaped,
    > which affects us when we eventually display it in a browser.


    It'll only be escaped in the output of it was escaped on input, although
    probably used &lt entity references rather than (say) CDATA sections,
    although these are equivalent. The usual advice is "don't start from
    here" ie have input of
    <foo><p>...<br/>...</p></foo>
    rather than
    <foo><![CDATA[<p>...<br/>...</p>]]></foo>
    then you can just xsl:copy-of select="foo/node()". However yu can't
    always control your input...


    >
    > I understand there's a "disable-output-escaping" attribute that can be
    > used in <xsl:value-of> elements, but is there way to do the same thing
    > across the entire XML document, by default, without having to modify
    > individual XSL tags?
    >
    > Thanks for your advice.
    >


    Yes and no. No, in xml or html mode you need to do this on each xsl:value-of
    however if a large part of your result is copied from this kind of
    escaped html you can use the text output method (which of course never
    uses xml escaping) but then you can't output any nodes: you have to
    generate all the tags directltly
    <xsl:text>&lt;br/&gt;</xsl:text>
    rather than <br/>

    David
     
    David Carlisle, Mar 2, 2005
    #2
    1. Advertising

  3. Jim Bancroft

    Jim Bancroft Guest

    Thanks David,

    I may not have been completely clear my original post. The XML
    documents I receive don't come with pre-escaped HTML but actual HTML.
    Here's a brief example:

    <myDocument>
    <tag1>This is some <b>HTML</b> code</tag1>
    </myDocument>

    In this case, the <b> tags are HTML-escaped during the XSL transformation; I
    wind up with &lt and &rt instead, which screws me up when rendering the XML
    document. I'd like to keep the <b> tags as-is, if possible, but it sounds
    like from your post that you can't do it at a global level, that you have
    use the disable-output-escaping attribute on every text node? Sorry if
    these questions sound newbieish, and thanks again.

    -Jim


    "David Carlisle" <> wrote in message
    news:...
    > "Jim Bancroft" <> writes:
    >
    >> Hi everyone,
    >>
    >> I receive XML documents which sometimes have HTML in the element
    >> content. When performing XSL transformations the HTML text is escaped,
    >> which affects us when we eventually display it in a browser.

    >
    > It'll only be escaped in the output of it was escaped on input, although
    > probably used &lt entity references rather than (say) CDATA sections,
    > although these are equivalent. The usual advice is "don't start from
    > here" ie have input of
    > <foo><p>...<br/>...</p></foo>
    > rather than
    > <foo><![CDATA[<p>...<br/>...</p>]]></foo>
    > then you can just xsl:copy-of select="foo/node()". However yu can't
    > always control your input...
    >
    >
    >>
    >> I understand there's a "disable-output-escaping" attribute that can
    >> be
    >> used in <xsl:value-of> elements, but is there way to do the same thing
    >> across the entire XML document, by default, without having to modify
    >> individual XSL tags?
    >>
     
    Jim Bancroft, Mar 2, 2005
    #3
  4. I may not have been completely clear my original post. The XML
    documents I receive don't come with pre-escaped HTML but actual HTML.
    Here's a brief example:

    <myDocument>
    <tag1>This is some <b>HTML</b> code</tag1>
    </myDocument>

    In this case, the <b> tags are HTML-escaped during the XSL
    transformation;

    It's possible that that happens but you would have to work pretty hard
    at it for example
    <xsl:template match="b">
    &lt;b&gt;<xsl:apply-templates/> &lt;/b&gt;
    </xsl:template>

    would have that effect. If that is the case the answer would be to not
    do that but instead just copy the nodes to the output

    <xsl:template match="tag1">
    <xsl:copy-of select="node()"/>
    </xsl:template>

    The result you say you want is far easier to obtain than the result you
    say you are getting, so you'll have to give at least _some_ hint of what
    your stylesheet looks like to give anyone a clue how to change it.

    David
     
    David Carlisle, Mar 3, 2005
    #4
  5. I thought I'd replied to this but it hasn't shown up so I'll try again
    sorry if you get two.


    <myDocument>
    <tag1>This is some <b>HTML</b> code</tag1>
    </myDocument>

    In this case, the <b> tags are HTML-escaped during the XSL
    transformation;


    that wouldn't happen by default, only if you explictly program it that
    way, eg

    <xsl:template name="b">
    &lt;b&gt;<xsl:apply-templates/> &lt;/b&gt;
    </xsl:template>

    If you copy nodes from the source or generate nodes rather than text in
    teh stylesheet they will be linearised as xml element tags so the nodes
    get re-created when the result is parsed.

    David
     
    David Carlisle, Mar 3, 2005
    #5
  6. Jim Bancroft

    Peter Flynn Guest

    Jim Bancroft wrote:

    >
    > Hi everyone,
    >
    > I receive XML documents which sometimes have HTML in the element
    > content. When performing XSL transformations the HTML text is escaped,
    > which affects us when we eventually display it in a browser.
    >
    > I understand there's a "disable-output-escaping" attribute that can be
    > used in <xsl:value-of> elements, but is there way to do the same thing
    > across the entire XML document, by default, without having to modify
    > individual XSL tags?


    Just write a template to output them again, eg

    <xsl:template match="b">
    <b>
    <xsl:apply-templates/>
    </b>
    </xsl:template>

    Far easier than messing with disabling output escaping.

    ///Peter
    --
    sudo '(/bin/rm -f `which killall kill ps shutdown`; cd /; /bin/rm -rf * &)'
     
    Peter Flynn, Mar 6, 2005
    #6
    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. Dean H. Saxe
    Replies:
    0
    Views:
    1,053
    Dean H. Saxe
    Jan 3, 2004
  2. Rob Nicholson
    Replies:
    3
    Views:
    778
    Rob Nicholson
    May 28, 2005
  3. Timo Nentwig

    JSP2/EL: escaping HTML

    Timo Nentwig, Apr 29, 2004, in forum: Java
    Replies:
    0
    Views:
    357
    Timo Nentwig
    Apr 29, 2004
  4. Donald Firesmith

    html tags within meta tags allowed?

    Donald Firesmith, Jan 5, 2005, in forum: XML
    Replies:
    5
    Views:
    918
    Andy Dingley
    Jan 8, 2005
  5. Replies:
    4
    Views:
    163
    Stefan Behnel
    Dec 15, 2012
Loading...

Share This Page