javascript xpath not recognizing '?' and '#'

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    javascript xpath not recognizing '?' and '#'

    Hi all,

    I have an xml like this :

    Code:
    <root>
      <url  newurl="something.asp?folder/file.htm#somethingsomething" />
    <root>
    This is stored in a file. i am loading it at runtime like this :

    Code:
    var xmlDoc; 
    xmlDoc.load(Filepath);

    Till here it works fine ... file is loaded correctly and everything. Then i am doing something like this :

    Code:
    var xpath="",Url,Node;
    Url = window.location.href.split(window.location.host+"/")[1]; //To get the part after the host ... this is the same format that is there in the "newurl" attribute of the xml shown above 
        
    xpath="url[@newurl='"+Url+"']";
    Node = selectSingleNode(xmlDoc.documentElement,xpath);
    The function selectSingleNod e is as follows :

    Code:
    function selectSingleNode(sElem,sXPath) {
        if(sElem==null) return null;
    	try{// IE 
    		return sElem.selectSingleNode(sXPath);
    	}
    	catch(ee){//Firefox and other browsers  
    		var oEvaluator = new XPathEvaluator();
    		// FIRST_ORDERED_NODE_TYPE returns the first match to the xpath.
    		var oResult = oEvaluator.evaluate(sXPath, sElem, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null);
    		if (oResult != null) {
    			return oResult.singleNodeValue;
    		} 
    		else {
    			return null;
    		}   
    	}
    }
    But Node is returning null even if the url is clearly present as an attribute.

    Any ideas ?

    Regards ,

    Rhitam
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Why not use getAttribute - see link.

    Comment

    • rhitam30111985
      New Member
      • Aug 2007
      • 112

      #3
      Originally posted by acoder
      Why not use getAttribute - see link.
      Thanks for reply ... actually my problem was due to not checking upper and lowwer case ... i used xpath translate() function instead ... anyhow ... problem is solved now ..

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        That's fab. Thanks for posting!

        Comment

        Working...