Left angle brackets in XML

Source: Internet
Author: User
Tags processing instruction

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.