Advertise Here
XML Files - Extensible Markup Language Tutorials for Developers - ?
Extensible Markup Language

Search XML Files

XML Files RSS Feed XML Files Updates

 


Searching an XML File with XSL and ASP

By
Michael Qualls

Introduction

This example assumes that you are familiar with ASP, XML and HTML 4.0.

Search XML files to update or delete records

This article is the final installment in a series of articles on manipulating XML data with ASP. Before proceding with this article, I recommend that you read Saving HTML Form Data to XML, Editing XML with XSL and ASP, and Appending to XML with ASP.

The first thing that I would recommend before proceeding would be to download the files that make up this example and copy them to your Web server so that you can follow along with the examples by running them in your browser as we go.

Download the example files (rolodex.zip)

So far we have learned to add, append, and edit records in XML files using ASP. In order to tie all of these processes together, we need to be able to list all of the records in an XML file. Then we need to be able to select a specific record and view its details. Once viewing a record's details, then we will be able to edit it or delete it.

Lets list the records in the XML file. This XML file contains a list of contacts (similar to the previous examples). We will use an XSL file to transform the XML file into HTML for display in a browser. Using ASP, we will perform the transformation on the server so that we can avoid any browser incompatability issues.

Listing XML file contents:

Here is a listing of the contents of the XML file.

rolodex.xml:


<?xml version="1.0" ?>
<rolodex>
 <contact>
  <field id="firstName" taborder="1">
   <field_value>Reggie</field_value>
  </field>
  <field id="lastName" taborder="2">
   <field_value>Walls</field_value>
  </field>
  <field id="address1" taborder="3">
   <field_value>202 East Haverbrook</field_value>
  </field>
  <field id="address2" taborder="4">
   <field_value>Oklahoma City, OK 73114</field_value>
  </field>
  <field id="phone" taborder="5">
   <field_value>4055551234</field_value>
  </field>
  <field id="email" taborder="6">
   <field_value>reggiew@vertiscope.com</field_value>
  </field>
 </contact>
 <contact>
  <field id="firstName" taborder="1">
   <field_value>Nancy</field_value>
  </field>
  <field id="lastName" taborder="2">
   <field_value>Quick</field_value>
  </field>
  <field id="address1" taborder="3">
   <field_value>2000 East Brooks</field_value>
  </field>
  <field id="address2" taborder="4">
   <field_value>Oklahoma City, OK 73114</field_value>
  </field>
  <field id="phone" taborder="5">
   <field_value>4055522345</field_value>
  </field>
  <field id="email" taborder="6">
   <field_value>nancyq@vertiscope.com</field_value>
  </field>
 </contact>
 <contact>
  <field id="firstName" taborder="1">
   <field_value>Gerry</field_value>
  </field>
  <field id="lastName" taborder="2">
   <field_value>Mays</field_value>
  </field>
  <field id="address1" taborder="3">
   <field_value>303 Cranston Street</field_value>
  </field>
  <field id="address2" taborder="4">
   <field_value>Oklahoma City, OK 73114</field_value>
  </field>
  <field id="phone" taborder="5">
   <field_value>4055259999</field_value>
  </field>
  <field id="email" taborder="6">
   <field_value>gerrym@vertiscope.com</field_value>
  </field>
 </contact>
 <contact>
  <field id="firstName" taborder="1">
   <field_value>Jenny</field_value>
  </field>
  <field id="lastName" taborder="2">
   <field_value>Woos</field_value>
  </field>
  <field id="address1" taborder="3">
   <field_value>110 Crown Drive</field_value>
  </field>
  <field id="address2" taborder="4">
   <field_value>Oklahoma City, OK 73114</field_value>
  </field>
  <field id="phone" taborder="5">
   <field_value>4055559873</field_value>
  </field>
  <field id="email" taborder="6">
   <field_value>jennyw@vertiscope.com</field_value>
  </field>
 </contact>
</rolodex>

Here is the XSL file that will be used to transform the XML file.

rolodex.xsl:


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
 <html>
 <body>
 <h1>Contacts</h1>
 <table border="1" cellpadding="2">
 <tr>
 <td>Name</td>
 <td>E-Mail</td>
 </tr>
 <xsl:for-each select="rolodex/contact">
  <tr>
  <td>
  <a>
  <xsl:attribute name="href">rolodex6.asp?
  mode=viewdetail&email=<xsl:value-of select="field
  [@id='email']/field_value"/></xsl:attribute><xsl:value-of
  select="field[@id='firstName']/field_value"/>
  </a>
  </td>
  <td><xsl:value-of select="field[@id='email']/field_value"/></td>
  </tr>
 </xsl:for-each>
 </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

This XSL file iterates through the content of the XML file and displays it in a HTML table. The display allows the viewer of the table to click on a contact name to view the detail for the contact. Lets take an initial look at the rolodex.asp file. This is the file that tranforms the XML file on the server. Later in this article we will add several methods to this ASP file in order to update and delete records.

rolodex.asp:


<%

'-------------------------------------------------------------------
'This function accepts two paramters:
'strXMLFile - The XML file that you wish to view
'strXSLFile - The XSL stylesheet that will transform the XML file
'-------------------------------------------------------------------
Function loadXMLFile(strXMLFile, strXSLFile)

 'Declare local variables
 Dim objXML
 Dim objXSL

 'Instantiate the XMLDOM Object that will hold the XML file.
 set objXML = Server.CreateObject("Microsoft.XMLDOM")

 'Turn off asyncronous file loading.
 objXML.async = false

 'Load the XML file.
 objXML.load(strXMLFile)

 'Instantiate the XMLDOM Object that will hold the XSL file.
 set objXSL = Server.CreateObject("Microsoft.XMLDOM")

 'Turn off asyncronous file loading.
 objXSL.async = false

 'Load the XSL file.
 objXSL.load(strXSLFile)

 'Use the "transformNode" method of the XMLDOM to apply the XSL
 'stylesheet to the XML document. Then the output is written to the
 'client.
 Response.Write(objXML.transformNode(objXSL))
End Function


'Call the loadXMLFile Method, passing it the names of the XML and XSL
'files that you wish to load.
loadXMLFile server.MapPath("rolodex.xml"), server.MapPath("rolodex.xsl")
%>  

One thing that you will notice about this code listing is that clicking on the link to view the contact's detail simply reloads the list and does not display the detail. We need to add this functionality. However, you will also notice that several query string parameters are appended to the URL when the contact name is clicked. The first parameter is "mode" which tells rolodex.asp which action should be executed when the page is loaded. The second query string parameter is "email" which is used to uniquely identify the contact.

In order to view the detail for a contact, we need to be able to search the XML file and return the desired record. We will add a new method to rolodex.asp in order to accomplish this.

Viewing XML record detail:

The viewDetail method will be used to search the XML file for a specific record.


