210 likes | 359 Views
XML and DTD. Please you speaker notes for additional information!. <?xml version="1.0"?> <!DOCTYPE customer [ <!ELEMENT customer (custid,name,addr,city,state,zip)> <!ELEMENT custid (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT addr (#PCDATA)>
E N D
XML and DTD Please you speaker notes for additional information!
<?xml version="1.0"?> <!DOCTYPE customer[ <!ELEMENT customer (custid,name,addr,city,state,zip)> <!ELEMENT custid (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT zip (#PCDATA)> ]> <customer> <custid>11111</custid> <name>Susan Ash</name> <addr>12 Elm St</addr> <city>Braintree</city> <state>MA</state> <zip>02184</zip> </customer> This holds just one customer - customer is the root. The elements are the declarations. I used the xml document shown here in a html program on the next slide to produce the output that you see. Note that the data you see comes from the xml, the rest of the information comes from the html.
<html> <head><title>Customers</title> <body> <xml id="Customer_Data" src="customer.xml"></xml> <p id="Page_Header">Fall River Store</p> <p id="Sub_Header">112 Main Street<br>Fall River, MA</p> <table> <tr> <td>Customer Id:</td> <td><span datasrc="#Customer_Data" datafld="custid"></span></td> </tr> <tr> <td>Name:</td> <td><span datasrc="#Customer_Data" datafld="name"></span></td> </tr> <tr> <td>Address:</td> <td><span datasrc="#Customer_Data" datafld="addr"></span></td> </tr> <tr> <td>City:</td> <td><span datasrc="#Customer_Data" datafld="city"></span></td> </tr> <tr> <td>State:</td> <td><span datasrc="#Customer_Data" datafld="state"></span></td> </tr> <tr> <td>Zip:</td> <td><span datasrc="#Customer_Data" datafld="zip"></span></td> </tr> </table></body></html> This establishes Customer_Id as customer.xml. This prints the header information at the top of the screen. This code establishes which data field is to be shown from the xml. This shows the identifying literals.
<?xml version="1.0"?> <!DOCTYPE customers[ <!ELEMENT customers (customer+)> <!ELEMENT customer (custid,name,addr,city,state,zip)> <!ELEMENT custid (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT zip (#PCDATA)> ]> <customers> <customer> <custid>11111</custid> <name>Susan Ash</name> <addr>12 Elm St</addr> <city>Braintree</city> <state>MA</state> <zip>02184</zip> </customer> <customer> <custid>22222</custid> <name>Stephen Daniels</name> <addr>45 East St</addr> <city>Brooklyn</city> <state>NY</state> <zip>11211</zip> </customer> </customers> I now want to add a second customer to my data. To do this I make the root the name of the customer group or collection (customers) and each individual customer becomes a record (customer).
<html> <head><title>Customers</title> <body> <xml id="Customer_Data" src="customer1.xml"></xml> <p id="Page_Header">Fall River Store</p> <p id="Sub_Header">112 Main Street<br>Fall River, MA</p> <table> <tr> <td>Customer Id:</td> <td><span datasrc="#Customer_Data" datafld="custid"></span></td> </tr> <tr> <td>Name:</td> <td><span datasrc="#Customer_Data" datafld="name"></span></td> </tr> <tr> <td>Address:</td> <td><span datasrc="#Customer_Data" datafld="addr"></span></td> </tr> <tr> <td>City:</td> <td><span datasrc="#Customer_Data" datafld="city"></span></td> </tr> <tr> <td>State:</td> <td><span datasrc="#Customer_Data" datafld="state"></span></td> </tr> <tr> <td>Zip:</td> <td><span datasrc="#Customer_Data" datafld="zip"></span></td> </tr> </table>
<br /><br /><br /> <button onClick="Customer_Data.recordset.moveFirst()">First</button> <button onClick="Customer_Data.recordset.movePrevious(); if (Customer_Data.recordset.BOF) Customer_Data.recordset.moveFirst()"> Previous</button> <button onClick="Customer_Data.recordset.moveNext(); if (Customer_Data.recordset.EOF) Customer_Data.recordset.moveLast()"> Next</button> <button onClick="Customer_Data.recordset.moveLast()">Last</button> </body> </html>
<?xml version="1.0"?> <!DOCTYPE customers [ <!ELEMENT customers (customer+)> <!ELEMENT customer (custid,name,addr,city,state,zip)> <!ELEMENT custid (#PCDATA)> <!ELEMENT name (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT zip (#PCDATA)> ]> <customers> <customer> <custid>11111</custid> <name>Susan Ash</name> <addr>12 Elm St</addr> <city>Braintree</city> <state>MA</state> <zip>02184</zip> </customer> <customer> <custid>22222</custid> <name>Stephen Daniels</name> <addr>45 East St</addr> <city>Brooklyn</city> <state>NY</state> <zip>11211</zip> </customer> </customers> When using the DTD you specify: <!DOCTYPE root [ declarations ]> Thecustomer elementcontains 6 child elements that are listed. The first element says that customers (the root element), can contain one or more occurrences of customer. Each child element is listed individually and is given the #PCDATA which indicates character/string data.
<?xml version="1.0"?> <!DOCTYPE customers [ <!ELEMENT customers (customer+)> <!ELEMENT customer (name,addr,city,state,zip)> <!ATTLIST customer custid ID #REQUIRED> <!ELEMENT name (#PCDATA)> <!ELEMENT addr (#PCDATA)> <!ELEMENT city (#PCDATA)> <!ELEMENT state (#PCDATA)> <!ELEMENT zip (#PCDATA)> ]> <customers> <customer custid=”A1111"> <name>Susan Ash</name> <addr>12 Elm St</addr> <city>Braintree</city> <state>MA</state> <zip>02184</zip> </customer> <customer custid=”B2222"> <name>Stephen Daniels</name> <addr>45 East St</addr> <city>Brooklyn</city> <state>NY</state> <zip>11211</zip> </customer> </customers> To validate, I used:http://validator.w3.org/ In this example, custid is not a child element, it is an attribute of customer. ID means calls for uniqueness and #REQUIRED means an entry is required. Quote from w3schools: “Should you avoid using attributes? Here are some of the problems using attributes: * attributes cannot contain multiple values (child elements can) * attributes are not easily expandable (for future changes) * attributes cannot describe structures (child elements can) * attributes are more difficult to manipulate by program code * attribute values are not easy to test against a DTD If you use attributes as containers for data, you end up with documents that are difficult to read and maintain. Try to use elements to describe data. Use attributes only to provide information that is not relevant to the data.
<?xml version="1.0" ?> <!-- document type declaration follows --> <!DOCTYPE donors [ <!ELEMENT donors (donor+)> <!ELEMENT donor(name, address, yrfirst?, contact?) <!ATTLIST donor idno ID #REQUIRED> <!ELEMENT name (#PCDATA) <!ATTLIST name title (Mr. | Mrs. | Ms. | Dr.) #IMPLIED> <!ELEMENT address (#PCDATA)> <!ELEMENT yrfirst (#PCDATA)> <!ELEMENT contact (#PCDATA)> ]> <donors> <donor idno="11111"> <name title="Mr.">John Doe</name> <address> <![CDATA[ 123 Elm St Braintree, MA 02184 ]]> <yrfirst>1995</yrfirst> <contact>Ann Smith</contact> </donor>
<?xml version="1.0" ?> <!-- document type declaration follows --> <!DOCTYPE donors [ <!ELEMENT donors (donor+)> <!ELEMENT donor(name, address, yrfirst?, contact?) <!ATTLIST donor idno ID #REQUIRED> <!ELEMENT name (#PCDATA) <!ATTLIST name title (Mr. | Mrs. | Ms. | Dr.) #IMPLIED> <!ELEMENT address (#PCDATA)> <!ELEMENT yrfirst (#PCDATA)> <!ELEMENT contact (#PCDATA)> ]> <donors> <donor idno="11111"> <name title="Mr.">John Doe</name> <address> <![CDATA[ 123 Elm St Braintree, MA 02184 ]]> <yrfirst>1995</yrfirst> <contact>Ann Smith</contact> </donor> Need the > at the end.
<?xml version="1.0" ?> <!-- document type declaration follows --> <!DOCTYPE donors [ <!ELEMENT donors (donor+)> <!ELEMENT donor (name, address, yrfirst?, contact?)> <!ATTLIST donor idno ID #REQUIRED> <!ELEMENT name (#PCDATA)> <!ATTLIST name title (Mr. | Mrs. | Ms. | Dr.) #IMPLIED> <!ELEMENT address (#PCDATA)> <!ELEMENT yrfirst (#PCDATA)> <!ELEMENT contact (#PCDATA)> ]> <donors> <donor idno="11111"> <name title="Mr.">John Doe</name> <address> <![CDATA[ 123 Elm St Braintree, MA 02184 ]]> </address> <yrfirst>1995</yrfirst> <contact>Ann Smith</contact> </donor>
<?xml version="1.0" ?> <!-- document type declaration follows --> <!DOCTYPE donors [ <!ELEMENT donors (donor+)> <!ELEMENT donor (name, address, yrfirst?, contact?)> <!ATTLIST donor idno ID #REQUIRED> <!ELEMENT name (#PCDATA)> <!ATTLIST name title (Mr. | Mrs. | Ms. | Dr.) #IMPLIED> <!ELEMENT address (#PCDATA)> <!ELEMENT yrfirst (#PCDATA)> <!ELEMENT contact (#PCDATA)> ]> <donors> <donor idno="A1111"> <name title="Mr.">John Doe</name> <address> <![CDATA[ 123 Elm St Braintree, MA 02184 ]]> </address> <yrfirst>1995</yrfirst> <contact>Ann Smith</contact> </donor> <donor idno="B2222"> <name title="Ms.">Mary Wilson</name> The idno is an attribute of donor. The ID means it must be unique. The title is an attribute of name and it can have any one of the titles included in the list. #IMPLIED means that this is an optional attribute. CDATA allows for a large block of text that the XML processors interprets only as text.
<address> <![CDATA[ 14 Main St Fall River, MA 02770 ]]> </address> <yrfirst>1996</yrfirst> <contact>David Costa</contact> </donor> <donor idno="C3333"> <name title="Ms.">Nancy Taylor</name> <address> <![CDATA[ 1 Heritage Rd New Bedford, MA 02775 ]]> </address> <yrfirst>1994</yrfirst> <contact>Ann Smith</contact> </donor> <donor idno="D4444"> <name title="Mr.">Robert Brooks</name> <address> <![CDATA[ 45 East St Weymouth, MA 02176 ]]> </address> <yrfirst>1996</yrfirst> <contact>Roger Brown</contact> </donor> </donors>
DONOR DONATION The XML has been set up to have a series of donors and information about the donor and for each donor, one or more donations and information about the donation.
<?xml version="1.0" ?> <!-- document type declaration follows --> <!DOCTYPE donors [ <!ELEMENT donors (donor+)> <!ELEMENT donor (name, address, yrfirst?, contact?, donations)> <!ATTLIST donor idno ID #REQUIRED> <!ELEMENT name (#PCDATA)> <!ATTLIST name title (Mr. | Mrs. | Ms. | Dr.) #IMPLIED> <!ELEMENT address (#PCDATA)> <!ELEMENT yrfirst (#PCDATA)> <!ELEMENT contact (#PCDATA)> <!ELEMENT donations (donation+)> <!ELEMENT donation (datecont, amtcont)> <!ATTLIST donation donationid ID #REQUIRED> <!ATTLIST donation givenby IDREF #REQUIRED> <!ELEMENT datecont (#PCDATA)> <!ELEMENT amtcont (#PCDATA)> ]> <donors> <donor idno="A1111"> <name title="Mr.">John Doe</name> <address> <![CDATA[ 123 Elm St Braintree, MA 02184 ]]> </address> <yrfirst>1995</yrfirst> <contact>Ann Smith</contact> Donations has been added to the list of elements in donor. Within donation there is room for multiple donations. Each with have a required unique number called donationid, a reference to the donor called given by and the elements date of contribution and amount of contribution.
<donations> <donation donationid="A123001" givenby = "A1111"> <datecont>7/5/2000</datecont> <amtcont>1200</amtcont> </donation> <donation donationid="B124001" givenby = "A1111"> <datecont>5/14/2000</datecont> <amtcont>500</amtcont> </donation> </donations> </donor> <donor idno="B2222"> <name title="Ms.">Mary Wilson</name> <address> <![CDATA[ 14 Main St Fall River, MA 02770 ]]> </address> <yrfirst>1996</yrfirst> <contact>David Costa</contact> <donations> <donation donationid="A123002" givenby = "B2222"> <datecont>5/15/2000</datecont> <amtcont>500</amtcont> </donation> </donations> </donor> <donor idno="C3333"> <name title="Ms.">Nancy Taylor</name> This shows the data for the first donor which includes donation 1 and donation2. For each donation, there are the identifying attributes and the date and amount of the contribution.
<address> <![CDATA[ 1 Heritage Rd New Bedford, MA 02775 ]]> </address> <yrfirst>1994</yrfirst> <contact>Ann Smith</contact> <donations> <donation donationid="A123003" givenby = "C3333"> <datecont>1/5/2000</datecont> <amtcont>1000</amtcont> </donation> <donation donationid="A123004" givenby = "C3333"> <datecont>2/20/2000</datecont> <amtcont>600</amtcont> </donation> <donation donationid="B124002" givenby = "C3333"> <datecont>1/12/2000</datecont> <amtcont>1000</amtcont> </donation> <donation donationid="C125001" givenby = "C3333"> <datecont>5/5/2000</datecont> <amtcont>100</amtcont> </donation> </donations> </donor> <donor idno="D4444"> <name title="Mr.">Robert Brooks</name>
<address> <![CDATA[ 45 East St Weymouth, MA 02176 ]]> </address> <yrfirst>1996</yrfirst> <contact>Roger Brown</contact> <donations> <donation donationid="A123005" givenby = "D4444"> <datecont>1/1/2000</datecont> <amtcont>500</amtcont> </donation> <donation donationid="A124003" givenby = "D4444"> <datecont>5/1/2000</datecont> <amtcont>1000</amtcont> </donation> <donation donationid="C125002" givenby = "D4444"> <datecont>8/1/2000</datecont> <amtcont>250</amtcont> </donation> </donations> </donor> </donors>