...

View Full Version : javascript /xpath match



Makien
05-27-2009, 03:15 AM
Okay here's my issue. I'm using javascript with xpath pulling info from the page. Now I have pulled all the img titles from the page, there are a total of 37. Now I have 15 items I want to match to these 37. How exactly do I go about matching the 15 items to the 37 and if they match I want to do something.

Been driving myself crazy on this and I hate asking for help but I'm not completely versed in javascript, I'm better with php and sql.

Old Pedant
05-27-2009, 08:00 AM
Ummm...if you have 37 titles and they all start with the word "frammis" and then you have 15 items and they all have only product *numbers* then...

Okay, a bit extreme, but what I'm trying to say is how can *we* guess how 15 items would match up with 37 titles??? Do some items match 2 or 3 titles? Of to some titles have no matches? Or???

And assuming you do manage to match something up with something, what do you *do* with that match?

Makien
05-27-2009, 04:05 PM
Ok, well what I was thinking is to have 3 arrays.
Examples:


array[0] = ["foo1, foo2, foo3"];
array[1] = ["foo4, foo5, foo6"];
array[2] = ["foo7, foo8, foo9"];

Now I pull the titles off the page with
var xpath = document.evaluate("the query", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

Then I check the length

if (xpath.snapshotLength > 0) {
}

So if I get titles the it'll be greater then 0 so we proceed to the next step.


for(var i = 0; i < xpath.snapshotLength; i++) {
}

Now I'll go through each and start matching them up. Inside the
for I want to match to the titles with the above arrays.


if (array[0][i] == "xpath's Info"){
alert(1);
} else if (array[1][i] == "xpath's Info" ){
alert(2);
}else if (array[2][i] == "xpath's Info" ) {
alert(3);
} else {
alert("empty");
}

So the whole thing would look like:

array[0] = ["foo1, foo2, foo3"];
array[1] = ["foo4, foo5, foo6"];
array[2] = ["foo7, foo8, foo9"];

var xpath = document.evaluate("the query", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);

if (xpath.snapshotLength > 0) {
for(var i = 0; i < xpath.snapshotLength; i++) {
if (array[0][i] == "xpath's Info"){
alert(1);
} else if (array[1][i] == "xpath's Info" ){
alert(2);
}else if (array[2][i] == "xpath's Info" ) {
alert(3);
} else {
alert("empty");
}
}
}

But I'm having issues with having anything match up and need a little advise on where to go from here.... (sorry if sloppy, I just wrote this on the fly)

Makien
05-27-2009, 05:36 PM
Whelp after search the forum for a bit I found this http://www.codingforums.com/showpost.php?p=820087&postcount=4 which helped a lot...

I came up with this:

function inArray(ray, str){
return ("|"+ ray.join("|") +"|" ).indexOf("|"+str+"|")!==-1;
}

// I know a break isn't needed after a return it's just a precaution.
function getTitles() {
var xpath = document.evaluate("//img/@title", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
if (xpath.snapshotLength > 0) {
for(var i = 0; i < xpath.snapshotLength; i++) {
if (inArray(aLang[2], xpath.snapshotItem(i).textContent)){
return 2;
break;
} else if (inArray(aLang[1], xpath.snapshotItem(i).textContent)){
return 1;
break;
}else if (inArray(aLang[0], xpath.snapshotItem(i).textContent)) {
return 0;
break;
} else if (xpath.snapshotLength < i + 2){
return false;
}
}
}
return false;
}

and it's working so far... hopefully it'll keep working.. :) Thanks rnd me for the function.

rnd me
05-28-2009, 12:31 AM
Whelp after search the forum for a bit I found this http://www.codingforums.com/showpost.php?p=820087&postcount=4 which helped a lot...


and it's working so far... hopefully it'll keep working.. :) Thanks rnd me for the function.



i love it when people actually search for info about their problem.

Ya see? it pays off. take note guys!

glad to be of help.

Makien
05-28-2009, 02:37 PM
Yea I like to figure things out on my own, the "teach a man to fish..." proverb and all... But yea that function does work nicely and it's working better then I though. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum