SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript and xpath problem

    Hello All,

    I have xml file conating book details.Now I want to search some book details.
    I want to serach using xpath in javascript.
    I searched and I come accross the following vbscript code.
    But I want to use xpath using javascript

    <script type="text/vbscript">

    set xmlDoc=CreateObject("Microsoft.XMLDOM")
    xmlDoc.async="false"
    xmlDoc.load("books.xml")

    set nodes=xmlDoc.selectNodes("/bookstore/book")

    for each x in nodes
    document.write("<xmp>")
    document.write(x.xml)
    document.write("</xmp>")
    next

    </script>
    Please help me how to use xpath in javascript.
    Barbara

  2. #2
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Give this a read: http://developer.mozilla.org/en/docs..._in_JavaScript

    I found it very helpful.

  3. #3
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the link
    From that link I tried and its running.But stuck at one point
    The example I am trying is

    if (window.XMLHttpRequest)
    {
    req=new XMLHttpRequest()
    }
    else if (window.ActiveXObject)
    {
    req=new ActiveXObject("Microsoft.XMLHTTP")
    }
    req.open("GET","book_detail.xml", false);
    req.send(null);
    var xmlDoc = req.responseXML;
    var nsResolver = xmlDoc.createNSResolver( xmlDoc.ownerDocument == null ? xmlDoc.documentElement : xmlDoc.ownerDocument.documentElement);
    var personIterator = xmlDoc.evaluate('//BOOK[PRICE=35]', xmlDoc, nsResolver, XPathResult.ANY_TYPE, null );
    var thisNode = personIterator.iterateNext();
    while (thisNode)
    {
    alert( thisNode.textContent );
    thisNode = personIterator.iterateNext();
    }
    it gives me the result if search condition is an integer value i.e.
    var personIterator = xmlDoc.evaluate('//BOOK[PRICE=35]', xmlDoc, nsResolver,
    But fails to give the result if search condition is a string value i.e.
    var personIterator = xmlDoc.evaluate('//BOOK[ID=P01]', xmlDoc, nsResolver,

    Please help me!
    Barbara

  4. #4
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I don't have much experience with XPath, but I think it might be as simple as surrounding the value in quotation marks:
    Code:
    var personIterator = xmlDoc.evaluate('//BOOK[ID="P01"]', xmlDoc, nsResolver,

  5. #5
    SitePoint Evangelist barbara1712's Avatar
    Join Date
    Apr 2007
    Location
    India
    Posts
    508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot.

    It's working properly for mozilla and got the results for integer and string value as well.
    But not working for IE.
    And I have written my code for both.

    Is there any problem with my code to run in IE?
    Barbara

  6. #6
    I meant that to happen silver trophybronze trophy Raffles's Avatar
    Join Date
    Sep 2005
    Location
    Tanzania
    Posts
    4,662
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    I've only used XPath for Greasemonkey scripts, so I've never needed to make it work in anything but Mozilla, but after a quick search it seems (unsurprisingly) that IE implements XPath in a different way. Looks like it should be fairly easy to use with object detection though.

    I also had a look for Safari's support and it seems like it's not worth thinking much about - see this Safari blog post from August last year.

    Opera 9 support it too and should work in exactly the same way as Mozilla's implementation.


Bookmarks

Posting Permissions

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