Re: regular expression performance - Mozilla

This is a discussion on Re: regular expression performance - Mozilla ; ----- Original Message ---- > From: Neil > * Walk the text nodes using a tree walker or xpath evaluator (but > this will miss embedded styles e.g. sex) Maybe I'm just being dense, but I can't find an expression ...

+ Reply to Thread
Results 1 to 3 of 3

Thread: Re: regular expression performance

  1. Re: regular expression performance


    ----- Original Message ----
    > From: Neil
    > * Walk the text nodes using a tree walker or xpath evaluator (but
    > this will miss embedded styles e.g. sex)


    Maybe I'm just being dense, but I can't find an expression to select all text nodes. None of the following work. Any suggestions? [Note: I can select single text nodes succesfully, so I know the general form and execution context of the below code is correct]

    var res = document.evaluate("/text()",
    document.documentElement, null, XPathResult.ANY_TYPE, null);
    var n =
    res.iterateNext();
    //n is null

    var res = document.evaluate("/html/text()",
    document.documentElement, null, XPathResult.ANY_TYPE, null);
    var n =
    res.iterateNext();
    // n is null

    var res = document.evaluate("/text()",
    document, null, XPathResult.ANY_TYPE, null);
    var n =
    res.iterateNext();
    // n is null

    var res = document.evaluate("/html/text()",
    document, null, XPathResult.ANY_TYPE, null);
    var n =
    res.iterateNext();
    // n is null




  2. Re: regular expression performance

    Eric H. Jung wrote:

    >Maybe I'm just being dense, but I can't find an expression to select all text nodes. None of the following work. Any suggestions? [Note: I can select single text nodes succesfully, so I know the general form and execution context of the below code is correct]
    >
    >var res = document.evaluate("/text()",
    >document.documentElement, null, XPathResult.ANY_TYPE, null);
    >

    I've only used XPath once before, and that time the magic incantation
    was "//text()", maybe that will work for you too?

    --
    Warning: May contain traces of nuts.

  3. Re: regular expression performance

    Neil wrote:
    > Eric H. Jung wrote:
    >
    >> Maybe I'm just being dense, but I can't find an expression to select
    >> all text nodes. None of the following work. Any suggestions? [Note: I
    >> can select single text nodes succesfully, so I know the general form
    >> and execution context of the below code is correct]
    >>
    >> var res = document.evaluate("/text()",
    >> document.documentElement, null, XPathResult.ANY_TYPE, null);
    >>

    > I've only used XPath once before, and that time the magic incantation
    > was "//text()", maybe that will work for you too?
    >


    Yes... One slash just means all immediate children of the context node,
    and in this case that is set by the 2nd argument, here
    document.documentElement (which in this case must be ). Two
    slashes together get all descendents from that point onward. You can
    combine them too: e.g., //div[@class='TOC']/table//td[1]

    text() just refers to text nodes at a given step, so you have to
    indicate you want the text nodes anywhere, starting from the root, which
    is what //text() does.

    Besides an extension like the DOM inspector, if you expand a little bit
    into the fabulously cool XQuery language (XPath is valid XQuery), you
    can test such queries out yourself on HTML or XML documents you
    encounter on the web (and more usefully, do things with the text you
    find). I'm looking for testers on Linux, btw, but this is already known
    to work in Windows: https://addons.mozilla.org/en-US/firefox/addon/5515

    w3schools has a basic XQuery tutorial. Anyone using XML regularly needs
    some XQuery imo, just as all programmers need to know regular
    expressions (and XQuery allows regexp too).

    best wishes,
    Brett

+ Reply to Thread