How to escape the < and ≤ symbols in xml?

12,280

Solution 1

You will have to escape those symbols as following:

  • < will be &lt;
  • > will be &gt;

The sign doesn't need escaping.

Solution 2

You have to escape those symbols for XML.

What characters do I need to escape in XML documents?

Solution 3

As said, for the < and >, you can use this:

<   &lt;
>   &gt;

You need to do this because the < and > aren't allowed in XML, so you need to escape them. You do that by typing: & + specific code for your character + ;, when you show this string, this will be recognized as being the special character (< or >).

You also wanted the smaller or equal sign (≤), which is afaik allowed in XML, so just use .

These are the characters you have to escape:

"   =   &quot;
'   =   &apos;
<   =   &lt;
>   =   &gt;
&   =   &amp;

Solution 4

Use this:

<   &lt;
>   &gt;

Solution 5

Use Unicode sequences for special characters.

You can find some of them here: http://jrgraphix.net/r/Unicode/2200-22FF

Use it like this:

\u2264

will be converted to

Share:
12,280
user2301281
Author by

user2301281

Programmer have no time to sleep, eventhough I'm not in such programmer level, I still have no time to SLEEP! I'm just can use my limited programming knowledge and English to study what I can learn. Thanks for everyone who's lend me a hand in this tough journey, appreciate it.

Updated on July 22, 2022

Comments

  • user2301281
    user2301281 almost 2 years

    I want to create a string <res> in android as below:

    <string name="bmi0">0: BMI≤18.5</string>
    <string name="bmi1">1: 18.5<BMI≤24</string>
    <string name="bmi2">2: 24<BMI≤27</string>
    

    But i shown the error of "Tag start is not closed".

    I tried to put a \ symbol in front, but it doesn't work, it has shown the same error.

    <string name="bmi0">0: BMI\≤18.5</string>
    <string name="bmi1">1: 18.5<BMI\≤24</string>
    <string name="bmi2">2: 24<BMI\≤27</string>
    

    How to escape the special XML symbols ?

  • unwind
    unwind over 10 years
    Doesn't this entierly miss the point of the "less than or equal" sign being used, ≤ ?
  • unwind
    unwind over 10 years
    @peter.petrov Ah, I guess the OP's errors are only from the use of regular <.
  • peter.petrov
    peter.petrov over 10 years
    I think I answered first on this question btw :) @kocko Isn't this &le; actually &lt; ?!
  • user2301281
    user2301281 over 10 years
    it shown "unescaped & or nonterminated character/entity reference" error. when i use "&lt"
  • Konstantin Yovkov
    Konstantin Yovkov over 10 years
    you will have to add a colon ( ; ) after it.
  • Selvin
    Selvin over 10 years
    why it has +1? \uxxxx is not for xml
  • Selvin
    Selvin over 10 years
    why it has +1? \uxxxx is not for xml
  • Lovis
    Lovis over 10 years
    @Selvin this is tagged android, and in strings.xml it 100% works PLUS its the only way to do it with some special chars, like "non breaking-hyphen" \uXXXX is even SUGGESTED BY LINT (e.g if you type "...")
  • Selvin
    Selvin over 10 years
    sure the only way .... <?xml version="1.0" encoding="utf-8"?> <root><test name="&#8209;"/></root> xml notation is &#XXXX; or &#xHHHH;
  • Lovis
    Lovis over 10 years
    @selvin yes you're right (plus I just saw, that lint is not suggesting it anymore) BUT still, why is it thing working, if its not for xml?
  • Selvin
    Selvin over 10 years
    from The Source w3.org/TR/REC-xml/#dt-charref ... there is no single word about \u notation
  • Lovis
    Lovis over 10 years
    I still don't see why it's wrong. It answers the question and works in android. (why? Probably because the string is first parsed and then translated)