[XML Embedded in HTML][CSS/XSL][Processing Instructions]

A Simple XML Server

XML can be generated on a server without any installed XML controls.


Storing XML on the Server

XML files can be stored on your Internet server.

XML files can be stored on your Internet server, just like any other HTML files.

Start up your Notepad editor and write the following lines:

<?xml version="1.0"?>
<note>
<from>Jani</from>
<to>Tove</to>
<message>Remember me this weekend</message>
</note>

All you have to do is to save the file on your Internet server with a proper name like "note.xml", before the XML document is ready to use.

Note: The XML file must be in the same directory (folder) as your HTML files, and the MIME type of XML files should be set to text/xml.


Generating XML with ASP

XML can be generated on a server without any installed XML software.

To generate an XML response from your server - simply write the following code and save it as an ASP file on your web server :

<%
Response.ContentType="text/xml"
Response.Write("<?xml version='1.0' ?>")
Response.Write("<note>")
Response.Write("<from>Jani</from>")
Response.Write("<to>Tove</to>")
Response.Write("<message>Remember me this weekend</message>")
Response.Write("</note>")
%>

Getting XML from a Database

XML can be generated from a database without any installed XML software.

The XML response from the previous example can easily be modified to fetch its data from a database.

To generate an XML database response from the server, simply write the following code and save it as an ASP file:

<%
Response.ContentType = "text/xml"
set conn=Server.CreateObject("ADODB.Connection")
conn.provider="Microsoft.Jet.OLEDB.4.0;"
conn.open server.mappath("../ado/database.mdb")
sql="select fname, lname from tblGuestBook"
set rs = Conn.Execute(sql)
rs.MoveFirst()
response.write("<?xml version='1.0' ?>")
response.write("<guestbook>")
while (not rs.EOF)
response.write("<guest>")
response.write("<fname>" & rs("fname") & "</fname>")
response.write("<lname>" & rs("lname") & "</lname>")
response.write("</guest>")
rs.MoveNext()
wend
rs.close()
conn.close()
response.write("</guestbook>")
%>

The Basic Criteria for a Valid XML Document

Every XML must be well-formed

The prolog must include a proper document type declaration which in turn contains the document type definintion which defines the structure of the document

The rest of the document must conform to the structure define by the DTD.


An error in a well-formed page is fatal where a validity error is only an error.

The Advantages of Making an XML Document Valid

To do a valid document:

First Fully define the document structure within its DTD

Then Create the document itself

The DTD defines the structure of The XML document and enforces the desired structure and guarantees that your document meets the required stanadards. Making XML documents valid, is especially usefull when ensuring uniformity among a group of similar documents. DTD is a “grammar for a class of documents”.

Can you think of some applications?

A good one is a Publishing company that uses all of its editors. Using DTD ensures that :

these documents comply weith the required structure

That the editors don’t add new elements arbitary

That they don’t add elements in the wrong order

Assign wrong data types to attributes etc


Including a DTD is especially important when the documents are going to be processed by custom software that expect a particular document structure.

A DTD provides a blueprint to the

Adding the DTD

A document type declaration is a block of XML markup that you must include in your prolog for a valid XML document

Using it you wont be able to use any elements or attributes in the document that you haven’t defined in the document type declaration. And every element and attribute that you include must much the specifications

If you open a page through HTML it will check for both well-formness and validity, if you directly open the xml it will only checj for well formness.

<!DOCTYPE INVENTORY
[
<!ELEMENT INVENTORY >

]
>

This shows that the document cann only contain elements of type INVENTORY and that these elements can have any possible type of content

A DTD Can have the following types of markup declarations:

Element type declarations (types of elements, order, and content)

Attribute list declarations (names of attributes that can be used with a particular element, as well as the data types and the default values of these attributes)

