N341 Client-side XML
|
Modified:
|
Use IE5 or above to view and run tutorials on XML.
Note
- Browser - IE caches files by default which can prevent opening of a later version. The
effect is particularly noticeable during repeated tests when XML references an
XSL file that has been changed but is not opened because of a cached version of
the XSL.
The solution is to require IE to check for new versions on every visit to a
page. In IE: Tools | Internet Options | Temporary Internet Files | Settings |
Every Visit to Page
- XML/XSL - Syntax checking of XML/XSL is rigorous, flagging errors
that would be ignored in HTML by the browser. For example, the
following is acceptable as HTML:
<FORM NAME=ONE>
<INPUT type=text name="TWO" >
</form>
but has the following errors as XML/XSL:
- ONE must be quoted,
- text must be quoted,
- INPUT requires a matching end tag </INPUT>
- </form> must match <FORM>
Below is valid XML/XSL:
<FORM NAME="ONE">
<INPUT type="text" name="TWO"/>
</FORM>
Overview
XML (eXtensible Markup Language) is a language for defining and representing
languages. One key use is to define a data structure and data within the
structure, useful for exchanging data between different applications and
computer systems. Since XML is in text that is readable and write able by
humans, it is also computer architecture neutral. Many database systems
such as Oracle can generate entries from a database in XML form for use
by other applications that understand XML without regard to the computer
system word size, etc. XML use is likely to grow in areas where multiple
computer systems must interact and exchange data.
A simple example of XML use is to supply structured data to an application.
Information for shipping a package from one Zip code to another can be
specified in XML with a hierarchy as:
<package>
<To>47150</To>
<From>47165</From>
<Weight>17.0</Weight>
<Rate>27.50</Rate>
</package> |
 |
