my Xpath function only print attributes not element

Discussion in 'Javascript' started by Mariano, Jan 13, 2008.

  1. Mariano

    Mariano Guest

    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...
     
    Mariano, Jan 13, 2008
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Jan 13, 2008
    #2
    1. Advertising

  3. 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-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
     
    Thomas 'PointedEars' Lahn, Jan 13, 2008
    #3
  4. 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
     
    Thomas 'PointedEars' Lahn, Jan 13, 2008
    #4
  5. Mariano

    Mariano Guest

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


    How does it works exactly???
     
    Mariano, Jan 13, 2008
    #5
  6. 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
     
    Thomas 'PointedEars' Lahn, Jan 13, 2008
    #6
  7. Mariano

    Mariano Guest

    On 13 Gen, 19:44, 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.
    >
    > 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?
     
    Mariano, Jan 13, 2008
    #7
  8. 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
     
    Thomas 'PointedEars' Lahn, Jan 13, 2008
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. edkawas
    Replies:
    0
    Views:
    507
    edkawas
    Jun 24, 2005
  2. Replies:
    0
    Views:
    506
  3. Mavis
    Replies:
    1
    Views:
    538
    =?iso-8859-1?q?Fl=E1vio_Barata?=
    Aug 8, 2006
  4. Ian Wilson
    Replies:
    2
    Views:
    571
    Ian Wilson
    Jul 26, 2007
  5. Mike N.
    Replies:
    3
    Views:
    592
    Joseph Kesselman
    Mar 17, 2008
Loading...

Share This Page