Vous êtes sur la page 1sur 25

WEB TECHNOLOGIES http://www.lectnote.blogspot.

com/

Document Type Declarations

Document Type Declaration is a statement embedded in XML document whose purpose


is to acknowledge the existence and location of the Document Type Definition (DTD).

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.

Internal Subset ( Internal DTD)


The internal subset, if present, is delimited by angle brackets as shown here
<!DOCTYPE…..[
<!—Internal Subset-->
]>
Eg:
<<!DOCTYPE apples[
<!ELEMENT apples(#PCDATA)>
]>

<apples>12</apples>

#PCDATA :- Parsed Character Data, which explicitly means that the text that not contain
markups, just simple character data.

External Subset (External DTD)


The external subset; if present consists of a reference to an external entity following the
DOCTYPE keyword.
apples.dtd
<!ELEMENT apples (#PCDATA)>

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/

<!ATTLIST apples color CDATA #REQUIRED>


]>
<apples color=”green”>12</apples>

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:

1) <!DOCTYPE mail indicates that mail is the root element


2) <!ELEMENT mail Root element mail has 4 sub elements
3) <!ELEMENT to Sub element ‘to’ will be of type PCDATA
4) PCDATA Element contain only text data
5) # # is reserved character indicates that #PCDATA is
a reserved word
2) External DTD

//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)>]>

Document Type Declaration is a construct in which any element type declaration,


attribute list declaration, entities declaration and notation declarations are housed.

Element Type Declarations


Element type declarations allow an xml application to constrain the element that can
occur in the document and to specify the order in which can occur.To validate an XML
document ,a validating parser needs to know three things about each element
1) What the element type is named
2) What elements of that type can contain(content model)
3) What attributes an element of that type has associated
Both the element type name and its content model are declared together in what is known
as Element Type declaration

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)>

Element Type Content Models

1) A sequence of elements one after another


<!ELEMENT person(name, address, telephone)>
This declaration says that “a person element consist of a name element, an address
element and a telephone element in exactly that order.
<!--valid person element-->
<person>
<name>……..
<address>……
<telephone>…..
3
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/

</person>

<!--Invalid person element-->


<person>
<address>……
<telephone>…..
</person>
In above example element <name> is missing.
2) A selection from a list of elements , only one allowed
<!ELEMENT fruit(apples|orange|banana)>
This declaration says “a fruit element consists of an apple element, an orange
element or a banana element”.
<!—valid fruit element-->
<fruit>
<apple>…….
</fruit>

<!- -Invalid fruit element- ->


<fruit>
<apple>…….
<orange>….
</fruit>
3) An element occurring once or not at all
<!ELEMENT person(name,address,telephone?)>
This declaration says “a person element consists of a name element followed by
an address element, optionally followed by a telephone element”.

<!--valid person element-->


<person>
<name>……..
<address>……
</person>

<!--valid person element-->


<person>
<name>……..
<address>……
<telephone>…..
</person>

<!--Invalid person element-->


<person>
<name>……..
<address>……
<telephone>…..
<telephone>…..

4
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/

</person>

<telephone> can occur only once or not at all


4) An element occurring zero or more times
<!ELEMENT (name,address,telephone*)>
This declaration says “a person element consists of a name element followed by
an address element followed by zero or more telephone element”.

<!--valid person element-->


<person>
<name>……..
<address>……
</person>

<!--valid person element-->


<person>
<name>……..
<address>……
<telephone>…..
<telephone>…..
</person>
5) An element occurring one or more times
<!ELEMENT (name,address,telephone+)>
This declaration says “a person element consists of a name element followed by
an address element followed by one or more telephone element”.

<!--Invalid person element-->


<person>
<name>……..
<address>……
</person>

<!--valid person element-->


<person>
<name>……..
<address>……
<telephone>…..
</person>

<!--valid person element-->


<person>
<name>……..
<address>……
<telephone>…..
<telephone>…..
</person>

5
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/

6) An element containing any other element


