I encountered a very difficult problem recently. I haven't found the cause for a long time, but I finally found it confusing. In my opinion, an xml configuration file with XML syntax errors can pass the XML schema validation. This xml configuration file with syntax errors eventually leads to errors in the entire program. The file fragment is as follows:
<snapmetric name="vendor" type="string"> aggregate type="none" groupBy="true"/></snapmetric>
The second line is actually an element at a glance, and there should be a "<" before "aggregate ". Obviously, an XML format error is caused by negligence, but such a file passes the XML schema verification. What is the problem, is schema validation not concerned with the XML format correct?
With such a question, I modified the XML:
<snapmetric name="vendor" type="string"> <aggregate type="none" groupBy="true"</snapmetric>
The left parenthesis was added but the right parenthesis was removed. This time, an unexpected error was reported, indicating that the format was incorrect. What's going on?
Finally, the answer is found in the XML document:
The Ampersand character (&) and the Left angle bracket (<may appear in their literal form
Only 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...
In XML files, "&" and "<" in user-defined values are absolutely not allowed. That is to say, ">" is actually allowed. Let's look at the first XML segment, missing "<",
"Aggregate type =" NONE "groupby =" true "/>" is actually treated as text content in the "snapmetric" element rather than child element by XML. This explains why this XML has not been validated for an error. So how can we make XML not treat "aggregate type =" NONE "groupby =" true "/>" as text?
There is a way, haha.
View my schema definition:
<Xs: element name = "snapmetric">
<Xs: complextype
Mixed = "false">
<Xs: All>
<Xs: Element ref = "aggregate" minoccurs = "0" maxoccurs = "1"/>
<Xs: Element ref = "alert" minoccurs = "0" maxoccurs = "1"/>
</Xs: All>
<Xs: attribute name = "type" use = "required">
<Xs: simpletype>
<Xs: Restriction base = "XS: string">
<Xs: enumeration value = "float"/>
<Xs: enumeration value = "integer"/>
<Xs: enumeration value = "string"/>
</Xs: Restriction>
</Xs: simpletype>
</Xs: attribute>
<Xs: attribute name = "name" type = "XS: string" use = "required">
</Xs: attribute>
</Xs: complextype>
</Xs: Element>
Do you see "mixed =" false "in the red font? It indicates that this element only contains child elements and does not contain text, this limits the possibility that XML regards the child element missing the left angle bracket as text.
However, if the element is designed to contain text, it will be the same!