Transcript XML

USING XML AS A DATA SOURCE
USING XML AS A DATA
SOURCE
• Data binding is a process by which information in a data
source is stored as an object in computer memory.
• In this presentation, the data source is an XML document
containing information.
• The Web pages uses placeholders which we will later
populate with data from two XML documents.
USING XML AS A DATA
SOURCE
DATA BINDING WITH SEVERAL
WEB PAGES
FIELDS, RECORDS, AND
RECORDSETS
•
Data in a data source is organized by fields,
records, and recordsets.
•
A field is an element that contains a single item
of information such as an employees last name.
•
A record is a collection of those fields.
•
A recordset is a collection of records.
DATA ISLANDS
• The first step in data binding is to attach the Web page to a
recordset. The attached data is called a data island. They
can be either external files or code entered into the HTML
file.
• The syntax to create a data island from an external file is:
<xml id=“id” src=“URL”></xml>
• Here, id is the id name assigned to the data island
• URL is the filename and location of the external XML file
DATA ISLANDS
• For example:
<xml id=“Company” src=“Company.xml”></xml>
• This creates a data island named Company
attached to Company.xml.
DATA ISLANDS
• To insert a data island directly into the HTML file,
use this syntax:
<xml id=“id”>
xml code
</xml>
DATA ISLANDS
• Data islands are stored by the XML parser as a
Data Source Object (DSO).
• The DSO takes care of the interaction between the
Web page and the data island. Also, program code
can be written to control the actions of the DSO
such as specifying which records will be displayed
in the Web page at any one time.
BINDING AN HTML ELEMENT
TO A FIELD
• After the data island has been created, the elements in the
XML document need to be bound to the HTML file.
• The syntax is:
<tag datasrc=“#id” datafld=“field”>
• Here, tag is the name of the HTML tag, id is the name of
the data island, and field is the name of the field in the data
source.
HTML ELEMENTS THAT
SUPPORT DATA BINDING
BINDING TO AN XML
ATTRIBUTE
Attributes are treated by the DSO as fields.
• If the attribute is part of a record element, it is
easy to bind attribute values to a Web page.
• If the attribute is part of a field element, it is still
treated by the DSO as a field element.
BINDING TO AN XML
ATTRIBUTE
• The field element containing the attribute becomes
a record element.
• Remember to reference all character data within
an element using the $TEXT field.
• It is a good idea not to use attributes in field
elements if you plan to do data binding.
THE DATA SOURCE OBJECT
• ActiveX Data Objects (ADO) is a data-access
technology developed by Microsoft. ADO allows
you to work with the Data Source Object by
applying a method or by changing one of the
properties of the DSO.
• The syntax for applying a method is:
id.recordset.method()
THE DATA SOURCE OBJECT
• Here, id is the name of the data island in the Web
document and method is the name of the method
supported by ADO.
• There are several methods that can be applied to
DSOs.
THE DATA SOURCE OBJECT
THE DATA SOURCE OBJECT
• When the user clicks the button, the browser runs
the command indicated by the onClick event
handler, displaying the last record.
TABLE BINDING
• Using table data binding, each record can be
displayed in a different row of a table. The
syntax is:
<table datasrc=“#id”>
<tr>
<td><span datafld=“field1”></span></td>
<td><span datafld=“field2”></span></td>
</tr>
</table>
TABLE BINDING
• In the example, id is the name of the data island,
field1, field2 are the fields from the recordset.
TABLE PAGES
• As you add more records to your XML document,
a table can become long and unwieldy. One way to
fix this is to give the user the option of limiting the
number of records displayed at any one time.
• The user can then move forward of backward that
number of records at a time. This is called paging.
TABLE PAGES
• To specify the page size, add the dataPageSize
attribute to the <table> tag:
datapagesize=“number”
• number is the number of records you want
displayed in a single page.
NAVIGATING A TABLE PAGE
• A unique identifier must be assigned to a table using the ID
attribute before writing a command to navigate a table
page. The syntax to do this is:
<table id=“id”>
• Here, id is the name you assign to the table object.
• This is needed because the commands to navigate the table
pages act on the table itself not the recordset.
TABLE METHODS AND
PROPERTIES
TABLE METHODS AND
PROPERTIES
• To run these commands, add the command to the
onClick event handler of a <button> tag.
• For example, to move to the last page in a data
table named “StaffTable”, you enter the attribute:
onClick=“productTable.lastPage( )”