Entity declarations (to store frequently used blocks of text

Notation declarations (to describe a data format or identify the program used to process the particular format.

Processing instructions

Comments

Parameter entity reference


Declaring Element Types

This indicates the name of the element type and the allowable contents of the elements. Actually it maps out the entire logical structure of the document.

The Form of an Element Type Declaration

<!ELEMENT SUBTITLE (#PCDATA)>

We have an element subtitle whixh is permitted to contain only character data.


<!DOCTYPE INVENTORY
[
<!ELEMENT INVENTORY (BOOK)+>
<!ELEMENT BOOK (#PCDATA)>
>
<INVENTORY>
<BOOK>The Adventures of Huckleberry Finn1</BOOK>
<BOOK>The Adventures of Huckleberry Finn2</BOOK>
<BOOK>The Adventures of Huckleberry Finn3</BOOK>
<BOOK>The Adventures of Huckleberry Finn4</BOOK>
</INVENTORY>

The Element Content Specification

Empty Content (like <!ELEMENT IMAGE EMPTY>)

Any Content (An element can have zero or more child elements, in any order or number of repetitions)

Element Content (The element can contain child elements but can’t directly contain character data)

Mixed Content


<?xml version="1.0"?>
<!-- File Name: Inventory Valid.xml -->
<!DOCTYPE INVENTORY
[
<!ELEMENT INVENTORY (BOOK)*>
<!ELEMENT BOOK (TITLE, AUTHOR, BINDING, PAGES, PRICE)> //Content model indicating the allowed types of child elements and their
order
//Sequence
<!ELEMENT TITLE (#PCDATA | SUBTITLE)*>
//CHOICE

<!ELEMENT SUBTITLE (#PCDATA)>
<!ELEMENT AUTHOR (#PCDATA)>
<!ELEMENT BINDING (#PCDATA)>
<!ELEMENT PAGES (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]
>
<INVENTORY>
<BOOK>
<TITLE>The Adventures of Huckleberry Finn</TITLE>
<AUTHOR Born="1835">Mark Twain</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>298</PAGES>
<PRICE>$5.49</PRICE>
</BOOK>
<BOOK>
<TITLE>Leaves of Grass</TITLE>
<AUTHOR Born="1819">Walt Whitman</AUTHOR>
<BINDING>hardcover</BINDING>
<PAGES>462</PAGES>
<PRICE>$7.75</PRICE>
</BOOK>
</INVENTORY>


? Zero or one of the preceding items
+ One or more of the preceding items
* Zero or more of the preceding items
What will happen if I have the following DTD


<!ELEMENT MOUNTAIN (NAME+, HEIGHT?, STATE)>
IS THE FOLLOWING LEGAL? WHY NOT?
<MOUNTAIN>
<NAME>MOUNT STELLA</NAME>
<NAME>MOUNT VASILIS</NAME>
<HEIGHT>1.50</HEIGHT>
<STATE>New York</STATE>
</MOUNTAIN>


What will happen if I have the following DTD


<!ELEMENT FILM (NAME* | NARRATOR | INSTRUCTOR)>

IS THE FOLLOWING LEGAL? WHY NOT?

1. <FILM>
<NAME>MOUNT STELLA</NAME>
<NAME>MOUNT VASILIS</NAME>
</FILM>
2. <FILM>
<NARRATOR>Sir me</NARRATOR>
<NAME>Sir you</NAME>
</FILM>
3. <FILM/>
<?xml version="1.0"?>
<!-- File Name: Inventory Valid.xml -->
<!DOCTYPE INVENTORY
[
<!ELEMENT INVENTORY (BOOK)*>
<!ELEMENT BOOK (TITLE, AUTHOR, BINDING, PAGES, PRICE)>
<!ATTLIST BOOK InStock (yes|no) #REQUIRED>
<!ELEMENT TITLE (#PCDATA | SUBTITLE)*>
<!ELEMENT SUBTITLE (#PCDATA)>
<!ELEMENT AUTHOR (#PCDATA)>
<!ATTLIST AUTHOR Born CDATA #IMPLIED>
<!ELEMENT BINDING (#PCDATA)>
<!ELEMENT PAGES (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
]
>
<INVENTORY>
<BOOK InStock="yes">
<TITLE>The Adventures of Huckleberry Finn</TITLE>
<AUTHOR Born="1835">Mark Twain</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>298</PAGES>
<PRICE>$5.49</PRICE>
</BOOK>
<BOOK InStock="no">
<TITLE>Leaves of Grass</TITLE>
<AUTHOR Born="1819">Walt Whitman</AUTHOR>
<BINDING>hardcover</BINDING>
<PAGES>462</PAGES>
<PRICE>$7.75</PRICE>
</BOOK>
<BOOK InStock="yes">
<TITLE>The Legend of Sleepy Hollow</TITLE>
<AUTHOR>Washington Irving</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>98</PAGES>
<PRICE>$2.95</PRICE>
</BOOK>
<BOOK InStock="yes">
<TITLE>The Marble Faun</TITLE>
<AUTHOR Born="1804">Nathaniel Hawthorne</AUTHOR>
<BINDING>trade paperback</BINDING>
<PAGES>473</PAGES>
<PRICE>$10.95</PRICE>
</BOOK>
<BOOK InStock="no">
<TITLE>Moby-Dick
<SUBTITLE>Or, the Whale</SUBTITLE>
</TITLE>
<AUTHOR Born="1819">Herman Melville</AUTHOR>
<BINDING>hardcover</BINDING>
<PAGES>724</PAGES>
<PRICE>$9.95</PRICE>
</BOOK>
<BOOK InStock="yes">
<TITLE>The Portrait of a Lady</TITLE>
<AUTHOR>Henry James</AUTHOR>
<BINDING>mass market paperback</BINDING>
<PAGES>256</PAGES>
<PRICE>$4.95</PRICE>
</BOOK>
<BOOK InStock="no">
<TITLE>The Turn of the Screw</TITLE>
<AUTHOR>Henry James</AUTHOR>
<BINDING>trade paperback</BINDING>
<PAGES>384</PAGES>
<PRICE>$3.35</PRICE>
</BOOK>
</INVENTORY>

In an attribute if you include more than one attribute list declaration for a given element type the contents of the two declarations are merged. If an attribute with a given name is declared more than once for the same element the first one is used.

You can specify the attribute in three different ways

String type

<!ATTLIST FILM Class CDATA “FICTIONAL”>
Tokenized type
<!ATTLIST AUTHOR Born ID #REQUIRED>
ID Must have a unique value
IDREFS can include reference to several identifiers
ENTITY must match an unparsed entity declared in the DTD
Ie <!ELEMENT IMAGE EMPTY>

<!ATTLIST IMAGE Source ENTITY #REQUIRED>
<IMAGE Source=”Logo”/>
ENTITIES
Ie <!ELEMENT IMAGE EMPTY>

<!ATTLIST IMAGE Source ENTITIES #REQUIRED>
<IMAGE Source=”LogGIF LOGOBMP”/>
NMTOKEN
NMTOKENS
Enumarated type
Default Declaration
#REQUIRED
#IMPLIED
AttValue
#FIXED
To include an external DTDyou will include it in the XML by saying
<!DOCTYPE SIMPLE SYSTEM “simple.dtd”>
and the DTD would be
<!DOCTYPE SIMPLE

<!ELEMENT SIMPLE ANY>






.