views:

238

answers:

1

I'm trying to filter some XML in JavaScript using E4X and have some specific needs. Given the following:

    var xml = <body>
      <div>
        <p>This is some text that I have.</p>
      </div>
    </div>;

I want to search the document for paragraphs starting with "This is some text".

Currently I can the following to get at the paragraph:

xml..div(p.text().toString().indexOf("This is some text") === 0)

However, the "real" XML is much more complex (think: a regular web page). There is no guarantee that there will be a div directly parenting the paragraph(s) in questions. There may be other paragraphs before/after the paragraph(s) in question within the same parent element.

Any ideas?

+2  A: 

Use the double dot operator to search for all <p> nodes of any nesting leveling and then filter out the nodes to only include ones that start with your desired string. You were close, you just need to get rid of the <div> since, as you've stated, the <div> might not be present.:

xml..p.( text().toString().indexOf("This is some text") == 0 );
darronschall