my Xpath function only print attributes not element

M

Mariano

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) {
var displayText;
if (window.XMLHttpRequest) { // mozilla
displayText = xmlURL.evaluate(pathString, xmlURL, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.nodeValue;
} else { // internet explorer
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, second correctly print MARIANO. Why the first
path return a null value? Both two functions works properly in xPath
Explorer. Thank you all...
 
M

Martin Honnen

Mariano said:
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) {
var displayText;
if (window.XMLHttpRequest) { // mozilla

That test and comment is wrong, if you want to use the evaluate method
then check for that method and not for an unrelated object like
XMLHttpRequest.
displayText = xmlURL.evaluate(pathString, xmlURL, null,
XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue.nodeValue;
} else { // internet explorer
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, second correctly print MARIANO. Why the first
path return a null value? Both two functions works properly in xPath
Explorer. Thank you all...

The first XPath expression selects an element node ('password') and in
the DOM model the nodeValue of elements nodes is defined to be null. You
need the textContent (W3C DOM Level 3) property or the text (MSXML DOM)
property to access the text content of an element. Or you need to access
the firstChild.nodeValue as in the case of your 'password' element that
child is a text node and text nodes have nodeValue as their contents.
 
T

Thomas 'PointedEars' Lahn

Mariano said:
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-Core/core.html#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
 
T

Thomas 'PointedEars' Lahn

Martin said:
[...]
The first XPath expression selects an element node ('password') and in
the DOM model the nodeValue of elements nodes is defined to be null. You
need the textContent (W3C DOM Level 3) property or the text (MSXML DOM)
property to access the text content of an element. Or you need to access
the firstChild.nodeValue as in the case of your 'password' element that
child is a text node and text nodes have nodeValue as their contents.

In this case firstChild.nodeValue may suffice, but not in the general case
as the text content of an element node may consists of several text nodes.
textContent and its equivalent are indeed the best choices here; the safest
would be iterating through the child nodes of the element node.


PointedEars
 
T

Thomas 'PointedEars' Lahn

Mariano said:
function isMethod(o, p)
{
return o && /\b(function|object)\b/i.test(typeof o[p]) && o[p];
}

How does it works exactly???

isMethod() returns `true' if `o' evaluates to `true' (`o' is a reference to
an object reference or something convertible to an object reference), if
typeof o[p] yields something that contains (case-insensitive match) the
word "function" or the word "object" (indicating that o[p] is something
callable), and if o[p] evaluates to `true' (making sure that it does not
yield `null', because `typeof null == "object"'). Otherwise, it returns
`false'.

Please leave in the attribution line Google includes automatically in order
to show the authorship of quoted material.


HTH

PointedEars
 
M

Mariano

Martin said:
[...]
The first XPath expression selects an element node ('password') and in
the DOM model the nodeValue of elements nodes is defined to be null. You
need the textContent (W3C DOM Level 3) property or the text (MSXML DOM)
property to access the text content of an element. Or you need to access
the firstChild.nodeValue as in the case of your 'password' element that
child is a text node and text nodes have nodeValue as their contents.

In this case firstChild.nodeValue may suffice, but not in the general case
as the text content of an element node may consists of several text nodes.
textContent and its equivalent are indeed the best choices here; the safest
would be iterating through the child nodes of the element node.

PointedEars

I'm not so experted in javascript, i've started few days ago to
learning it. How could modify myPath() function to suite my necessity?
 
T

Thomas 'PointedEars' Lahn

Mariano said:
Martin said:
[...]
The first XPath expression selects an element node ('password') and in
the DOM model the nodeValue of elements nodes is defined to be null. You
need the textContent (W3C DOM Level 3) property or the text (MSXML DOM)
property to access the text content of an element. Or you need to access
the firstChild.nodeValue as in the case of your 'password' element that
child is a text node and text nodes have nodeValue as their contents.
In this case firstChild.nodeValue may suffice, but not in the general case
as the text content of an element node may consists of several text nodes.
textContent and its equivalent are indeed the best choices here; the safest
would be iterating through the child nodes of the element node.
[...]

Please quote only the necessary minimum to retain context; don't quote
signatures unless you explicitly refer to them.

http://jibbering.com/faq/
I'm not so experted in javascript, i've started few days ago to
learning it. How could modify myPath() function to suite my necessity?

Replace `nodeValue' with `textContent' in the Document::evaluate() branch
and `nodeValue' with `text' in the XMLDOMDocument::selectSingleNode() branch.

Have you even written the existing code yourself, or is this all just
copy-and-pray? Because from your question it appears you don't know
what you are doing in the first place.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top