Document Type Declarations
Document Type Declarations
com/
Document Type Definition is a set of rules that defines the structure of an XML
document.
It tells, what tags we can use in a document, what order they should appear in, which tags
can appear inside other ones, which tags have attribute so on.
All document type declaration begins with the string “<!DOCTYPE”
<!DOCTYPE…………>
The document type declaration can have an internal and/or an external component known
as the internal subset (Internal DTD) or an External subset (External DTD) respectively.
<apples>12</apples>
#PCDATA :- Parsed Character Data, which explicitly means that the text that not contain
markups, just simple character data.
apples.xml
<!DOCTYPE SYSTEM “apples.dtd”>
<apples>12</apples>
A document with both an external and an internal document type declaration is shown
here.
apples.dtd
<!ELEMENT apples(#PCDATA)>
apples.xml
<!DOCTYPE SYSTEM “apples.dtd” [
1
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
Note: If PUBLIC the DTD can be used by anyone by referring the URL.If SYSTEM
that means it resides on local hard disk and may not be available for use by other
application.
EXAMPLES
1) Internal DTD
<?xml version=”1.0”?>
<!DOCTYPE mail [
<!ELEMENT mail(to,from,heading,body)>
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>]>
<mail>
<to>Rani</to>
<from>Ravi</from>
<heading>Remainder</heading>
<body>About our parents Wedding Anniversary</body>
</mail>
The DTD is interpreted as follows:
//mail.xml
<?xml version=”1.0”?>
<!DOCTYPE mail SYSTEM “mail.dtd”>
<mail>
<to>Rani</to>
<from>Ravi</from>
<heading>Remainder</heading>
<body>About our parents Wedding Anniversary</body>
</mail>
//mail.dtd
<?xml version=”1.0”?>
<!ELEMENT mail(to,from,heading,body)>
2
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
<!ELEMENT to (#PCDATA)>
<!ELEMENT from (#PCDATA)>
<!ELEMENT heading (#PCDATA)>
<!ELEMENT body (#PCDATA)>]>
Element Type declaration must start with the string “<!ELEMENT “ followed by the
name and content specification
Every element has certain allowed content. There are four general types of content
specification
1) EMPTY content may not have content
Eg: <!ELEMENT stock EMPTY>
2) ANY content may have any combination of elements in any order
Eg: <!ELEMENT stock ANY>
3) Mixed content may have character data or mix of character data and sub
elements
Eg: <!ELEMENT to(#PCDATA)>
4) Element Content May have only sub elements in element content
specifications.
Eg: <!ELEMENT mail(to, from, heading, body)>
</person>
4
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
</person>
5
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
? Optional (0 or 1 times)
* Optional & repeatable (0 or more times)
+ Required & repeatable (1 or more times)
6
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
7
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
• STRING ATTRIBUTES
The simplest type of attribute is the CDATA or string attribute. CDATA attribute
values can be any string of characters. For example
<!ATTLIST product name CDATA …… >
An element of type product has an attribute called name whose values can be
any string of characters except <,>,& . String attribute can contain an arbitrary collection
characters of any length as long as any occurrence of “<” or “&” is escaped with entity
references.
Sample:<product name=”Acme”> or
<product name=”Profit &Loss”>
• ENUMERATED ATTRIBUTES
A list of permissible values can be supplied using an enumerated type attributes.
An enumerated attribute is one that can take on one of a fixed set of values supplied as
part of its declaration.
An element of type product has two attributes known as name and color. The name
attribute can have any string of characters except <,>,&.color attribute must be either the
string “red” or “green” .
<product name=”acme” color=”red”>
Remember the enumerated attribute elements are case sensitive.
• ID/IDREF/IDREFS
There three attribute types are treated as together as they are strongly interrelated.
Any ID attributes occurring in an XML document must be unique in order for the
document to be valid. Attributes of type ID thus provide a handy way of a name to an
element to uniquely identify it. ID’s must begin with a letter, a “_” ,or a “:” character .In
this example a UniqueName attribute is attached to hello element :
An IDREF attribute assigned values in a valid XML document must match the value
assigned to an ID attribute somewhere in the document.
<!ATTLIST bar Reference IDREF ….>
8
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
Here is an example of bar element using its IDREF attribute to point to the hello element
of the last example.
<bar Reference=”P1234”>
• ENTITY/ENTITIES
Attribute of type ENTITY or ENTITIES are treated together as they are strongly
related. An Attribute of type ENTITY must have a value corresponding to the name of an
unparsed entity declared somewhere in the Document Type Declaration. In this example
an external data entity, bob, is declared and the salutation attribute of the letter element is
declared to be type ENTITY.
• NMTOKEN /NMTOKENS
Attribute of type NMTOKEN or NMTOKENS are treated together as they are strongly
related. An NMTOKEN attribute is restricted to contain characters allowed in a name:
any combination of letters, digits and some punctuation characters “.”, ”-”, ”_” and
“:”.Note that this list does not contain any white space characters.
An NMTOKENS attribute is one that can contain one or more NMTOKEN separated by
white spaces.
<!ATTLIST product code NMTOKEN ….>
Product elements might look look like this:
<product code=”Alpha-123”> or<product code=”333”>
Here is an example of invalid NMTOKEN attribute
<product code=”A 123”>
• NOTATION
An attribute list declaration of type NOTATION must specify one or more notations
declared somewhere in the Document Type Declaration. Its value is constrained to
match one of those notations.
In this example, a notation EDIFACT is declared and the format attribute of the invoice
element is declared to be of type NOTATION.
<!NOTATION EDIFACT SYSTEM “EDIFACT Format”>
<!ATTLIST invoice format NOTATION (EDIFACT)>
…………………..
9
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
<invoice format=”EDIFACT”>
ATTRIBUTE DEFAULTS
An Attribute list declaration includes information about whether or not a value must be
supplied for it and if not, what the XML processor should do.
#IMPLIED -These are attributes that can be left unspecified if desired. The XML
processor passes the fact that the attribute was unspecified through out the XML
application, which can then choose what best to do.
Valid document.
<!DOCTYPE fruit[<!ELEMENT fruit EMPTY>
<!ATTLIST fruit type CDATA #IMPLIED>]>
<fruit />
<!ATTLIST product color(red|green) #IMPLIED>
An element of type product has an attribute called color. Color attribute must be either
string “red” or “green”. If the value is not supplied, leave it up to the XML application to
decide what to do.
<product color=”red”>…………</product> or <product> is also valid.
10
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
#FIXED – An attribute declaration may specify that an attribute has a fixed value. In this
case attribute is not required, but if it occurs it must have a specific value.
<!ATTLIST product name CDATA #FIXED “Acmepc”>
An element of type product has an attribute called name having a fixed value Acmepc.
Any other value is an Error.<product name=”Acmepc”>
• Microsoft Internet Explorer contains built in support for XML in the form of an XML
parser known as msxml. Microsoft has an overall strategy for making structured
information available to HTML browser in the form of Data Source Object.
• Microsoft has added support for XML via XMLDSO applet which exposes XML data
source in the same as the traditional databases.
• Getting XML elements into HTML elements with XMLDSO consists of following
stage
a. Create the HTML page and include an applet element to load the
XMLDSO object that specifies the name of the XML document to load.
b. Create the HTML elements that are data aware .Use datasrc attribute of
table to connect to the table and applet with id xmldso.
c. Use the datafld attribute to specify the name of the data field each cell will
contain.
• The applet element in the HTML document looks like this:
The com.ms.xml.dso package contains an applet called XMLDSO that can be used as
an XML data provider in conjunction with the data binding features of Internet
Explorer 4.0. for binding XML data to HTML element on the page .
The APPLET parameters are as follows:
URL A relative or absolute URL that points to an XML file. Typically, this data
comes from the same place as the HTML page containing the APPLET tag.
inline The XML can be placed inside the APPLET so that you don't have to make
XML another HTTP request to get the data.
param Param element is used to specify the name of the XML document to be
processed
• Next we need to create an HTML table to lay out the information .We can write the
start of the table element to fit our data which is to be displayed.
11
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
This table row is declared to contain three table cells .The datafld attribute is used to
specify the name of the data field each cell will contain.
• These fields are XML elements namely name,capacity,and price element from the
XML source document.The XMLDSO applet works with the browser to expand this
table to as many rows as required to display all the information in the XML
document.The XML document is given below.
//catalog.xml
<?xml version="1.0"?>
<PCS>
<PC>
<NAME>Zenith</NAME>
<CAPACITY>100</CAPACITY>
<PRICE>20000</PRICE>
</PC>
<PC>
<NAME>DELL</NAME>
<CAPACITY>200</CAPACITY>
<PRICE>40000</PRICE>
</PC>
<PC>
<NAME>Acer</NAME>
<CAPACITY>300</CAPACITY>
<PRICE>35000</PRICE>
</PC>
</PCS>
12
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
//catalog.html
<html>
<body>
<applet code=com.ms.xml.dso.XMLDSO.class width=100% height=1 id=xmldso >
<PARAM NAME="url" VALUE="catalog.xml">
</applet>
<table datasrc="#xmldso" border="1" align="center" width=200>
<thead>
<th>Name</th><th>Capacity</th><th>Price</th>
</thead>
<tr> <td><div datafld="NAME"></td>
<td><div datafld="CAPACITY"></td>
<td><div datafld="PRICE"></td>
</tr>
</table>
</body>
</html>
//output
Name Capacity Price
Zenith 100 20000
DELL 200 40000
Acer 300 35000
XMLDSO produces XML data as a part of the HTML page, rather than accessing it
externally .This can be done by embedding the source XML within applet element .The
applet may be allowed to access the HTML page. To allow this access, set the
MAYSCRIPT attribute to TRUE .The HTML document looks like this:
//catalog1.html
<html>
<body>
<p>XML catalog displayed in an HTML table</p>
<applet code=com.ms.xml.dso.XMLDSO.class width=100% height=1 id=xmldso
mayscript=TRUE>
<!- -XML stored in an HTML page as part of the applet element -->
<PCS>
<PC>
<NAME>Zenith</NAME>
<CAPACITY>100</CAPACITY>
<PRICE>20000</PRICE>
</PC>
<PC>
<NAME>DELL</NAME>
<CAPACITY>200</CAPACITY>
<PRICE>40000</PRICE>
</PC>
<PC>
<NAME>Acer</NAME>
<CAPACITY>300</CAPACITY>
<PRICE>35000</PRICE>
</PC>
</PCS>
</applet><! --table declaration starts here-->
<table datasrc="#xmldso" border="1" align="center" width=200>
<thead>
<th>Name</th><th>Capacity</th><th>Price</th>
</thead>
<tr> <td><div datafld="NAME"></td>
<td><div datafld="CAPACITY"></td>
<td><div datafld="PRICE"></td>
</tr></table></body></html>
NB: In the same way we can store XML data within an HTML document using XML
(<xml>add xml code here</xml>)tag.there is no need of applet tag use the root element
name instead of xmldso as datasrc in table tag
14
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
<html>
<body>
<applet code=com.ms.xml.dso.XMLDSO.class width=70% height=25 id=xmldso
MAYSCRIPT=true>
<PARAM NAME="url" VALUE="catalog.xml">
</applet>
<table border="1" align="center" width=200>
<tr> <td><b>Name</b></td>
<td><div datasrc="#xmldso" datafld="NAME"></td></tr>
<tr><td><b>Capacity</b></td>
<td><div datasrc="#xmldso" datafld="CAPACITY"></td></tr>
<tr><td><b>Price</b></td>
<td><div datasrc="#xmldso" datafld="PRICE"></td></tr>
</table>
<p align=center>
<input type=button value="Previous"
onclick='xmldso.recordset.moveprevious()'>
<input type=button value="Next"
onclick='xmldso.recordset.movenext()'>
<input type=button value="First"
onclick='xmldso.recordset.movefirst()'>
<input type=button value="Last"
onclick='xmldso.recordset.movelast()'>
</p></body></html>
//output
We had created the XML record with Previous ,Next ,First ,Last buttons allowing the
user to navigate through the XML record one at a time .The individual cells rather than
the table specify the data source as xmldso.
This signals to the browser that it should not process the entire document in one go,but
rather process it one record at atime,keeping track of the current position.
15
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
16
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
}
var cells = rows(i).cells;
for (j = 0; j < cells.length; j++) {
var e = cells(j);
e.style.backgroundColor = color;
}}
return result; }
</script>
<P align=center>
<input type=button value="Add Record" onclick='AddRecord();'>
<input type=button value="Delete Record" onclick='RemoveRecord();'>
</p>
<table datasrc="#xmldso" border="1" align="center" width=200
onClick='MoveToRecord(selectCell())'>
<thead>
<th>Name</th><th>Capacity</th><th>Price</th>
</thead>
<tr> <td><div datafld="NAME"></td>
<td><div datafld="CAPACITY"></td>
<td><div datafld="PRICE"></td>
</tr>
</table></body></html>
//output
18
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
XSL a transformation language that enables the conversion of XML into grammar and
structure suitable for display in the browser .XSL allows direct browsing of XML file
using explorer or any browser. The basic tags that are used in the XSL is given below.
• xsl:stylesheet :This is the first element in the style sheet .In this element we can
specify the Namespace for style sheet.
XML Namespace is a collection of names identified by a URI (Uniform Resource
Identifier) or a URL (Uniform Resource Locater) reference which are used in the XML
document as element type and attribute names .
By using the namespace, developers can qualify element names uniquely on the web
and thus avoid conflicts between element that have same name. Imporatant namespaces
are given below.
a) xmlns:xsl='http://www.w3.org/1999/XSL/Transform'
This is the official standard transformation namespace used as a part of the W3C
recommendation.[W3C is World Wide Web Consortium , which is a forum for
information, commerce ,communication and collective understanding] .It is not
supported by IE5.0
b) xmlns:xsl='http://www.w3.org/TR/WD-xsl
This is the namespace used for style sheet processed by IE5.
The important xsl:stylesheet attributes are as follows:
1)id style sheet unique identifier
2) xmlns:xsl Declares the namespace for the current specification. This is a fixed
attribute with the value 'http://www.w3.org/1999/XSL/Transform'
• xsl:template : Template is a structured container that manages the way a source tree
or a portion of the source tree is transformed. when you build a style sheet ,you will
build a series of template that match elements you would like to attach some styles to.
xsl:template defines a set of riles for transforming nodes in a source document into a
result tree .This is handled with match attributes. The important xsl:template attributes
are as follows:
1)mode Identifies the processing mode and matches it against an apply-template
element that has matching template value.
2)Name Give a name to the template so that it can be accessed.
3)match Identifies the template to be processed.
19
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
• xsl:for-each : This element locates the set of elements in the XML data and repeats a
portion of the template for each one .Possible attributes are
1)select Identify the node to be processed.
2)xml-space indicates whether or not white space present.
20
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
//hello1.xsl
<xsl:stylesheet version="1.0"
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="hello">
<html><body><table>
<tr><td align="center">
<h1><xsl:value-of select="wel"/></h1></td></tr>
<xsl:for-each select="xm">
<tr><td>
<h2><xsl:value-of select="xm1"/></h2></td></tr>
<xsl:for-each select="xm2">
<tr><td>
<h3><xsl:value-of select="sg1"/></h3></td></tr>
<tr><td>
<h3><xsl:value-of select="sg2"/></h3></td></tr>
</xsl:for-each></xsl:for-each>
</table></body></html>
</xsl:template>
</xsl:stylesheet>
Example 3:
21
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
//Email.xml
<?xml version="1.0"?>
<!-- This is a sample email data file -->
<?xml:stylesheet href="email.xsl" type="text/xsl"?>
<mail>
<Recipient>[email protected]</Recipient>
<Sender>[email protected]</Sender>
<Date>Mon, 21 Apr 1997 09:27:55 +0200</Date>
<Subject>XML literature</Subject>
<Textbody>
<sal>Hello Mrs Sarju,</sal>
<content>Please read Jon Bosak's introductory text
"SGML, Java and the Future of the Web"</content>
<thanks> Best wishes, </thanks>
<name>Krishnalal G </name>
</Textbody>
</mail>
//Email.xsl
<!--Since Style Sheet is an XML itself, the file begins with xml declaration
the <xsl:stylesheet> element indicates that this document is a style sheet file and provides
the location for declaring XSL namespace.
Also wrap the entire template with <xsl:template match="/"> to indicate that this
template corresponds to the root(/)of the XML document -->
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<HTML>
<BODY><TABLE BORDER="0">
<TR><TD>MAIL DETAILS</TD> </TR>
<xsl:for-each select="mail">
<TR> <TD><xsl:value-of select="Recipient"/></TD></TR>
<TR> <TD><xsl:value-of select="Sender"/></TD></TR>
<TR> <TD><xsl:value-of select="Date"/></TD></TR>
<TR><TD><xsl:value-of select="Subject"/></TD></TR>
<TR><TD><xsl:for-each select="Textbody">
<xsl:value-of select="sal"/>
<br/><xsl:value-of select="content"/>
<br/><br/><xsl:value-of select="thanks"/>
<br/><xsl:value-of select="name"/>
</xsl:for-each>
</TD></TR>
</xsl:for-each></TABLE></BODY></HTML>
</xsl:template></xsl:stylesheet>
//output
22
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
Example 4:
Write a program which creates a valid book CATALOG document in XML .The book
catalog stores any CATEGORY of books and each BOOK ELEMENT stores
BOOKNAME,AUTHORNAME,ISBN,PUBLISHER
,PAGES,PRICES etc.The element BOOK having an enumerated attribute list called
Best Seller and the Price element having an attribute called Currency.You can further
expand the elements if necessary…Write the valid internal DTD and create XSL file to
display it in browser..
//bcatalogxml
<?xml version="1.0"?>
<?xml:stylesheet href="bcatalog.xsl" type="text/xsl"?>
<!DOCTYPE CATALOGS[
<!ELEMENT CATALOGS (CATEGORY)*>
<!ELEMENT CATEGORY (BOOK)>
<!ATTLIST CATEGORY TYPE CDATA #REQUIRED>
23
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
<!ELEMENT BOOK
(BOOKNAME,AUTHORNAME,ISBN,PUBLISHER,PAGES,PRICES)>
<!ATTLIST BOOK BESTSELLER (YES|NO) #REQUIRED>
<!ELEMENT BOOKNAME (#PCDATA)>
<!ELEMENT AUTHORNAME (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT PUBLISHER (#PCDATA)>
<!ELEMENT PAGES (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>
<!ATTLIST PRICE CURRENCY CDATA #REQUIRED>
]><CATALOGS>
<CATEGORY TYPE="XML">
<BOOK BESTSELLER="NO">
<BOOKNAME>CLOUDES TO CODE</BOOKNAME>
<AUTHORNAME>JESSE</AUTHORNAME>
<ISBN>111-S223</ISBN><PUBLISHER>WROX</PUBLISHER>
<PAGES>276</PAGES>
<PRICE CURRENCY="usd">42.00</PRICE>
</BOOK></CATEGORY>
<CATEGORY TYPE="XML">
<BOOK BESTSELLER="YES">
<BOOKNAME>XML IN ACTION</BOOKNAME>
<AUTHORNAME>WILLIAM</AUTHORNAME>
<ISBN>222-S223</ISBN>
<PUBLISHER>TECHMEDIA</PUBLISHER><PAGES>476</PAGES>
<PRICE CURRENCY="usd">87.00</PRICE>
</BOOK></CATEGORY></CATALOGS>
bcatalog.xsl
<?xml version="1.0"?>
<xsl:stylesheet version = '1.0'
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
<xsl:template match="/">
<html><body><table border="1">
<tr>
<th>BOOKNAME</th>
<th>AUTHORNAME</th>
<th>ISBN</th>
<th>PUBLISHER</th>
<th>PAGES</th>
<th>PRICE</th>
</tr>
<xsl:for-each select="CATALOGS/CATEGORY">
<!--<tr><td colspan="6">
<xsl:apply-templates/> </td>
24
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/
</tr>-->
<xsl:for-each select="BOOK">
<tr><td valign="top"><xsl:value-of select="BOOKNAME"/> </td>
<td valign="top"><xsl:value-of select="AUTHORNAME"/> </td>
<td valign="top"><xsl:value-of select="ISBN"/> </td>
<td valign="top"><xsl:value-of select="PUBLISHER"/> </td>
<td valign="top"><xsl:value-of select="PAGES"/> </td>
<td valign="top"><xsl:value-of select="PRICE"/> </td>
</tr>
</xsl:for-each>
</xsl:for-each>
</table></body></html>
</xsl:template>
</xsl:stylesheet>
//output
25