'-------------------------------------------------------------------
'This function accepts three paramters:
'strXMLFile - The XML file that you wish to view
'strXSLFile - The XSL stylesheet that will transform the XML file
'strEMail - The E-Mail address of the contact that you wish to view
'-------------------------------------------------------------------
Function viewDetail(strXMLFile, strXSLFile, strEMail)

 'Declare local variables
 Dim objXML
 Dim objNode
 Dim objXSL

 'Instantiate the XMLDOM Object that will hold the XML file.
 set objXML = Server.CreateObject("Microsoft.XMLDOM")

 'Turn off asyncronous file loading.
 objXML.async = false

 'Load the XML file.
 objXML.load(strXMLFile)

 'Get the XML record that you wish to view by calling the
 'SelectSingleNode method and passing in the e-mail address of the
 'contact.
 Set objNode = objXML.SelectSingleNode("rolodex/contact
 [field/field_value='" & strEMail & "']")

 'Instantiate the XMLDOM Object that will hold the XSL file.
 set objXSL = Server.CreateObject("Microsoft.XMLDOM")

 'Turn off asyncronous file loading.
 objXSL.async = false

 'Load the XSL file.
 objXSL.load(strXSLFile)

 'Use the "transformNode" method of the XMLDOM to apply the XSL
 'stylesheet to the XML document. Then the output is written to the
 'client.
 Response.Write(objNode.transformNode(objXSL))
End Function

The viewDetail method queries the XML file by calling the SelectSingleNode method of the XMLDOM Object. This method accepts a query string to locate the node (record) that you wish to view. In this case, we pass in the E-Mail address of the contact that we wish to see detail for. You should also notice that in this method when we call the transformNode method, we are transforming only the node that we got from SelectSingeNode and not the entire XML document.

Since we are viewing the detail for an XML record, we will need a different XSL file to transform the XML file.

viewdetail.xsl:


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="rolodex/contact">
 <html>
 <body>
 <h1>Contact Detail</h1>
 <table border="1" cellpadding="2">
 <xsl:for-each select="field">
  <tr>
  <td>
  <xsl:value-of select="@id"/>
  </td>
  <td>
  <xsl:value-of select="field_value"/>
  </td>
  </tr>
 </xsl:for-each>
 </table>
 <table border="0" cellpadding="2">
 <tr>
 <td>
 <a>
 <xsl:attribute name="href">rolodex6.asp?
 mode=editdetail&email=<xsl:value-of select="field
 [@id='email']/field_value" /></xsl:attribute>Edit this record
 </a>
 </td>
 </tr>
 <tr>
 <td>
 <a>
 <xsl:attribute name="href">rolodex6.asp?
 mode=deletedetail&email=<xsl:value-of select="field
 [@id='email']/field_value" /></xsl:attribute>Delete this record
 </a>
 </td>
 </tr>
 <tr>
 <td>
 <a>
 <xsl:attribute name="href">rolodex6.asp</xsl:attribute>Return to
 contact list
 </a>
 </td>
 </tr>
 </table>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

One thing that we have done in this XSL file, after the details of the XML record have been formatted in a HTML table, is add three hyperlinks. The first is a link to edit this record, the second is a link to delete this record, and the third is a link to return to the main list.

Finally, after having added the viewDetail method to the rolodex.asp file, we need to add logic to rolodex.asp so that viewDetail will execute when it is supposed to.


<%

'-----------------------------------------------------------
'The loadXMLFile and viewDetail methods are inserted here
'-----------------------------------------------------------

'Declare variables to hold the values of the mode and email query
'strings.
Dim strMode
Dim strEMail

'Pass the values of the mode and email query strings to the variables.
strMode = Request.QueryString("mode")
strEMail = Request.QueryString("email")

'Use a Select Case statement to test the value of strMode.
Select Case strMode
 Case "viewdetail"

  'Call the viewDetail method passing in the XML file, XSL file, and the
  'e-mail address of the contact.
  viewDetail server.MapPath("rolodex6.xml"),server.MapPath
  ("viewdetail.xsl"),strEMail
 Case Else

  'Call the loadXMLFile method passing in the XML file, XSL file. This
  'will return the entire list.
  loadXMLFile server.MapPath("rolodex6.xml"), server.MapPath
  ("rolodex6.xsl")
End Select
%>  

You might wonder why we used a select case statement in the last code listing rather than an if...then...else statement. The reason is that we are going to be adding several more cases.

With the code that we have added to rolodex.asp we are now able to view the list of contacts and we can view the detail for a selected contact. However, when viewing the detail for the contact, there are links to edit and delete the contact. We need to add code to handle these actions.

Editing XML record detail:

We will now add logic to rolodex.asp to allow the end user to edit the XML record detail. The viewDetail method can actually be used to format the selected XML record into a HTML form for editing. It is just a matter of passing in a different XSL stylesheet, editdetail.xsl, when calling the viewDetail method.

editdetail.xsl:


<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="rolodex/contact">
 <html>
 <body>
 <form id="frmEditContact" name="frmEditContact" method="post">
 <xsl:attribute name="action">rolodex6.asp?mode=updatedetail&email=
 <xsl:value-of select="field[@id='email']/field_value"/></xsl:attribute>
 <h1>Edit Contact Detail</h1>
 <table border="1" cellpadding="2">
 <xsl:for-each select="field">
  <tr>
  <td>
  <xsl:value-of select="@id"/>
  </td>
  <td>
  <input type="text" size="30"> <xsl:attribute name="id"> <xsl:value-of
  select="@id" /> </xsl:attribute> <xsl:attribute
  name="name"><xsl:value-of select="@id" /></xsl:attribute>
  <xsl:attribute name="value"> <xsl:value-of select="field_value" />
  </xsl:attribute></input>
  </td>
  </tr>
 </xsl:for-each>
 </table>
 <table border="0" cellpadding="2">
 <tr>
 <td>
 <a href="javascript:document.forms(0).submit();">Submit record
 update</a>
 </td>
 </tr>
 <tr>
 <td>
 <a>
 <xsl:attribute name="href">rolodex6.asp?
 mode=deletedetail&email=<xsl:value-of select="field
 [@id='email']/field_value" /></xsl:attribute>Delete this record
 </a>
 </td>
 </tr>
 <tr>
 <td>
 <a>
 <xsl:attribute name="href">rolodex6.asp</xsl:attribute>Return to
 contact list
 </a>
 </td>
 </tr>
 </table>
 </form>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>

After the necessary edits are made the "Submit record update" link is clicked to submit the record edits. The action of the form is rolodex6.asp. However, the mode query string is now changed to "updatedetail". This tells rolodex6.asp to call the updateDetail method.


'-------------------------------------------------------------------
'This function accepts three paramters:
'strXMLFile - The XML file that you wish to view
'strXSLFile - The XSL stylesheet that will transform the XML file
'strEMail - The E-Mail address of the contact that you wish to view
'-------------------------------------------------------------------
Function updateDetail(strXMLFile, strXSLFile, strEMail)

 'Declare local variables.
 Dim objDom
 Dim objRoot
 Dim objContact
 Dim objField
 Dim strNewEMail

 'Pass the new e-mail value to a local variable
 strNewEMail = Request.Form("email")

 'Instantiate the XMLDOM Object.
 set objXML = Server.CreateObject("Microsoft.XMLDOM")

 'Turn off asyncronous file loading.
 objXML.async = false

 'Load the XML file.
 objXML.load strXMLFile

 'Set the objRoot variable equal to the root element of the XML file by
 'calling the documentElement method of the objDOM (XMLDOM) object.
 Set objRoot = objXML.documentElement

 'Set a reference to the node (record) of the contact that we are
 'editing by calling the SelectSingleNode method and passing it the
 'e-mail address of the contact we are editing.
 Set objContact = objRoot.SelectSingleNode("contact[field/field_value='"
 & strEMail & "']")

 'Iterate through the form colllection of the request object updating
 'the values of the XML record.
 For each objItem in Request.Form
  Set objField = objContact.selectSingleNode("field[@id='" & objItem &
  "']/field_value")
  objField.text = Request.Form(objItem)
 Next 

 'After the XML file has been edited, is must be saved.
 objXML.save strXMLFile

 'Call the viewDetail method, passing in the newly edited XML file and
 'the viewdetail.xsl style sheet. This will allow you to see the edited
 'information.
 viewDetail strXMLFile, strXSLFile, strNewEMail
End Function  

Now that we have introduced the code necessary to edit the XML record, lets look at the additional logic that needs to be added to rolodex.asp. Two new case statements are added to the select statement. One to handle showing the XML record in an HTML form and another to actually submit the updates to the record.

<%

'-----------------------------------------------------------
'The loadXMLFile, viewDetail, updateDetail methods are 
'inserted here
'-----------------------------------------------------------

'Declare variables to hold the values of the mode and email query
'strings.
Dim strMode
Dim strEMail

'Pass the values of the mode and email query strings to the variables.
strMode = Request.QueryString("mode")
strEMail = Request.QueryString("email")

'Use a Select Case statement to test the value of strMode.
Select Case strMode
 Case "viewdetail"

  'Call the viewDetail method passing in the XML file, XSL file, and the
  'e-mail address of the contact.
  viewDetail server.MapPath("rolodex6.xml"),server.MapPath
  ("viewdetail.xsl"),strEMail
 Case "editdetail"

  'Call the viewDetail method passing in the XML file, XSL file, and the
  'e-mail address of the contact. The XSL file that is passed in for
  'this condition will build a HTML form to allow editing of the XML
  'record.
  viewDetail server.MapPath("rolodex6.xml"), server.MapPath
  ("editdetail.xsl"), strEMail
 Case "updatedetail"

  'Call the updateDetail method passing in the XML file, XSL file, and
  'the e-mail address of the contact. This method will update the XML
  'file with the changes submitted. The viewdetail.xsl file is used to
  'format the XML so we can view the updated record.
  updateDetail server.MapPath("rolodex6.xml"), server.MapPath
  ("viewdetail.xsl"), strEMail
 Case Else

  'Call the loadXMLFile method passing in the XML file, XSL file. This
  'will return the entire list.
  loadXMLFile server.MapPath("rolodex6.xml"), server.MapPath
  ("rolodex6.xsl")
End Select
%>  

The final piece of the puzzle is to be able to delete records. We will add a method to rolodex.asp that will handle deleting a record when the "Delete this record" link is clicked.

Deleting XML record detail:

The viewdetail.xsl and editdetail.xsl stylesheets have links that allow the current to be deleted. We will add the deleteDetail method to rolodex.asp in order to execute the deletion.

editdetail.xsl:


'-------------------------------------------------------------------
'This function accepts three paramters:
'strXMLFile - The XML file that you wish to use
'strXSLFile - The XSL stylesheet that will transform the XML file
'strEMail - The E-Mail address of the contact that you wish to delete
'-------------------------------------------------------------------
Function deleteDetail(strXMLFile, strXSLFile, strEMail)

 'Declare local variables.
 Dim objDom
 Dim objRoot
 Dim objNode

 'Instantiate the XMLDOM Object.
 set objXML = Server.CreateObject("Microsoft.XMLDOM")

 'Turn off asyncronous file loading.
 objXML.async = false

 'Load the XML file.
 objXML.load strXMLFile

 'Set the objRoot variable equal to the root element of the XML file by
 'calling the documentElement method of the objDOM (XMLDOM) object.
 Set objRoot = objXML.documentElement

 'Set a reference to the node (record) that we want to delete by calling
 'the SelectSingleNode method and passing it the contact e-mail address.
 Set objNode = objRoot.SelectSingleNode("contact[field/field_value='" &
 strEMail & "']")

 'Call the removeChild method to delete the node (record) from the XML
 'file.
 objRoot.removeChild(objNode)

 'After the XML file has been edited, is must be saved.
 objXML.save strXMLFile

 'Call the loadXMLFile method, passing in the newly edited XML file and
 'the rolodex6.xsl style sheet. This will show the complete listing of
 'contacts minus the one just deleted.
 loadXMLFile strXMLFile,strXSLFile
End Function

We need to make one final additon to the select case statement to provide logice for deleting records.


<%

'-----------------------------------------------------------
'The loadXMLFile, viewDetail, updateDetail, and deleteDetail
'methods are inserted here
'-----------------------------------------------------------


'Declare variables to hold the values of the mode and email query
'strings.
Dim strMode
Dim strEMail


'Pass the values of the mode and email query strings to the variables.
strMode = Request.QueryString("mode")
strEMail = Request.QueryString("email")


'Use a Select Case statement to test the value of strMode.
Select Case strMode
 Case "viewdetail"

  'Call the viewDetail method passing in the XML file, XSL file, and the
  'e-mail address of the contact.
  viewDetail server.MapPath("rolodex6.xml"),server.MapPath
  ("viewdetail.xsl"),strEMail
 Case "editdetail"

  'Call the viewDetail method passing in the XML file, XSL file, and the
  'e-mail address of the contact. The XSL file that is passed in for
  'this condition will build a HTML form to allow editing of the XML
  'record.
  viewDetail server.MapPath("rolodex6.xml"), server.MapPath
  ("editdetail.xsl"), strEMail
 Case "updatedetail"

  'Call the updateDetail method passing in the XML file, XSL file, and
  'the e-mail address of the contact. This method will update the XML
  'file with the changes submitted. The viewdetail.xsl file is used to
  'format the XML so we can view the updated record.
  updateDetail server.MapPath("rolodex6.xml"), server.MapPath
  ("viewdetail.xsl"), strEMail
 Case "deletedetail"

  'Call the deleteDetail method passing in the XML file, XSL file, and
  'the e-mail address of the contact. This method will delete the
  'record of the selected contact.
  deleteDetail server.MapPath("rolodex6.xml"), server.MapPath
  ("rolodex6.xsl"), strEMail
 Case Else

  'Call the loadXMLFile method passing in the XML file, XSL file. This
  'will return the entire list.
  loadXMLFile server.MapPath("rolodex6.xml"), server.MapPath
  ("rolodex6.xsl")
End Select
%>  

Well, all of the pieces are in place now. By reviewing and applying everything from the 4 articles on storing data in XML you should now be able to build a complete data storage system using XML. In this article I showed you how to search an XML file to update and delete records. Take a look at the Appending to XML with ASP article and see if you can figure out how to add the ability to add new records to this example.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 DevX XML Content 
- Easily Create Responsive Websites Using Bootstrap

Jump to : Top Of Page or HOME