Section 8.2. Vocabulary

8.2. Vocabulary

When working with XML documents, you will encounter several terms that might be unfamiliar. The following example shows an XML document that is an XHTML document:

 <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html      PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"      ""> <html xmlns="" xml:lang="en" lang="en">   <head>     <title>XML Example</title>   </head>   <body background="bg.png">     <p>       Moved to <a href=""></a>.       <br />       foo &amp; bar     </p>   </body> </html> 

The first line is the XML declaration; it specifies the XML version and the XML file encoding. Notice that the line starts with <?. This combination of characters can cause a problem if you use this file as a PHP script. If you have the PHP setting short open tags enabled (the default), PHP sees the tag <? as the opening tag of a PHP section. If you work with XML in combination with PHP, change the short_open_tag setting in the php.ini file to Off.

After the XML declaration, you'll find the DOCTYPE declaration on three lines, enclosed by < and >. In this case, the DOCTYPE statement specifies that the root tag in the XML document is html, that the document type is PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN", and that a DTD (Document Type Definition) for this type of document can be found at A DTD file describes the structure of a document type. Validating parsers can use the DTD file to see whether the XML file being parsed is a valid XML file in relation to the given DTD. Not all parsers are validating parsers; some only care that the document is well-formed. A well-formed document conforms to the XML standard (for example, all elements in the document follow the XML specifications). A valid XML document conforms to the DTD associated with the document type, as well as to the XML specifications. To check whether an XHTML (and HTML) document type is valid according to the specified document type, you can use the validator available online at

The rest of the document consists of the content itself, starting with the root element (also called root node):

 <html xmlns="" xml:lang="en" lang="en"> 

According to the XHTML 1.0 Transitional DTD, the root element (html) must contain an xmlns declaration for the XHTML namespace. A namespace provides a means of mixing two separate document types into one XML document, such as embedding MathML into XHTML.

The child elements of the root node follow:

 <head>   <title>XML Example</title> </head> <body background="bg.png">   <p>     Moved to <a href=""></a>.     <br />     foo &amp; bar   </p> </body> 

The head tags (<head> and </head>) enclose the nested title tag that specify the title XML Example.

The body tag includes the background attribute. Attributes contain extra information about a specific tag. XML standards require all attributes to have a value. Values for attributes must be enclosed with single or double quotes. Using one quoting style throughout your document is recommended but not required. In this case, background specifies a background picture to be found in the file bg.png. Another correct attribute is <option selected="true"></option>. Specifying an option with the code <option selected></option> is incorrect by XML standards because the selected attribute has no value.

All opening tags, such as <p>, need a matching closing tag, such as </p>. For elements that have no content, you can merge the opening and closing tag. Instead of using <br></br> in your document, you can use <br/>. Because some browsers may have problems parsing <br/>, add a space before the /, so that the resulting tag is <br />.

Some special characters cause problems in XML documents. For example, < and > are used for tags, so if you use < or > in an XML document, the character is treated as a tag. Entities were developed to enable you to use special characters in your document without using confusing XML. Entities are character combinations, beginning with an ampersand (&) and ending with a semicolon (; ), that you can use in your document instead of special characters. The entity is recognized correctly and not treated as a special character. For instance, you can use &lt; to represent < and &gt; to represent >. When you use the entities, the characters are included in your document correctly and not treated as tags. Entities are also used to input non-ASCII characters into your XML file, for example, ë or . The entities for these two symbols are &euml; and &euro;. For a fairly complete list of entities, see If you want to use the & character itself, of course, you need to use an entity&amp;, as shown in the example XML file.

    PHP 5 Power Programming
    PHP 5 Power Programming
    ISBN: 013147149X
    EAN: 2147483647
    Year: 2003
    Pages: 240

    Similar book on Amazon © 2008-2017.
    If you may any questions please contact us: