Part three of the tutorial will outline two methods for creating a valid XML document. The first utilizes a simple text editor, and the second utilizes an Access database and ASP scripts. The DTD and XSL stylesheet created in parts one and two of the tutorial will be used to create the examples.
<?xmlversion"1.0"?> <!DOCTYPE rootelement SYSTEM "Apart_Dir2.dtd"> <?xml-stylesheet href="xml.xsl" type="text/xsl"?>
<?xmlversion"1.0"?> <!DOCTYPE rootelement SYSTEM "Apart_Dir2.dtd"> <?xml-stylesheet href="xml.xsl" type="text/xsl"?> <Directory>
<?xmlversion"1.0"?> <!DOCTYPE rootelement SYSTEM "Apart_Dir2.dtd"> <?xml-stylesheet href="xml.xsl" type="text/xsl"?> <Directory> <City> Chapel Hill <Apartment>
<?xmlversion"1.0"?> <!DOCTYPE Directory SYSTEM "Apart_Dir2.dtd"> <?xml-stylesheet href="xml.xsl" type="text/xsl"?> <Directory> <City> Chapel Hill <Apartment> <Name> Alta Springs </Name> <Address> 1000 Spring Meadow Drive </Address> <Office_phone> 919-932-9300 </Office_phone> <Pets> Yes, $300 deposit. </Pets> <Utilities> Water </Utilities> <Features> Fireplace, Balcony, Pool, Workout and Laundry Facilities</Features>
<?xmlversion"1.0"?> <!DOCTYPE Directory SYSTEM "Apart_Dir2.dtd"> <?xml-stylesheet href="xml.xsl" type="text/xsl"?> <Directory> <City> Chapel Hill <Apartment> <Name> Alta Springs </Name> <Address> 1000 Spring Meadow Drive </Address> <Office_phone> 919-932-9300 </Office_phone> <Pets> Yes, $300 deposit. </Pets> <Utilities> Water </Utilities> <Features> Fireplace, Balcony, Pool, Workout and Laundry Facilities</Features> <Unit> <Bedroom> Three </Bedroom> <Bathroom> Two </Bathroom> <Sq_feet> 1200 </Sq_feet> <Rent> $1450 </Rent> </Unit> <Unit> <Bedroom> Two</Bedroom> <Bathroom> Two </Bathroom> <Sq_feet> 900 </Sq_feet> <Rent> $1150 </Rent> </Unit> <Unit> <Bedroom> One </Bedroom> <Bathroom> One </Bathroom> <Sq_feet> 750 </Sq_feet> <Rent> $1000 </Rent> </Unit> </Apartment>
<?xmlversion"1.0"?> <!DOCTYPE Directory SYSTEM "Apart_Dir2.dtd"> <?xml-stylesheet href="xml.xsl" type="text/xsl"?> <Directory> <City> Chapel Hill <Apartment> <Name> Alta Springs </Name> <Address> 1000 Spring Meadow Drive </Address> <Office_phone> 919-932-9300 </Office_phone> <Pets> Yes, $300 deposit. </Pets> <Utilities> Water </Utilities> <Features> Fireplace, Balcony, Pool, Workout and Laundry Facilities</Features> <Unit> <Bedroom> Three </Bedroom> <Bathroom> Two </Bathroom> <Sq_feet> 1200 </Sq_feet> <Rent> $1450 </Rent> </Unit> <Unit> <Bedroom> Two</Bedroom> <Bathroom> Two </Bathroom> <Sq_feet> 900 </Sq_feet> <Rent> $1150 </Rent> </Unit> <Unit> <Bedroom> One </Bedroom> <Bathroom> One </Bathroom> <Sq_feet> 750 </Sq_feet> <Rent> $1000 </Rent> </Unit> </Apartment> <Apartment> <Name> Bradford Place </Name> <Address> 1010-A Kingswood Drive </Address> <Office_phone> 919-933-7985 </Office_phone> <Utilities> Water, Sewer </Utilities> <Features> Fireplace, Balcony, W/D in unit, Walk-in closets </Features> <Pets> Yes, $300 deposit </Pets> <Unit> <Bedroom> Townhouse </Bedroom> <Bathroom> Two 1/2 </Bathroom> <Sq_feet> 1050 </Sq_feet> <Rent> $850 </Rent> </Unit> <Unit> <Bedroom> Two </Bedroom> <Bathroom> Two </Bathroom> <Sq_feet> 990 </Sq_feet> <Rent> $790 </Rent> </Unit> <Unit> <Bedroom> One </Bedroom> <Bathroom> One </Bathroom> <Sq_feet> 750 </Sq_feet> <Rent> $690 </Rent> </Unit> </Apartment>
</City> </Directory>
<html> <head> <title> XML Apartment Guide </title> </head> <body>
<%
query = "SELECT City FROM Apartment ORDER BY Apartment_ID"
' Create a connection to the database
set connection = Server.CreateObject("ADODB.Connection")
' Open a connection
connection.open "xml"
' Send the query and collect the result in "listData"
set listData = connection.execute (query)
%>
<p> <h2>Locate an Apartment in Your City</h2>
<hr>
<form action="apart_xml.asp" method="GET">
<p> Please select the city:
<select name="city">
<%
' Loop through the record set getting all the matching data
do while not listData.eof
name=listData("Apartment")
msg = "<option value= " & name & ">" & name & vbCrLf
Response.Write (msg)
listData.MoveNext
loop
%>
</select>
<INPUT type="submit" value="Find Apartment">
</form>
</body>
</html>
<% Response.ContentType = "text/xml" %> <?xmlversion "1.0"?> <!DOCTYPE Directory SYSTEM "ApartDir2.dtd"> <?xml-stylesheet href="xml.xsl" type="text/xsl"?>
<%
city = request.querystring("city")
apart_query = "SELECT Apartment_ID, Apartment, Address, City,"
apart_query = apart_query & "Telephone, Pets, Utilities, Features "
apart_query = apart_query & "FROM Apartment WHERE City = '" & city & "'"
' Create a connection to the database
set connection = Server.CreateObject("ADODB.Connection")
' Open a connection
connection.open "kemp"
' Send the query and collect the result in "apartData"
set apartData = connection.execute (apart_query)
' Define the root element, directory, and the city element
root = "<Directory><City>" & vbCrLf
root = root & apartData("City")& vbCrLf
Response.Write (root)
' Loop through the apartment complexes in the selected city
do while not apartData.eof
bapart = "<Apartment>" & apartData("Apartment_ID") & vbCrLf
bapart = bapart & "<Name>" & apartData("Apartment") & "</Name>" & vbCrLf
bapart = bapart & "<Address>" & apartData("Address") & "</Address>" & vbCrLf
bapart = bapart & "<Office_phone>" & apartData("Telephone") & "</Office_phone>" & vbCrLf
bapart = bapart & "<Pets>" & apartData("Pets") & "</Pets>" & vbCrLf
bapart = bapart & "<Utilities>" & apartData("Utilities") & "</Utilities>" & vbCrLf
bapart = bapart & "<Features>" & apartData("Features") & "</Features>" & vbCrLf
Response.Write (bapart)
' Retrieve the units associated with each apartment
id = apartData("Apartment_ID")
unit_query = "SELECT Bedroom, Bathroom, Square_feet, Rent "
unit_query = unit_query & "FROM Unit WHERE Apartment_ID=" & id
set unitData = connection.execute (unit_query)
' Loop through the units associated with each apartment
do while not unitData.eof
msg = "<Unit>" & vbCrLf
msg = msg & "<Bedroom>" & unitData("Bedroom") & "</Bedroom>" & vbCrLf
msg = msg & "<Bathroom>" & unitData("Bathroom") & "</Bathroom>" & vbCrLf
msg = msg & "<Sq_feet>" & unitData("Square_Feet") & "</Sq_feet>" & vbCrLf
msg = msg & "<Rent>" & unitData("Rent") & "</Rent>" & vbCrLf
msg = msg & "</Unit>" & vbCrLf
Response.Write (msg)
unitData.MoveNext
loop
' Close the apartment element and move to the next complex
endapart = "</Apartment>" & vbCrLf
Response.Write (endapart)
apartData.MoveNext
loop
' Close the City and Directory elements endroot = "</City></Directory>" & vbCrLf Response.Write (endroot) ' Close the connection connection.close set connection = nothing %>