Vous êtes sur la page 1sur 17

WTA Topic 3 :

XML Data Source Objects


Reference:
1. W3Schools
2. http://www.brainbell.com/tutors/XML/XML_Book_B/The_XML_DSO.htm

Subject Incharge
Pratidnya S. Hegde Patil
1

XML Data Source


Objects (DSO)
Deals with the usage of an XML
document as a data source in
much the same way that Access
or Oracle is used.

XML DSO


The XML DSO allows you to bring data


over to the client as XML and then
manipulate the XML data directly on the
client.

DSO


DSO is an object that operates like a database on


the client side. An instance of it has to be created
for use, which gives an effect of a miniature data
engine embedded in the HTML.
HTML

XML DSO is used to bind data of XML document


to controls on an HTML page.

It lets you access the data without walking


through the document tree.
tree
4

XML element


The XML DSO allows you to work with XML data


that is embedded directly in your HTML code.
This embedded XML code creates data islands.
islands

To create a data island, you will use the new


HTML xml element.
The DSO will find the data located in the xml
element and use it as a data island.

XML element


The xml element includes an id attribute


that will be used to reference the data
island. The xml element looks as follows:
<xml id="orderDSO"> <!--XML data goes here --> </xml>

You can also reference XML data in an


external XML document by using the xml
element and the src attribute as follows:
<xml id="orderDSO" src="NorthwindOrder.xml"></xml>

Binding HTML Elements to XML Data


Using the XML DSO


Once you have created a reference to an


XML data source using the xml element,
you can bind this data to some of the
HTML elements in a document.
document

With some of the elements, you can


display the content that is within the
element as either text or as HTML.
HTML

DHTML tags used for XML DSO


<STYLE>

Style tag used to embed style information within a document.


The style tag should always include the TYPE declaration.

ID

ID attribute has an unique value over the document. The ID


name declaration should be preceded by a #, in the <STYLE>
declaration.

SPAN

Extracts a section of the document with the styles applied.


Used within the DIV tag.

DIV

DIV tag is the actual container of the data.

DATASRC

DATASRC attribute pertains to databound objects. The ID of


the DSO instance on the page is specified by the DATASRC
attribute.

DATAFLD

DATAFLD attribute specifies the name of the column to which


the HTML element is bound. This attribute is always used
along with the DATASRC attribute.
8

Ex1:Accessing all records together


The data is bound to a multivalue such as a table element rather than
several single-valued elements such as SPAN tags.
<html>
<xml src="catalogs.xml" id="CatalogDSO"></xml>
<body>
<table border="2" width="100%" datasrc="#CatalogDSO" cellpadding="5">
<thead>
<th>Book Name</th>
<th>Author Name</th>
<th>ISBN</th>
<th>Publisher Name</th>
<th>Pages</th>
<th>Price</th>
</thead>
<tbody>

<tr>
<td valign="top"><span
<td valign="top"><span
<td valign="top"><span
<td valign="top"><span
<td valign="top"><span
<td valign="top"><span
</tr>
</tbody>
</table>
</body>
</html>

datafld="BOOKNAME"></span></td>
datafld="AUTHORNAME"></span></td>
datafld="ISBN"></span></td>
datafld="PUBLISHER"></span></td>
datafld="PAGES"></span></td>
datafld="PRICE"></span></td>

first.html

catalogs.xml
catalogs.dtd
9

catalogs.dtd

<!ELEMENT CATALOGS (BOOK)*>


