Creating an XML Document

Home ~ Creating a DTD ~ Creating an XSL Stylesheet ~ Creating an XML Document ~ XML Applications

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.

Using a Text Editor

  1. Select a text editor such as Notepad or WordPad.
  2. Create the three-line prologue to ensure the document references the correct DTD and XSL stylesheet.
    <?xmlversion"1.0"?>
    <!DOCTYPE rootelement SYSTEM "Apart_Dir2.dtd"> 
    <?xml-stylesheet href="xml.xsl" type="text/xsl"?>
    
  3. Review the DTD to determine the required structure of the document. The opening tag of the root element must be placed at the beginning of the document. In the apartment example, Directory is the root element.
    <?xmlversion"1.0"?>
    <!DOCTYPE rootelement SYSTEM "Apart_Dir2.dtd"> 
    <?xml-stylesheet href="xml.xsl" type="text/xsl"?>
    
    <Directory>
    
  4. Following the root element, all of the elements and their children must be incorporated. These elements can be nested within each other, as seen in the apartment example where the City element contains the Apartment element. In turn, the Apartment element contains the elements comprising apartment_info and the Unit element, which contains the elements comprising unit_info. To replicate this structure within the XML document, the opening tags for the City and Apartment elements are included first.
    <?xmlversion"1.0"?>
    <!DOCTYPE rootelement SYSTEM "Apart_Dir2.dtd"> 
    <?xml-stylesheet href="xml.xsl" type="text/xsl"?>
    
    <Directory>
    <City> Chapel Hill
    	<Apartment>
    
  5. Within the Apartment element, the apartment_info elements describing the apartment complex are included.
    <?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>
    
  6. The Unit element and the elements comprising unit_info are then included. The unit_info elements are repeatable, that is, the Apartment element can contain more than one Unit element. The closing tag for the Apartment should be included after all of the Unit information has been entered.
    <?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>
    	
  7. The Apartment element is also repeatable and can be added to the document following the closure of the first Apartment element. The second Apartment element must also contain the apartment_info elements, as well as the Unit element and the elements comprising unit_info.
    <?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>
    	
  8. Once all of the apartments and their associated units have been created, the closing tags for the City and Directory elements must be included at the end of the document.
    </City>
    </Directory>
    
  9. The XML document should be saved and placed into a Web accessible directory. The directory should also contain the DTD and XSL stylesheets referenced by the document. It is now ready to be accessed through a browser such as Internet Explorer 5. To view the example, click here.

Using an Access Database and ASP Scripts

  1. The basic outline of the XML document created using a text editor will remain the same for a document created using an Access database and an ASP script. The prologue must be included at the top of the document, followed by the root element and its child elements. However, instead of hand-tagging and entering the content, the content is tagged and generated through an Access database and ASP scripts. Because ASP can be used to perform an infinite number of database operations, the tutorial will address this topic using a specific example. The example will allow the user to select a city and find all apartment listings within that city. The first step is to ensure the Access database has been created and is accessible from the ASP script location.
  2. In this example, an Access database called "kemp" is used. It contains two tables, Apartment and Unit. These tables have a foreign key relationship on Apartment_ID.
  3. After the database has been established, open an ASP script file in an editor such as Microsoft Interdev.
  4. In Microsoft Interdev, to start a new file, go to File and select New File. Select the Visual InterDev folder from the display. Select ASP page. Alternatively, to open an existing file, go to File and select Open File.
  5. Two scripts will comprise this example. The first will be the city selection page, and the second will be display the apartments in the selected city. The initial city selection page is an HTML file with a drop-down list, whose options are generated by querying the Access database. When creating this ASP script, begin by entering the header information for an HTML document.
    <html>
    <head>
       <title> XML Apartment Guide </title>
    </head>
    <body>
    
  6. Each segment of ASP code must be contained between <% and %>. In this example, the code assigns the query variable (listData), creates and opens a connection with the database, queries the database and places the results in listData.
    <%
    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)
    %>
    
  7. Once the query has been run, the HTML page is displayed. Using a loop, the drop-down list of cities is generated from the query results. The selected city is submitted to the second ASP script using the GET method.
    <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>
    
  8. Once the selected city has been submitted, the second script contains the code to generate the XML document. The first part of this script is the HTTP header, which declares the resulting document to be of type XML, and the prologue of the XML document. Notice the header is generated using ASP code while the prologue is regular text.
    <%
    Response.ContentType = "text/xml"
    %>
    
    <?xmlversion "1.0"?> 
    <!DOCTYPE Directory SYSTEM "ApartDir2.dtd">
    <?xml-stylesheet href="xml.xsl" type="text/xsl"?>
    
  9. Next, the city variable from the first script is re-assigned to the city variable in this script. It is then used in the SELECT statement to retrieve all apartment information from the database where the City is the same as the selected city.
    <%
    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 & "'" 
    
  10. The code creates and opens a connection with the database, queries the database and places the results in the apartData variable.
    ' 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)
    
  11. The root element, Directory, and the City element with its associated city content are written to the XML document.
    ' Define the root element, directory, and the city element
    root = "<Directory><City>" & vbCrLf
    root = root & apartData("City")& vbCrLf
    Response.Write (root)
    
  12. Using a loop, the code goes through the apartData set and retrieves the information for one apartment. Given the Apartment_ID, it finds all units associated with that apartment. A nested loop locates the unit information, which is then XML-tagged and written to the document. The code repeats this looping pattern for the apartData set.
    ' 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 
    
  13. Finally, the City and Directory closing tags are created, and the connection to the database is closed.
    ' Close the City and Directory elements
       endroot = "</City></Directory>" & vbCrLf
       Response.Write (endroot)
       
    ' Close the connection
    connection.close 
    set connection = nothing 
    %>
    
  14. To view the final results, please click here.