Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
216 views

XML Examples 1

XML Examples - How to Use XML Viewers, Parsers and Formatting. Many of the samples below rely on client-side XML handling and were written to utilize functionality implemented in Internet Explorer 5 and above. To browse these examples as they were designed to be seen, you should ideally be using Microsoft Internet Explorer 5 or later.

Uploaded by

NiamatKhan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
216 views

XML Examples 1

XML Examples - How to Use XML Viewers, Parsers and Formatting. Many of the samples below rely on client-side XML handling and were written to utilize functionality implemented in Internet Explorer 5 and above. To browse these examples as they were designed to be seen, you should ideally be using Microsoft Internet Explorer 5 or later.

Uploaded by

NiamatKhan
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 26

XML Examples - How to Use XML Viewers, Parsers and Formatting

Note: Many of the samples below rely on client-side XML handling and were written to utilize functionality implemented in Internet Explorer 5 and above. To browse these examples as they were designed to be seen, you should ideally be using Microsoft Internet Explorer 5 or later.

This rich notes file covers following tasks with XML: Viewing XML Files
These examples are taken from the chapter Viewing XML Files in the XML Section of this Web. A Note by Jan Egil Refsnes An XML CD catalog by Jan Egil Refsnes An XML plant catalog by Jan Egil Refsnes A Simple Menu by Jan Egil Refsnes

Viewing XML Files with a DTD


These examples are taken from the chapter Introduction to DTD. XML file with an internal DTD by Jan Egil Refsnes XML file with an external DTD by Jan Egil Refsnes

Using the Microsoft XML Parser


This example is taken from the chapter Parsing the DOM in the DOM Section of this Web. Loading XML into the Parser by Jan Egil Refsnes These examples are taken from the chapter Accessing the DOM in the DOM Section of this Web. Traversing the XML Nodes by Jan Egil Refsnes Loading XML into HTML by Jan Egil Refsnes

Formatting XML with JavaScript


These examples are taken from the chapter Displaying XML in the XML Section of this Web. An XML Note by Jan Egil Refsnes Formatted by JavaScript by Jan Egil Refsnes Try It Yourself by Jan Egil Refsnes

Formatting XML with CSS


These examples are taken from the chapter Displaying XML in the XML Section of this Web. An XML CD Catalog by Jan Egil Refsnes A CSS file by Jan Egil Refsnes The CD Catalog formatted with the CSS file by Jan Egil Refsnes

Formatting XML with XSL


These examples are taken from the chapter Displaying XML in the XML Section of this Web. A Simple XML Menu by Jan Egil Refsnes Formatted with XSL by the Browser by Jan Egil Refsnes Formatted with XSL by the Server by Jan Egil Refsnes

Transforming XML documents


These examples are taken from the chapter The XML XSL in the XSL Section of this Web. The XML file to be transformed by Jan Egil Refsnes The XSL template by Jan Egil Refsnes The Result by Jan Egil Refsnes

Navigating an XML file


These examples are taken from the chapter XML Applications in the XML Applications Section of this Web. The XML file to navigate by Jan Egil Refsnes Displaying the file as a recordset in a table by Jan Egil Refsnes

Navigate the recordset by Jan Egil Refsnes An application prototype by Jan Egil Refsnes

Requesting XML data from a server


These examples are taken from the chapter XML Applications in the XML Application Section of this Web. Sending a HTTP Request by Jan Egil Refsnes Communicating with a server using XML by Jan Egil Refsnes Try It Yourself by Jan Egil Refsnes

http://www.xmlfiles.com/examples/

A CD catalog as XML file


This XML file does not appear to have any style information associated with it. The document tree is shown below.
<CATALOG> <CD> <TITLE>Empire Burlesque</TITLE> <ARTIST>Bob Dylan</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>10.90</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Hide your heart</TITLE> <ARTIST>Bonnie Tylor</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS Records</COMPANY> <PRICE>9.90</PRICE> <YEAR>1988</YEAR> </CD> <CD> <TITLE>Greatest Hits</TITLE> <ARTIST>Dolly Parton</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>RCA</COMPANY> <PRICE>9.90</PRICE> <YEAR>1982</YEAR> </CD> <CD> <TITLE>Still got the blues</TITLE> <ARTIST>Gary More</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Virgin redords</COMPANY>

