Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why ">" is ok to be in value of XML file, but "<" is not?

Tags:

validation

xml

Here is an example of XML file:

<tag value=">this is well formatted xml"/>

This can be loaded successfully in MSXML and Apache Xerces.

<tag value="<this is not"/>

This will failed.

I think the Greater Than sign and Less Than sign are both illegal in the XML file, when they are not being used as tag seperator. But the above example shows the Greater Than sign is ok.

Can anybody explain or give a link to a document about this? Thanks.

like image 259
Ben Lin Avatar asked Dec 08 '25 21:12

Ben Lin


1 Answers

Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.

 Entity References

    Some characters have a special meaning in XML.

    If you place a character like "<" inside an XML element, it will generate an error because the parser interprets it as the start of a new element.

    This will generate an XML error:
    <message>if salary < 1000 then</message>

    To avoid this error, replace the "<" character with an entity reference:
    <message>if salary &lt; 1000 then</message>

    There are 5 predefined entity references in XML:
    &lt;    <   less than
    &gt;    >   greater than
    &amp;   &   ampersand 
    &apos;  '   apostrophe
    &quot;  "   quotation mark

    Note: Only the characters "<" and "&" are strictly illegal in XML. The greater than character is legal, but it is a good habit to replace it.

EDIT 1:

source link http://www.w3schools.com/xml/xml_syntax.asp

you can get more information related to XMl

Example:

Here is what the variable looks like improperly coded in an XML file:

<mail id="a1" to="&<manager>@mycompany.com" …

Here is what the variable looks like properly coded in an XML file:

<mail id="a1" to="&amp;&lt;manager&gt;@mycompany.com" …>
like image 78
NPKR Avatar answered Dec 10 '25 09:12

NPKR