Hierarchical element structure - XML documents must have a strictly
hierarchical tag structure. That is, start tags must have corresponding
end tags. In XML vocabulary, a pair of start and end tags is called an
element.
Any element must be properly nested within another. As illustrated above,
the snippet is well-formed because if there is a <To> start tag
there must be an </To> end tag.
Many languages and applications such as Web browsers can parse
XML to extract the data from the structure. The following brief example
illustrates how XML, HTML, and JavaScript would be used to define and access
the data structure containing shipping information. The results can only
be viewed in an XML enabled browser such as IE4 and above that support
the MicroSoft XML objects.
The following JavaScript is embedded within an HTML file interpreted by a XML
aware browser.
The syntax below has the following meaning:
-
<SCRIPT LANGUAGE="xml" ID="UPSOnLine"> - Whatever follows
to the </SCRIPT> tag is XML. The XML structure has ID UPSOnLine.
-
<SCRIPT LANGUAGE="JavaScript"> - Whatever follows to the </SCRIPT> is
JavaScript.
-
document.write("<H2>UPS ShippingRate</h2>"); - Write to
the browser display as HTML <H2>UPS ShippingRate</h2>.
-
UPSOnLine.XMLDocument.documentElement.childNodes.item(1).text
-
UPSOnLine - the XML structure.
-
XMLDocument - the complete XML document attribute.
-
documentElement - top level of the XML document
(i.e. <package>).
-
childNodes - nodes below the top level (i.e.
<package>).
-
item(1) - <From> child node.
-
item(1).text - 47165 is text of child node.
Client-side XML
<SCRIPT LANGUAGE="xml" ID="UPSOnLine">
<package>
<To>47150</To>
<From>47165</From>
<Weight>17.0</Weight>
<Rate>27.50</Rate>
</package>
</SCRIPT>
<SCRIPT LANGUAGE="JavaScript">
document.write("<H2>UPS Shipping Rate</h2>");
document.write(
"To: " +UPSOnLine.XMLDocument.documentElement.childNodes.item(0).text+"<br>"
+
"From: " + UPSOnLine.XMLDocument.documentElement.childNodes.item(1).text+"<br>"
+
"Weight: " +
UPSOnLine.XMLDocument.documentElement.childNodes.item(2).text+"pounds<br>" +
"Price: $" +UPSOnLine.XMLDocument.documentElement.childNodes.item(3).text+"<br>");
</Script>
Result of XML/JavaScript
UPS Shipping Rate
From: 47150 To: 47165 Weight: 17.0 pounds
Price: $27.50
|
Exercise 1 - XML Tutorial
-
On paper or in your head, create the XML to
define a Person with your age, first and last name instead of Package.
-
Client-side.
- Save the above client-side script as Ex1.htm to
W:\N341.
- Test that it works by loading into the IE browser.
- Modify to display your Person XML.
|
Client Side Table Generation from XML
XML can serve as a data source to a XML-capable client browser such as
IE which can then generate tables and other HTML document information by
parsing and extracting tags, attributes, etc. The data source can be a
data
island in the HTML or a URL to an ASP or CGI that generates the XML
as in the above examples. The following generates a table from the XML
datasource xmlDoc1 below and from the Employees.xml file
as datasource xmlDoc2 located on the server.
Note that to generate the XML from an ASP script Employees.Asp simply replace the
Employees.XML file name with the ASP script name.
James
Borg
Joyce
English
Client-side table Generation
| Table Generated
|
Employees.xml - The XML datasource
<Employees>
<Employee>
<First>James</First> <Last>Borg</Last> <SSN>123456789</SSN>
</Employee>
<Employee>
<First>Joyce</First> <Last>English</Last> <SSN>222334444</SSN>
</Employee>
</Employees>
|
Employees.htm - HTML table generation
<XML ID="xmlDoc2" SRC="Employees.xml"></XML>
<table BORDER dataSrc="#xmlDoc2" >
<THEAD>
<tr><th>First</th><th>Last</th></tr></THEAD>
<tr>
<td><span dataFld=First></span></td>
<td><span dataFld=Last></span></td>
</tr>
</table>
|
Exercise 2 - Client
Side Table Generation
In the above example, xmlDoc2 is the XML data source to the table.
The XML defines two tags, First and Last, that are repeated
for each definition of Employee. The HTML table references these
tags by:
<TD><SPAN dataFld=First></SPAN></TD>
<TD><SPAN dataFld=Last></SPAN></TD>
-
Copy and paste the Employee.htm above into a file.
-
Modify the Employee.htm to display the SSN in the table.
|
XML from file
XML can be dynamically generated, such as retrieving from a database
file.
The key is defining the dataFld to match those of the XML.
The following HTML (portfolio.htm) downloads the static XML file (Portfolio.xml)
and creates a table with selected fields.
Portfolio.xml
<xml
xmlns:rs="urn:schemas-microsoft-com:rowset"
xmlns:z="#RowsetSchema">
<rs:data>
<z:row ID="Ray" SYMBOL="IBM" SHARES="10"/>
<z:row ID="Ray" SYMBOL="MSFT" SHARES="100"/>
<z:row ID="Harold" SYMBOL="FORD" SHARES="15"/>
<z:row ID="Robin" SYMBOL="UAL" SHARES="230"/>
<z:row ID="Robin" SYMBOL="FORD" SHARES="100"/>
<z:row ID="Harold" SYMBOL="GMC" SHARES="20"/>
</rs:data>
</xml> |
Portfolio.htm
<xml ID="xmlDoc2" SRC="Portfolio.xml"></xml>
<table datasrc="#xmlDoc2" datafld="xml">
<tr><td>
<table datasrc="#xmlDoc2" datafld="rs:data">
<tr><td>
<table datasrc="#xmlDoc2" datafld="z:row"
border=1>
<thead>
<tr>
<th>Trader
ID</th>
<th>Stock
SYMBOL</th>
</tr>
</thead>
<tr>
<td><span datafld="ID"></span></td>
<td><span datafld="SYMBOL"></span></td>
</tr>
</table>
</td></tr>
</table>
</td></tr>
</table> |
|
The table definition must reflect the structure of the XML; nested data
require nested tables.
The outer table datafld corresponds to the xml element, the
next table datafld to the rs:data element; the inner-most table
datafld to the z:row element, whose attributes are the values of the
fields for that row.
To display data from the XML, the spans within the table are bound to the
actual field names of the XML (e.g. datafld="ID").
| ID |
SYMBOL |
| Ray |
IBM |
| Ray |
MSFT |
| Harold |
FORD |
| Robin |
UAL |
| Robin |
FORD |
| Harold |
GMC |
|
|
Notice that thead defines the table heading, which is not repeated.
XML from dynamic Server-side database
To download XML from a dynamic server-side database requires changing the
SRC attribute of the HTML file to the program generating the XML. To
execute Portfolio.asp requires:
| <xml ID="xmlDoc2" SRC="Portfolio.asp"></xml> |
The ASP code to generate XML data identical to that of the
Portfolio.xml file is:
Portfolio.asp
<%@ Language=JScript%>
<%
conn = Server.CreateObject("ADODB.Connection");
conn.Mode = 3;
conn.Open ("DRIVER={Microsoft Access Driver (*.mdb)};DBQ=" +
Server.MapPath("Project.mdb"));
rs = conn.Execute("SELECT * FROM PORTFOLIO");
var xmlDoc = Server.CreateObject("Msxml2.DOMDocument");
xmlDoc.async = false;
rs.Save (xmlDoc, 1) ;
// Convert rs recordset to XML tree
rs.Save (Response, 1); //
Output XML as Response
conn.Close();
%> |
|
User Interaction
As a data source, XML elements can be selectively displayed in HTML on
the browser with no interaction with the server. The following moves forward
and backward through a XML data recordset. Click
to view the table (in theory it should interact in this page but I was
not been successful in mixing so much HTML and XML together). Note that
an error is generated and not trapped if the recordset is exceeded by moving
beyond the range of the data.
<XML id=xmlDocUniversity>
<universities>
<university nickname="Surfers">
<address>
<name>UCSB</name>
<location>Santa Barbara, CA</location>
</address>
</university>
<university nickname="Short Horns">
<address>
<name>University of Texas at Arlington</name>
<location>Arlington, TX</location>
</address>
</university>
</universities>
</XML>
Nickname:<SPAN DATASRC="#xmlDocUniversity"
DATAFLD="nickname"></SPAN>
<form>
<INPUT TYPE=BUTTON VALUE="Previous" onclick="xmlDocUniversity.recordset.movePrevious()"
/>
<INPUT TYPE=BUTTON VALUE="Next" onclick="xmlDocUniversity.recordset.moveNext()" />
<BR/>
</form>
<TABLE DATASRC="#xmlDocUniversity" DATAFLD="address"
style="table-layout:fixed" BORDER>
<colwidth="150"> <colwidth="150">
<THEAD><TH>Name</TH><TH>Location</TH></THEAD>
<TR>
<TD><SPAN DATAFLD="name"></SPAN></TD>
<TD><SPAN DATAFLD="location"></SPAN></TD>
</TR>
</TABLE>
|
Exercise 4 - User Interaction
-
Copy and paste the above into a file named xmlUser.htm to
W:\N341 and load into IE browser.
-
Modify to add a button to reset to the first record of the XML data source.
The record set object method to move to the first record is moveFirst()
as with ADO.
|