Jump to content

edit xml with xslt and asp classic


Recommended Posts

I want to edit xml from asp and xslt like the demo from w3schools. I am running into some snags.What I am trying to do is create a job list that has an approval function. I already have the form uploading to the xml document that allows users to submit their jobs. I am using xslt to be able to change the status of them from pending to approve. so the new xslt file will display for users all of the approved jobs.Here is my xml file

<?xml version="1.0" encoding="utf-8"?><blast><job status='approve'>  <position>developer</position>  <posted>01/19/2012</posted>  <company>JCC</company>  <city>Jackson</city>  <state>MI</state>  <process>webaddress.com</process>  <contact>da boss</contact></job><job status='pending'>  <position>lunch lady</position>  <posted>01/19/2012</posted>  <company>high school</company>  <city>lansing</city>  <state>MI</state>  <process>website.com</process>  <contact>da boss lady</contact></job></blast>

Here is my xslt file that displays the pending jobs for approval

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>   <head>   </head>   <body>	<form method="post" action="editBlast.asp">	 <h2>Job Approval</h2>	 <table cellpadding="5" cellspacing="0" border="1">	  <tr>	   <th>Status</th>	   <th>Approval Action</th>	   <th>Position</th>	   <th>Company</th>	   <th>City</th>	   <th>State</th>	   <th>Contact</th>	   <th>Posted</th>	  </tr>	  <xsl:for-each select="blast/job[@status='pending']">	   <tr>		<td><xsl:value-of select="@status"/></td>		<td>		 <input type="textbox" id="status" name="status" >		  <xsl:attribute name="value">		   <xsl:value-of select="@status"/>		  </xsl:attribute>		 </input>		 Approve		</td>		<td><xsl:value-of select="position"/></td>		<td><xsl:value-of select="company"/></td>		<td><xsl:value-of select="city"/></td>		<td><xsl:value-of select="state"/></td>		<td><xsl:value-of select="contact"/></td>		<td><xsl:value-of select="posted"/></td>	   </tr>	  </xsl:for-each>	 </table>	 <p><input type="submit" id="submit" name="submit" value="Submit"/> <input type="reset" id="reset" name="reset" value="Reset"/></p>	</form>   </body>  </html></xsl:template></xsl:stylesheet>

Here is my asp file:

<%'-----------------------------------------------------------'The "loadXMLFile" Function accepts two parameters.'strXMLFile - The path and file name of the XML file.'strXSLFilee - The path and file name of the XSL file.'-----------------------------------------------------------Function loadXMLFile(strXMLFile, strXSLFile)'Declare local variablesDim objXMLDim 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 '-----------------------------------------------------------'The "updateXML" Function accepts one parameter.'strXMLFile - The path and file name of the XML file.'-----------------------------------------------------------Function updateXML(strXMLFile)'Declare local variables.Dim objDomDim objRootDim objFieldDim x 'Instantiate the XMLDOM Object.set objDOM = Server.CreateObject("Microsoft.XMLDOM")'Turn off asyncronous file loading.objDOM.async = false'Load the XML file.objDOM.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 = objDom.documentElement 'Iterate through the Form Collection and write the'submitted values to the XML file.For x = 1 to Request.Form.Count  'Check see if "btn" is in the submitted value, if so,  'it is a button and should be ignored.  If instr(1,Request.Form.Key(x),"btn") = 0 Then   'Set objField variable equal to a field_value element by   'calling the selectSingleNode method of the objRoot   '(documentElement) object.  The SelectSingleNode method   'accepts a string parameter for querying the XML document.   'In this case, the current value of the key property of   'the Form Collection is used to find the appropriate   'field_value element (more on this later).   Set objField = objRoot.selectSingleNode("job[@status='" & _   Request.Form.Key(x) & "']")   'Set the text property of the objField (field_value)   'element equal to the value of the current form field.   objField.Text = Request.Form(x)  End IfNext 'After the XML file has been edited, is must be saved.objDom.save strXMLFile 'Release all of your object references.Set objDom = NothingSet objRoot = NothingSet objField = Nothing 'Call the loadXMLFile method, passing in the newly edited'XML file and the updatedcontact.xsl style sheet. This will'allow the client to see the edited information. More on the'updatedcontact.xsl file later.loadXMLFile strXMLFile,server.MapPath("blast_approved.xsl")End Function 'Test to see if the form has been submitted. If it has,'update the XML file. If not, transform the XML file for'editing.If Request.Form("btnSubmit") = "" Then  loadXMLFile server.MapPath("../../forms-results/xml/text.xml"), _  server.MapPath("blast_pending.xsl")Else  updateXML server.MapPath("../../forms-results/xml/text.xml")End If%>

and finally my xslt file to display the approved jobs

<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">  <html>  <body><h2>Job Blast</h2>	<table cellpadding="5" cellspacing="0" border="1">	 <thead>	  <tr>	   <th>Position(s)</th>	   <th>Company</th>	   <th>City</th>	   <th>State</th>	   <th>Contact Information</th>	   <th>Posted</th>	  </tr>	 </thead>	 <tbody>	  <xsl:for-each select="blast/job[@status='approve']">	   <xsl:sort select="posted" order="descending"/>	   <tr>		<td><xsl:value-of select="position"/></td>		<td><xsl:value-of select="company"/></td>		<td><xsl:value-of select="city"/></td>		<td><xsl:value-of select="state"/></td>		<td><xsl:value-of select="contact"/></td>		<td><xsl:value-of select="posted"/></td>	   </tr>	  </xsl:for-each>	 </tbody>	</table>   </body>  </html></xsl:template></xsl:stylesheet>

Thanks for your help!

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...