Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
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 software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 11th, 2008, 07:38 AM
Authorized User
 
Join Date: Jun 2008
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...

 
Old June 11th, 2008, 07:41 AM
Friend of Wrox
 
Join Date: Nov 2007
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
 
Old June 11th, 2008, 08:25 AM
Friend of Wrox
 
Join Date: Nov 2007
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
 
Old June 11th, 2008, 08:29 AM
Authorized User
 
Join Date: Jun 2008
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>
 
Old June 11th, 2008, 08:36 AM
Friend of Wrox
 
Join Date: Nov 2007
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
 
Old June 11th, 2008, 08:39 AM
samjudson's Avatar
Friend of Wrox
 
Join Date: Aug 2007
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 -/
 
Old June 11th, 2008, 08:41 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
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
 
Old June 11th, 2008, 09:00 AM
Authorized User
 
Join Date: Jun 2008
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.

 
Old June 11th, 2008, 09:35 AM
Friend of Wrox
 
Join Date: Nov 2007
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
 
Old June 11th, 2008, 11:09 AM
Authorized User
 
Join Date: Jun 2008
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.






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





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