Tải bản đầy đủ (.doc) (14 trang)

Tai lieu= Tieng anh

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (80.19 KB, 14 trang )

How XML can be used
XML can keep data separated from your HTML.
HTML pages are used to display data. Data is often stored inside HTML pages.
With XML this data can now be stored in a separate XML file. This way you can
concentrate on using HTML for formatting and display, and be sure that changes
in the underlying data will not force changes to any of your HTML code.
XML can be used to store data inside HTML documents.
XML data can also be stored inside HTML pages as Data Islands. You can still
concentrate on using HTML for formatting and displaying the data.
XML can be used as a format to exchange information.
In the real world, computer systems and databases contain data in incompatible
formats. One of the most time consuming challenges for developers has been to
exchange data between such systems over the Internet. Converting the data to
XML can greatly reduce this complexity and create data that can be read by
different types of applications.
XML can be used to store data in files or in the databases.
Applications can be written to store and retrieve information from the store, and
generic applications can be used to display the data.
XML Example
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!!!</body>
</note>
Line-by-line code Explanation
<?xml version="1.0"?>
The XML declaration should always be included. It defines the XML version of
the document. In this case, the document conforms to the 1.0 specification of
XML.


<note>
Defines the first element (the root element) of the document.
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!!!</body>
Defines 4 elements of the root (to, from, heading and body).
</note>
The last line defines the end of the root element.
What's an XML doc looks like
Let's save a piece of code above as note.xml (by the way, XML document should
have .xml as it's extension) and open it in the IE. Below is what you actually see
on the browser.
XML Syntax - General Idea
1. All XML elements must have a closing tag
In HTML some elements do not have to have a closing tag. The following code is
legal in HTML: <p>This is a paragraph
<p>This is another paragraph
In XML all elements must have a closing tag like this: <p>This is a
paragraph</p>
<p>This is another paragraph</p>
2. XML tags are case sensitive
XML tags are case sensitive. Opening and closing tags must therefore be written
with the same case.
<Message>This is incorrect</message>
<message>This is correct</message>
Important: Tags should begin with either a letter, an underscore (_) or a colon (
followed by some combination of letters, numbers, periods (.), colons,
underscores, or hyphens (-) but no white space, with the exception that no tags
should begin with any form of "xml". It is also a good idea to not use colons as

the first character in a tag name even if it is legal. Using a colon first could be
confusing. Here are some legal and illegal tags examples: Legal tags Illegal tags
<first-name> <first - name>
<last.name> <last . name>
<namexml> <xmlname>
3. All XML elements must be properly nested
In HTML some elements can be improperly nested within each other like this:
<b><i>This text is bold and italic</b></i>
In XML, all elements must be properly nested within each other like this:
<b><i>This text is bold and italic</i></b>
4. All XML documents must have a root tag
All XML documents must contain a single tag pair to define the root element. All
other elements must be nested within the root element. All elements can have
sub (children) elements. Sub elements must be in pairs and correctly nested
within their parent element. eg. <root>
<child>
<subchild>
</subchild>
</child>
</root>
5. Attribute values must always be quoted
XML elements can have attributes in name/value pairs just like in HTML. In XML
the attribute value must always be quoted. eg.
Correct Incorrect
<?xml version="1.0"?> <?xml version="1.0"?>
<note date="25/06/00"> <note date=25/06/00>
Avoid using attributes?
Attributes are handy in HTML. But in XML you should try to avoid them (you
could easily substitute attributes by elements - I will show you later so you could
get the idea!!!). Why? Below are some of the problems using attributes.

Attributescan not contain multiple values
Attribute are not expandable
Attribute are more difficult to manipulate by program code
Attribute values are not easy to test against DTD
Let me clear up your doubt by looking at the following example:
An XML example
<?xml version="1.0"?>
<note>
<date>12/11/00</date>
<to>Tove</to>
<from>Jani</from>>
<subject>Reminder</subject>
<body>Don't forget me this weekend</body>
</note>
If you look at the element <date> above, how do you interpret it??? Is this 12. of
November or 11. of December???
Now, let see how you can expand the <date> element:
<?xml version="1.0"?>
<note>
<date>
<date>12</date>
<month>11</month>
<year>99</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend</body>
</note>
Got the idea???

XML Well-formed
If you have read the XML Syntax - General Idea section above, by now you
should have a very fair idea about XML in general. So, I am going to move on to
more interesting topic that is XML well-formed. XML Documents consider well
formed should satisfy three simple rules:
The document must contain one or more elements.
It must contain a uniquely name element, no part of which appears in the content
of any other element, known as the root element.
All other elements within the root element must be correctly nested.
So, according to these rules, the following are examples of well formed
documents:
example1.xml
<empty_tag></empty_tag>
example2.xml
<?xml version="1.0"?>
<class>Mammalia</class>
example3.xml
<root>
<class>Mammalia</class>
</root>
example4.xml
<empty_tag/>
Note: example1.xml and example4.xml are the same.
The following is example of not well formed documents:
bad_example.xml
<bad_parent>
<naughty_child>Some text info
</bad_parent>
</naughty_child>
Explanation: If you look carefully, you can see that the element <naughty_child>

overshoots the end of the <bad_parent> element, which should encapsulate the
<naughty_child> element completely (According to rule 3 above).
XML doc structure
Physically, documents are composed of a set of entities (we will talk about this
topic in a bit) that are identified by unique names. All documents begin with a root
or document entity. All other entities are optional.
As opposed to physical structure, XML documents have a logical structure as
well. Logically, documents are composed of declarations, elements, comments,
character references and processing instructions, all of which are indicated in the
document by explicit markup.
Data vs Markup
All XML documents may be understood in terms of the data they contain and the
markup that describe that data.
Data is typically "character data" (i.e. anything within the boundaries of valid
Unicode such as letters, numbers, punctuation and so on) but can also be binary
data as well.
Markup includes tags, comments, processing instructions, DTDs and references
and so forth.
For example: <name>John Smith</name>
Explanation: <name>and </name> tags comprise the markup and "John Smith"
comprises the character data.
XML Declaration
To begin an XML document, it is a good idea to include the XML declaration at
the very first line of the document. Though the XML declaration is optional, but
the W3C specification (World Wide Web Consortium - the group developed XML)
suggests that we should include it to indicate the version of XML, used to
construct the document so that an appropriate parser or parsing process can be
matched to the document.
Essentially, the XML declaration is a processing instruction that notifies the
processing agent (browser) that the following document has been marked up as

an XML document. It will look something like the following:
<?xml version="1.0"?>

Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×