Mastering the XML Series (3)---Creating well-formed XML documents continuation

Source: Internet
Author: User
Tags cdata

A summary of the previous article I wrote a section on creating a good XML document: element, which contains a few small sections: document root, legal and illegal name, non-character, entity reference. Then we'll go on.

5.CDATA section:

Previously said in the document, if you include characters such as &, we're going to escape with an entity reference. But, if we don't want to traverse every invalid character in a larger document, that is, we don't want to look at the characters one by one, and then we're going to escape them illegally, Then we can wrap them in CDATA and do batch escapes, like the following is illegal:

<data>I don't know how to use "<" and "&" in xml</data>

Because the "<" and "&" are used directly in the pcdata above, the secondary XML is not valid. But we can use the following:

<data><![CDATA[ I don't know how to use "<" and "&" in xml ]]></data>

The above is legal, generally using CDATA to escape the large segments of XML text that might contain illegal characters. In addition, you can add this and the rules to the DTD and XML schema, which we'll say later.

Note: use "<! [cdata[]]>] also contains your text.

Take a look at the following example, test to see if the following is legal:

<?xml version= "1.0"?>
<capitals>
<country name= "Scotland" >Edinburgh</country>
<country name= "Chile" >Santiago</country>
<country name= "Italy" >Rome</country>
<copyright>
The content of this &lt;xml&gt; File Is©nocompanyatall Limited
</copyright>
<footer>this is just section this contains a whole load of "characters" that
Maybe could cause some problems when working with XML files. We know that < and & characters cause problems, but what about $ and%?? So, by the time you read this and you'll know
</footer>
</capitals>

You can save the document as an. xml text and then open it in a browser, and the result will be an error! (everyone should try) to see how to change to welcome everyone to ask questions!

6. Whitespace in XML is meaningful.

In an XML document, the most confusing is the whitespace. When using HTML, whitespace is meaningless, and if there are lots of whitespace (space characters) together, the browser resolves them to a blank, but we can use &nbsp; to make a lot of whitespace exist.

In XML the hollow lattice is meaningful, and there are many ways to deal with whitespace that is the most important element of the data processing and processing two elements of the gap between the mark is different. See below:

If whitespace is in the text of the element (PCDATA),

<data><mydata>It is        whitespace</mydata></data>

There are three blank spaces between the word is and whitespace in the code, as shown in the browser:

It iswhitespace (Blank merged)

However, if there is a gap between the tags, it will be handled in a different way. such as:

<data>  <mydata>It is    whitespace</mydata>  </data>

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.