Title: New Perspectives on XML, 2nd Edition
1 TUTORIAL 9
- USING XML AS A DATA SOURCE
2USING 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 tutorial, the data source is an XML
document containing information about the
employees at Freezing Point. - The Web pages uses placeholders which we will
later populate with data from two XML documents.
3USING XML AS A DATA SOURCE
4DATA BINDING WITH SEVERAL WEB PAGES
5FIELDS, 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.
6FIELDS, RECORDS, AND RECORDSETS
This figure shows fields, records, and a
recordset of an XML document
7DATA 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 - ltxml idid srcURLgtlt/xmlgt
- Here, id is the id name assigned to the data
island - URL is the filename and location of the external
XML file
8DATA ISLANDS
- For example
- ltxml idCompany srcCompany.xmlgtlt/xmlgt
- This creates a data island named Company attached
to Company.xml.
9DATA ISLANDS
- To insert a data island directly into the HTML
file, use this syntax - ltxml ididgt
- xml code
- lt/xmlgt
- While this technique can be used, it is not
recommended. After all, the philosophy of XML is
to separate data content from data formatting.
10DATA 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.
11CREATING A DATA ISLAND
12BINDING 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
- lttag datasrcid datafldfieldgt
- 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.
13HTML ELEMENTS THAT SUPPORT DATA BINDING
14BINDING AN HTML ELEMENT TO A FIELD
15BINDING TO AN XML ATTRIBUTE
- Attributes, like the Status attribute of the
Employee element, 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.
16BINDING TO AN XML ATTRIBUTE
- This code has the ID attribute as part of the
Employee element - ltemployee IDE304gt
- ltnamegtAlice Ashmanlt/namegt
- ltdeptNamegtAccountinglt/deptNamegt
- lt/employeegt
17BINDING TO AN XML ATTRIBUTE
- And it is interpreted by the DSO as
- ltemployeegt
- ltIDgtE304lt/IDgt
- ltnamegtAlice Ashmanlt/namegt
- ltdeptNamegtAccountinglt/deptNamegt
- lt/employee
- If the attribute is part of a field element, it
is still treated by the DSO as a field element.
18BINDING 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.
19BINDING TO AN XML ATTRIBUTE
20THE 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()
21THE 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.
22THE DATA SOURCE OBJECT
23THE DATA SOURCE OBJECT
- For example, if you want to display the last
record in a DSO whose id is staffInfo, run the
following method - staffInfo.recordset.moveLast( )
- The simplest way to run a method is to assign the
method to the onClick event handler of a ltbuttongt
as shown below - ltbutton onClickstaffInfo.recordset.moveLast(
)gt
24THE DATA SOURCE OBJECT
- When the user clicks the button, the browser runs
the command indicated by the onClick event
handler, displaying the last record.
25ASSIGNING A RECORDSET METHOD
26TABLE BINDING
- Using table data binding, each record can be
displayed in a different row of a table. The
syntax is - lttable datasrcidgt
- lttrgt
- lttdgtltspan datafldfield1gtlt/spangtlt/tdgt
- lttdgtltspan datafldfield2gtlt/spangtlt/tdgt
- lt/trgt
- lt/tablegt
27TABLE BINDING
- In the example, id is the name of the data
island, field1, field2 are the fields from the
recordset.
28TABLE 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.
29TABLE PAGES
- To specify the page size, add the dataPageSize
attribute to the lttablegt tag - datapagesizenumber
- number is the number of records you want
displayed in a single page.
30NAVIGATING 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 - lttable ididgt
- 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.
31TABLE METHODS AND PROPERTIES
32TABLE METHODS AND PROPERTIES
- To run these commands, add the command to the
onClick event handler of a ltbuttongt tag. For
example, to move to the last page in a data table
named StaffTable, you enter the attribute - onClickstaffTable.lastPage( )
33HIERARCHICAL RECORDSETS
34HIERARCHICAL RECORDSETS
- To bind the Employee fields in the previous slide
to a table, you create a table as follows - lttable datasrcstaffInfo
datafldemployeegt - lttrgt
- lttdgtltspan datafldnamegtlt/spangtlt/tdgt
- lttdgtltspan datafldpositiongtlt/spangtlt/td
gt - lttdgtltspan datafldphonegtlt/spangtlt/tdgt
-
- lt/trgt
- lt/tablegt
35THE FINAL WEB PAGE