Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > XML > XML
Password Reminder
Register
| FAQ | Members List | Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old June 11th, 2008, 07:38 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default Search certain word using XML

Hello, I have a problem. I would like to search certain word from xml. For example:

I would like to search "black". And in XML file there is value "black board". But it does not appear.

Does anyone know how to solve it?

I mean when I type "black", the "black board" also appear...

Reply With Quote
  #2 (permalink)  
Old June 11th, 2008, 07:41 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

You can use XPath to search for elements containing that word 'black':
//*[contains(., 'black')]

--
  Martin Honnen
  Microsoft MVP - XML
Reply With Quote
  #3 (permalink)  
Old June 11th, 2008, 08:25 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to make sure you find the element(s) containing the word 'black' but exclude any ancestor element(s) of those then use
//*[contains(., 'black') and not(*[contains(., 'black')])]

--
  Martin Honnen
  Microsoft MVP - XML
Reply With Quote
  #4 (permalink)  
Old June 11th, 2008, 08:29 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I use Xpath...

Here is my Xpath:

path="form/description[contains(<%= request.Form("search_by")%>='<%= request.Form("txtSearch")%>')]//*";

I try to use "contains" but it does not work

Here is my XML(description2.xml):

<form>
    <description>
        <desc_id>048/26/SMD/HRD/9/00</desc_id>
        <title>Kebijakan2</title>
        <dates>3/21/1999</dates>
        <author>Smart Co</author>
        <file_name>Policy2</file_name>
        <type>pdf</type>
        <category>memorandum</category>
        <status>tidak_berlaku</status>
        <year>1983</year>
    </description>
</form>
Reply With Quote
  #5 (permalink)  
Old June 11th, 2008, 08:36 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

What "does not work" mean exactly, do you get an error, if so which one? And tell us exactly which XPath API (e.g. .NET or Java or PHP) you are using.

What you currently show is a mixture of ASP and some other code, can you show us the final XPath expression without the ASP code? I suspect you need a comma between those two ASP expressions e.g.
form/description[contains(title, 'Keb')]
not an equal ('=') sign.

--
  Martin Honnen
  Microsoft MVP - XML
Reply With Quote
  #6 (permalink)  
Old June 11th, 2008, 08:39 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

Think for a moment what you will produce there - for example say search_by is 'title' and txtSearch = 'fred' this will produce the following xpath:

path = "form/description[contains(title='fred')]//*";

Now if you do a quick search on the contains() xpath function (e.g. search google, or look it up at w3school.com) you will realise that contains takes two parameters, so title and 'fred' probably shouldn't have the '=' symbol between them, but perhaps a comma instead.

/- Sam Judson : Wrox Technical Editor -/
Reply With Quote
  #7 (permalink)  
Old June 11th, 2008, 08:41 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

The syntax is

contains(title, 'xyz')

whereas your ASP code is generating

contains(title='xyz')

Also, you really need to think about preventing injection attacks. For example (after you correct the above mistake), typing

')]/system-property('java.user

into the form would give the expression

form/description[contains(title, '')]/system-property('java.user')[1]

which might well reveal the username of the account under which the XSLT processor is running.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
Reply With Quote
  #8 (permalink)  
Old June 11th, 2008, 09:00 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Does not work means there is no error but it appears nothing.

I get "title" from the text field by using ASP to get it. So, it works not only for "title" but also author or dates or the other.

Reply With Quote
  #9 (permalink)  
Old June 11th, 2008, 09:35 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

When I use your XML sample with the following JScript code

var doc = new ActiveXObject('Msxml2.DOMDocument.3.0');
doc.async = false;
if (doc.load('file.xml')) {
  doc.setProperty('SelectionLanguage', 'XPath');
  var descs = doc.selectNodes('form/description[contains(title, \'bij\')]');
  WScript.Echo(descs.length);
}
else {
  WScript.Echo(doc.parseError.reason);
}

run with cscript on Windows then it outputs 1 so it finds the description element.

You will need to show us your code so that we can find out why you get no result.

--
  Martin Honnen
  Microsoft MVP - XML
Reply With Quote
  #10 (permalink)  
Old June 11th, 2008, 11:09 AM
Authorized User
 
Join Date: Jun 2008
Location: , , .
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
Default

example: Type=black board


How if I want to get the value (e.g. "black") from the textfield and do not type word "black" inside the code?

and I also want to get the "type" from the textfield. So, it can change depends on the user who will insert.


Here is my Xpath:

path="form/description[<%= request.Form("search_by")%>='<%= request.Form("txtSearch")%>']//*";

the error said that unexpected "=" but when I refresh again, it shows the value.

Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Word search - Count Hits bonekrusher XSLT 5 January 2nd, 2008 10:26 AM
how to use word and pdf filter in full text search connect2sandep SQL Server 2005 0 February 9th, 2006 02:00 PM
How to highlight search results in Word haiying Access VBA 0 July 25th, 2005 11:13 AM
how can i search ms-word used in c#.net project k_kalyan .NET Framework 2.0 1 February 28th, 2005 03:20 AM
Search Does not go beyond 1st word in field phungleon Classic ASP Databases 2 December 27th, 2004 08:11 PM



All times are GMT -4. The time now is 05:52 AM.


Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.