Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Oct 2005
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts

    javascript /xpath match

    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.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,650
    Thanks
    80
    Thanked 4,638 Times in 4,600 Posts
    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?

  • #3
    New Coder
    Join Date
    Oct 2005
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Ok, well what I was thinking is to have 3 arrays.
    Examples:

    Code:
    array[0] = ["foo1, foo2, foo3"];
    array[1] = ["foo4, foo5, foo6"];
    array[2] = ["foo7, foo8, foo9"];
    Now I pull the titles off the page with
    Code:
    var xpath = document.evaluate("the query", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null);
    Then I check the length
    Code:
    if (xpath.snapshotLength > 0) {
    }
    So if I get titles the it'll be greater then 0 so we proceed to the next step.

    Code:
    for(var i = 0; i < xpath.snapshotLength; i++) {
    }
    Now I'll go through each and start matching them up. Inside the
    Code:
    for
    I want to match to the titles with the above arrays.

    Code:
    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:
    Code:
    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)
    Last edited by Makien; 05-27-2009 at 05:14 PM.

  • #4
    New Coder
    Join Date
    Oct 2005
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Whelp after search the forum for a bit I found this http://www.codingforums.com/showpost...87&postcount=4 which helped a lot...

    I came up with this:
    Code:
    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.

  • #5
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,461
    Thanks
    11
    Thanked 600 Times in 580 Posts
    Quote Originally Posted by Makien View Post
    Whelp after search the forum for a bit I found this http://www.codingforums.com/showpost...87&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.
    my site (updated 2014/10/20)
    BROWSER STATS [% share] (2014/9/03) IE7:0.1, IE8:4.3, IE11:9.2, IE9:2.7, IE10:2.6, FF:16.8, CH:47.5, SF:7.8, NON-MOUSE:37%

  • #6
    New Coder
    Join Date
    Oct 2005
    Posts
    14
    Thanks
    1
    Thanked 0 Times in 0 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •