Mariano wrote:
> I will try to explain it better. Then, i've an XML document like this:
> <utenti>
> <utente username="mariano" id="1">
> <password>prova</password>
> <isAdmin>0</isAdmin>
> </utente>
> </utenti>
>
> and i've a javascript function to parse it:
> function myPath(xmlURL, pathString) {
Most of the identifiers here are not well-chosen. myPath() is
not supposed to return a path but a string value, xmlURL does
not designate a URL but an XML document object reference.
I recommend renaming.
> var displayText;
> if (window.XMLHttpRequest) { // mozilla
That is not only error-prone object inference -- see
http://PointedEars.de/scripts/test/whatami#inference --, it is
also an erroneous one: IE 7 also supports (window.)XMLHttpRequest.
You should feature-test exactly what you are about to use:
function isMethod(o, p)
{
return o && /\b(function|object)\b/i.test(typeof o[p]) && o[p];
}
if (isMethod(xmlURL, "evaluate"))
{
> displayText = xmlURL.evaluate(pathString, xmlURL, null,
> XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.nodeValue;
> } else { // internet explorer
}
else if (isMethod(xmlURL, "selectSingleNode"))
{
> displayText = xmlURL.selectSingleNode(pathString).nodeValue;
> }
> return displayText;
> }
>
> OK. Now there's the question: two uses of the same function with
> different path:
> var pwd = myPath(xmldoc, "/utenti/utente[@username='mariano']/
> password");
> var usr = myPath(xmldoc, "/utenti/utente[@id='1']/@username");
>
> First case print NULL,
It prints
null
as it should (it is an Element node):
http://www.w3.org/TR/DOM-Level-2-Cor...#ID-1841493061
You wanted to access the child text node(s) instead, which is why you
need the `textContent' property instead of the `nodeValue' property.
> second correctly print MARIANO.
As it should, you are accessing the attribute node directly.
> Both two functions works properly in xPath Explorer.
MSHTML does not implement the W3C DOM properly.
PointedEars