On Jan 24, 9:01 am, Nicholas <nbar...@gmail.com> wrote:
> For a short summary on my issue, all that remains is the text()
> function returning the string of all the node values under the one i'm
> currently working with. This is by design, but I don't want it to do
> this and for the life of me cannot discover a way to exclude all the
> child nodes from text()!.
>
> The XML example:
> <letter>
> <paragraph>data
> <paragraph>more data
> <paragraph>even more data
> </paragraph></paragraph></paragraph>
> </letter>
>
> I've designed a recursive function to move through the list until it
> reaches the end. However, the output looks like this (using the
> example above):
>
> 1. data more data even more data
> 2. more data even more data
> 3. even more data
>
> So, how can i get the text from only the node i'm currently working
> with instead of the text from the current and all child nodes? I've
> tried various filters and selectors but the results are always the
> same, either everything or nothing.

I think you mean you want the value of the text nodes that are
children of a particular node.  Something like the following should
work:

function getNodeText(el) {
  var node, txt = [];
  for (var i=0, len=el.childNodes.length; i<len; i++) {
    node = el.childNodes[i]
    if (node.nodeType == 3) {
      txt.push(node.data);
    }
  }
  return txt.join('');
}

There may be different amounts of whitespace in there depending on the
browser, you may need to use a RegExp to normalise that.


--
Rob

Reply via email to