entities in attribute values

Discussion in 'XML' started by luca, Jan 12, 2009.

  1. luca

    luca Guest

    Hi, I just figured that XML is touchy about using entities in attribute
    value even for super-simple XML files with no DTD:

    <elem name="entity_euro" value="&euro;"/>

    Caused by: org.xml.sax.SAXParseException: The entity "euro" was
    referenced, but
    not declared.
    at
    org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1189)


    is there a standard way to escape entities in XML?

    Thanks
    luca, Jan 12, 2009
    #1
    1. Advertising

  2. luca wrote:
    > Hi, I just figured that XML is touchy about using entities in attribute
    > value even for super-simple XML files with no DTD:
    >
    > <elem name="entity_euro" value="&euro;"/>
    >
    > Caused by: org.xml.sax.SAXParseException: The entity "euro" was
    > referenced, but
    > not declared.
    > at
    > org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1189)
    >
    >
    >
    > is there a standard way to escape entities in XML?


    You need a DTD that declares the entity named 'euro'.
    Or you need to use a numeric character reference e.g.


    And that has nothing to do with whether you use the entity reference in
    an attribute or in an element, that does not matter. You need to declare
    any entity you want to reference, with the exception of 'lt', 'gt',
    'quot', 'apos', 'amp'.


    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Jan 12, 2009
    #2
    1. Advertising

  3. luca

    luca Guest

    Martin Honnen wrote:
    >
    > You need a DTD that declares the entity named 'euro'.
    > Or you need to use a numeric character reference e.g.
    > €
    >
    > And that has nothing to do with whether you use the entity reference in
    > an attribute or in an element, that does not matter. You need to declare
    > any entity you want to reference, with the exception of 'lt', 'gt',
    > 'quot', 'apos', 'amp'.


    so, here is my problem. I use this XML file simply to hold configuration info.
    One of the strings I need to store in the config file is "&euro;" which,
    unfortunately, happens to be an XML entity and have special meaning for XML and
    XML parsers.

    I tried:

    <elem name="entity_euro" value="<![CDATA[&euro;]]>"/>

    but it does not work.

    I can work this around outside of XML by using a different expression and having
    Java re-build the entity string:

    <elem name="entity_euro" value="amp_euro;"/>

    before I do it, though, I would like to hear whether there is a better (and more
    elegant!) way, because my heart cries when I see XML suck so bad. :)

    Luca
    luca, Jan 12, 2009
    #3
  4. luca

    luca Guest

    luca wrote:
    > Hi, I just figured that XML is touchy about using entities in attribute
    > value even for super-simple XML files with no DTD:
    >
    > <elem name="entity_euro" value="&euro;"/>
    >
    > Caused by: org.xml.sax.SAXParseException: The entity "euro" was
    > referenced, but
    > not declared.
    > at
    > org.apache.xerces.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1189)
    >
    >
    >
    > is there a standard way to escape entities in XML?



    This did the trick:

    <elem name="entity_euro" value="&amp;euro;"/>

    Thanks

    Luca
    luca, Jan 12, 2009
    #4
    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. R Paley
    Replies:
    2
    Views:
    496
    Paul Uiterlinden
    Nov 20, 2004
  2. Bostonasian
    Replies:
    1
    Views:
    810
    Joris Gillis
    Sep 18, 2005
  3. Replies:
    2
    Views:
    1,067
    Henry S. Thompson
    Mar 6, 2006
  4. Replies:
    3
    Views:
    1,525
    Brandon McCombs
    May 2, 2007
  5. Jim Higson
    Replies:
    3
    Views:
    215
    Eric Amick
    Jul 25, 2004
Loading...

Share This Page