Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
| Search | Today's Posts | Mark Forums Read
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old March 19th, 2008, 08:39 AM
Authorized User
 
Join Date: Jun 2006
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default Get NodesList by Xpath with Attributes Filters

//////////////////////////////////////////
       XML File
//////////////////////////////////////////

<Campaigns>
  <Campaign ScriptID="1111111" SeqNo="1">
    <ID>C38BA51F-5D86-4395-92B5-010F7A64D999</ID>
    <Url>http://www.test1.com</Url>
  </Campaign>
  <Campaign ScriptID="1111111" SeqNo="2">
    <ID>4395-92B5-010F7A64D999</ID>
    <Url>http://www.test2.com</Url>
  </Campaign>
  <Campaign ScriptID="1111111" SeqNo="3">
    <ID>C38BA51F-010F7A64D999</ID>
    <Url>http://www.test3.com</Url>
  </Campaign>
  <Campaign ScriptID="1111111" SeqNo="4">
    <ID>010F7A64D999</ID>
    <Url>http://www.test4.com</Url>
  </Campaign>
</Campaigns>

///////////////////////////////////////
 CODE to parse the XML selected nodes
///////////////////////////////////////
var ScriptID = '921924826';
var random_num = 0;
while(random_num == 0)
{
  random_num = parseInt((Math.round(Math.random() *4)));
}

x=xmlDoc.getElementsByTagName
('Campaign[@ScriptID = \'' + ScriptID + '\' and @SeqNo = \'' + random_num + '\']');

alert('x.length : ' + x.length);

campaignID = x[0].childNodes[0].childNodes[0].nodeValue;

linkURL = x[0].childNodes[1].childNodes[0].nodeValue;

alert('ID : ' + campaignID + ' Url :' + linkURL);

//////////////////////////////
           Problem
//////////////////////////////
This code is parsing the above XML file perfectly in IE , but failed in
firefox(2.0.0.12).
It shows the x.length = 0 , why?
May be there is reason of XPath with attributes (ScriptID , SeqNo).
If any idea then please share it.


 
Old March 19th, 2008, 08:56 AM
mhkay's Avatar
Wrox Author
Points: 18,487, Level: 59
Points: 18,487, Level: 59 Points: 18,487, Level: 59 Points: 18,487, Level: 59
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Apr 2004
Location: Reading, Berks, United Kingdom.
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

My guess would be that it's the whitespace problem. Microsoft parsers by default strip the whitespace-only text nodes from your document while parsing; nearly all other parsers leave them intact.

Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old March 19th, 2008, 09:06 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

getElementsByTagName is a W3C DOM Core method and takes a single element name as its argument (e.g. "Campaign"), not an XPath expression.
If you want to to use XPath with Mozilla or with Opera then use the W3C DOM Level 3 XPath API documented here: http://developer.mozilla.org/en/docs..._in_JavaScript and here: http://www.w3.org/TR/DOM-Level-3-XPath/.

 
Old March 20th, 2008, 04:13 AM
Authorized User
 
Join Date: Jun 2006
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Martin Honnen! Thanks for ur time.
I ve reveiwed both of these links but did not get any idea.
(How to list all Campaigns with specfic ScriptID?)

 
Old March 20th, 2008, 08:20 AM
Friend of Wrox
Points: 6,676, Level: 34
Points: 6,676, Level: 34 Points: 6,676, Level: 34 Points: 6,676, Level: 34
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2007
Location: Germany
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well those documents describe the XPath API and how to use that, basically the DOM document gets a method named 'evaluate' to which you pass an XPath expression, a context node, an optional namespace resolver, a result type and an optional result.
Here is an example how to use that with Mozilla:
Code:
var httpRequest = new XMLHttpRequest();
httpRequest.open('GET', 'campaigns.xml', true);
httpRequest.onreadystatechange = function () {
  if (httpRequest.readyState == 4) {
    var doc = httpRequest.respon****ML;
    var id = '1111111';
    if (typeof doc.evaluate != 'undefined') {
      var xpathResult = doc.evaluate('Campaigns/Campaign[@ScriptID = "' + id + '"]', doc, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);
      var campaign;
      while ((campaign = xpathResult.iterateNext()) != null) {
        var id = campaign.getElementsByTagName('ID')[0];
        alert(id.textContent);
      }
    }
  }
};

httpRequest.send(null);
(editing) The property the code is supposed to use is called 'r e s p o n s e X M L' without the spaces, it seems the forum software censors that as it contains a certain three letter word.
 
Old March 26th, 2008, 01:02 AM
Authorized User
 
Join Date: Jun 2006
Location: , , .
Posts: 28
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks a lot Martin Honnen!
       Excellent Solution ,this is working fine now.
 For Cross Domain Accesss , I ve just added the
   "netscape.security.PrivilegeManager.enablePrivileg e("UniversalBrowserRead");"






Similar Threads
Thread Thread Starter Forum Replies Last Post
Xpath - Selecting attributes bonekrusher XSLT 3 October 1st, 2008 12:27 PM
Xpath expressions with attributes rbooth XSLT 3 November 11th, 2007 04:51 AM
About filters alf.guzman Servlets 0 December 16th, 2004 06:07 PM
Filters filters and again .... GregoryHu Access VBA 4 July 28th, 2004 10:59 PM
Filters and Images. Nasimov HTML Code Clinic 1 November 18th, 2003 12:57 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.