#XML

It refers to the XML Extensible Markup Language;
XML is designed to transmit and store data.

1. What is XML?

XML refers to extensible markup language (EXtensible Markup Language)
XML is a markup language, much like HTML
XML is designed to transmit data rather than display data
XML tags are not predefined. You need to define your own labels.
XML is designed to be self-descriptive.
XML is a W3C Recommendation

The main difference between HTML-2.XML

XML is not a replacement for HTML.
XML and HTML are designed for different purposes:
XML is designed to transmit and store data, which is the focus of the content data.
HTML was designed to display data, which is the focus of appearance data.
HTML is about displaying information, while XML is designed to transmit information.

3. No any act of XML

XML is inaction.

Maybe it's a bit difficult to understand, but XML does not do anything. XML was created to structure, store, and transfer information.
Here is John George wrote the notes, stored as XML:

<note>
<to> George </ to>
<from> John </ from>
<heading> Reminder </ heading>
<body> the Do not forget at The Meeting! </ body>
</ note>
above this note self-descriptive. It has a title and a message, contains both the sender and recipient.

However, the XML document still does not do anything. It is only wrapped in XML tags purely informational. We need to write software or programs, in order to transmit, receive and show this document.

4.XML just plain text

XML is nothing special. It is just plain text only. Ability to handle plain text software can handle XML.
However, you can read XML applications can be targeted to deal with XML tags. The functional significance of the label depends on the nature of the application.

5. Adoption of XML you can invent your own label

Tag in the example above had not defined (such as <to> and <from>) in any XML standard. These labels are invented by the creator of the document.
This is because XML has no predefined tags.
Tag (and HTML structure) used in HTML is predefined. HTML documents only been defined in the standard HTML tag (such as <p>, <h1> etc.).
XML allows the author to define his own tags and his own document structure.

HTML is not a substitute for 6.XML

XML is a complement to the HTML.
XML does not replace HTML, it is important to understand. In most web applications, XML is used to transmit data, while HTML is used to format and display the data.
The best description of XML are:
XML is independent of software and hardware information transfer tool.

DETAILED Overview:
a XML uses
XML web used in many aspects of development, often used to simplify storage and sharing of data.

XML data separate from HTML

If you need to display dynamic data in an HTML document, then whenever the data changes will take a lot of time to edit the HTML.

By XML, data can be stored in a separate XML file. So you can concentrate on using HTML for layout and display, and be sure to change the underlying data is no longer required to make any changes to the HTML.

By using a few lines of JavaScript, you can read an external XML file and then update the data content in HTML.

XML simplifies data sharing

In the real world, computer systems and data using an incompatible format to store data.

XML data is stored in plain text format, thus there is provided a method of storing data independent of software and hardware.

This allows to create different applications can share data easier.

XML simplifies data transmission

By XML, you can easily exchange data between incompatible systems.

For developers, one of the most time-consuming challenge has been to exchange data between incompatible systems on the Internet.

Since data can be read by a variety of incompatible applications to exchange data XML reduces this complexity.

XML simplifies the platform change

Upgrade to the new system (hardware or software platforms), it is always very time consuming. Must convert large amounts of data, incompatible data is often lost.

XML data is stored in text format. This makes XML without loss of data, easier to expand or upgrade to a new operating system, a new application or a new browser.

XML makes your data more useful

Since XML is independent of hardware, software and applications, XML makes your data more available, and more useful.

Different applications can access your data, not only can also be accessed from XML data source in HTML pages.

By XML, your data is available using a variety of reading devices (handheld computer, voice equipment, news readers, etc.), but also for the blind or other disability.

 

Two XML tree structure
of XML documents form a tree structure, from which "roots" started, and then expand into the "leaves."

An XML document instance

Using a simple XML syntax self-descriptive:

<? xml Version = "1.0" encoding = "ISO-8859-1"?>
<note>
<to> George </ to>
<from> John </ from>
<heading> Reminder </ heading>
<body> Don 'The Meeting forget T! </ body>
</ Note>
The first line is the XML declaration. It defines the XML version (1.0) and used for encoding (ISO-8859-1 = Latin-1 / Western European character sets).

The next line describes the root element of the document (like saying: "This document is a note"):

<note>
The next 4 lines describe 4 child elements root (to, from, heading and body):

<to> George </ to>
<from> John </ from>
<heading> Reminder </ heading>
<body> the Do not forget at The Meeting </ body>!
end of the last line of the definition of the root element:

</ note>
From this example, it is conceivable that the XML document contains a note of John to George.

XML documents form a tree structure

XML document must contain a root element. The element is the parent of all other elements.

XML document elements form a document tree. This tree starts from the root, and extends to the bottom of the tree.

All elements can have sub-elements:

<the root>
  <Child>
    <subchild> ..... </ subchild>
  </ Child>
</ the root>
parent, child, and a fellow like terms describe relationships between elements. Parent elements have children. Sub-elements on the same level to be siblings (brothers or sisters).

All elements can have text content and attributes (similar to the HTML).

Examples

 

The figure represents a book in the following XML:





















<Book> element has four sub-elements: <title>, <author>, <year>, <price>.

 

Three XML syntax rules of
XML syntax rules very simple and very logical. These rules are easy to learn, and very easy to use.

All XML elements must have a closing tag

In HTML, you will often see no closing tag elements:

<P> This IS A paragraph
<P> This IS Another paragraph
in XML, the closing tags are omitted illegal. All elements must have a closing tag:

<the p-> This IS A paragraph </ the p->
<the p-> This IS Another paragraph </ the p->
Note: You may have noticed that the XML declaration did not close the tab. This is not an error. XML declaration not part of itself. It is not an XML element does not require a closing tag.

XML tags are case sensitive

XML elements are defined using XML tags.

XML tags are case sensitive. In XML, the tag <Letter> tag <letter> are different.

You must use the same case to write the open and closing tags:

<Message> This is wrong. </ the Message>
<the Message> That's right. </ message>
NOTE: opening and closing tags commonly referred to as the start and end tags. Whether you like better term, their concepts are the same.

XML must be properly nested

In HTML, you often see are not properly nested elements:

<b> <i> This text is bold and italic </ b> </ i>
In XML, all elements must be properly nested within each other:

<b> <i> This text is bold and italic </ i> </ b>
In the above example, the correct nested meaning: As <i> element is unfolded in <b> element, then it must closed within <b> element.

XML documents must have a root element

XML document must have a element is the parent of all other elements. This element is called the root element.

<the root>
  <Child>
    <subchild> ..... </ subchild>
  </ Child>
</ the root>
the XML attribute value shall be quoted

Similar to HTML, XML can have attributes (for name / value).

In XML, XML attribute values ​​must be quoted. Please study the following two XML documents. The first one is incorrect, the second is correct:

1)

<note date=08/08/2008>
<to>George</to>
<from>John</from>
</note>
2)

<note DATE = "08/08/2008">
<to> George </ to>
<from> John </ from>
</ note>
error in the first document is, date attribute in the note element is not added quotation marks.

Entity Reference

In XML, some characters have special significance.

If you put the character "<" on the XML element, the error occurs because the parser will use it as the beginning of a new element.

This will generate an XML error:

<message> if salary <1000 then </ message>
To avoid this error, please use entity references to replace the "<" character:

<message> if salary & lt; 1000 then </ message>
In XML, there are five predefined entity references:

  & Lt; <less than

  & Gt;> greater than

  &amp;  &  和号

  & Apos; 'single quote

  & Quot; "quotes

Note: In XML, only the characters "<" and "&" is indeed illegal. Greater than sign is legal, but use entity references to replace it is a good habit.

XML comments in

Notes written in XML syntax and HTML syntax is very similar:

<-! This is a comment - >
in XML, the space will be reserved

HTML will cut multiple consecutive space characters (merged) into one:

HTML:   Hello           my name is David.
输出:   Hello my name is David.

In XML, document spaces are not truncated.

XML to store LF line feed

In Windows applications, typically a pair of line feed characters stored: carriage return (CR) and line feed (LF). This has similarities to the character and typewriter set up a new line of action. In Unix applications, a new line LF character to storage. The Macintosh applications use CR to store the new row.

 

Four XML elements in
the XML document contains XML elements.

What is XML elements?

XML element is the time from (and including) the start tag until (and including) the end of the label.

Mixture with other elements, text, or may contain elements of both. Element can also have attributes.

<Bookstore>
<Book category = "the CHILDREN">
  <title> Harry Potter </ title>
  <author> J K. Rowling </ author>
  <year> 2005 </ year>
  <. price> 29.99 </. price>
</ Book >
<Book category = "the WEB">
  <title> Learning XML </ title>
  <author> Erik T. Ray </ author>
  <year> 2003 </ year>
  <. price> 39.95 </. price>
</ Book>
< / bookstore>
in the example above, <bookstore> and <book> have element content, because they contain other elements. <author> only text, because it only contains text.

In the above example, only <book> element has attributes (category = "CHILDREN").

XML naming rules

XML elements must follow these naming rules:

The name can contain letters, numbers and other characters
names can not start with a number or punctuation
names can not be character "xml" (or XML, Xml) begin
name can not contain spaces
words can use any name, no reservations.

Best naming conventions

So descriptive name. Use the underlined name is also very good.
Name should be relatively short, such as: <book_title>, instead: <the_title_of_the_book>.
Avoid "-" character. If you name this way: "first-name", some software will think you need to extract the first word.
Avoid. "" Characters. If you name this way: "first.name", some software may think that "name" is the object "first" of the property.
Avoid ":" character. Colon will be converted into namespace to use (described later).
XML documents often have a corresponding database, in which fields correspond to elements in the XML document. There is a practical experience that the use of the name of the database rules to name the elements in the XML document.
Non-English letters such as éòá is valid XML element name, but need to pay attention when software developers do not support these characters may arise.
XML is extensible element

XML elements that can be extended to carry more information.

Look at the following XML example:

<note>
<to> George </ to>
<from> John </ from>
<body> the Do not forget at The Meeting! </ body>
</ note>
Let us imagine that we created an application that the <to>, <from> and <body> element is extracted, and generates the following output:

MESSAGE
To: George
From: John
Don't forget the meeting!

Imagine this XML document after the document of the ED added some additional information:

<note>
<DATE> 2008-08-08 </ DATE>
<to> George </ to>
<from> John </ from>
<heading> Reminder </ heading>
<body> the Do not forget at The Meeting! < / body>
</ note>
then this application will break or crash?

will not. This application can still be found in the XML document <to>, <from> and <body> element, and produce the same output.

One of the advantages of XML is that you can always be expanded without interrupting the application.

 

Five XML attributes
XML elements can contain attributes in the start tag, like HTML.

Property (Attribute) provides additional (additional) information on the elements.

XML attributes

From HTML, you will remember this: <img src = "computer.gif">. "Src" attribute provides additional information about the <img> element.

In HTML (and in XML) attributes provide additional information about the elements

<IMG the src = "computer.gif">
<a href="demo.asp">
attribute data are not generally provide information components. In the following example, the file type regardless of the data, but very important to deal with this element of the software for:

<File of the type = "GIF"> computer.gif </ File>
XML attributes must be quoted

Attribute values ​​must be surrounded by quotation marks, although single or double quotes can be used. For example, a person's gender, person tag can be written like this:

<person sex = "female">
or may be this:

<person sex = 'female'>
NOTE: If the attribute value contains a double quote, then it is necessary to use single quotes surrounding it, like in this example:

<gangster name = 'George "Shotgun " Ziegler'>
or may use the entity reference:

<gangster name="George &quot;Shotgun&quot; Ziegler">
XML 元素 vs. 属性

Consider these examples:

<Person Sex = "FEMALE">
  <FirstName> Anna </ FirstName>
  <LastName> Smith </ LastName>
</ Person>
<Person>
  <Sex> FEMALE </ Sex>
  <FirstName> Anna </ FirstName>
  <LastName> smith </ LastName>
</ Person>
in the first example, sex is an attribute. In the second example, sex is a child element. Two examples can provide the same information.

No rules to tell us when to use attributes, and when to use child elements. My experience is in HTML, the property is very convenient to use them, but in XML, you should try to avoid using property. If the information feels like data, use sub-element of it.

The following three XML documents contain exactly the same information:

Date property using the first example:

<note date="08/08/2008">
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
第二个例子中使用了 date 元素:

<note>
<DATE> 08/08/2008 </ DATE>
<to> George </ to>
<from> John </ from>
<heading> Reminder </ heading>
<body> the Do not forget at The Meeting! < / body>
</ Note>
the third example is used in the extended date elements (easy to use):

<note>
<date>
  <day>08</day>
  <month>08</month>
  <year>2008</year>
</date>
<to>George</to>
<from>John</from>
<heading>Reminder</heading>
<body>Don't forget the meeting!</body>
</note>
避免 XML 属性?

Some of the problems caused by the use of attributes:

Attributes can not contain multiple values (elements can)
attributes can not describe the tree structure (elements can)
attributes can not be extended (for future changes)
attributes are difficult to read and maintain
Try to use elements to describe data. But only uses attributes to provide data independent information.

Do not do stupid things (this is not the way XML should be used):

<note day="08" month="08" year="2008" to="George" from="John"
heading="Reminder" body="Don't forget the meeting!"></note>
针对元数据的 XML 属性

Sometimes assign ID references to elements. The index ID is used to identify the XML elements, the way it works in HTML ID attribute is the same. This example demonstrates this to us:

<messages>
  <note the above mentioned id = "501">
    <to> George </ to>
    <from> John </ from>
    <heading> Reminder </ heading>
    <body> the Do not forget at The Meeting! </ body>
  < / Note>
  <Note ID = "502">
    <to> John </ to>
    <from> George </ from>
    <heading> Re: Reminder </ heading>
    <body> the I Will Not </ body>
  </ Note >
</ messages>
the above is just an identifier ID, used to identify different notes. It is not part of the note data.

In this concept we are trying to transfer you to: metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.

 

Six XML validation
has the correct syntax of XML is called "good form" of XML.

By validating XML DTD is "legal" XML.

Well-formed XML documents

"Good form" or "well-formed" XML document has correct syntax.

"Good form" (Well Formed) XML document will abide by the rules of XML syntax introduced in the previous chapters:

XML document must have root element
XML document must have a closing
XML tags are case-sensitive
XML elements must be properly nested
XML attribute must be quoted
<? Xml version = "1.0" encoding = "ISO-8859-1"?>
<note>
<to> George </ to>
<from> John </ from>
<heading> Reminder </ heading>
<body> the Do not forget at The Meeting! </ body>
</ note>
validate an XML document

Legal XML document is a "good form" of XML documents, to comply with the same document type definition (DTD) syntax rules:

<? xml Version = "1.0" encoding = "ISO-8859-1"?>
<! DOCTYPE note the SYSTEM "Note.dtd">
<note>
<to> George </ to>
<from> John </ from>
< heading> Reminder </ heading>
body> the Do not forget at the Meeting! </ body> <
</ note>
in the example above, DOCTYPE declaration is a reference to an external DTD file. The following paragraphs show the contents of this document

XML DTD

A DTD is to define the structure of XML documents. It uses a series of legal elements define the structure of the document:

<!DOCTYPE note [
  <!ELEMENT note (to,from,heading,body)>
  <!ELEMENT to      (#PCDATA)>
  <!ELEMENT from    (#PCDATA)>
  <!ELEMENT heading (#PCDATA)>
  <!ELEMENT body    (#PCDATA)>
]>
XML Schema

W3C supports an XML-based DTD instead of who it called XML Schema:

<xs:element name="note">

<xs:complexType>
  <xs:sequence>
    <xs:element name="to"      type="xs:string"/>
    <xs:element name="from"    type="xs:string"/>
    <xs:element name="heading" type="xs:string"/>
    <xs:element name="body"    type="xs:string"/>
  </xs:sequence>
</xs:complexType>

</xs:element>
 
七  XML 验证器
XML 错误会终止您的程序

XML document XML error will terminate your program.

The W3C XML specification states: If the XML document there is an error, then the program should not continue processing the document. The reason is, XML software should be lightweight, fast, has good compatibility.

If you use HTML, create a document that contains a large number of errors is possible (for example, you forget the end tag). One of the main reason is quite bloated HTML browser, compatibility is very poor, and they have their own way to determine when the found documents should show why look like an error.

When using XML, this situation should not exist.

Your XML syntax checking

View more detailed http://www.w3school.com.cn/xml/xml_validator.asp

 

Eight XML browser supports
almost all mainstream browsers support XML and XSLT.

 

Nine view the XML file
in all modern browsers are able to view the original XML file.

Do not expect XML files are directly displayed as an HTML page.

 

Ten use CSS to display XML
by using CSS, you can add display information to an XML document.

Use CSS to display your XML?

Use CSS to format an XML document is possible.

The following is an example of how to use CSS style sheets to format XML documents:

http://www.w3school.com.cn/xml/xml_display.asp

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/css" href="cd_catalog.css"?>
<CATALOG>
  <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
  </CD>
  <CD>
    <TITLE>Hide your heart</TITLE>
    <ARTIST>Bonnie Tyler</ARTIST>
    <COUNTRY>UK</COUNTRY>
    <COMPANY>CBS Records</COMPANY>
    <PRICE>9.90</PRICE>
    <YEAR>1988</YEAR>
  </CD>
</CATALOG>

Note: Use CSS to format XML is not a commonly used method, but can not represent the future of XML documents styled. W3C recommend using XSLT.

 

Eleven display XML using XSLT
through the use of XSLT, you can add display information to an XML document.

Display XML using XSLT

XSLT XML is the preferred style sheet language.

XSLT (eXtensible Stylesheet Language Transformations) is more perfect than CSS.

Before using one of the methods XSLT XML file is displayed in the browser, first convert it to HTML, as the following example illustrates it:

Here is a snippet of this XML file. The second line, <?? Xml-stylesheet type = "text / xsl" href = "simple.xsl">, the link to the XML file XSL file:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="simple.xsl"?>
<breakfast_menu>
  <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
       two of our famous Belgian Waffles
    </description>
    <calories>650</calories>
  </food>
</breakfast_menu>

Guess you like

Origin www.cnblogs.com/liurui-bk517/p/10951667.html