XML Tutorial/Introduction/Well Formed — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:26, 26 мая 2010

Case Sensitivity

<first> is different from <FIRST>, which is different from <First>. 
It"s a good idea to pick a naming style and stick to it. 
Some examples of common styles are as follows: 
<first_name> 
<firstName> 
<first-name> 
<FirstName>


Empty Elements

Sometimes an element has no PCDATA. 

<name nickname="007"> 
    <first>James</first> 
    <middle></middle <!--James does not have middle name -->  > 
    <last>Bond</last> 
</name> 
You can write this element using a self-closing tag: 
<middle/> 
The / and > always have to be together
<middle /> or this  <middle/> 
but not like this <middle/ > or this <middle / > 
Empty elements are used for elements with no PCDATA but attributes. 
<name first="Bond" 
      middle="James" 
      last="Last"/>


Nesting Elements

None of your sets of tags should overlap any other set.
Each interior set should be completely enclosed within the next larger set. 
You can nest as many levels of elements as you like.
<animal>
  <animal>
    <name>T1</name>
    <project>project1</project>
    <weight>3 points</weight>
  </animal>
</animal>


Writing Non-Empty Elements

A simple XML element comprises an opening tag, content, and a closing tag whose only difference with the opening tag is an initial forward slash. 
<animal>
    <animal>T1</animal>
</animal>
  
The closing tag is not optional.
The rules for naming regular elements: case matters; names must begin with a letter, underscore or colon; names may contain letters, digits, underscores, hyphens, periods, and colons; colons are generally only used for specifying namespaces; and names that begin with the letters x, m, and l (in any combination of upper-and lowercase) are reserved by the W3C.
Names need not be in English or even the Latin alphabet.
You define which tags are allowed in an XML document by using a schema.


XML documents must adhere to certain rules to be well formed.

Every start-tag must have a matching end-tag, or be a self-closing tag. 
Every element must have a closing tag. 
Empty tags can either use an all-in-one opening and closing tag with a slash before the final > or a separate closing tag.
<?xml version="1.0" ?>
<animal>
    <name>T1</name>
    <picture filename="tiger.jpg"/>
</animal>
Tags can"t overlap and elements must be properly nested. 
XML documents can have only one root element. 
The only pieces of XML allowed outside (preceding) the root element are comments and processing instructions.
<?xml version="1.0" ?>
<animal>
    <name>T1</name>
</animal>
Element names must obey XML naming conventions. 
XML is case sensitive. 
The namel, Name, and NAME elements are considered completely separate and unrelated.
<name>T1</name>
<Name>T1</Name>
<NAME>T1</NAME>


XML Naming Conventions

Names can start with letters including non-Latin characters.
Names can start with dash (-) character.
Names cannot start with numbers or other punctuation characters. 
After the first character, numbers, hyphens, and periods are allowed. 
Names can"t contain spaces. 
Names can"t contain the colon (:) character. 
Names can"t start with the letters xml, in uppercase, lowercase, or mixed.
There can"t be a space after the opening <
There can be space before the closing > character. 
Here are some examples of valid names: <first.name> 
Following are some examples of invalid names: 
<xml-element> which starts with xml, 
<123> which starts with a number, 
<your=xml> because the equals sign (=)sign is illegal, and 
<your element> which contains a space.