How Is XML Structured?

I l @ ve RuBoard

When you define XML languages and create XML documents, you must follow a few rules. DTD documents help you keep your XML documents consistent, but a few more rules must be followed.

Always Remember the Header

In all the examples, you might have noticed the special XML header:

 <?xml version="1.0" encoding="UTF-8"?> 

This is always required. It tells the parser what version of XML and what encoding you are using so that the parser can display the XML document correctly. If you don't include the header, the parser returns an error. XML encoding is required in XML documents. XML supports two types ”UTF-8 and UTF-16. When you set XML encoding, you tell the XML parser what encoding type to use to encode the XML document into a format the computer processor will understand. (A computer processor understands only numbers , so you use encoding to state how all the characters in the XML relate to their numeric counterparts.) Encoding differs depending on what encoding type you set. In other words, the letter-to-number encoding differs depending on whether you use UTF-8 or UTF-16.

Always Close Nodes

XML languages always need a close tag. Unlike, say, HTML, which can have single tags as well as open and close tags, XML tags always require an open tag and a close tag. For example, our XML language has the NAME tag, which we open with <NAME> and close with </NAME> .

Note

If XML doesn't have tags within tags, do you always need a close tag? In theory, yes, but you can use a shorthand syntax. For example, in HTML, the <IMG> tag has no tags within it. However, the XHTML version of the tag still needs a close tag. Thus, you can use <IMG/> , a shorthand form of a close tag. Also note that XML is case-sensitive, so the correct syntax would be <img/> . Furthermore, some browsers require a space before the ending slash, or they won't recognize the tag, so it's always good form to write it as <img /> .


Never Cross Nodes

XML tags must never cross one another. For example, the following would be invalid:

 <?xml version="1.0" encoding="UTF-8"?>  <PEOPLE>       <PERSON>            <NAME>Andrew</PERSON></NAME>  </PEOPLE> 

Here, the PEOPLE node is closed before the NAME node is closed. In theory, you can get away with this without the parser's returning an error, but you will get unexpected results when you try to work with the XML document. You can enforce the rule about not crossing nodes within the DTD document ” another reason why it is important to have one.

I l @ ve RuBoard


PHP Programming for Windows
PHP Programming for Windows (Landmark (New Riders))
ISBN: 0735711690
EAN: 2147483647
Year: 2002
Pages: 99

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net