<PRICE>10.20</PRICE> <YEAR>1990</YEAR> </CD> <CD> <TITLE>Eros</TITLE> <ARTIST>Eros Ramazzotti</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>BMG</COMPANY> <PRICE>9.90</PRICE> <YEAR>1997</YEAR> </CD> <CD> <TITLE>One night only</TITLE> <ARTIST>Bee Gees</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Polydor</COMPANY> <PRICE>10.90</PRICE> <YEAR>1998</YEAR> </CD> <CD> <TITLE>Sylvias Mother</TITLE> <ARTIST>Dr.Hook</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>CBS</COMPANY> <PRICE>8.10</PRICE> <YEAR>1973</YEAR> </CD> <CD> <TITLE>Maggie May</TITLE> <ARTIST>Rod Stewart</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Pickwick</COMPANY> <PRICE>8.50</PRICE> <YEAR>1990</YEAR> </CD> <CD> <TITLE>Romanza</TITLE> <ARTIST>Andrea Bocelli</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Polydor</COMPANY> <PRICE>10.80</PRICE> <YEAR>1996</YEAR> </CD> <CD> <TITLE>When a man loves a woman</TITLE> <ARTIST>Percy Sledge</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Atlantic</COMPANY> <PRICE>8.70</PRICE> <YEAR>1987</YEAR> </CD> <CD> <TITLE>Black angel</TITLE> <ARTIST>Savage Rose</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Mega</COMPANY> <PRICE>10.90</PRICE>

<YEAR>1995</YEAR> </CD> <CD> <TITLE>1999 Grammy Nominees</TITLE> <ARTIST>Many</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Grammy</COMPANY> <PRICE>10.20</PRICE> <YEAR>1999</YEAR> </CD> <CD> <TITLE>For the good times</TITLE> <ARTIST>Kenny Rogers</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Mucik Master</COMPANY> <PRICE>8.70</PRICE> <YEAR>1995</YEAR> </CD> <CD> <TITLE>Big Willie style</TITLE> <ARTIST>Will Smith</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Columbia</COMPANY> <PRICE>9.90</PRICE> <YEAR>1997</YEAR> </CD> <CD> <TITLE>Tupelo Honey</TITLE> <ARTIST>Van Morrison</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Polydor</COMPANY> <PRICE>8.20</PRICE> <YEAR>1971</YEAR> </CD> <CD> <TITLE>Soulsville</TITLE> <ARTIST>Jorn Hoel</ARTIST> <COUNTRY>Norway</COUNTRY> <COMPANY>WEA</COMPANY> <PRICE>7.90</PRICE> <YEAR>1996</YEAR> </CD> <CD> <TITLE>The very best of</TITLE> <ARTIST>Cat Stevens</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Island</COMPANY> <PRICE>8.90</PRICE> <YEAR>1990</YEAR> </CD> <CD> <TITLE>Stop</TITLE> <ARTIST>Sam Brown</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>A and M</COMPANY> <PRICE>8.90</PRICE> <YEAR>1988</YEAR>

