View
217
Download
1
Embed Size (px)
Citation preview
Web-based Programming Lanjut Pertemuan 10
Matakuliah : M0492 / Web-based Programming Lanjut Tahun : 2007
Bina Nusantara
XML and ADO
• ADO Recordsets Stored as XML• ADO Recordset Namespace• ADO Recordset Schema• Data Islands and Binding• Saving Recordsets as XML• Opening Recordsets• Extensible Styling Language (XSL)
Bina Nusantara
ADO Recordsets Stored as XML
• The first thing is how ADO presents its data as XML.• This is the only way XML can be extracted from ADO at the
moment.
• We have an element for each row in the table, and the fields are attributes of the row element.
• This way of defining data using attributes for each data item means that some of that repetition is reduced
<z:row au_id=“172-32-1176” au_lname=“White” au_fname=“Johnson” /><z:row au_id=“213-46-8915” au_lname=“Green” au_fname=“Marjorie” />
Bina Nusantara
ADO Recordset Namespace
• If using ADO to send and retrieve XML data then there will be a schema associated with the XML data file.
• The schema is embedded into the XML, at the top of document.<xml xmlns:s= “uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882”
xmlns:dt= “uuid:C2F41010-65B3-11d1-A29F-00AA00C14882”
xmlns:rs= “urn:schemas-microsoft-com:rowset”
xmlns:z= “#RowsetSchema”>
Bina Nusantara
ADO Recordset Namespace
Namespace Description
s Identifies the URI for the schema itself.
dt Identifies the URI for data types
rs Identifies the rowset (recordset)
z Identifies the individual rows
Using namespaces ensures that the element names chosen by Microsoft are applied to the correct schema.
Bina Nusantara
ADO Recordset Schema
• The Row Element– Element that identifies schema:
– The definition of an element by ElementType
<s:Schema id=“RowsetSchema”>
<s:ElementType name=“row” content=“eltOnly”>
Value of Content Description
empty The element cannot contain any content
textOnly The element can only contain text, and not other elements
eltOnly The element can only contain other elements, and not text.
both The element can contain both text and other elements
Bina Nusantara
ADO Recordset Schema
– A row element that can only contain other elements.
– This is essentially an empty tag, but for the schema, empty means no content at all, including attributes.
<z:row au_id=“172-32-1176” au_lname=“White” au_fname=“Johnson”
phone =“408 496-7223” address=“10932 Bigge Rd.”
city=“Menlo Park” state=“CA” zip=“94025” contract=“True” />
Bina Nusantara
ADO Recordset Schema
• The Field Attributes– At the XML data, the field values are represented as
attributes.– Schema must define attributes.
• First, the definition of the attribute itself• Second, the definition of the data type for the attribute.
– Define an attribute• define what it will be called – this should map to the field
name of the data.• define 2 other attributes :
– First, a unique number for each attribute – this is the first attribute, the remaining attributes are sequentially numbered.
– The other attribute is “writeunknown” which identifies whether the attribute can be updated
Bina Nusantara
ADO Recordset Schema
<s:AttributeType name=“au_id” rs:number=“1” rs:writeunknown=“true” >
AttributeType also has a child element to define the data type of the attribute:
<s:datatype dt:type=“string” dt:maxLength=“11” rs:maybenull=“false” />
• Data Types– When generating XML from ADO, the data types are
automatically created.
Bina Nusantara
ADO Recordset SchemaThe table below lists the data type supported by the XML-data schema
Type Description
bin.base64 A binary object
bin.hex Hexadecimal octets
boolean 0 or 1 (0 is false, and 1 is true)
char A one character length string
date An ISO 8601 date, without the time. The format is yyyy-mm-dd
dateTime An ISO 8601 date, optionally with the time. The format is yyyy-mm-ddThh:mm:ss
dateTime.tz
An ISO 8601 date, optionally with the time and timezone. The format is yyyy-mm-ddThh:mm:ss-hh:mm. The timezoneindicates the number of hours + or – GMT
fixed.14.4 Fixed with floating point number, with up to 14 digits to the left of the decimal place and up to 4 to the right.
float Floating point number
int Integer number
number Floating point number
Bina Nusantara
ADO Recordset Schema
Type Description
time An ISO 8601 time. The format is hh:mm:ss
time.tz An ISO 8601 time with the optional timezone. The format is hh:mm:ss-hh:mm
i1 An 8 bit integer (1 byte)
i2 A 16 bit integer (2 byte)
i4 A 32 bit integer (4 byte)
r4 A 4 byte real number
r8 An 8 byte real number
ui1 An 8 bit unsigned integer (1 byte)
ui2 A 16 but unsigned integer (2 byte)
ui4 A 32 bit unsigned integer (4 byte)
uri An Universal Resource Indicator
uuid A set of hex digits representing a universally unique identifier. A GUID is an example of this
Bina Nusantara
ADO Recordset Schema
The W3C also allow a set of primitive types
Type Description
entity Represents the XML ENTITY type
entities Represents the XML ENTITIES type
enumeration Represents an enumerated type
id Represents the XML ID type
idref Represents the XML IDREF type
idrefs Represents the XML IDREFS type
nmtoken Represents the XML NMTOKEN type
nmtokens Represents the XML NMTOKENS type
notation Represents the XML NOTATION type
string Represents a string type
Bina Nusantara
Data Islands and Binding
• Create a data island with the <XML> tag in an HTML page
This reference an external XML file as the source of the data. Alternatively you can embed the XML data within the <XML> tag.
<XML ID=“dsoData” SRC=“authors.xml”></XML>
<XML ID=“dsoData”> <Authors> <Author>
<au_id>172-32-1176</au_id><au_lname>White</au_lname><au_fname>Johnson</au_fname>
</Author> <Authors></XML>
Bina Nusantara
Data Islands and Binding• Binding can take two forms
– First, bind a single elements
– Second, use table binding
<INPUT TYPE=“TEXT” DATASRC=“#dsoData” DATAFLD=“au_id”></INPUT>
<TABLE DATASRC =“#dsoData”>
<TR>
<TD> <INPUT TYPE=“TEXT” DATAFLD=“au_id”></INPUT></TD>
<TD> <INPUT TYPE=“TEXT” DATAFLD=“au_fname”></INPUT></TD>
</TR>
</TABLE>
Bina Nusantara
Data Islands and Binding
• Binding Hierarchical DataThere’ll be a problem if you try to bind a set of XML data generated by ADO. Because IE doesn’t recognize schemas as a definition of the data.
So IE sees two sets of data – the schema and the actual
<XML> <s:Schema> ….. schema data </s:Schema> <rs:data> <z:row … /> <z:row … /> </rs:data></XML>
Bina Nusantara
Data Islands and BindingThe solution to this hierarchical binding is to use two levels of binding
<TABLE ID=“tblData” DATASRC=“#dsoData” DATAFLD=“rs:data”>
<TR><TD>
<TABLE ID=“tblData” BORDER=“1” DATASRC =“#dsoData” DATAFLD=“z:row”>
<TR>
<TD> <SPAN DATAFLD=“au_id”></SPAN></TD>
<TD> <SPAN DATAFLD=“au_fname”></SPAN></TD>
</TR>
</TABLE>
</TD></TR>
</TABLE>
Bina Nusantara
Data Islands and Binding• <Publishers>• <Publisher> <pub_id>0736</pub_id>• <pub_name>New Moon Books</pub_name>• <city>Boston</city>• <state>MA</state>• <country>USA</country>• <titles> <title_id>BU2075</title_id>• <title>You Can Combat Computer Stress!</title>• <price>$2.99</price>• <pubdate>6/30/91</pubdate>• <sale>• <stor_id>7896</stor_id>• <ord_num>X999</ord_num>• <ord_date>2/21/93</ord_date>• <qty>35</qty>• </sale>• </titles>• <employee> <emp_id>PDH4740M</emp_id>• <fname>Palle</fname>• <minit>D</minit>• <lname>Ibsen</lname>• </employee>• <employee> <emp_id>KFJ64308F</emp_id>• <fname>Karin</fname>• <minit>F</minit>• <lname>Josephs</lname>• </employee>• </Publisher>• </Publishers>
Bina Nusantara
1. <HTML><HEAD><TITLE>DataBinding.html</TITLE></HEAD>
2. <BODY>3. <XML ID="dsoData" SRC="publishers.xml"></XML>
4. <TABLE ID="tblPublishers" DATASRC="#dsoData" BORDER="1">5. <TR><TD><DIV DATAFLD = "pub_name"></DIV></TD>6. <TD><DIV DATAFLD = "city"></DIV></TD>7. <TD><DIV DATAFLD = "state"></DIV></TD> </TR>8. <TR> <TD COLSPAN=4>9. <TABLE>10. <TR><TD>Titles</TD> </TR>11. <TR><TD COLSPAN=4>12. <TABLE DATASRC="#dsoData" DATAFLD="titles" BORDER="1">13. <TR><TD><DIV DATAFLD = "title"></DIV></TD><TD><DIV DATAFLD = "price"></DIV></TD>14. </TR>15. <TR><TD COLSPAN=3>16. <TABLE DATASRC="#dsoData" DATAFLD="sale" BORDER="1">17. <TR><TD><DIV DATAFLD = "ord_date"></DIV></TD><TD><DIV DATAFLD = "qty"></DIV></TD></TR>18. </TABLE>19. </TD>20. </TR>21. </TABLE>22. </TD> </TR>23. <TR> <TD>Employees</TD></TR>24. <TR> <TD COLSPAN=4>25. <TABLE DATASRC="#dsoData" DATAFLD="employee" BORDER="1">26. <TR><TD><DIV DATAFLD = "fname"></DIV></TD> <TD><DIV DATAFLD = "lname"></DIV></TD> </TR>27. </TABLE>28. </TD> </TR>29. </TABLE>30. </TD>31. </TR>32. </TABLE>33. </BODY>
Bina Nusantara
Saving Recordsets as XML
• ADO generate XML recordsets by using the Save method of the Recordset object, and specifying the format as adPersistXML or 1.
• Persisting ADO Recordsets to a StreamA Stream is simply a block of data in memory, which is not processed in any way. ADO 2.5 introduces the new Stream object, and can be the target for saving a recordset
Set rsAuthors = Server.CreateObject(“ADODB.Recordset”)Set stmAuthors = Server.CreateObject(“ADODB.Stream”)
rsAuthors.Open “authors”, strConn
rsAuthors.Save stmAuthors, 1 ‘adPersistXML
Bina Nusantara
Saving Recordsets as XML
Use the methods and properties of the stream to manipulate the data.For example, extract the XML into a string using the ReadText method:
At this stage strXMLAuthors contains the complete XML recordset, including the schema.
• Persisting ADO Recordsets to the Response ObjectSaves the recordset as XML directly into the Response object.
strXMLAuthors = stmAuthors.ReadText
rsAuthors.Save Response, 1 ‘adPersistXML
Bina Nusantara
Saving Recordsets as XML<%
‘Using the DOM to create a Data Island• Dim strConn• Dim xmlDOM• Dim strXML
• Set rsAuthors = Server.CreateObject("ADODB.Recordset")• Set xmlDOM = Server.CreateObject("MSXML.DOMDocument")
• rsAuthors.Open "authors", strConn• rsAuthors.Save xmlDOM, 1
• rsAuthors.Close• Set rsAuthors = Nothing
• strXML = xmlDOM.xml
• strXML = "<XML" & Mid(strXML,5,Len(strXML)-10) & "XML>"
• Response.Write strXML%>
Bina Nusantara
Saving Recordsets as XML<XML ID="dsoAuthors"><%
‘Using Response to Create a Data Island• Dim rsAuthors• Set rsAuthors = Server.CreateObject ("ADODB.Recordset")
• rsAuthors.Open "authors", strConn
• rsAuthors.Save Response, 1• rsAuthors.Close• Set rsAuthors = Nothing%></XML>
To see the result on our HTML page, activate the View Source menu
Bina Nusantara
Saving Recordsets as XML<xml xmlns:s='uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882'
xmlns:dt='uuid:C2F41010-65B3-11d1-A29F-00AA00C14882'xmlns:rs='urn:schemas-microsoft-com:rowset'xmlns:z='#RowsetSchema'>
<s:Schema id='RowsetSchema'><s:ElementType name='row' content='eltOnly' rs:CommandTimeout='30'>
<s:AttributeType name='au_id' rs:number='1' rs:writeunknown='true'><s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='11' rs:maybenull='false'/>
</s:AttributeType><s:AttributeType name='au_lname' rs:number='2' rs:writeunknown='true'>
<s:datatype dt:type='string' rs:dbtype='str' dt:maxLength='40' rs:maybenull='false'/></s:AttributeType>….<s:extends type='rs:rowbase'/>
</s:ElementType></s:Schema><rs:data>
<z:row au_id='172-32-1176' au_lname='White' au_fname='Johnson' phone='408 496-7223' address='10932 Bigge Rd.' city='Menlo Park' state='CA' zip='94025' contract='True'/>
<z:row au_id='213-46-8915' au_lname='Green' au_fname='Marjorie' phone='415 986-7020' address='309 63rd St. #411' city='Oakland' state='CA' zip='94618' contract='True'/>
<z:row au_id='238-95-7766' au_lname='Carson' au_fname='Cheryl' phone='415 548-7723' address='589 Darwin Ln.' city='Berkeley' state='CA' zip='94705' contract='True'/>
<z:row au_id='267-41-2394' au_lname='O'Leary' au_fname='Michael' phone='408 286-2428' address='22 Cleveland Av. #14'
city='San Jose' state='CA' zip='95128' contract='True'/><z:row au_id='274-80-9391' au_lname='Straight' au_fname='Dean' phone='415 834-2919' address='5420 College Av.'
city='Oakland' state='CA' zip='94609' contract='True'/><z:row au_id='341-22-1782' au_lname='Smith' au_fname='Meander' phone='913 843-0462' address='10 Mississippi Dr.'
city='Lawrence' state='KS' zip='66044' contract='False'/><z:row au_id='409-56-7008' au_lname='Bennet' au_fname='Abraham' phone='415 658-9932' address='6223 Bateman St.'
city='Berkeley' state='CA' zip='94705' contract='True'/>….
</rs:data></xml>
Bina Nusantara
Opening Recordsets1. <%2. Set rsData = Server.CreateObject("ADODB.Recordset")3. Set stmData = Server.CreateObject("ADODB.Stream")4. Set domXML = Server.CreateObject("MSXML.DOMDocument")
5. rsData.Open "authors", strConn
6. rsData.Save domXML, 1 ‘adPersistXML=17. rsData.Close
8. stmData.Open9. stmData.WriteText domXMl.xml10. stmData.SetEOS11. stmData.Position = 012. rsData.Open stmData
13. Response.Write "<H1>Opening Authors Recordset from a DOM Object<HR></H1>"14. Response.Write "<TABLE><TR><TD>au_id</TD><TD>au_fname</TD><TD>au_lname</TD><TD>phone</
TD></TR>”15. While rsData.EOF=False16. Response.Write "<TR>"17. Response.Write "<TD><INPUT TYPE='TEXT' VALUE=" & rsData(0) & "></INPUT></TD>"18. Response.Write "<TD><INPUT TYPE='TEXT' VALUE=" & rsData(1) & "></INPUT></TD>"19. Response.Write "<TD><INPUT TYPE='TEXT' VALUE=" & rsData(2) & "></INPUT></TD>"20. Response.Write "<TD><INPUT TYPE='TEXT' VALUE=" & rsData(3) & "></INPUT></TD>"21. Response.Write "</TR>"22. rsData.moveNext23. Wend24. Response.Write "</TABLE>"25. %>
Bina Nusantara
Extensible Styling Language (XSL)
• XSL is an XML-based language to allow us to transform XML data.– This transformation can be between one format of XML
and another, or from XML to HTML or from XML to any type of text output.
• XML tags just identify the data. If we want to display XML data we need to style it in some way.
• XSL made up of two parts:– a transformation language– a formatting language
Bina Nusantara
Extensible Styling Language (XSL)
• XSL Stylesheets– The idea behind XSL is that we have a set of rules that
match elements or attributes in the XML.– These rules are known as template, and within a template
we can loop through elements and attributes, apply other templates and perform other types of processing
– Text that is not part of an XSL processing instruction is output, so this is how we transform XML – by matching elements and outputting text and element values
Bina Nusantara
Extensible Styling Language (XSL)
• XSL Stylesheets– The top-level tag identifies the stylesheet and the
namespace, which applies to it.
This finishing tag:
– within the stylesheet, the tags take the form of templates, which match portions of the XML document.
<xsl:stylesheet xmlns:xsl=http://www.w3.org/TR/WD-xsl>
</xsl:stylesheet>
Bina Nusantara
Extensible Styling Language (XSL)• This stylesheet converts an ADO recordset in XML into an HTML table.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"><xsl:template match ="/">
<HTML><BODY>
<xsl:apply-templates select="//rs:data" />
</BODY></HTML>
</xsl:template>
<xsl:template match="//rs:data"><TABLE BORDER="1"><xsl:for-each select="z:row"> <TR> <xsl:for-each select="(@*)"> <TD> <xsl:value-of/> </TD> </xsl:for-each> </TR></xsl:for-each></TABLE>
</xsl:template></xsl:stylesheet>
Bina Nusantara
Extensible Styling Language (XSL)– We start with the stylesheet declaration:
– The first command is a template command telling us which element we should match. In this case, we are matching the root element, denoted by the / symbol:
– At this stage we have started the processing of the XML tree. If we are converting to HTML, we need to output HTML tags that start an HTML document.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match ="/">
<HTML><BODY>
Bina Nusantara
Extensible Styling Language (XSL)
– Now we need to find the actual data. Use apply-templates to search for another template. This template will be applied, and once all processing in it has completed, processing returns to the current template.
– After the inner template has been processed we can finish off our HTML document
<xsl:apply-templates select="//rs:data" />
</BODY></HTML>
</xsl:template>
Bina Nusantara
Extensible Styling Language (XSL)
– To process each row of data, use the “for-each” statement, which is as much the same as a “for..each” loop in VBScript. The “select” part of the statement tells XSL which elements to loop though.
– To process the attributes, again use the “for-each” construct, with a different “select”. To match an attribute, we have to use the @ symbol, so @* means match every attribute.
<xsl:for-each select="z:row">
<xsl:for-each select="(@*)">
Bina Nusantara
Extensible Styling Language (XSL)
– The “value-of” instruction tells XSL to output the value of a node. And since we are not stating anything specific to output, the value of the current node is output:
• Embedded StylingThe easiest way to apply an XSL stylesheet is to specify it in the XML file.
<xsl:value-of/>
<?xml-stylesheet type=“text/xsl” href=“xsl_file_name.xsl”?>
Bina Nusantara
Extensible Styling Language (XSL)1. <?xml version = "1.0"?>2. <?xml:stylesheet type = "text/xsl" href = "contact_list.xsl"?>3. <!-- contact.xml -->4. <contacts>5. <contact>6. <lastname>Black</lastname>7. <firstname>John</firstname>8. </contact>9. <contact>10. <lastname>Green</lastname>11. <firstname>Sue</firstname>12. </contact>13. <contact>14. <lastname>Red</lastname>15. <firstname>Bob</firstname>16. </contact>17. <contact>18. <lastname>Blue</lastname>19. <firstname>Mary</firstname>20. </contact>21. <contact>22. <lastname>White</lastname>23. <firstname>Mike</firstname>24. </contact>25. <contact>26. <lastname>Brown</lastname>27. <firstname>Jane</firstname>28. </contact>29. </contacts>
Bina Nusantara
1. <?xml version = "1.0"?>2. <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">3. <!-- contact_list.xsl -->4. <xsl:template match ="/">5. <HTML>6. <BODY>7.8. <DIV ID = "data"> <xsl:apply-templates/> </DIV>9.10. <SCRIPT TYPE="text/javascript" LANGUAGE="JavaScript">11. <xsl:comment><![CDATA[12. var styleSheet = document.XSLDocument;13. var xmlDocument = document.XMLDocument;14. 15. function update(scope, sortKey)16. {17. var sortBy = styleSheet.selectSingleNode("//@order-by");18. sortBy.value = sortKey;19. var scopeBy = styleSheet.selectSingleNode("//xsl:for-each/@select");20. scopeBy.value = scope;21. data.innerHTML = xmlDocument.documentElement.transformNode(styleSheet);22. }23. ]]>24. </xsl:comment>25. </SCRIPT>26.27. <TABLE BORDER = "1" DATASRC = "#xmlData" DATAPAGESIZE = "4" ID = "tbl">28. <THEAD>29. <TR> <TH>Last Name</TH> <TH>First Name</TH> </TR>30. </THEAD>31. <TR> <TD><SPAN DATAFLD = "lastname"></SPAN></TD>32. <TD><SPAN DATAFLD = "firstname"></SPAN></TD>33. </TR>34. </TABLE>
Bina Nusantara
35.36. <INPUT TYPE = "button" VALUE = "Revert" ONCLICK = "update('contact','+firstname;+lastname');"/>37. <BR/>38. <INPUT TYPE = "button" VALUE = "Sort By Last Name" ONCLICK =
"update('contact','+lastname;+firstname');"/>39. <INPUT TYPE = "button" VALUE = "Sort By First Name" ONCLICK =
"update('contact','-firstname;+lastname');"/>40. <BR/>41. <INPUT TYPE = "button" VALUE = "Filter Last Name Start with 'B'" 42. ONCLICK = "update('contact[lastname > \'B\' and lastname < \'C\']','+firstname;
+lastname');"/>43. <BR/>44. <INPUT TYPE = "button" VALUE = "|<" ONCLICK = "tbl.firstPage();"/>45. <INPUT TYPE = "button" VALUE = "<" ONCLICK = "tbl.previousPage();"/>46. <INPUT TYPE = "button" VALUE = ">" ONCLICK = "tbl.nextPage();"/>47. <INPUT TYPE = "button" VALUE = ">|" ONCLICK = "tbl.lastPage();"/>48.49. </BODY>50. </HTML>51. </xsl:template>
52. <xsl:template match="contacts">53. <XML ID = "xmlData">54. <contacts>55. <xsl:for-each select = "contact"56. order-by = "+firstname;+lastname">57. <contact>58. <lastname><xsl:value-of select = "lastname"/> </lastname>59. <firstname><xsl:value-of select = "firstname"/> </firstname>60. </contact>61. </xsl:for-each>62. </contacts>63. </XML>64. </xsl:template>65. </xsl:stylesheet>