why are multiple '>' allowed in xml

Viewed 30

Here's a "valid" XML:

<?xml version="1.0"?><class>>>>>>>>>>>>>>>>>>>>>>>>>>>>><abc att="da"/></class>

I have a relatively simple question: why is this XML correctly validated by virtually all possible parsers? I checked latest xml specs from w3.org https://www.w3.org/TR/2008/REC-xml-20081126/ but I cannot find anything related to this. Is this something implementation specific?

The symbol '<' cannot be used multiple times ie.

<?xml version="1.0"?><class><<abc att="da"/></class>

this XML is invalid.

1 Answers

https://www.w3.org/TR/2008/REC-xml-20081126/#syntax

The ampersand character (&) and the left angle bracket (<) must not appear in their literal form, except when used as markup delimiters, or within a comment, a processing instruction, or a CDATA section. If they are needed elsewhere, they must be escaped using either numeric character references or the strings "&amp;" and "&lt; " respectively. The right angle bracket (>) may be represented using the string "&gt;", and must, for compatibility, be escaped using either "&gt;" or a character reference when it appears in the string "]]>" in content, when that string is not marking the end of a CDATA section.

If you have an unencoded literal <, then the parser will assume that you are starting an element. You can't have a sequence of them, because you can't have < in an element name.

If you have an unencoded literal >, there isn't any confusion about closing an element. It is just a sequence of > in the text() node of an element.

You can encode the < as &lt;:

<class>&lt;<abc att="da"/></class>
Related