es because it is reserved to be used for something called namespaces (more later).
XML Attributes
XML elements can have attributes in the start tag, just like HTML.
Attributes are used to provide additional information about elements.
XML Attributes
XML elements can have attributes.
From HTML you will remember this: <IMG SRC="computer.gif">. The SRC attribute provides additional information about the IMG element.
In HTML (and in XML) attributes provide additional information about elements:
<img src="computer.gif">
<a href="demo.asp">
Attributes often provide information that is not a part of the data. In the example below, the file type is irrelevant to the data, but important to the software that wants to manipulate the element:
<file type="gif">computer.gif</file>
Quote Styles, "female" or ''''female''''?
Attribute values must always be enclosed in quotes, but either single or double quotes can be used. For a person''''s sex, the person tag can be written like this:
<person sex="female">
or like this:
<person sex=''''female''''>
Note: If the attribute value itself contains double quotes it is necessary to use single quotes, like in this example:
<gangster name=''''George "Shotgun" Ziegler''''>
Note: If the attribute value itself contains single quotes it is necessary to use double quotes, like in this example:
<gangster name="George ''''Shotgun'''' Ziegler">
Use of Elements vs. Attributes
Data can be stored in child elements or in attributes.
Take a look at 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 last, sex is a child element. Both examples provide the same information.
There are no rules about when to use attributes, and when to use child elements. My experience is that attributes are handy in HTML, but in XML you should try to avoid them. Use child elements if the information feels like data.
My Favorite Way
I like to store data in child elements.
The following three XML documents contain exactly the same information:
A date attribute is used in the first example:
<note date="12/11/2002">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>
A date element is used in the second example:
<note>
<date>12/11/2002</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>
An expanded date element is used in the third: (THIS IS MY FAVORITE):
<note>
<date>
<day>12</day>
<month>11</month>
<year>2002</year>
</date>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>
Avoid using attributes?
Should you avoid using attributes?
Some of the problems with using attributes are:
- attributes cannot contain multiple values (child elements can)
- attributes are not easily expandable (for future changes)
- attributes cannot describe structures (child elements can)
- attributes are more difficult to manipulate by program code
- attribute values are not easy to test against a Document Type Definition (DTD) - which is used to define the legal elements of an XML document
If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.
Don''''t end up like this ( if you think this looks like XML, you have not understood the point):
<note day="12" month="11" year="2002"
to="Tove" from="Jani" heading="Reminder"
body="Don''''t forget me this weekend!">
</note>
An Exception to my Attribute rule
Rules always have exceptions.
My rule about attributes has one exception:
Sometimes I assign ID references to elements. These ID references can be used to access XML elements in much the same way as the NAME or ID attributes in HTML. This example demonstrates this:
<messages>
<note id="p501">
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>
<note id="p502">
<to>Jani</to>
<from>Tove</from>
<heading>Re: Reminder</heading>
<body>I will not!</body>
</note>
</messages>
The ID in these examples is just a counter, or a unique identifier, to identify the different notes in the XML file, and not a part of the note data.
What I am trying to say here is that metadata (data about data) should be stored as attributes, and that data itself should be stored as elements.
XML Validation
XML with correct syntax is Well Formed XML.
XML validated against a DTD is Valid XML.
"Well Formed" XML documents
A "Well Formed" XML document has correct XML syntax.
A "Well Formed" XML document is a document that conforms to the XML syntax rules that were described in the previous chapters:
<?xml version="1.0" encoding="ISO-8859-1"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>
"Valid" XML documents
A "Valid" XML document also conforms to a DTD.
A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE note SYSTEM "InternalNote.dtd">
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don''''t forget me this weekend!</body>
</note>
XML DTD
A DTD defines the legal elements of an XML document.
The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. You can read more about DTD, and how to validate your XML documents in our DTD tutorial.
XML Schema
XML Schema is an XML based alternative to DTD.
W3C supports an alternative to DTD called XML Schema. You can read more about XML Schema in our Schema tutorial.
Errors will Stop you
Errors in XML documents will stop the XML program.
The W3C XML specification states that a program should not continue to process an XML document if it finds a validation error. The reason is that XML software should be easy to write, and that all XML documents should be compatible.
With HTML it was possible to create documents with lots of errors (like when you forget an end tag). One of the main reasons that HTML browsers are so big and incompatible, is that they have their own ways to figure out what a document should look like when they encounter an HTML error.
With XML this should not be possible.
A general XML Validator
To help you validate your xml files, we have created this link so that you can Validate any XML file.
XML Browser Support
In our XML tutorial we have focused on Internet Explorer 5.0+.
XML on this Web Site
Many browsers support XML. We are focusing on Internet Explorer 5.0+.
Some of you have complained about this, but we think this is the most practical way to demonstrate real XML examples for you over the Internet.
XML in Mozilla Firefox 1.0
Mozilla Firefox 1.0 supports the XML 1.0 standard.
XML in Netscape 6
Netscape 6 supports XML.
To look at the XML source in Netscape 6: right-click on the page and select "View Page Source".
XML in Internet Explorer 5.0
Internet Explorer 5.0 supports the XML 1.0 standard.
Internet Explorer 5.0 supports most of the international standards for XML 1.0 and the XML DOM (Document Object Model). These standar 上一页 [1] [2] [3] [4] [5] 下一页 |