Jump to content

form and xml question


prichardson

Recommended Posts

Trying to use xml for first time. I got the a xml page that has got the tags and data inside the tags and I have a xslt page that displays it in a simple table format.I want to add a new page which has a form and in asp. So when the user selects a field (e.g. dropdownlist field) and selects the option then clicks on the search button. It to then display the results in the xml page witht the xslt style page in a table format.My question is how do i get to link the new form page into what i have got already working. The main thing is that the xml page will not have the data already in the tags. So when the user clicks the search button and goes to the xml page - it will have to dynamically find the results and display it.How best to get this to work? Would it have to have a database table with the fields? If i can do this without the need of a database - that would be better and keep this simple.What do i need to put into my asp form page and in my xml page and xsl page?This is my code:asp form page:

<%@LANGUAGE="VBSCRIPT" CODEPAGE="65001"%><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>Untitled Document</title></head><body><table width="100%" border="0" cellspacing="2" cellpadding="1">  <tr>	<th colspan="2" align="left" valign="bottom" scope="col"><h2>Search Properties</h2></th>  </tr>  <tr>	<td colspan="2"> </td>  </tr>  <tr>	<td width="9%" valign="top"><strong>Location:</strong></td>	<td width="91%" valign="bottom"><form id="form1" name="form1" method="post" action="properties.xml">	  <select name="location" id="location">		<option selected="selected">Please Select Location</option>		<option value="Chiswick">Chiswick</option>		<option value="London">London</option>	  </select>	</td>  </tr>  <tr>	<td colspan="2"> </td>  </tr>  <tr>	<td colspan="2">	  <input type="submit" name="Search" id="Search" value="Search" />		</form>	</td>  </tr></table></body></html>

xml page

<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="properties.xsl"?><properties> <property> <location>Chiswick</location> <bedrooms>3</bedrooms> <price>10000</price> </property> <property> <location>Chiswick</location> <bedrooms>4</bedrooms> <price>25000</price> </property></properties>
xsl page
<?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>Property Results:</h2>  <table border="1">	<tr bgcolor="#9acd32">	  <th>Location</th>	  <th>Bedrooms</th>	</tr>	<xsl:for-each select="properties/property">	<tr>	  <td><xsl:value-of select="location"/></td>	  <xsl:choose>	  <xsl:when test="price > 10000">		 <td bgcolor="#ff00ff">		 <xsl:value-of select="bedrooms"/>		 </td>	  </xsl:when>	  <xsl:when test="price > 20000">		 <td bgcolor="#cccccc">		 <xsl:value-of select="bedrooms"/></td>	  </xsl:when>	  <xsl:otherwise>		 <td><xsl:value-of select="bedrooms"/></td>	  </xsl:otherwise>	  </xsl:choose>	  </tr>	</xsl:for-each>  </table></body></html></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Would the answer be using an XForm? I have not got any knowledge on XForms - does anyone have any information or can tell me if this is the answer?If it is - how would I get the data from the selected option in a dropdownlist from the form to be displayed in the xml/xslt page?

Link to comment
Share on other sites

Hi have managed to get the form to spit out the result in a xml page.But it overwrites which is fine, but it does not include the essenial lines such as the include file for XSL and also the tags are not correct as my properties.xml page which i want it to display.This is my xml page that spits out after the search form:

<?xml version="1.0" ?><property location="Chiswick"></property>

but i would like it to display it like this (bold biuts important):

[code]<?xml version="1.0" encoding="ISO-8859-1"?><?xml-stylesheet type="text/xsl" href="properties.xsl"?><properties> <property> <location>Chiswick</location> <bedrooms>3</bedrooms> <price>10000</price> </property> <property> <location>Chiswick</location> <bedrooms>4</bedrooms> <price>25000</price> </property></properties>[/code]

How can I get it to display the include file etc in the xml page?Would i write something in my asp page that spit out the xml page?

<%strlocation = Request.Form("location")strFileName = strServerPath & "properties.xml"QUOT =Chr(34)CRLF = VbCrLfSet objFSO = Server.CreateObject("Scripting.FileSystemObject")Set objFile = objFSO.CreateTextFile(strFileName, True)strLine = "<?xml version=" & QUOT & "1.0" & QUOT & " ?>"objFile.WriteLine strLine & CRLFstrLine = "<property location=" & QUOT & strlocation & QUOT & ">"objFile.WriteLine strLine & CRLFstrLine = "</property>"objFile.WriteLine strLineobjFile.CloseSet objFile = NothingSet objFSO = Nothing%>

Link to comment
Share on other sites

