Ben Lin
Ben Lin

Reputation: 835

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

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.

Upvotes: 3

Views: 663

Answers (2)

Michael Kay
Michael Kay

Reputation: 163262

You ask "why" one is illegal and the other not. The answers to "why" questions about XML syntax are often rooted in its SGML history - the designers of XML wanted to ensure that it was a strict subset of SGML and could be parsed by SGML parsers. SGML allowed freedom in areas where XML does not, for example omitting the quotes around attribute values, and this accounts for some of the restrictions that XML has inherited.

Upvotes: 2

NPKR
NPKR

Reputation: 5496

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" …>

Upvotes: 3

Related Questions