5

i need to search the html document for <p class="content"> text here </p>

and then output the full node path (CSS or XPATH)

for isntance

html > body > div class ="something" > table > tr > p class="content"

i tried with nokogiri but it doesn't handle the class and other attributes well..

i need a parser that does this without problem.

h34y
  • 489
  • 1
  • 6
  • 11

1 Answers1

9

Ok try this:

var path = [];

var el = document.getElementById('myNode');

do {
    path.unshift(el.nodeName + (el.className ? ' class="' + el.className + '"' : ''));
} while ((el.nodeName.toLowerCase() != 'html') && (el = el.parentNode));

alert(path.join(" > "));
nickf
  • 537,072
  • 198
  • 649
  • 721
  • how about if the element doesnt have a id. (i didnt write the page, i get to process it after render) ? – rcphq Jan 03 '11 at 20:19
  • use whatever method you have to get a reference to that element - `getElementByTagName`, or jQuery or whatever. – nickf Jan 03 '11 at 23:57