Web Development Tutorials




  
  

Valid XML Documents

After you have created an XML document and confirmed that it is "well formed", the next step is to link the XML file to a DTD to ensure that the XML document is valid. The purpose of this section is to show how to assign a DTD to an XML file. In addition, several examples will be presented to further demonstrate the concepts presented in the preceeding sections.

A DTD can be assigned to an XML file in two ways. The DTD can be declared or coded inline inside of the XML file, or the DTD can be declared or coded externally and then linked to the XML document.

Below is the format for an inline DTD:

<!DOCTYPE root-element [element-declarations]>

Listing 2-10. Inline DTD.

An inline DTD is assigned to an XML file below:

employees.xml

<?xml version="1.0"?>

    <!DOCTYPE Employee [
    <!ELEMENT Employee (SSN,FirstName,LastName,Salary,Department)>
    <!ELEMENT SSN (#PCDATA)>
    <!ELEMENT FirstName (#PCDATA)>
    <!ELEMENT LastName (#PCDATA)>
    <!ELEMENT Salary (#PCDATA)>
    <!ELEMENT Department(#PCDATA)>

    <!ATTLIST Employee id CDATA>
    ]>

    <Employee id="1111">
        <SSN>111-11-1111</SSN>
        <FirstName>Ann</FirstName>
        <LastName>Adams</LastName>
        <Salary>65000.00</Salary>
        <Department>Accounting</Department>
    </Employee>

Listing 2-11. Inline DTD assigned.

The inline DTD is coded below the XML declaration <?xml version="1.0"?>. First, !DOCTYPE Employee defines that the root element of this document is Employee. Next, !ELEMENT Employee defines that the Employee element contains four child elements: "SSN, FirstName, LastName, Salary, and Department". Third, all of the child elements are defined and designated type #PCDATA. Finally, id is defined as an attribute of the Employee element.

The final step is to test whether the XML document is valid or conforms to the rules specified in the DTD. This can be done using any XML editor program.

In most cases, it is best to separate the XML from the DTD. This can be done using an external DTD declaration. The general format is shown below:

<!DOCTYPE root-element SYSTEM "filename">

Listing 2-12. External DTD declaration.

The example above is presented again using an external DTD.

employees.xml

<?xml version="1.0"?>

<!DOCTYPE Employee SYSTEM "employees.dtd">

    <Employee id="1111">
        <SSN>111-11-1111</SSN>
        <FirstName>Ann</FirstName>
        <LastName>Adams</LastName>
        <Salary>65000.00</Salary>
        <Department>Accounting</Department>
    </Employee>

Listing 2-13. External DTD example (XML).

employees.dtd

<!ELEMENT Employee (SSN,FirstName,LastName,Salary,Department)>
<!ELEMENT SSN (#PCDATA)>
<!ELEMENT FirstName (#PCDATA)>
<!ELEMENT LastName (#PCDATA)>
<!ELEMENT Salary (#PCDATA)>
<!ELEMENT Department(#PCDATA)>

<!ATTLIST Employee id CDATA>

Listing 2-14. External DTD example (DTD).

Here the XML is separate from the DTD. They are linked together by the external declaration <!DOCTYPE Employee SYSTEM "employees.dtd">.


TOP | NEXT: Chapter 3 - Schemas