Subject: Error with oNodes zXPath in chapter 4
Posted By: chung Post Date: 1/15/2007 8:37:15 AM
Hi:

I like this book so far but hit a problem in chapter 4, "Cross-Browser XPath", p108.

In file "NameSpaceBooks.htm":

var oRoot = oXmlDom.documentElement;
var sXPath = "na:book/pub:name";
var oNodes = zXPath.selectNodes(oRoot, sXPath, sNameSpace);
if (oNodes.length > 0) {
alert(oNodes.length + " nodes selected with '" + sXPath + "'");
}


oNodes.length returns 0 for me.
The file NameSpaceBooks.htm shows as a blank page in both FF and IE.
NameSpaceBooks.xml is fine as far as I can tell.

I am using * JavaScript zXml Library * Version 1.0.2

Any advice appreciated.

Chung Wang

Reply By: joefawcett Reply Date: 1/15/2007 9:33:01 AM
There is a slight error in the book regarding how to set namespace mappings for zXml which should be fixed for the next print run.
You need something like:

var namespace1 = "<namespace URI for na here>";
var namespace2 = "<namespace URI for pub here>";
var oNamespaceMapper = {na: namespace1, pub: namespace2};
var oRoot = oXmlDom.documentElement;
var sXPath = "na:book/pub:name";
var oNodes = zXPath.selectNodes(oRoot, sXPath, oNamespaceMapper);
if (oNodes.length > 0)
{
  alert(oNodes.length + " nodes selected with '" + sXPath + "'");
}

You will need to replace the namespace declarations with the real ones.

--

Joe (Microsoft MVP - XML)

Go to topic 54600

Return to index page 63
Return to index page 62
Return to index page 61
Return to index page 60
Return to index page 59
Return to index page 58
Return to index page 57
Return to index page 56
Return to index page 55
Return to index page 54