<!ELEMENT person ANY>
This declaration says,”a person element consists of any combination of elements
in any order. It can also contain character data any where”.
7) Character Data: The term character data describes the content of an xml document
that occurs outside any markup (with exception of CDATA sections).
In the element type content models, the keyword “#PCDATA” denotes character
data.
Eg:<!ELEMENT para(#PCDATA|quotation)>
<!ELEMENT quotation(#PCDATA)>
If the content model includes the #PCDATA, the element type declaration is said
to have mixed content.
If the content model consists of purely of element type names the element is said
to have element content.

Occurrence Indicator Description

? Optional (0 or 1 times)
* Optional & repeatable (0 or more times)
+ Required & repeatable (1 or more times)

Element Type Declaration Interpretation

<!ELEMENT stock EMPTY> An element of type stock does not contain


anything ex: <stock/>

<!ELEMENT An element of type contact contains 3 sub


contact(name,address,phone)> elements name, address and phone exactly in
that order.
Ex:<contact>
<name>aaa</name>
<address>SJCET,pala</address>
<phone>239301</phone>
</contact>

<!ELEMENT An element of type contact contains name


contact(name,address?,phone)> element followed by an optional address
element and phone .address can occur once or
not at all
Ex:<contact>
<name>aaa</name>
<phone>239301</phone></contact>

<!ELEMENT An element of type fruit contains either a single


fruit(apple|orange)> apple element or a single orange element. A

6
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/

selection from a list of element, only one


allowed
<fruit><apple>---</apple></fruit>

An element of type fruit contains one or more


<!ELEMENT sub elements that are either apple element or
fruit(apple|orange)+> orange element
Ex1:<fruit><apple>---/apple>
<apple>---</apple>
<orange>--</orange>
</fruit>
Ex2:<fruit>
<orange>--</orange></fruit>

An element of type fruit contains zero or more


<!ELEMENT sub elements that are either apple element or
fruit(apple|orange)*> orange element
Ex1:<fruit><apple>---/apple>
<orange>---</orange><fruit>
Ex2:<fruit></fruit>

An element of type para contains a mixture of


<!ELEMENT character data and list elements in any order.
para(#PCDATA|list)*> Ex1:<para>Here is my list
<list>---</list></para>
Ex2:<para>aaa,bbb,ccc</para>
Ex3:<para><list>---</list></para>

An invoice element consists of a from element


<!ELEMENT followed by to element followed by one or
invoice(from,to,item+)> more item element

An invoice element consists of a from element


<!ELEMENT followed by zero or more to element followed
invoice(from,to*,item+)> by one or more item element

An invoice element consists of any combination


<!ELEMENT invoice ANY> of elements or character data, in any order .

Attribute List Declarations


Attribute list declarations serve to specify the name, type and optionally the default value
of the attribute associate with an element. There are ten different attribute types exist.
They are String, Enumerated, ID, IDREF,IDREFS, ENTITY, ENTITIES, NMTOKEN,
NMTOKENS,NOTATION.

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 &amp;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.

Ex:<!ATTLIST product name CDATA ……. Color(red|green) ………>

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.

<!ATTLIST apple quality(GOOD|BAD|INDIFFERENT) .....>


The quality attribute is declared to have three permissible values “GOOD”, ”BAD” and
“INDIFFERENT” .here is a valid apple element
<apple quality=”GOOD”> and following is an invalid apple element
<apple quality=”good”>

• 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 :

<!ATTLIST hello UniqueName ID …….>


the sample element might look like this
<hello UniqueName =”P1234”>

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”>

IDREFS is a variation on IDREF in which an attribute is allowed to contain multiple


referenced IDs .So given this declaration:
<!ATTLIST bar References IDREFS ……>
bar element might look like this:<bar References=”P1234Q5678”>
thus the value IDREFS attribute may contain multiple IDREF values separated by White
Spaces.

• 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.

<!ENTITY bob system “bob.gif” NDATA gif>


<!ATTLIST letter salutation ENTITY …>
The letter element might look like this: <letter salutation=”bob”>
Like IDREFS above, ENTITIES is a variation on ENTITY that allows an attribute to
contain one or more entity names in its value.

• 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.

There are three different variations:


1)Required --->a value must be specified.
2)Implied --->the XML processor tells the application that no value was supplied. The
application can decide what best to do.
3)Fixed --->A value is supplied in the declaration. No value need be supplied in the
document and the XML processor will pass the specified fixed value through the
document. If a value is supplied in the document, it must exactly match the fixed value.