</CD> <CD> <TITLE>Bridge of Spies</TITLE> <ARTIST>T`Pau</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Siren</COMPANY> <PRICE>7.90</PRICE> <YEAR>1987</YEAR> </CD> <CD> <TITLE>Private Dancer</TITLE> <ARTIST>Tina Turner</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>Capitol</COMPANY> <PRICE>8.90</PRICE> <YEAR>1983</YEAR> </CD> <CD> <TITLE>Midt om natten</TITLE> <ARTIST>Kim Larsen</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Medley</COMPANY> <PRICE>7.80</PRICE> <YEAR>1983</YEAR> </CD> <CD> <TITLE>Pavarotti Gala Concert</TITLE> <ARTIST>Luciano Pavarotti</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>DECCA</COMPANY> <PRICE>9.90</PRICE> <YEAR>1991</YEAR> </CD> <CD> <TITLE>The dock of the bay</TITLE> <ARTIST>Otis Redding</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>Atlantic</COMPANY> <PRICE>7.90</PRICE> <YEAR>1987</YEAR> </CD> <CD> <TITLE>Picture book</TITLE> <ARTIST>Simply Red</ARTIST> <COUNTRY>EU</COUNTRY> <COMPANY>Elektra</COMPANY> <PRICE>7.20</PRICE> <YEAR>1985</YEAR> </CD> <CD> <TITLE>Red</TITLE> <ARTIST>The Communards</ARTIST> <COUNTRY>UK</COUNTRY> <COMPANY>London</COMPANY> <PRICE>7.80</PRICE> <YEAR>1987</YEAR> </CD>

<CD> <TITLE>Unchain my heart</TITLE> <ARTIST>Joe Cocker</ARTIST> <COUNTRY>USA</COUNTRY> <COMPANY>EMI</COMPANY> <PRICE>8.20</PRICE> <YEAR>1987</YEAR> </CD> </CATALOG>

An xml plant catelog


This XML file does not appear to have any style information associated with it. The document tree is shown below.
<CATALOG> <PLANT> <COMMON>Bloodroot</COMMON> <BOTANICAL>Sanguinaria canadensis</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$2.44</PRICE> <AVAILABILITY>031599</AVAILABILITY> </PLANT> <PLANT> <COMMON>Columbine</COMMON> <BOTANICAL>Aquilegia canadensis</BOTANICAL> <ZONE>3</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$9.37</PRICE> <AVAILABILITY>030699</AVAILABILITY> </PLANT> <PLANT> <COMMON>Marsh Marigold</COMMON> <BOTANICAL>Caltha palustris</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Mostly Sunny</LIGHT> <PRICE>$6.81</PRICE> <AVAILABILITY>051799</AVAILABILITY> </PLANT> <PLANT> <COMMON>Cowslip</COMMON> <BOTANICAL>Caltha palustris</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$9.90</PRICE> <AVAILABILITY>030699</AVAILABILITY> </PLANT> <PLANT> <COMMON>Dutchman's-Breeches</COMMON> <BOTANICAL>Diecentra cucullaria</BOTANICAL> <ZONE>3</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$6.44</PRICE>

<AVAILABILITY>012099</AVAILABILITY> </PLANT> <PLANT> <COMMON>Ginger, Wild</COMMON> <BOTANICAL>Asarum canadense</BOTANICAL> <ZONE>3</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$9.03</PRICE> <AVAILABILITY>041899</AVAILABILITY> </PLANT> <PLANT> <COMMON>Hepatica</COMMON> <BOTANICAL>Hepatica americana</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$4.45</PRICE> <AVAILABILITY>012699</AVAILABILITY> </PLANT> <PLANT> <COMMON>Liverleaf</COMMON> <BOTANICAL>Hepatica americana</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$3.99</PRICE> <AVAILABILITY>010299</AVAILABILITY> </PLANT> <PLANT> <COMMON>Jack-In-The-Pulpit</COMMON> <BOTANICAL>Arisaema triphyllum</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$3.23</PRICE> <AVAILABILITY>020199</AVAILABILITY> </PLANT> <PLANT> <COMMON>Mayapple</COMMON> <BOTANICAL>Podophyllum peltatum</BOTANICAL> <ZONE>3</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$2.98</PRICE> <AVAILABILITY>060599</AVAILABILITY> </PLANT> <PLANT> <COMMON>Phlox, Woodland</COMMON> <BOTANICAL>Phlox divaricata</BOTANICAL> <ZONE>3</ZONE> <LIGHT>Sun or Shade</LIGHT> <PRICE>$2.80</PRICE> <AVAILABILITY>012299</AVAILABILITY> </PLANT> <PLANT> <COMMON>Phlox, Blue</COMMON> <BOTANICAL>Phlox divaricata</BOTANICAL> <ZONE>3</ZONE> <LIGHT>Sun or Shade</LIGHT> <PRICE>$5.59</PRICE> <AVAILABILITY>021699</AVAILABILITY>

</PLANT> <PLANT> <COMMON>Spring-Beauty</COMMON> <BOTANICAL>Claytonia Virginica</BOTANICAL> <ZONE>7</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$6.59</PRICE> <AVAILABILITY>020199</AVAILABILITY> </PLANT> <PLANT> <COMMON>Trillium</COMMON> <BOTANICAL>Trillium grandiflorum</BOTANICAL> <ZONE>5</ZONE> <LIGHT>Sun or Shade</LIGHT> <PRICE>$3.90</PRICE> <AVAILABILITY>042999</AVAILABILITY> </PLANT> <PLANT> <COMMON>Wake Robin</COMMON> <BOTANICAL>Trillium grandiflorum</BOTANICAL> <ZONE>5</ZONE> <LIGHT>Sun or Shade</LIGHT> <PRICE>$3.20</PRICE> <AVAILABILITY>022199</AVAILABILITY> </PLANT> <PLANT> <COMMON>Violet, Dog-Tooth</COMMON> <BOTANICAL>Erythronium americanum</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$9.04</PRICE> <AVAILABILITY>020199</AVAILABILITY> </PLANT> <PLANT> <COMMON>Trout Lily</COMMON> <BOTANICAL>Erythronium americanum</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$6.94</PRICE> <AVAILABILITY>032499</AVAILABILITY> </PLANT> <PLANT> <COMMON>Adder's-Tongue</COMMON> <BOTANICAL>Erythronium americanum</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$9.58</PRICE> <AVAILABILITY>041399</AVAILABILITY> </PLANT> <PLANT> <COMMON>Anemone</COMMON> <BOTANICAL>Anemone blanda</BOTANICAL> <ZONE>6</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$8.86</PRICE> <AVAILABILITY>122698</AVAILABILITY> </PLANT>

<PLANT> <COMMON>Grecian Windflower</COMMON> <BOTANICAL>Anemone blanda</BOTANICAL> <ZONE>6</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$9.16</PRICE> <AVAILABILITY>071099</AVAILABILITY> </PLANT> <PLANT> <COMMON>Bee Balm</COMMON> <BOTANICAL>Monarda didyma</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$4.59</PRICE> <AVAILABILITY>050399</AVAILABILITY> </PLANT> <PLANT> <COMMON>Bergamont</COMMON> <BOTANICAL>Monarda didyma</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$7.16</PRICE> <AVAILABILITY>042799</AVAILABILITY> </PLANT> <PLANT> <COMMON>Black-Eyed Susan</COMMON> <BOTANICAL>Rudbeckia hirta</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Sunny</LIGHT> <PRICE>$9.80</PRICE> <AVAILABILITY>061899</AVAILABILITY> </PLANT> <PLANT> <COMMON>Buttercup</COMMON> <BOTANICAL>Ranunculus</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$2.57</PRICE> <AVAILABILITY>061099</AVAILABILITY> </PLANT> <PLANT> <COMMON>Crowfoot</COMMON> <BOTANICAL>Ranunculus</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$9.34</PRICE> <AVAILABILITY>040399</AVAILABILITY> </PLANT> <PLANT> <COMMON>Butterfly Weed</COMMON> <BOTANICAL>Asclepias tuberosa</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Sunny</LIGHT> <PRICE>$2.78</PRICE> <AVAILABILITY>063099</AVAILABILITY> </PLANT> <PLANT>

<COMMON>Cinquefoil</COMMON> <BOTANICAL>Potentilla</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$7.06</PRICE> <AVAILABILITY>052599</AVAILABILITY> </PLANT> <PLANT> <COMMON>Primrose</COMMON> <BOTANICAL>Oenothera</BOTANICAL> <ZONE>3 - 5</ZONE> <LIGHT>Sunny</LIGHT> <PRICE>$6.56</PRICE> <AVAILABILITY>013099</AVAILABILITY> </PLANT> <PLANT> <COMMON>Gentian</COMMON> <BOTANICAL>Gentiana</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Sun or Shade</LIGHT> <PRICE>$7.81</PRICE> <AVAILABILITY>051899</AVAILABILITY> </PLANT> <PLANT> <COMMON>Blue Gentian</COMMON> <BOTANICAL>Gentiana</BOTANICAL> <ZONE>4</ZONE> <LIGHT>Sun or Shade</LIGHT> <PRICE>$8.56</PRICE> <AVAILABILITY>050299</AVAILABILITY> </PLANT> <PLANT> <COMMON>Jacob's Ladder</COMMON> <BOTANICAL>Polemonium caeruleum</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$9.26</PRICE> <AVAILABILITY>022199</AVAILABILITY> </PLANT> <PLANT> <COMMON>Greek Valerian</COMMON> <BOTANICAL>Polemonium caeruleum</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$4.36</PRICE> <AVAILABILITY>071499</AVAILABILITY> </PLANT> <PLANT> <COMMON>California Poppy</COMMON> <BOTANICAL>Eschscholzia californica</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Sun</LIGHT> <PRICE>$7.89</PRICE> <AVAILABILITY>032799</AVAILABILITY> </PLANT> <PLANT> <COMMON>Shooting Star</COMMON>

<BOTANICAL>Dodecatheon</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Mostly Shady</LIGHT> <PRICE>$8.60</PRICE> <AVAILABILITY>051399</AVAILABILITY> </PLANT> <PLANT> <COMMON>Snakeroot</COMMON> <BOTANICAL>Cimicifuga</BOTANICAL> <ZONE>Annual</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$5.63</PRICE> <AVAILABILITY>071199</AVAILABILITY> </PLANT> <PLANT> <COMMON>Cardinal Flower</COMMON> <BOTANICAL>Lobelia cardinalis</BOTANICAL> <ZONE>2</ZONE> <LIGHT>Shade</LIGHT> <PRICE>$3.02</PRICE> <AVAILABILITY>022299</AVAILABILITY> </PLANT> </CATALOG>

A SIMPLE MENU IN XML FILE


This XML file does not appear to have any style information associated with it. The document tree is shown below.
<breakfast-menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description> two of our famous Belgian Waffles with plenty of real maple syrup </description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description> light Belgian waffles covered with strawberrys and whipped cream </description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description> light Belgian waffles covered with an assortment of fresh berries and whipped cream </description> <calories>900</calories> </food> <food>

<name>French Toast</name> <price>$4.50</price> <description> thick slices made from our homemade sourdough bread </description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description> two eggs, bacon or sausage, toast, and our ever-popular hash browns </description> <calories>950</calories> </food> </breakfast-menu>

Introduction to DTD (Document Type Definition)


The purpose of a DTD is to define the legal building blocks of an XML document. It defines the document structure with a list of legal elements. A DTD can be declared inline in your XML document, or as an external reference.

Internal DTD
This is an XML document with a Document Type Definition: (Open it in IE5, and select view source)

<?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)> ]> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>

CDATAA=Character-DATA: That does not need parsing. Just text is picked as it appears. PCDATA= Parsed Character DATA: Text is between open/close tags and parser is required to separate the text from TAGS.

#CDATA means the element contains character data that is not supposed to be parsed by a parser.

#PCDATA means that the element contains data that IS going to be parsed by a parser.
Line: <!ELEMENT note (to,from,heading,body)> defines total number of child nodes to expect under parent node: e.g 4 elements

The DTD is interpreted like this: !ELEMENT note (in line 2) defines the element "note" as having four elements: "to,from,heading,body". !ELEMENT to (in line 3) defines the "to" element to be of the type "CDATA". !ELEMENT from (in line 4) defines the "from" element to be of the type "CDATA" and so on..... NOTE :: Public class CDATA extends text. An XML CDATA section. Represents
character-based content within an XML document that should be output within special CDATA tags.

External DTD
This is the same XML document with an external DTD: (Open it in IE5, and select view source)

<?xml version="1.0"?> <!DOCTYPE note SYSTEM "note.dtd"> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend!</body> </note>
This is a copy of the file "note.dtd" containing the Document Type Definition:

<?xml version="1.0"?> <!ELEMENT note (to,from,heading,body)> <!ELEMENT to (#PCDATA)> <!ELEMENT from (#PCDATA)> <!ELEMENT heading (#PCDATA)> <!ELEMENT body (#PCDATA)>

Why use a DTD?


XML provides an application independent way of sharing data. With a DTD, independent groups of people can agree to use a common DTD for interchanging data. Your application can use a standard DTD to verify that data that you receive from the outside world is valid. You can also use a DTD to verify your own data.A lot of forums are emerging to define standard DTDs for almost everything in the areas of data exchange.

The building blocks of XML documents


XML documents (and HTML documents) are made up by the following building blocks:

Elements, Tags, Attributes, Entities, PCDATA, and CDATA


This is a brief explanation of each of the building blocks:

Elements
Elements are the main building blocks of both XML and HTML documents. Examples of HTML elements are "body" and "table". Examples of XML elements could be "note" and "message". Elements can contain text, other elements, or be empty. Examples of empty HTML elements are "hr", "br" and "img".

Tags
Tags are used to markup elements. A starting tag like <element_name> mark up the beginning of an element, and an ending tag like </element_name> mark up the end of an element. Examples: A body element: <body>body text in between</body>. A message element: <message>some message in between</message>

Attributes
Attributes provide extra information about elements. Attributes are placed inside the start tag of an element. Attributes come in name/value pairs. The following "img" element has an additional information about a source file:

<img src="computer.gif" />


The name of the element is "img". The name of the attribute is "src". The value of the attribute is "computer.gif". Since the element itself is empty it is closed by a " /".

PCDATA
PCDATA means parsed character data. Think of character data as the text found between the start tag and the end tag of an XML element. PCDATA is text that will be parsed by a parser. Tags inside the text will be treated as markup and entities will be expanded.

CDATA
CDATA also means character data. CDATA is text that will NOT be parsed by a parser. Tags inside the text will NOT be treated as markup and entities will not be expanded.

Entities
Entities as variables used to define common text. Entity references are references to entities.
Most of you will known the HTML entity reference: "&nbsp;" that is used to insert an extra space in an HTML document. Entities are expanded when a document is parsed by an XML parser. The following entities are predefined in XML: Entity References Character

&lt; &gt; &amp; &quot; &apos;

< > & " '

DTD - Elements
by Jan Egil Refsnes

Declaring an Element
In the DTD, XML elements are declared with an element declaration. An element declaration has the following syntax:

<!ELEMENT element-name (element-content)>

Empty elements
Empty elements are declared with the keyword EMPTY inside the parentheses:

<!ELEMENT element-name (EMPTY)> example: <!ELEMENT img (EMPTY)>

Elements with data


Elements with data are declared with the data type inside parentheses:

<!ELEMENT or <!ELEMENT or <!ELEMENT example: <!ELEMENT

element-name (#CDATA)> element-name (#PCDATA)> element-name (ANY)> note (#PCDATA)>

#CDATA means the element contains character data that is not supposed to be parsed by a parser. #PCDATA means that the element contains data that IS going to be parsed by a parser. The keyword ANY declares an element with any content. If a #PCDATA section contains elements, these elements must also be declared.

Elements with children (sequences)


Elements with one or more children are defined with the name of the children elements inside the parentheses:

<!ELEMENT element-name (child-element-name)> or <!ELEMENT element-name (child-element-name,child-element-name,.....)> example: <!ELEMENT note (to,from,heading,body)>
When children are declared in a sequence separated by commas, the children must appear in the same sequence in the document. In a full declaration, the children must also be declared, and the children can also have children. The full declaration of the note document will be:

<!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT

note (to,from,heading,body)> to (#CDATA)> from (#CDATA)> heading (#CDATA)> body (#CDATA)>

Wrapping
If the DTD is to be included in your XML source file, it should be wrapped in a DOCTYPE definition with the following syntax:

<!DOCTYPE root-element [element-declarations]> example: <?xml version="1.0"?> <!DOCTYPE note [ <!ELEMENT note (to,from,heading,body)>

]> <note> <to>Tove</to> <from>Jani</from> <heading>Reminder</heading> <body>Don't forget me this weekend</body> </note>

<!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT

to from heading body

(#CDATA)> (#CDATA)> (#CDATA)> (#CDATA)>

Declaring only one occurrence of the same element


<!ELEMENT element-name (child-name)> example <!ELEMENT note (message)>
The example declaration above declares that the child element message can only occur one time inside the note element.

Declaring minimum one occurrence of the same element


<!ELEMENT element-name (child-name+)> example <!ELEMENT note (message+)>

The + sign in the example above declares that the child element message must occur one or more times inside the note element.

Declaring zero or more occurrences of the same element


<!ELEMENT element-name (child-name*)> example <!ELEMENT note (message*)>

The * sign in the example above declares that the child element message can occur zero or more
times inside the note element.

Declaring zero or one occurrences of the same element


<!ELEMENT element-name (child-name?)> example <!ELEMENT note (message?)>

The ? sign in the example above declares that the child element message can occur zero or one times
inside the note element.

Declaring mixed content


example <!ELEMENT note (to+,from,header,message*,#PCDATA)>

The example above declares that the element note must contain at least one to child element, exactly one from child element, exactly one header, zero or more message, and some other parsed character data as well. Puh!

DTD - Attributes
by Jan Egil Refsnes

Declaring Attributes
In the DTD, XML element attributes are declared with an ATTLIST declaration. An attribute declaration has the following syntax:

<!ATTLIST element-name attribute-name attribute-type default-value>


As you can see from the syntax above, the ATTLIST declaration defines the element which can have the attribute, the name of the attribute, the type of the attribute, and the default attribute value. The attribute-type can have the following values: Value Explanation

CDATA ID IDREF IDREFS NMTOKEN NMTOKENS ENTITY ENTITIES NOTATION xml:

The value is character data The value is an unique id The value is the id of another element The value is a list of other ids The value is a valid XML name The value is a list of valid XML names The value is an entity The value is a list of entities The value is a name of a notation The value is predefined

(eval|eval|..) The value must be an enumerated value

The attribute-default-value can have the following values: Value Explanation

#DEFAULT value #REQUIRED #IMPLIED #FIXED value

The attribute has a default value The attribute value must be included in the element The attribute does not have to be included The attribute value is fixed

Attribute declaration example


DTD example: <!ELEMENT square EMPTY> <!ATTLIST square width CDATA "0"> XML example: <square width="100"></square>
In the above example the element square is defined to be an empty element with the attributes width of type CDATA. The width attribute has a default value of 0.

Default attribute value


Syntax: <!ATTLIST element-name attribute-name CDATA "default-value"> DTD example: <!ATTLIST payment type CDATA "check"> XML example: <payment type="check">
Specifying a default value for an attribute, assures that the attribute will get a value even if the author of the XML document didn't include it.

Implied attribute
Syntax: <!ATTLIST element-name attribute-name attribute-type #IMPLIED> DTD example: <!ATTLIST contact fax CDATA #IMPLIED> XML example: <contact fax="555-667788">
Use an implied attribute if you don't want to force the author to include an attribute and you don't have an option for a default value either.

Required attribute
Syntax: <!ATTLIST element-name attribute_name attribute-type #REQUIRED> DTD example: <!ATTLIST person number CDATA #REQUIRED> XML example: <person number="5677">

Use a required attribute if you don't have an option for a default value, but still want to force the attribute to be present.

Fixed attribute value


Syntax: <!ATTLIST element-name attribute-name attribute-type #FIXED "value"> DTD example: <!ATTLIST sender company CDATA #FIXED "Microsoft"> XML example: <sender company="Microsoft">
Use a fixed attribute value when you want an attribute to have a fixed value without allowing the author to change it. If an author includes another value, the XML parser will return an error.

Enumerated attribute values


Syntax: <!ATTLIST element-name attribute-name (eval|eval|..) default-value> DTD example: <!ATTLIST payment type (check|cash) "cash"> XML example: <payment type="check"> or <payment type="cash">
Use enumerated attribute values when you want the attribute values to be one of a fixed set of legal values.

DTD - Entities
by Jan Egil Refsnes

Entities

Entities as variables used to define shortcuts to common text. Entity references are references to entities. Entities can be declared internal. Entities can be declared external

Internal Entity Declaration


Syntax: <!ENTITY entity-name "entity-value">

DTD Example: <!ENTITY writer "Jan Egil Refsnes."> <!ENTITY copyright "Copyright XML101."> XML example: <author>&writer;&copyright;</author>

External Entity Declaration


Syntax: <!ENTITY entity-name SYSTEM "URI/URL"> DTD Example: <!ENTITY writer SYSTEM "http://www.xml101.com/entities/entities.xml"> <!ENTITY copyright SYSTEM "http://www.xml101.com/entities/entities.dtd"> XML example: <author>&writer;&copyright;</author>

DTD Validation
by Jan Egil Refsnes

Validating with the XML Parser


If you try to open an XML document, the XML Parser might generate an error. By accessing the parseError object, the exact error code, the error text, and even the line that caused the error can be retrieved:

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.validateOnParse="true" xmlDoc.load("note_dtd_error.xml") document.write("<br>Error Code: ") document.write(xmlDoc.parseError.errorCode) document.write("<br>Error Reason: ") document.write(xmlDoc.parseError.reason) document.write("<br>Error Line: ") document.write(xmlDoc.parseError.line)
Try it Yourself or or just look at the XML file

note> <to>Tove</to> <fromm>Jani</fromm> <heading>Reminder</heading> <body>Don't forget me this weekend!</body>

<ps>I love you</ps> </note>

Turning Validation off


Validation can be turned off by setting the XML parser's validateOnParse="false".

var xmlDoc = new ActiveXObject("Microsoft.XMLDOM") xmlDoc.async="false" xmlDoc.validateOnParse="false" xmlDoc.load("note_dtd_error.xml") document.write("<br>Error Code: ") document.write(xmlDoc.parseError.errorCode) document.write("<br>Error Reason: ") document.write(xmlDoc.parseError.reason) document.write("<br>Error Line: ") document.write(xmlDoc.parseError.line)
Try it Yourself

DTD - Examples from the Net


by Jan Egil Refsnes

TV Scedule DTD
By David Moisan. Copied from his Web: http://www1.shore.net/~dmoisan/

<!DOCTYPE TVSCHEDULE [ <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT TVSCHEDULE (CHANNEL+)> CHANNEL (BANNER, DAY+)> BANNER (#PCDATA)> DAY ((DATE, HOLIDAY) | (DATE, PROGRAMSLOT+))+> HOLIDAY (#PCDATA)> DATE (#PCDATA)> PROGRAMSLOT (TIME, TITLE, DESCRIPTION?)> TIME (#PCDATA)>

<!ELEMENT TITLE (#PCDATA)> <!ELEMENT DESCRIPTION (#PCDATA)> <!ATTLIST <!ATTLIST <!ATTLIST <!ATTLIST <!ATTLIST ]> TVSCHEDULE NAME CDATA #REQUIRED> CHANNEL CHAN CDATA #REQUIRED> PROGRAMSLOT VTR CDATA #IMPLIED> TITLE RATING CDATA #IMPLIED> TITLE LANGUAGE CDATA #IMPLIED>

A Report DTD
By Richard Erlander. Copied from his Web: http://pdbeam.uwaterloo.ca/~rlander/

<!DOCTYPE REPORT [ <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT REPORT (TITLE,(SECTION|SHORTSECT)+)> SECTION (TITLE,%BODY;,SUBSECTION*)> SUBSECTION (TITLE,%BODY;,SUBSECTION*)> SHORTSECT (TITLE,%BODY;)> TITLE %TEXT;> PARA %TEXT;> LIST (ITEM)+> ITEM (%BLOCK;)> CODE (#PCDATA)> KEYWORD (#PCDATA)> EXAMPLE (TITLE?,%BLOCK;)> GRAPHIC EMPTY>

<!ATTLIST REPORT security (high | medium | low ) "low"> <!ATTLIST CODE type CDATA #IMPLIED> <!ATTLIST GRAPHIC file ENTITY #REQUIRED> <!ENTITY <!ENTITY <!ENTITY <!ENTITY <!ENTITY <!ENTITY xml "Extensible Markup Language"> sgml "Standard Generalized Markup Language"> pxa "Professional XML Authoring"> % TEXT "(#PCDATA|CODE|KEYWORD|QUOTATION)*"> % BLOCK "(PARA|LIST)+"> % BODY "(%BLOCK;|EXAMPLE|NOTE)+">

<!NOTATION GIF SYSTEM ""> <!NOTATION JPG SYSTEM ""> <!NOTATION BMP SYSTEM ""> ]>

Newspaper Article DTD


Copied from http://www.vervet.com/

<!DOCTYPE NEWSPAPER [ <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ATTLIST <!ATTLIST <!ATTLIST <!ATTLIST NEWSPAPER (ARTICLE+)> ARTICLE (HEADLINE, BYLINE, LEAD, BODY, NOTES)> HEADLINE (#PCDATA)> BYLINE (#PCDATA)> LEAD (#PCDATA)> BODY (#PCDATA)> NOTES (#PCDATA)> ARTICLE ARTICLE ARTICLE ARTICLE AUTHOR CDATA #REQUIRED> EDITOR CDATA #IMPLIED> DATE CDATA #IMPLIED> EDITION CDATA #IMPLIED>

<!ENTITY NEWSPAPER "Vervet Logic Times"> <!ENTITY PUBLISHER "Vervet Logic Press"> <!ENTITY COPYRIGHT "Copyright 1998 Vervet Logic Press"> ]>

Product Catalog DTD


Copied from http://www.vervet.com/

<!DOCTYPE CATALOG [ <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT <!ELEMENT CATALOG (PRODUCT+)> PRODUCT (SPECIFICATIONS+, OPTIONS?, PRICE+, NOTES?)> SPECIFICATIONS (#PCDATA)> OPTIONS (#PCDATA)> PRICE (#PCDATA)> NOTES (#PCDATA)>

<!ATTLIST PRODUCT NAME CDATA #IMPLIED> <!ATTLIST CATEGORY (HandTool | Table | Shop-Professional) "HandTool"> <!ATTLIST PARTNUM CDATA #IMPLIED> <!ATTLIST PLANT (Pittsburgh | Milwaukee | Chicago) "Chicago"> <!ATTLIST INVENTORY (InStock | Backordered | Discontinued) "InStock"> <!ATTLIST SPECIFICATIONS WEIGHT CDATA #IMPLIED> <!ATTLIST POWER CDATA #IMPLIED> <!ATTLIST OPTIONS FINISH (Metal | Polished | Matte) "Matte"> <!ATTLIST OPTIONS ADAPTER (Included | Optional | NotApplicable) "Included"> <!ATTLIST OPTIONS CASE (HardShell | Soft | NotApplicable) "HardShell"> <!ATTLIST PRICE MSRP CDATA #IMPLIED> <!ATTLIST PRICE WHOLESALE CDATA #IMPLIED> <!ATTLIST PRICE STREET CDATA #IMPLIED> <!ATTLIST PRICE SHIPPING CDATA #IMPLIED> <!ENTITY AUTHOR "John Doe"> <!ENTITY COMPANY "JD Power Tools, Inc.">

<!ENTITY EMAIL "jd@jd-tools.com">


]>

You might also like