Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Search in XML

Status
Not open for further replies.

lee2k

Programmer
Aug 19, 2001
28
0
0
IN
hi,

i want javascript or vbscript codings to find "bk102" record in the following xml file. is there any way to findnext() method to find next matching.

thanx in advance

======xml file starts======
<?xml version=&quot;1.0&quot;?>
<catalog>
<book id=&quot;bk101&quot;>
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id=&quot;bk102&quot;>
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies, an evil sorceress, and her own childhood to become queen of the world.</description>
</book>
<book id=&quot;bk103&quot;>
<author>Corets, Eva</author>
<title>Maeve Ascendant</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-11-17</publish_date>
<description>After the collapse of a nanotechnology society in England, the young survivors lay the foundation for a new society.</description>
</book>
</catalog>
======xml file ends======

 
Are you looking to allow a user to input the book id and then pull up the information? If so, you would need to step through the document in JavaScript using the DOM. This can be astoundingly slow for larger files. The &quot;find next&quot; question... were you wanting to find the next occurence of the string &quot;bk102&quot;, or find the value of the next bookid tag?
 
Use can use an easy XPATH expressions to find a node if you are using xml3 ...

....

e.g.
selectNodes or selectSingleNode methods that will return you the nodes according the query that you used...

for you case I can try to guess - it can be the next query (I didn't test it):
'catalog/book [@id=&quot;bk102&quot;]'

..more about this read in MSDN XPATH overview...
Please, notify me if it helped you.
Thanks


Issahar Gourfinkel
senior software engineer
Israel

 
Better idea for a search page, it involves using Javascript, XSL and ASP...

Step One: Place the following code anywhere, inserting what you want for link, title, and description in your search.xsl page...

<FORM NAME=&quot;database&quot;>
<input type=&quot;hidden&quot; name=&quot;list&quot;><xsl:attribute name=&quot;value&quot;><xsl:for-each select=&quot;whatever&quot;><xsl:value-of select=&quot;field[@id='link']/field_value&quot;/>~<xsl:value-of select=&quot;field[@id='title']/field_value&quot;/>|<xsl:value-of select=&quot;field[@id='description']/field_value&quot; />^<xsl:value-of select=&quot;field[@id='description']/field_value&quot;/>!*</xsl:for-each></xsl:attribute></input>
</FORM>

Step Two: Place this code into the body of your search.xsl page

<script language=&quot;JavaScript&quot;>
<xsl:comment><![CDATA[
function Page(url,title,keywords,description) {
while ((url.length > 0) && (url.charAt(0) == &quot; &quot;)) {
url = url.substring(1,url.length);
}
this.url = url;
while ((title.length > 0) && (title.charAt(0) == &quot; &quot;)) {
title = title.substring(1,title.length);
}
this.title = title;
this.keywords = keywords;
this.description = description;
return this;
}
function Database() {
var pos = 0;
while ((pos1 = amorphous.indexOf(&quot;~&quot;,pos)) != -1) {
pos2 = amorphous.indexOf(&quot;|&quot;,pos1+1);
pos3 = amorphous.indexOf(&quot;^&quot;,pos2+1);
pos4 = amorphous.indexOf(&quot;*&quot;,pos3+1);
if ((pos2 != -1)
&& (pos2 < pos3) && (pos3 < pos4)
&& (pos4 <= amorphous.indexOf(&quot;*&quot;,pos))) {
this[database_length++] = new Page(amorphous.substring(pos,pos1),
amorphous.substring(pos1+1,pos2),
amorphous.substring(pos2+1,pos3),
amorphous.substring(pos3+1,pos4));
pos = pos4+1;
} else { // error reading amorphous database
if (pos+30 <= amorphous.length)
alert('Error reading in amorphous database around &quot;'
+ amorphous.substring(pos,pos+30) + '&quot;');
pos = amorphous.indexOf(&quot;*&quot;,pos) + 1;
}
}
return this;
}
function search(str) {
menu_length = 0;
temp = new Object();
temp_length = 0;
words_length = 0;
words = new Object();
pos = 0;
while ((pos = str.indexOf(&quot; &quot;)) != -1
&& and_search != &quot;exact&quot;) {
words[words_length] = str.substring(0,pos);
if (words[words_length].length > 0)
words_length++;
if (str.length == 1)
str=&quot;&quot;;
else
str = str.substring(pos+1,str.length);
}
if (str.length > 0)
words[words_length++] = str;
for (q=0;q<words_length;q++) {
temp_length = 0;
str = words[q].toLowerCase();
len = (and_search==&quot;and&quot;&&q>0?menu_length:database_length);
for (n=0; n<len; n++) {
if (and_search==&quot;and&quot;&&q>0) {
combo = (menu[n].title + &quot; &quot; + menu[n].description
+ &quot; &quot; + menu[n].keywords).toLowerCase();
} else {
combo = (database[n].title + &quot; &quot; + database[n].description
+ &quot; &quot; + database[n].keywords).toLowerCase();
}
if (combo.indexOf(str) != -1) // found
temp[temp_length++] = (and_search==&quot;and&quot;&&q>0?menu[n]:database[n]);
}
if (and_search!=&quot;and&quot; && q>0) {
added = 0;
for (i=0;i<temp_length;i++) {
duplicate = false;
for (j=0;j<menu_length&&!duplicate;j++) {
if (menu[j] == temp) {
duplicate = true;
}
}
if (!duplicate)
menu[menu_length+(added++)] = temp;
}
menu_length += added;
} else {
for(h=0;h<temp_length;h++)
menu[h] = temp[h];
menu_length = temp_length;
}
}
}
function entry() {
if ((document.entryform.keyword.value.length == 0)
|| (document.entryform.keyword.value == &quot; &quot;)) {
alert(&quot;First you must enter a keyword to search for.&quot;);
return false;
}
and_search = (document.entryform.and_or.selectedIndex == 0?&quot;and&quot;:&quot;or&quot;);
if (document.entryform.and_or.selectedIndex == 2)
and_search = &quot;exact&quot;;
location.href = location.pathname + &quot;?&quot;
+ escape(document.entryform.keyword.value)
+ (and_search != &quot;or&quot;?&quot;&&quot;+and_search:&quot;&quot;);
return false;
}
function redWord(str) {
for(r=0; r<words_length; r++) {
pos = -3;
word = words[r].toLowerCase();
while ((pos = str.toLowerCase().indexOf(word,pos+3)) != -1) {
val = pos+word.length;
str = str.substring(0,pos) + &quot;*&quot;
+ str.substring(pos,val) + &quot;|&quot;
+ str.substring(val,str.length);
}
}
pos = -16;
while ((pos = str.toLowerCase().indexOf(&quot;*&quot;,pos+16)) != -1)
str = str.substring(0,pos) + &quot;<font color=red>&quot;
+ str.substring(pos+1,str.length);
pos = -7;
while ((pos = str.toLowerCase().indexOf(&quot;|&quot;,pos+7)) != -1)
str = str.substring(0,pos) + &quot;</font>&quot;
+ str.substring(pos+1,str.length);
return str;
}

var amorphous = document.database.list.value;
temp_str = amorphous.substring(amorphous.length-2,amorphous.length);
if (temp_str.indexOf(&quot;*&quot;) == -1)
amorphous += &quot;* &quot;;
else
amorphous += &quot; &quot;; // amorphous database must have characters after last asterisk

database_length = 0; // Netscape 2 fix
var database = new Database(); // read in from amorphous database

menu_length = 0; // Netscape 2 fix
var menu = new Object();

string = &quot;&quot;;
and_search = &quot;or&quot;;
if (location.search.length > 1) {
string = unescape(location.search.substring(1,location.search.length));
pos = 0;
while ((pos = string.indexOf('&quot;',pos)) != -1) {
string = string.substring(0,pos) + '\\&quot;' + string.substring(pos+1,string.length);
pos += 2;
}
if (string.substring(string.length-4,string.length) == &quot;&and&quot;) {
string = string.substring(0,string.length-4);
and_search = &quot;and&quot;;
} else if (string.substring(string.length-6,string.length) == &quot;&exact&quot;) {
string = string.substring(0,string.length-6);
and_search = &quot;exact&quot;;
} else if (string.substring(string.length-3,string.length) == &quot;&or&quot;) {
string = string.substring(0,string.length-3);
and_search = &quot;or&quot;;
}
search(string);
}
document.write('<form name=&quot;entryform&quot; onSubmit=&quot;return entry()&quot;>'
+'<b>Search for:</b><br /><input type=&quot;text&quot; size=20 '
+'name=&quot;keyword&quot; value=&quot;'+string+'&quot;> '
+'<input type=&quot;button&quot; value=&quot;Search&quot; onClick=&quot;entry()&quot;><br /><select name=&quot;and_or&quot; '
+'size=1><option'+(and_search==&quot;and&quot;?&quot; selected&quot;:&quot;&quot;)+'>find all words '
+'(AND)<option'+(and_search==&quot;or&quot;?&quot; selected&quot;:&quot;&quot;)+'>find any word '
+'(OR)<option'+(and_search==&quot;exact&quot;?&quot; selected&quot;:&quot;&quot;)+'>exact '
+'match</select></form><br />');
if (location.search.length > 1)
document.write('<b>Results:</b><br /><br />\n');

// eliminate the keywords portion of the statement below to eliminate them from your display

for (n=0; n<menu_length; n++)
document.write('<b><a href=&quot;'+menu[n].url+'&quot;>'+menu[n].title
+'</a></b><br />'+redWord(menu[n].description)+'<br />Keywords: '
+redWord(menu[n].keywords)+'<br /><br />\n');
if ((menu_length == 0) && (location.search.length > 1))
document.write('Keyword &quot;'+string+'&quot; not found!\n');
// -->
]]></xsl:comment>
</script>

Step 3: Make an ASP page w/ the following code, such as search.asp

<%
'Load the XML
set xml = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
xml.async = false
xml.load(Server.MapPath(&quot;whateveryourxmlfileis.xml&quot;))

'Load the XSL
set xsl = Server.CreateObject(&quot;Microsoft.XMLDOM&quot;)
xsl.async = false
xsl.load(Server.MapPath(&quot;search.xsl&quot;))

Response.Write(xml.transformNode(xsl))
%>

What will happen: the ASP page will pick up your XML file and the search.xsl file will basically turn that XML file content into a set of search possibilities within the makeshift javascript database where each element, such as the link, title, etc is separated by odd characters such as ~, |, and ! --- for example, if I had the following xml document...

<whatever>
<title>Russ</title>
<description>Russ is rad</description>
<link>viewer.asp?select=Russ</link>
</whatever>

it would look like this in the code when parsed...

<form name=&quot;database&quot; value=&quot;viewer.asp?select=Russ~Russ|Russ is rad^Russ is rad!&quot;>



So if you fill out the search on the page with say... &quot;russ&quot; and one of your records in your XML document has &quot;russ&quot; in its description node, or whatever node value you put in between the special characters, it would spit out the search result...

<a href=&quot;viewer.asp?select=Russ&quot;>Russ:</a>
Russ is Rad, Russ Is Rad


Does this make any sense? You can do this with attributes too.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top