#REQUIRED -The attribute must have an explicitly specified value on every


occurrence of the element in the document.
<!ATTLIST product name CDATA #REQUIRED>
An element of type product has an attribute called name whose value can be any string of
chars except <,>,&.The value must be supplied when it is used in the document.
<product name=”Acmepc”>
In this example the type attribute of the fruit element is declared to be required.

<!DOCTYPE fruit[ <!ELEMENT fruit EMPTY>


<!ATTLIST fruit type CDATA #REQUIRED>]>
<fruit type=”apple”/>

A validating XML parser would thus reject the following document


.<!DOCTYPE fruit[<!ELEMENT fruit EMPTY>
<!ATTLIST fruit type CDATA #REQUIRED>]>
<fruit />

#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”>

DISPLAYING XML DATA IN HTML BROWSER AS HTML


TABLES

• 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:

<applet code=com.ms.xml.dso.XMLDSO.class width=100% height=1 id=xmldso >


<PARAM NAME="url" VALUE="catalog.xml">
</applet>

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.

id Id attribute give a unique name to the Applet.

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/

<table datasrc="#xmldso" border="1" align="center">


<thead>
<th>Name</th>
<th>Capacity</th>
<th>Price</th>
</thead>

This is a simple HTML start tag with table heading information.


The datasrc tag connect the table with the applet with the id xmldso.

• The body of the table should look like this


<tr> <td><div datafld="NAME"></td>
<td><div datafld="CAPACITY"></td>
<td><div datafld="PRICE"></td>
</tr>

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>

The full HTML document appears like this:

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

ALTERNATE METHOD FOR DISPLAY IN BROWSER WITHOUT XMLDSO


We can use the <xml> tag with id and src as attribute instead of applet tag.Id takes the
root element’s value and src is the xml document it points to.We can get the same output
using the following code.
//catalog1.html
<html>
<body>
<xml id="PCS" src="catalog.xml">
</xml>
<table datasrc="#PCS" border="1" align="center">
<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>

STORING XML DATA IN HTML BROWSER


13
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/

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

VIEWING XML DATA ONE RECORD AT A TIME USING XMLDSO


Step1 :use the same catalog.xml

Step2:create the cat.html file with the following content

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/

ADDING EDITABLE FIELDS UP TO A LIVE UPDATING TABLE


Step1:use the same catalog.xml
Step2: create cat2.html with the following code
<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><input type=text size=50 datasrc="#xmldso"
datafld="NAME"></td></tr>
<tr> <td><b>Capacity</b></td> <td><input type=text size=50 datasrc="#xmldso"
datafld="CAPACITY"></td></tr>
<tr> <td><b>Price</b></td> <td><input type=text size=50 datasrc="#xmldso"
datafld="PRICE"></td></tr></table>
<script>
function AddRecord()
{ xmldso.recordset.AddNew();
UpdateRecord(); }
function UpdateRecord()
{ xmldso.recordset("name") = document.all.item("name").value;
xmldso.recordset("capacity") = document.all.item("capacity").value;
xmldso.recordset("price") = document.all.item("price").value;
}
function RemoveRecord()
{ xmldso.recordset.Delete(); }
function MoveToRecord(i)
{ xmldso.recordset.MoveFirst();
while (i > 0) {
xmldso.recordset.MoveNext();
i = i - 1;
}}
function selectCell()
{ var row = window.event.srcElement;
while (row.tagName != "TR")
row = row.parentElement;
var table = row;
while (table.tagName != "TABLE")
table = table.parentElement;
var rows = table.rows;
var result = -1;
for (i=0; i < rows.length; i++) {
color = "";
if (row == rows(i)) {
color = "pink";
result = i-1;

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

DISPLAY HIERARCHIAL XML AS NESTED HTML TABLES


17
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/

Step1: create catalog1.xml


<?xml version="1.0"?>
<PCS><PC>
<NAME>Zenith</NAME>
<CAPACITY><RAM>10</RAM>
<DISK>200</DISK> </CAPACITY>
<PRICE>20000</PRICE></PC>
<PC><NAME>DELL</NAME>
<CAPACITY><RAM>20</RAM>
<DISK>300</DISK></CAPACITY>
<PRICE>40000</PRICE> </PC>
</PCS>
Step2: create the cat3.html with the following content
<html><body>
<applet code=com.ms.xml.dso.XMLDSO.class width=70% height=25 id=xmldso
MAYSCRIPT=true>
<PARAM NAME="url" VALUE="catalog1.xml">
</applet>
<table datasrc="#xmldso" border="1" align="center" width=300>
<thead><th>Name</th><th>Capacity</th><th>Price</th></thead>
<tr><td><div datafld="NAME"></td>
<td><table datasrc="#xmldso" datafld="CAPACITY" border="1" align="center"
width=150>
<thead><th>RAM</th><th>DISK</th></thead>
<TR><TD><div datafld="RAM"></td>
<TD><div datafld="DISK"></td>
</TR></table></td>
<td><div datafld="PRICE"></td>
</tr></table>
</body></html>
//output

18
WEB TECHNOLOGIES http://www.lectnote.blogspot.com/

EXTENSIBLE STYLE SHEET LANGUAGE (XSL)

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.

• xsl:apply-template : This element tells the processor to process a named template


that has been defined using xsl:template element. Possible attributes are given below:
1) select Identify the node to be processed. If this attribute is not specified, then
processor will process the template of the current node in the order of their appearance
in the root document
2)mode  Identify the processing node and selects only those template elements that
have a matching node value.

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.

• xsl:value-of : within <xsl:for-each> element we can further drill down to select


children.<xsl:value-of> element specifies a specific child and then insert the text
content of that child into that template using select attribute .select attribute specifies
the node to be processed.

CONVERTING XML INTO HTML WITH XSL


An XSL style sheet are themselves XML documents. The XSL is based on set of rules
that trigger when specified elements are encountered in an XML document. Every
XML page should contain an XSL file to display it in an explorer.
Example 1 :To display a Welcome Message
Step1: create a hello.xml file with the following content
<?xml version="1.0"?>
<!---Internal Document type Definition-->
<!DOCTYPE hello[
<!ELEMENT hello (msg)>
<!ELEMENT msg (#PCDATA)>]>
<!---Style sheet reference-->
<?xml:stylesheet href="hello.xsl" type="text/xsl"?>
<!--standard XML code-->
<hello>
<msg>Welcome to XML world</msg>
</hello>
Step2:create the hello.xsl file
<xsl:stylesheet version="1.0"
xmlns:xsl='http://www.w3.org/1999/XSL/Transform'>
</xsl:stylesheet>
Step3: browser the hello.xml file on the browser you will get the output.
The DTD part can also be neglected for simple file like this the XSL will take care of
the document.
Example 2 :To display different Message
//hello1.xml
<?xml version="1.0"?>
<?xml:stylesheet href="hello1.xsl" type="text/xsl"?>
<hello> <wel>welcome to XML</wel>
<xm><xm1>XML is eXtensible Markup Language</xm1>
<xm2>
<sg1>SGML is Standard Generalized Markup Language</sg1>
<sg2>XML is a subset of SGML</sg2> </xm2></xm></hello>

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>sarjusjcet@gmail.com</Recipient>
<Sender>krishnalalg@gmail.com</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

Vous aimerez peut-être aussi