mozilla and XPath Expressions

Discussion in 'XML' started by Neil Zanella, Dec 9, 2003.

  1. Neil Zanella

    Neil Zanella Guest

    Hello,

    I would like to know whether the mozilla web browser has built in support
    for searching XML documents via XPath expressions as with IE's xmlobject's
    and xmlDoc's function selectNodes() or similar.

    Thanks!

    Neil
     
    Neil Zanella, Dec 9, 2003
    #1
    1. Advertising

  2. Neil Zanella wrote:

    > I would like to know whether the mozilla web browser has built in support
    > for searching XML documents via XPath expressions as with IE's xmlobject's
    > and xmlDoc's function selectNodes() or similar.


    Mozilla has XPath support but not the restricted MSXML selectNodes but
    rather a more complete approach that is being under development, Mozilla
    attempts to implement the W3C DOM Level 3 XPath module:
    http://www.w3.org/TR/2003/CR-DOM-Level-3-XPath-20030331/

    At the heart of that is method
    document.evaluate(
    string xpathexpression,
    node contextNode,
    XPathNSResolver namespaceresolver,
    number desiredResultType,
    object resultObject
    )

    Some examples on how to use it, lets create a short example document first:

    var xmlSource = '';
    xmlSource += '<gods>';
    xmlSource += '<god name="Kibo" />';
    xmlSource += '<god name="Xibo" />';
    xmlSource += '<\/gods>';
    var xmlDocument = new DOMParser().parseFromString(xmlSource, 'text/xml');

    var xmlSerializer = new XMLSerializer();

    Selecting a single node:

    var xpathResult = xmlDocument.evaluate(
    '//god',
    xmlDocument,
    null,
    9,
    null
    );

    if (xpathResult.singleNodeValue) {
    alert(xmlSerializer.serializeToString(xpathResult.singleNodeValue));
    }


    Selecting an ordered node list:

    var xpathResult = xmlDocument.evaluate(
    '//god',
    xmlDocument,
    null,
    5,
    null
    );

    var resultNode;

    while ((resultNode = xpathResult.iterateNext())) {
    alert(xmlSerializer.serializeToString(resultNode));
    }


    Evaluating an expression that returns a number:

    var xpathResult = xmlDocument.evaluate(
    'count(//*/@*)',
    xmlDocument,
    null,
    0,
    null
    );

    if (xpathResult.resultType == 1) {
    alert('Counted ' + xpathResult.numberValue + ' nodes.');
    }


    That should help you to get started if you then have a look at the
    specification.
    Or ask here if you have further questions.
    There is even some attempt to extend XPath to HTML documents:

    <html>
    <head>
    <title>"XPath" on HTML in Mozilla</title>
    <script type="text/javascript">
    function countCenteredParagraphs () {
    if (typeof document.evaluate != 'undefined') {
    var xpathResult =
    document.evaluate(
    'count(//p[@align = "center"])',
    document,
    null,
    1,
    null
    );
    alert('Found ' + xpathResult.numberValue + ' centered paragraphs.');
    }
    }
    window.onload = countCenteredParagraphs;
    </script>
    </head>
    <body>
    <p align="center">
    A paragraph with centered text.
    </p>
    <p align="center">
    A second paragraph with centered text.
    </p>
    </body>
    </html>
    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 9, 2003
    #2
    1. Advertising

  3. Neil Zanella

    Neil Zanella Guest

    Martin Honnen <> wrote in message news:<>...
    > Neil Zanella wrote:
    >
    > > I would like to know whether the mozilla web browser has built in support
    > > for searching XML documents via XPath expressions as with IE's xmlobject's
    > > and xmlDoc's function selectNodes() or similar.

    >
    > Mozilla has XPath support but not the restricted MSXML selectNodes but
    > rather a more complete approach that is being under development, Mozilla
    > attempts to implement the W3C DOM Level 3 XPath module:
    > http://www.w3.org/TR/2003/CR-DOM-Level-3-XPath-20030331/


    Thank you for your reply. I recently upgraded to mozilla 1.4.1 and I can see
    that it has a "DOM Inspector" feature accessible from the menu bar under
    Tools -> Web Development. I guess this is what you must be referring to.
    So how do I run the code snippets that you posted?

    > At the heart of that is method
    > document.evaluate(
    > string xpathexpression,
    > node contextNode,
    > XPathNSResolver namespaceresolver,
    > number desiredResultType,
    > object resultObject
    > )
    >
    > Some examples on how to use it, lets create a short example document first:
    >
    > var xmlSource = '';
    > xmlSource += '<gods>';
    > xmlSource += '<god name="Kibo" />';
    > xmlSource += '<god name="Xibo" />';
    > xmlSource += '<\/gods>';
    > var xmlDocument = new DOMParser().parseFromString(xmlSource, 'text/xml');


    > That should help you to get started if you then have a look at the
    > specification. Or ask here if you have further questions.
    > There is even some attempt to extend XPath to HTML documents:


    Well, I thought that at least this should already be possible as far
    as XHTML is concerned since XHTML is a reformulation of HTML as an XML
    application, right?

    Thanks!

    Neil
     
    Neil Zanella, Dec 14, 2003
    #3
    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. Jay Douglas
    Replies:
    0
    Views:
    610
    Jay Douglas
    Aug 15, 2003
  2. Thor W Hammer
    Replies:
    0
    Views:
    1,761
    Thor W Hammer
    Mar 14, 2006
  3. Henri
    Replies:
    9
    Views:
    1,848
    Toby Inkster
    Feb 22, 2005
  4. Tjerk Wolterink

    XPath: efficiency in xpath expressions

    Tjerk Wolterink, Nov 13, 2004, in forum: XML
    Replies:
    1
    Views:
    1,645
    Richard Tobin
    Nov 13, 2004
  5. Noman Shapiro
    Replies:
    0
    Views:
    235
    Noman Shapiro
    Jul 17, 2013
Loading...

Share This Page