Hello, i have to build a search fuction in my website. In main page(JSP) i have a text field and a submit button. When button clicked, user should transfer to goods list (JSP) where all my goods retrieved from store(XML). I have stuck at the point where only goods with name similar to the keyword should be displyed. (ex. If user insert "to" in main page, goods list page should present "Tomatoes" and "Toaster" or if insert "apples" only "Apples")
Any help???
Thank you
5 replies to this topic
#1
Posted 22 March 2011 - 10:37 AM
|
|
|
#2
Posted 22 March 2011 - 11:51 AM
Dunno what you use to parse your XML, but it should have some kind of Xpath option. If it can't handle xpath, take something else.
I made this xml:
I made this xml:
<food> <item> <name>tomato</name> <color>red</color> </item> <item> <name>banana</name> <color>yellow</color> </item> <item> <name>toast</name> <color>yellow/brown</color> </item> </food>To search for 'to', all I need to do is use this xpath expression, and it returns the items
/food/item[contains(name,'to')]To get only the names:
/food/item/name[contains(.,'to')]
#3
Posted 22 March 2011 - 01:47 PM
Hello, yes im using XPath to compile the XML, i didn't really get the meaning of "name" before 'to' and fullstop?
#4
Posted 23 March 2011 - 01:39 AM
Well plainly
The brackets [ ] Can actually be read as a WHERE condition.
So
In xpath a dot/point/'.' means current element. So because I select the name, i don't need to go deeper and check a child or attribute, but the name itself --> '.'
So you mean that request.getParameter('textFieldId') returns null?
Please make sure the textField is actually inside <form> </form> tags, together with the button.
/food/itemWould result in a list of all 'item' nodes in 'food' right.
The brackets [ ] Can actually be read as a WHERE condition.
So
/food/item[contains(name,'to')]Would be: Select all 'item' from 'food' where the 'name' element of 'item' contains 'to'.
/food/item/name[contains(.,'to')]This only selects the names WHERE . contains 'to'.
In xpath a dot/point/'.' means current element. So because I select the name, i don't need to go deeper and check a child or attribute, but the name itself --> '.'
Quote
Yes im using XPath to compile my xml, im working on two jsp files now, the main page and the list of goods. Main page has 4 images and each one goes to list of goods page but print out different goods (Grosery, bakery...). This working fine. Now i have to create search engine. My text field and go button are on main page, so when i click on go button web site should go to goods list page and print out goods who contain letters from keyword. Until now when im clicking the button text inside the text field doesn't transfered to the other page, print out null always
Any idea?
Any idea?
Please make sure the textField is actually inside <form> </form> tags, together with the button.
#5
Posted 23 March 2011 - 06:31 AM
Thank you, web site working fine, without exceptions. The problem now is the cointained keyword would vary. If i put ....name[contains(./'to')] print me out just tomatoes name and that's right...but what happens if i put a variable there? Prints me out all products whatever keyword be. Maybe because it wants the single quotes?
Thank you
Thank you
#6
Posted 23 March 2011 - 07:55 AM
String xpath = "/food/item/name[contains(.,'" + variable + "')]";
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









