XML Example

This is an example of an XML document that used an external DTD file and cascading style sheet (CSS) file.

The XML File

The following file is called "parts.xml".

<?xml version="1.0"?>
<!DOCTYPE PARTS SYSTEM "parts.dtd">
<?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>
<PARTS>
   <TITLE>Computer Parts</TITLE>
   <PART>
      <ITEM>Motherboard</ITEM>
      <MANUFACTURER>ASUS</MANUFACTURER>
      <MODEL>P3B-F</MODEL>
      <COST> 123.00</COST>
   </PART>
   <PART>
      <ITEM>Video Card</ITEM>
      <MANUFACTURER>ATI</MANUFACTURER>
      <MODEL>All-in-Wonder Pro</MODEL>
      <COST> 160.00</COST>
   </PART>
   <PART>
      <ITEM>Sound Card</ITEM>
      <MANUFACTURER>Creative Labs</MANUFACTURER>
      <MODEL>Sound Blaster Live</MODEL>
      <COST> 80.00</COST>
   </PART>
   <PART>
      <ITEMᡋ inch Monitor</ITEM>
      <MANUFACTURER>LG Electronics</MANUFACTURER>
      <MODEL> 995E</MODEL>
      <COST> 290.00</COST>
   </PART>
</PARTS>

This file specifies the use of two external files.

  • A DTD file called "parts.dtd". This is done on the second line:

    <!DOCTYPE PARTS SYSTEM "parts.dtd">



The name after !DOCTYPE which is "PARTS" must be the same name as the encapsulating element in the XML file which is <PARTS> in this case.

  • A CSS file called "xmlpartsstyle.css". This is done on the third line using a processing instruction:

    <?xml-stylesheet type="text/css" href="xmlpartsstyle.css"?>

    The DTD File

    The following file is called "parts.dtd".

    <!ELEMENT PARTS (TITLE?, PART*)>
    <!ELEMENT TITLE (#PCDATA)>
    <!ELEMENT PART (ITEM, MANUFACTURER, MODEL, COST)+>
    <!ATTLIST PART
       type (computer|auto|airplane) #IMPLIED>
    <!ELEMENT ITEM (#PCDATA)>
    <!ELEMENT MANUFACTURER (#PCDATA)>
    <!ELEMENT MODEL (#PCDATA)>
    <!ELEMENT COST (#PCDATA)>
    

    The root element is PARTS. The root element may contain no TITLE element or one TITLE element along with any number of PART elements. The PART element must contain one each of items ITEM, MANUFACTURER, MODEL, and COST in order. The PART element may contain an attribute called "type" which may have a value of "computer", "auto", or "airplane". The elements TITLE, ITEM, MANUFACTURER, MODEL, and COST all contain PCDATA which is parsed character data.

    The Style File

    The following file is used to set the style of the elements in the XML file. It is called "xmlpartstyle.css".

    PARTS 
         { display: block }
    TITLE
         { display: block;
           font-family: arial;
           color: #008000; 
           font-weight: 600; 
           font-size: 22;
           margin-top: 12pt;
           text-align: center }
    PART
         { display: block }
    ITEM
         { display: block;
           font-family: arial;
           color: #000080; 
           font-weight: 400; 
           margin-left: 15pt;
           margin-top: 12pt;
           font-size: 18 }
    MANUFACTURER
         { display: block;
           font-family: arial;
           color: #600060; 
           font-weight: 400; 
           margin-left: 45pt;
           margin-top: 5pt;
           font-size: 18 }
    MODEL
         { display: block;
           font-family: arial;
           color: #006000; 
           font-weight: 400; 
           margin-left: 45pt;
           margin-top: 5pt;
           font-size: 18 }
    COST
         { display: block;
           font-family: arial;
           color: #800000; 
           font-weight: 400; 
           margin-left: 45pt;
           margin-top: 5pt;
           font-size: 18 }
    

    How it Displays on your browser

    If you click on the following link and the loaded document does not display in color, your browser probably does not support XML. In this case, you should update your browser to the most recent version.

    Click Here to see the PARTS.XML file (USE your browser's BACK button to return).