It seems to me you're missing on a very important feature ASP and almost every other S3L has for XML - the ability to execute XSLT transformations on the server, and give the result. You can then simply output the result or, as it seems you want to, perform another transformation over it, taking the first result as input.Now, I don't know any ASP, but if I follow what W3Schools has for XSLT on the server, I think this should kind'a work:

<%'Load XMLset xml = Server.CreateObject("Microsoft.XMLDOM")xml.async = falsexml.load(Server.MapPath("properties.xml"))'Load XSLset xsl = Server.CreateObject("Microsoft.XMLDOM")xsl.async = falsexsl.load(Server.MapPath("properties.xsl"))'Transform fileResponse.Write(xml.transformNode(xsl))%>

This will transform properties.xml with properties.xsl as a stylesheet and return the output of it to the browser. Instead of giving the file to the browser though, you can execute another transformation by simply storing the result as a variable and go over that sort of code again.I may have misunderstood the point though. You're generating an XML file from a form, right? Well, OK. You could still transform that XML on the server and only give the result to the browser. That would be best in almost all scenarios.As for XForms, they are in all ways better then HTML forms. And they generate XML when they are submitted, meaning you'll skip the creation of the XML file on the server.However, the problem with XForms is that it's not that widely supported yet. There's an XForms Firefox extension to add XForms support to Firefox, and there's MozzIE that adds that same extension as part of IE (and also enables XHTML btw... really neat), but the problem with those is that they require the installation of a plug-in and that's not something everyone could do.There's also FormFaces, which however is slow on performance, and is embedded in pages served as HTML which just doesn't seem quite right. Last, but not least - because the W3C validator is not schema aware, the XHTML you end up with is always invalid.

Link to comment
Share on other sites

Hi,Thank you for helping me.It got to use the xslt to display the table from the xml sheet - Many thanksIt would only display one column (the bedrroms) and did not display the location column.I tested the code to see if it was pulling the results on the base of the location query or if it was ignoring the query and just pulling all the results - and it was the latter. I think there must be something i have to put something in that will query the xml on the base of the location. Therefore the end result will show all the results that meet the location criteria that the user selected from the search form.I saw a term used passing the parameter to query the xml sheet for a result on a criteria - but i am not sure if this applys to what i am trying to do and i have not found any code on this - what do you think about this and i would be appreciated for any more help you can give to me.

Link to comment
Share on other sites

Honestly said, I'm not sure what you're talking about. This topic shows an XSLT transformation in ASP.NET that also passes a parameter to the stylesheet. I don't know how to do the same in ASP.But if you're asking whether passing a parameter is the solution, well... I don't have a definitive answer to that either. But it sure could be, so give it a try.

Link to comment
Share on other sites

Hi,I have tried a few ways to achieve this - but have come to dead-ends. I am sure there is a simple answer to what i am trying to do as i am sure its something that is probably used quite frequently. It may be just me not good at explaining what i am trying to do - sorry. I will try to make it clearer:An estate agent has all these properties and wants to put his search function to use xml to be able to obtain the properties that the user has searched for from the search form. As using xml i am trying to get xslt to make the results look in a particular way, but my main objective at this moment is to get the requested search (e.g. properties in chiswick) to display all results that have the xml tag <location> = Chiswick, only from all the properties in all locations. So you have helped me to get the xslt sheet to display, and i am just wondering how to be able to put a parameter that querys the xml sheet and displays the results that the web user has searched under (from the search form). I have used the requestquery to gather the search by (location = chiswick).What would i put to get the results to display according to the what the web user asked to be displayed from the form?Many thanks for you trying to help me.

Link to comment
Share on other sites

hi boen_robot - got a quick question.....would it be possible to put a 'where clause' at the end of this statement:

set xml = Server.CreateObject("Microsoft.XMLDOM")xml.async = falsexml.load(Server.MapPath("properties.xml"))

I am not sure if where would be the right text to put down, but to make a filter, something like this:

set xml = Server.CreateObject("Microsoft.XMLDOM")xml.async = falsexml.load(Server.MapPath("properties.xml")) WHERE location= " & strlocation & "			 (strlocation e.g. chiswick)

???So it only loads up thexml page with the location = chiswick ?Or maybe it has to be at the response.write statement:

Response.Write(xml.transformNode(xsl))

like

Response.Write(xml.transformNode(xsl))  WHERE location= " & strlocation & "			 (strlocation e.g. chiswick)

???Any ideas would be great.Thanks

Link to comment
Share on other sites

