Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > my Xpath function only print attributes not element

Reply
Thread Tools

my Xpath function only print attributes not element

 
 
Mariano
Guest
Posts: n/a
 
      01-13-2008
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...
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      01-13-2008
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) {
> 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.


--

Martin Honnen
http://JavaScript.FAQTs.com/
 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2008
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
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2008
Martin Honnen wrote:
> [...]
> 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
 
Reply With Quote
 
Mariano
Guest
Posts: n/a
 
      01-13-2008
> function isMethod(o, p)
> {
> return o && /\b(function|object)\b/i.test(typeof o[p]) && o[p];
> }


How does it works exactly???
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2008
Mariano wrote:
>> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
Mariano
Guest
Posts: n/a
 
      01-13-2008
On 13 Gen, 19:44, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
> Martin Honnen wrote:
> > [...]
> > 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?
 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      01-13-2008
Mariano wrote:
> [...] Thomas 'PointedEars' Lahn [...] wrote:
>> Martin Honnen wrote:
>>> [...]
>>> 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
--
Anyone who slaps a 'this page is best viewed with Browser X' label on
a Web page appears to be yearning for the bad old days, before the Web,
when you had very little chance of reading a document written on another
computer, another word processor, or another network. -- Tim Berners-Lee
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
xtml: combining attributes from one element into a different element Mike N. XML 3 03-17-2008 01:30 PM
XPath expression for element based on content of other element withsame parent Ian Wilson XML 2 07-26-2007 04:11 PM
Generate XPath for element, type, and attributes in XSD file Mavis Java 1 08-08-2006 11:31 AM
Query : Number of Attributes under an element & atleast 1 occurence of element/Attribute kosaraju.puneeth@gmail.com XML 0 10-26-2005 07:09 AM
XPath that does not include other XPath Anna XML 0 07-31-2003 07:55 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57