XML annotations, processing commands, and CDATA

Source: Internet
Author: User
Tags cdata processing instruction xml parser

In this chapter, you will learn how to add three XML tags to files: Annotations, processing commands, and CDATA segments. Although these items are not necessary in a correctly formatted (or valid) XML file, they are of great use. You can use annotations to make the file more readable, or use processing commands to modify the method for processing or displaying the file in the application. You can also use CDATA segments to include any combination of Characters in the element's character data.

Add Annotation

As you learned in chapter 1, the 6th goal of the XML specification is "XML files should be easy to read and reasonably clear 」. Although the XML processor usually ignores annotations, proper and meaningful annotations can greatly improve the readability and clarity of XML files, just like annotations can make the program's original Code such as C or basic easier to understand.
Note:
In Microsoft Internet Explorer 5, the XML processor does not parse the annotated text into an XML volume, nor process the annotation in any other way. However, the processor allows the annotated text to be used by scripts in HTML webpages. In chapter 9, you will learn how to use scripts to access annotation text, just like using other components in XML files.
If you open the file directly in the browser and the file has no reference style sheet, Internet Explorer 5 will display all the annotations in the XML file.

Type of Annotation

Annotate with <! -- Starts with a character and ends with a --> character. Between the two definition symbols, you can enter any text you want-except the double-link symbol. You can even enter the prohibited (<) and (&) symbols. The following is an example of legal annotation:
<! -- Here you can type any text before t a double hyphen. The <and & characters are OK! -->

You can place comments

You can place comments anywhere except the labels in the XML file. In other words, you can put them in the sequence of the file:
<? XML version = "1.0"?>
<! -- Here is a comment in the PROLOG. -->
<Docelement>
This is a very simple XML document.
</Docelement>
You can append the annotation to the element of the file:
<? XML version = "1.0"?>
<Docelement>
This is a very simple XML document.
</Docelement>
<! -- This comment follows the document element. -->
You can also put comments in the file content:
<? XML version = "1.0"?>
<Docelement>
<! -- This comment is part of the content of the root element. -->
This is a very simple XML document.
</Docelement>
The following is an illegal annotation example because the annotation is placed in the tag:
<? XML version = "1.0"?>
<Docelement <! -- This is an illegal comment! -->
This is a very simple XML document.
</Docelement>
However, you can place the annotation in the file type declaration (DTD)-although DTD is also a type of tag-as long as the annotation is not in other tags in the DTD. In chapter 5, you will learn about the DTD and how to place the annotation in the DTD.

Use processing commands

The purpose of processing commands is to provide information sent by the XML processor to the application software.
Note:
As described in Chapter 2, the XML processor is a software module that reads and stores XML file content. Application Software is a software module that obtains the file content from the processor and manages and displays the content. When you display XML in internetexplorer 5, the browser will provide the XML processor and a small part of the application software. (If you write scripts to manage and display XML files, you must provide some application software by yourself)

Type of Processing Command

The Processing Command has the following general types:
<? Target instruction?>
The target here is the name of the application that the command is directed. Any name can follow the following rules:
The name must start with a letter or bottom line (_) followed by zero or more letters, numbers, periods (.), hyphens (-), or hyphens (_).
The name "XML" is retained whether it is composed of uppercase or lowercase letters. (As you can see, you can use XML to represent the XML declaration of a file, which is also a type of Processing Command .)
Instruction is the information sent to the application software. It is composed of any consecutive characters except "?> 」 Character pair (it is reserved for use as a termination instruction ).

How to Use processing commands

The special processing commands you can use in XML files depend on the processor used to read files. If you use internetexplorer 5 as an XML processor (as is commonly used in this book), you will find two main purposes of the processor:
You can use standard and reserved processing commands to tell Internet Explorer 5 how to process or display files. The example you see in this book is that the Processing Command tells Internet Explorer 5 to use a specific style sheet to display files. For example, the following Processing Command tells Internet Explorer 5 to use inventory01.css CSS to display files:
<? XML-stylesheet type = "text/CSS" href = "inventory01.css"?>
If you write a web script to process and display XML files, you can add any non-reserved processing commands to the file, in addition, your script will read these commands and perform proper processing. For example, you may add the following processing commands to the file to show the details of the script:
<? Myscript detail = "2"?>
In chapter 9, you will learn how to use scripts to access components in XML files, including any processing commands.

You can place processing commands

You can add processing commands to any place other than labels in the XML file-meaning, you can place commands in the same place as placing comments: in the file's ordinal, the file element is followed by the element content. The following is an XML file that processes commands in every valid location:
<? XML version = "1.0"?>
<! -- The following is a processing instruction in the PROLOG: -->
<? XML-stylesheet type = "text/CSS" href = "inventory01.css"?>
<Inventory>
<Book>
<! -- Here's a processing instruction within
Element's content: -->
<? Scripta emphasize = "yes"?>
<Title> The Adventures of Huckleberry Finn </title>
<Author> Mark Twain </author>
<Binding> mass market paperback </binding>
<Pages> 298 </pages>
<Price> $5.49 </price>
</Book>
<Book>
<Title> Leaves of Grass </title>
<Author> Walt Whitman </author>
<Binding> hardcover </binding>
<Pages> 462 </pages>
<Price> $7.75 </price>
</Book>
</Inventory>
<! -- And here's one following the document element: -->
<? Scripta Category = "books" style = "formal"?>
The following is an example of an illegal handling instruction in the volume label:
<! -- The following element contains an illegal
Processing Instruction: -->
<Book <? Scripta emphasize = "yes" ???>
<Title> Leaves of Grass </title>
<Author> Walt Whitman </author>
<Binding> hardcover </binding>
<Pages> 462 </pages>
<Price> $7.75 </price>
</Book>
However, you can place processing commands in the file type definition (DTD)-although the DTD is also a volume label-as long as the processing commands are not placed in other labels of the same DTD. In chapter 5, you will learn about DTD and how to place processing commands in DTD.

CDATA section

As you learned in Chapter 3, you cannot directly place the (<) or (&) symbol in the character data in the element content. To avoid this restriction, use the character reference (& #60; or & #38;) or pre-defined normal object reference (& lt; or & amp ;), as you will learn in chapter 6. However, if you need to add multiple (<) or (&) characters, using these references will become inappropriate and make the materials hard to read. In this case, it is easier to place text containing restricted characters in CDATA segments.
CDATA segment type
The CDATA section uses the character "<! [CDATA ["and end with the"]> "character. Between the two definition symbols, you can enter any character (including "<" or "&") except "]>. (Because the character is interpreted as the end of the CDATA segment .) All characters in the CDATA segment are considered as the literal part of the element character data, rather than the XML tag.
The following is an example of a valid CDATA segment:
<! [CDATA [
Here you can type any characters character t Two right brackets followed
By a greater-than symbol.
]>
Note:
The keyword CDATA, like other XML keywords you see, must be written in uppercase letters.
If you want to include a piece of original code or tag as a part of the real character data of the elements displayed in the browser, you can use CDATA segments to prevent the XML Parser from interpreting the "<" or "&" characters as XML volumes. The following is an example:
<A-SECTION>
The following is an example of a very simple HTML page:
<! [CDATA [
<HTML>
<Head>
<Title> r. Jones & Sons </title>
</Head>
<Body>
<P> welcome to our home page! </P>
</Body>
</Html>
]>
A-SECTION>
For example, if there is no CDATA segment, the processor assumes that <HTML> is the starting part of the stacked element, not the starting part of the character data of the A-SECTION element.
Note:
Since you can directly add the "<" and "&" characters to the CDATA section, you do not need to use character references (& #60; and &) or pre-defined general object references (& lt; and & amp;), I will explain the character references and object references in chapter 6. In fact, if you use such references, the parser will literally interpret each character in the references and will not replace the References containing the "<" or "&" characters.
You can place CDATA segments
You can place CDATA segments anywhere character data can appear-that is, the content of the element is not in the XML tag. The following is an example of CDATA segment placement.
<? XML version = "1.0"?>
<Musical>
<Title_page>
<! [CDATA [
<Oklahoma!>
By
Rogers & Hammerstein
]>
</Title_page>
<! -- Other elements here... -->
</Musical>
The Error Type XML file displayed on the next page contains two illegal CDATA segments. The first one is not in the element content. The second is in the content of the file element, but it is not in the starting tag.
<? XML version = "1.0"?>
<! [CDATA [illegal: Not within Element Content!]>
<Doc_element>
<Sub_element <! [CDATA [illegal: Inside of markup!]>
Sub-element content...
</Sub_element>
</Doc_element>
Note:
CDATA segments cannot be nested. That is, you cannot put a CDATA segment into another segment.

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.