Ah. I see. You're sort of trying to create a search engine, right? Well, there are a number of approaches to that, each with it's pros and cons. You'll have to consider them particularly for this case.1. As you suggested, pass a parameter with the search string and filter the output with a predicate of the form [element = $searchQuery] to match the complete content or [contains(element,$searchQuery)] to match part of it (possibly the whole). The pros are that this is extremely easy to implement and it is extremely easy to migrate to another S3L if you wished. Those however are the only pros. The rest are only cons- By default the search is case sensitive. You can make it case insensitive by translate()-ing both the element and the parameter to lowercase- Only does exact matches. It's (almost?) impossible to make it intelligent the way it's possible to make search engines that use a S3L (ASP in your case) to perform the search. For numbers, you're also allowed to use greather then and less then though.- Hard, actually almost impossible in XSLT 1.0 to create highlighting of the actual matches that were found. Easier in XSLT 2.0, but still.2. Perform the search with ASP by whatever means needed and construct a new XML file containing only the elements that have what it takes. DOM is usually a good weapon in this case. You'll get all text nodes, search through each of them and construct a new tree containing only the ones that passed. I can't help you with any actual code, as I don't know any ASP. The pros and cons about this approach are practically reversed with the above. What's good about it is what's bad about the above and vise versa.

Link to comment
Share on other sites

Hi boen_robotThank you for your help. What if the most of the searchs will have fields. Example - to search for houses in london. You would have a dropdownlist with many field options, london, New York, Paris, etc etc. The user selects London- so the querystring will have London on the end.I suppose this will make it easier but anything would be different or aware from what you mentioned in the previous post?

Link to comment
Share on other sites

Hi boen_robot -I still not be able to only get the Chiswick proeprties to appear. I believe it has to be somethign on this page - i think i have not coded the param tags correctly or something.

<?xml version="1.0" encoding="iso-8859-1"?><!-- DWXMLSource="properties.xml" --><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" encoding="iso-8859-1"/><xsl:output method="xml" encoding="iso-8859-1"/><xsl:template name="location"><xsl:param name="Chiswick"/></xsl:template><xsl:template match="/">  <h2>Property Results:</h2>  <table width="20%" cellpadding="0" cellspacing="0" border="1">	<tr bgcolor="#9acd32">	  <th>Location</th>	  <th>Bedrooms</th>	  <th>Price</th>	</tr>	<xsl:for-each select="properties/property">	<xsl:sort select="location" order="ascending" />	<tr>	  <td>	  <xsl:variable name="Chiswick">	  <xsl:call-template name="location">	  <xsl:with-param name="Chiswick" select="location"/>	  </xsl:call-template>	  </xsl:variable>	  </td>   	  <xsl:choose>	  <xsl:when test="price > 10000">		 <td bgcolor="#ff00ff">		 <xsl:value-of select="bedrooms"/>		 </td>	  </xsl:when>	  <xsl:when test="price > 20000">		 <td bgcolor="#cccccc">		 <xsl:value-of select="bedrooms"/></td>	  </xsl:when>	  <xsl:otherwise>		 <td><xsl:value-of select="bedrooms"/></td>	  </xsl:otherwise>	  </xsl:choose>	  <td><xsl:value-of select="price"/></td>	  </tr>	</xsl:for-each>  </table></xsl:template></xsl:stylesheet>

Link to comment
Share on other sites

Hi boen_robotThank you for your help. What if the most of the searchs will have fields. Example - to search for houses in london. You would have a dropdownlist with many field options, london, New York, Paris, etc etc. The user selects London- so the querystring will have London on the end.I suppose this will make it easier but anything would be different or aware from what you mentioned in the previous post?
Well, if the list is predefined, then that's pure filtering, for which XSLT does a perfect job, so I'll go with the first approach. I like to distict filtering and searching, the difference being that "search"-ing implies the ability of the user to specify the filter string, rather then using a predefined value.
Link to comment
Share on other sites

Hi boen_robot,If you were going to create a xsl page to filter the the location (chiswick) - how would you do it?Which tags would you use? would you use param or if tags? Would you completely dismantle the xsl sheet i done above or would it be just a tweak?Cheers

Link to comment
Share on other sites

I'd filter then by a parameter used in a predicate. Parameters in XSLT can be global though. I'm assuming that's your mistake in the XSLT you've presented. So, you could do something like:

<?xml version="1.0" encoding="iso-8859-1"?><!-- DWXMLSource="properties.xml" --><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" encoding="iso-8859-1"/><xsl:output method="xml" encoding="iso-8859-1"/><xsl:param name="Chiswick"/><xsl:template match="/">  <h2>Property Results:</h2>  <table width="20%" cellpadding="0" cellspacing="0" border="1">	<tr bgcolor="#9acd32">	  <th>Location</th>	  <th>Bedrooms</th>	  <th>Price</th>	</tr>	<xsl:for-each select="properties/property[location=$Chiswick]">	<xsl:sort select="location" order="ascending" />	<tr>	  <td>		 <xsl:value-of select="location"/>	  </td>   	  <xsl:choose>	  <xsl:when test="price > 10000">		 <td bgcolor="#ff00ff">		 <xsl:value-of select="bedrooms"/>		 </td>	  </xsl:when>	  <xsl:when test="price > 20000">		 <td bgcolor="#cccccc">		 <xsl:value-of select="bedrooms"/></td>	  </xsl:when>	  <xsl:otherwise>		 <td><xsl:value-of select="bedrooms"/></td>	  </xsl:otherwise>	  </xsl:choose>	  <td><xsl:value-of select="price"/></td>	  </tr>	</xsl:for-each>  </table></xsl:template></xsl:stylesheet>

I'm only guessing when editing yours though. Still, the approach is pretty much the same. A global parameter and a for-each (or a template match) statement that will have a predicate to filter by it.The above has one problem though. It requires a filter by any location. There isn't a mode to display it all. There are numerous kinds of workarounds (as there are always with XSLT), but the quickest one I can think of and edit it right into that code would be the contains() function:

<?xml version="1.0" encoding="iso-8859-1"?><!-- DWXMLSource="properties.xml" --><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="xml" encoding="iso-8859-1"/><xsl:output method="xml" encoding="iso-8859-1"/><xsl:param name="Chiswick"/><xsl:template match="/">  <h2>Property Results:</h2>  <table width="20%" cellpadding="0" cellspacing="0" border="1">	<tr bgcolor="#9acd32">	  <th>Location</th>	  <th>Bedrooms</th>	  <th>Price</th>	</tr>	<xsl:for-each select="properties/property[contains(location,$Chiswick)]">	<xsl:sort select="location" order="ascending" />	<tr>	  <td>		 <xsl:value-of select="location"/>	  </td>   	  <xsl:choose>	  <xsl:when test="price > 10000">		 <td bgcolor="#ff00ff">		 <xsl:value-of select="bedrooms"/>		 </td>	  </xsl:when>	  <xsl:when test="price > 20000">		 <td bgcolor="#cccccc">		 <xsl:value-of select="bedrooms"/></td>	  </xsl:when>	  <xsl:otherwise>		 <td><xsl:value-of select="bedrooms"/></td>	  </xsl:otherwise>	  </xsl:choose>	  <td><xsl:value-of select="price"/></td>	  </tr>	</xsl:for-each>  </table></xsl:template></xsl:stylesheet>

When there are no filters, $Chiswick will be empty, thus an empty string would always be contained in some other string. This sort of filter wouldn't work if you want to filter for prices lower or higher then a certain value. If you want that, I think the code would need a complete makeover.

Link to comment
Share on other sites

Hi boen_robot,Really grateful for the last post which has shown me a great insight to the best way to filter the xml data for location = chiswick. The only downside is that it still shows all the locations. Any idea to why that might be?I really appreciate you help here.

Link to comment
Share on other sites

Right now it does, because the default value is empty and as I said, with the contains() function, when $Chiswick is empty, all locations are shown.Try to give $Chiswick a value you know exists and try again. That is, turn the param line into something like

<xsl:param name="Chiswick" select="'London'"/>

Note that the select attribute is surrounded by apostrophes (single quites - ') to indicate that the value is a literal string. Without them, the value will be taken from a certain element in the source XML file (which in that case doesn't exist).The point is with this structure, you can manipulate the param's value from the outside with ASP. As I mentioned earlier, I don't know how to pass parameters to XSLT in ASP, but I gave you a link as to how it could be done in ASP.NET. You'll have to look at that for yourself.

Link to comment
Share on other sites

Hi boen_robot Where would i place this line of code into the xsl?

<xsl:param name="Chiswick" select="'London'"/>

Would it change it for this line <xsl:value-of select="location"/> ???I looked at that link you gave to me that was in asp.net. I think it may be something like this in asp

Dim strResultDim templatesDim transformertemplates = Server.CreateObject(""MSML2.XSLTemplate")templates.stylesheet = objXSL.documentElementtransformer = templates.createProcessor()transformer.input = objXMLtransformer.addParameter("location", Request.QueryString("location"), "")strResult = transformer.transform()

Anyways I think we are getting closer to getting this to work. Cheers

Link to comment
Share on other sites

That is, turn the param line into something like
<xsl:param name="Chiswick" select="'London'"/>

the bold part means to replace the param line. And the param line is the xsl:param line, the only one in this XSLT being
<xsl:param name="Chiswick"/>

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...