Is it possible NOT to replace entity references?

Discussion in 'XML' started by Stephan Hoffmann, Sep 5, 2005.

  1. Hi,

    I use XML mainly as a source for HTML. HTML browsers 'know'
    certain entity references like é or ä.

    When I use XSL to transform XML to HTML or XML, these entities are replaced
    by what they refer to.

    Is there a way to avoid that?

    Two reasons to avoid that:
    - On my linux machine xsltproc replaced the entities in a way that
    my browser did not correctly display the resulting HTML
    (I updated my linux distribution and it now works).

    - &lt; is replaced by < and the output is no longer valid XML/HTML


    I worked with the Python xml.sax module today,
    which had the same 'issue'. I can of course perform the
    inverse substitution before I write to the result document,
    but that seems like a lot of unnecessary work.

    Any hints?

    Thanks, Stephan
    Stephan Hoffmann, Sep 5, 2005
    #1
    1. Advertising

  2. Stephan Hoffmann wrote:


    > I use XML mainly as a source for HTML. HTML browsers 'know'
    > certain entity references like &eacute; or &auml;.
    >
    > When I use XSL to transform XML to HTML or XML, these entities are replaced
    > by what they refer to.
    >
    > Is there a way to avoid that?


    XSLT/XPath 1.0 at least which is the current version and the one
    implemented by lots of processors and in wide-spread use does not
    provide anything in its data model or in its instructions to create
    entity references and to ensure that these are preserved and not
    replaced by the entity content when the result of a transformation is
    serialized.
    You would need to look at a specific XSLT processor and check whether it
    provides any mechanisms outside the standards to deal with entity and
    entity references.
    Saxon 6 has an extension function documented here:
    <http://saxon.sourceforge.net/saxon6.5.4/extensions.html#saxon:entity-ref>

    > Two reasons to avoid that:
    > - On my linux machine xsltproc replaced the entities in a way that
    > my browser did not correctly display the resulting HTML
    > (I updated my linux distribution and it now works).
    >
    > - &lt; is replaced by < and the output is no longer valid XML/HTML


    But &lt; and &gt; are references to entities predefined in XML and
    certainly if any application supposed to output XML or HTML outputs &lt;
    as a plain '<' character then the application is seriously broken.
    This is a different issue, those characters '<' and '>' are obviously
    special as they delimit tags in both XML and HTML and therefore need to
    be escaped as &lt; respectively &gt;.
    &auml; in HTML 4 stands for the character 'ä' and that has no special
    meaning in XML or HTML so if an XSLT processor or other application
    supposed to output XML or HTML simply inserts 'ä' instead of &auml; in a
    document properly encoded and with the proper encoding used and declared
    then there are no problems with well-formedness (or even validity).



    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Sep 5, 2005
    #2
    1. Advertising

  3. Stephan Hoffmann () wrote:
    : Hi,

    : I use XML mainly as a source for HTML. HTML browsers 'know'
    : certain entity references like &eacute; or &auml;.

    : When I use XSL to transform XML to HTML or XML, these entities are replaced
    : by what they refer to.

    : Is there a way to avoid that?

    Perhaps judicious use of the "disable-output-escaping" attribute would
    help.

    That attribute can be specified in various template tags, including <text>
    and <value-of> (perhaps others).

    It takes the value of either "yes" or "no".

    --

    This programmer available for rent.
    Malcolm Dew-Jones, Sep 5, 2005
    #3
  4. Hi,

    thanks for the detailed explanation.

    You are right, these are two 'issues', I confused them because
    the Python SAX parser I use replaces both the predefined and the not
    predefined entity references, which is ok. I simply assumed an XSLT
    processor would also replace both, but that assumption is probably wrong.

    I don't know why I prefer &auml; over 'ä', maybe because 7-bit
    ASCI seems to be more portable, but I can't really find a use case
    where 'ä' would be less portable.

    Thanks, Stephan

    Martin Honnen wrote:
    >
    >
    > Stephan Hoffmann wrote:
    >
    >
    >> I use XML mainly as a source for HTML. HTML browsers 'know'
    >> certain entity references like &eacute; or &auml;.
    >>
    >> When I use XSL to transform XML to HTML or XML, these entities are
    >> replaced by what they refer to.
    >>
    >> Is there a way to avoid that?

    >
    > XSLT/XPath 1.0 at least which is the current version and the one
    > implemented by lots of processors and in wide-spread use does not
    > provide anything in its data model or in its instructions to create
    > entity references and to ensure that these are preserved and not
    > replaced by the entity content when the result of a transformation is
    > serialized.
    > You would need to look at a specific XSLT processor and check whether it
    > provides any mechanisms outside the standards to deal with entity and
    > entity references.
    > Saxon 6 has an extension function documented here:
    > <http://saxon.sourceforge.net/saxon6.5.4/extensions.html#saxon:entity-ref>
    >
    >> Two reasons to avoid that:
    >> - On my linux machine xsltproc replaced the entities in a way that
    >> my browser did not correctly display the resulting HTML
    >> (I updated my linux distribution and it now works).
    >>
    >> - &lt; is replaced by < and the output is no longer valid XML/HTML

    >
    > But &lt; and &gt; are references to entities predefined in XML and
    > certainly if any application supposed to output XML or HTML outputs &lt;
    > as a plain '<' character then the application is seriously broken.
    > This is a different issue, those characters '<' and '>' are obviously
    > special as they delimit tags in both XML and HTML and therefore need to
    > be escaped as &lt; respectively &gt;.
    > &auml; in HTML 4 stands for the character 'ä' and that has no special
    > meaning in XML or HTML so if an XSLT processor or other application
    > supposed to output XML or HTML simply inserts 'ä' instead of &auml; in a
    > document properly encoded and with the proper encoding used and declared
    > then there are no problems with well-formedness (or even validity).
    >
    >
    >
    Stephan Hoffmann, Sep 6, 2005
    #4
  5. In article <kM5Te.71904$>,
    Stephan Hoffmann <> wrote:

    >You are right, these are two 'issues', I confused them because
    >the Python SAX parser I use replaces both the predefined and the not
    >predefined entity references, which is ok. I simply assumed an XSLT
    >processor would also replace both, but that assumption is probably wrong.


    It doesn't really make sense to contrast a parser with an XSLT
    processor.

    An XSLT processor will use a parser to read the document and
    stylesheet, and that parser must replace entity references with the
    characters they represent as it reads the files. Your problem is with
    what happens on *output*: whether the program replaces characters with
    entity references.

    >I don't know why I prefer &auml; over 'ä', maybe because 7-bit
    >ASCI seems to be more portable, but I can't really find a use case
    >where 'ä' would be less portable.


    XML parsers have to be able to handle UTF-8, so it won't be a problem
    for any XML tools. It may be a problem for other tools (or humans)
    that only understand ASCII. You can use the encoding attribute on
    xsl:eek:utput to tell the XSLT processor what output encoding to use
    (though it isn't guaranteed to support them all). If you tell it
    to use ASCII and you output a non-ascii character, it should use
    a numeric characters reference. It can't use &auml; when outputting
    XML because in general that won't be defined, so it will output
    ä or ä.

    -- Richard
    Richard Tobin, Sep 6, 2005
    #5
  6. Stephan Hoffmann

    Peter Flynn Guest

    Stephan Hoffmann wrote:

    > Hi,
    >
    > I use XML mainly as a source for HTML. HTML browsers 'know'
    > certain entity references like &eacute; or &auml;.
    >
    > When I use XSL to transform XML to HTML or XML, these entities are
    > replaced by what they refer to.
    >
    > Is there a way to avoid that?


    Not really: this is what transformation is *supposed* to do.
    Martin suggested some processors might provide facilities for
    doing this, but I haven't seen them in operation.

    > Two reasons to avoid that:
    > - On my linux machine xsltproc replaced the entities in a way that
    > my browser did not correctly display the resulting HTML
    > (I updated my linux distribution and it now works).
    >
    > - &lt; is replaced by < and the output is no longer valid XML/HTML


    The topic is covered in the FAQ at http://xml.silmaril.ie/authors/cdata/

    ///Peter
    Peter Flynn, Sep 7, 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. David Madore
    Replies:
    1
    Views:
    506
    Richard Tobin
    Aug 28, 2003
  2. Samuel van Laere

    Entity Name or Entity Number?

    Samuel van Laere, Feb 24, 2007, in forum: HTML
    Replies:
    4
    Views:
    1,599
    Jukka K. Korpela
    Feb 24, 2007
  3. markla
    Replies:
    1
    Views:
    533
    Steven Cheng
    Oct 6, 2008
  4. Norm
    Replies:
    3
    Views:
    2,679
  5. Jack Bates
    Replies:
    3
    Views:
    295
    Steven D'Aprano
    Aug 6, 2011
Loading...

Share This Page