<!ELEMENT BOOK (BOOKNAME, AUTHORNAME,
ISBN, PUBLISHER, PAGES?, PRICE)>
<!ELEMENT BOOKNAME (#PCDATA)>
<!ELEMENT AUTHORNAME (#PCDATA)>
<!ELEMENT ISBN (#PCDATA)>
<!ELEMENT PUBLISHER (#PCDATA)>
<!ELEMENT PAGES (#PCDATA)>
<!ELEMENT PRICE (#PCDATA)>

catalogs.xml
<?xml version="1.0"?>
<!DOCTYPE CATALOGS SYSTEM "catalogs.dtd">
<CATALOGS>
<BOOK>
<BOOKNAME>Oracle 8i</BOOKNAME>
<AUTHORNAME>Richard
Gosling</AUTHORNAME>
<ISBN>0-07-913702-4</ISBN>
<PUBLISHER>BPB Publications</PUBLISHER>
<PAGES>393</PAGES>
<PRICE>410.00</PRICE>
</BOOK>
<BOOK>
<BOOKNAME>XML in Action</BOOKNAME>
<AUTHORNAME>William J.
Pardi</AUTHORNAME>
<ISBN>0-07-914702-9</ISBN>
<PUBLISHER>Microsoft Press</PUBLISHER>
<PAGES>492</PAGES>
<PRICE>470.00</PRICE>
</BOOK>

<BOOK>
<BOOKNAME>XML to code</BOOKNAME>
<AUTHORNAME>Jesse Liberty</AUTHORNAME>
<ISBN>1-861000-95-2</ISBN>
<PUBLISHER>Wrox Press</PUBLISHER>
<PAGES>393</PAGES>
<PRICE>560.00</PRICE>
</BOOK>
<BOOK>
<BOOKNAME>Java Unleashed</BOOKNAME>
<AUTHORNAME>James Spencer</AUTHORNAME>
<ISBN>0-7456-0964-1</ISBN>
<PUBLISHER>Techmedia
Publications</PUBLISHER>
<PAGES>491</PAGES>
<PRICE>670.00</PRICE>
</BOOK>
</CATALOGS>

Accessing one record at a time


(Properties & Methods of Recordset object)
Property

Action

moveFirst()

Moves the iterator to the first record.

moveLast()

Moves the iterator to the last record.

moveNext()

Moves the iterator to the next record.

movePrevious()

Moves the iterator to the previous record.

EOF

Property of recordset that is true if the iterator is at


the end of file.

BOF

Property of recordset that is true if the iterator is at


the file start.

move(integer)

Moves the iterator to the record number passed.

Delete()

Deletes the current recordset.

AddNew()

Adds a new record to the end of the file.

cancelUpdate

Cancels all the updates to the recordset since it was


loaded.

Fields()

When passed either the name of the field or an index


number, returns the fields value.

11

Example : Accessing one record at a time




Loads an XML document into the XML DSO and binds


various data elements to SPAN elements in the HTML page.

A SPAN element appears for every element to be displayed


that corresponds to an element in the XML document.

The DATASRC attribute of the SPAN tag specifies the DSO


that has to be used (a pound sign followed by the name of
the DSO) and the DATAFLD attribute specifies the element
of the XML document that is supplying the data.

The HTML page contains buttons that are tied to


recordset.moveFirst(),recordset.movePrevious(),recordset.
moveNext(),recordset.moveLast() methods of DSO.
12

Ex2:Accessing one record at a time


through buttons

13

buttons.html

Accessing one record at a time


<html>
<body>
<xml src="catalogs.xml" id="CatalogsDSO"></xml>
<table border="1">
<tr>
<td>Book Name: </td>
<td><span datasrc="#CatalogsDSO"
datafld="BOOKNAME"></span> </td>
</tr>
<tr>
<td>Author Name: </td>
<td><span datasrc="#CatalogsDSO"
datafld="AUTHORNAME"></span></td>
</tr>
<tr>
<td>ISBN: </td>
<td><span datasrc="#CatalogsDSO
datafld="ISBN"></span></td>
</tr>
<tr>
<td>Publisher: </td>
<td><span datasrc="#CatalogsDSO"
datafld="PUBLISHER"></span></td>
</tr>
<tr>
<td>Pages: </td>
<td><span datasrc="#CatalogsDSO"
datafld="PAGES"></span></td>
</tr>
<tr>
<td>Price: </td>
<td><span datasrc="#CatalogsDSO"
datafld="PRICE"></span></td>
</tr>
</table>

<!-- navigation buttons -->


<table>
<tr>
<td><input id="cmdMoveFirst" name="cmdMoveFirst"
type="button" value="Move First
onClick="CatalogsDSO.recordset.moveFirst();"></input>
</td>
<td><input id="cmdMovePrevious
name="cmdMovePrevious"
type="button" value="Move Previous"
onClick="CatalogsDSO.recordset.movePrevious();
if (CatalogsDSO.recordset.BOF) {
CatalogsDSO.recordset.moveFirst();}"></input></td>
<td><input id="cmdMoveNext" name="cmdMoveNext"
type="button" value="Move Next"
onClick="CatalogsDSO.recordset.moveNext();
if (CatalogsDSO.recordset.EOF) {
CatalogsDSO.recordset.moveLast();}"></input></td>
<td><input id="cmdMoveLast" name="cmdMoveLast"
type="button" value="Move Last
onClick="CatalogsDSO.recordset.moveLast();"></input>
</td>
</tr>
</table>
</body>
</html>

catalogs.xml
catalogs.dtd
14

Ex3:All record display with inner


table

15

innertable.html

All record display with inner table


<html>
<xml src="NorthwindPO2.xml"
id="NorthwindDSO"></xml>
<body>
<table border="2" width="100%"
datasrc="#NorthwindDSO" cellpadding="5">
<thead>
<th>Line Item</th>
<th>Details</th>
</thead>
<tbody>
<td valign="top"><span
datafld="line"></span>
</td>
<td>
<table border="1" width="100%"
datasrc="#NorthwindDSO"
datafld="Item" cellpadding="5">
<thead>
<th>Part No</th>
<th>Quantity</th>
<th>UOM</th>
<th>Unit Price</th>
<th>Discount</th>
<th>Total</th>
</thead>

<tbody>
<tr>
<td valign="top">
<span datafld="partno"></span></td>
<td valign="top">
<span datafld="qty"></span></td>
<td valign="top">
<span datafld="uom"></span></td>
<td valign="top">
<span datafld="unitprice"></span></td>
<td valign="top">
<span datafld="discount"></span></td>
<td valign="top">
<span datafld="totalAmount></span></td>
</tr>
</tbody>
</table>
</td>
</tbody>
</table>
</body>
NorthwindPO2.xml
</html>

NorthwindPO2.dtd
16

NorthwindPO2
.dtd

<!ELEMENT po (POLine+)>
<!ELEMENT POLine (line , Item+)>
<!ELEMENT Item (partno , qty , uom , unitPrice , discount , totalAmount)>
<!ELEMENT line (#PCDATA)>
<!ELEMENT partno (#PCDATA)>
<!ELEMENT qty (#PCDATA)>
<!ELEMENT uom (#PCDATA)>
<!ELEMENT unitPrice (#PCDATA)>
<!ELEMENT discount (#PCDATA)>
<!ELEMENT totalAmount (#PCDATA)>

NorthwindPO2.xml
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE po SYSTEM "NorthwindPO2.dtd">
<po>
<POLine>
<line>101</line>
<Item>
<partno>pc1010</partno>
<qty>200</qty>
<uom>EACH</uom>
<unitPrice>800.00</unitPrice>
<discount>10</discount>
<totalAmount>144000.00</totalAmount>
</Item>
<Item>
<partno>monitor17</partno>
<qty>200</qty>
<uom>EACH</uom>
<unitPrice>300.00</unitPrice>
<discount>20</discount>
<totalAmount>48000.00</totalAmount>
</Item>
</POLine>

<POLine>
<line>102</line>
<Item>
<partno>pc2010</partno>
<qty>100</qty>
<uom>EACH</uom>
<unitPrice>1200.00</unitPrice>
<discount>10</discount>
<totalAmount>108000.00</totalAmount>
</Item>
<Item>
<partno>monitor19</partno>
<qty>100</qty>
<uom>EACH</uom>
<unitPrice>500.00</unitPrice>
<discount>10</discount>
<totalAmount>45000.00</totalAmount>
</Item>
</POLine>
</po>

17

Vous aimerez peut-être aussi