 |
| 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
|
|
|
|

June 11th, 2008, 07:38 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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...
|
|

June 11th, 2008, 07:41 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
You can use XPath to search for elements containing that word 'black':
//*[contains(., 'black')]
--
Martin Honnen
Microsoft MVP - XML
|
|

June 11th, 2008, 08:25 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

June 11th, 2008, 08:29 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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>
|
|

June 11th, 2008, 08:36 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

June 11th, 2008, 08:39 AM
|
 |
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
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 -/
|
|

June 11th, 2008, 08:41 AM
|
 |
Wrox Author
|
|
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
|
|
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
|
|

June 11th, 2008, 09:00 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|

June 11th, 2008, 09:35 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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
|
|

June 11th, 2008, 11:09 AM
|
|
Authorized User
|
|
Join Date: Jun 2008
Posts: 46
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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.
|
|
 |