Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 10-08-2010, 10:36 AM   PM User | #1
spiderplant0
New to the CF scene

 
Join Date: Oct 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
spiderplant0 is an unknown quantity at this point
document.evaluate() on an HTML string

Hi,
I was wondering if it's possible to capture a piece of HTML using XPath where the source is a sting rather than a document.

I know how to use xPath and document.evaluate() as I've used it before on actual web pages - I just dont understand how to run it on a string of HTML.

E.g. if I have this string:
var stg = "<div>The best-laid schemes o' <span>mice</span> an' men</div>";
I'd like to convert this string into something that I can run the document.evaluate() on so that I can find the contents of the SPAN element (without loading the string into a real browser page).

Thanks
spiderplant0 is offline   Reply With Quote
Old 10-08-2010, 12:52 PM   PM User | #2
DaveyErwin
Regular Coder

 
Join Date: Aug 2010
Posts: 810
Thanks: 12
Thanked 168 Times in 166 Posts
DaveyErwin is on a distinguished road
Code:
<script type = "text/javascript">

var stg = "<div>The best-laid schemes o' <span>mice</span> an' men</div>";
if (window.DOMParser)
  {
  parser=new DOMParser();
  xml=parser.parseFromString(stg,"text/xml");}
else // Internet Explorer
  {
  xml=new ActiveXObject("Microsoft.XMLDOM");
  xml.async="false";
  xml.loadXML(stg);  }

path="/div/span";
// code for IE
if (window.ActiveXObject)
{
var nodes=xml.selectNodes(path);

for (i=0;i<nodes.length;i++)
  {
  document.write(nodes[i].childNodes[0].nodeValue);
  document.write("<br />");
  }
}
// code for Mozilla, Firefox, Opera, etc.
else if (document.implementation && document.implementation.createDocument)
{
var nodes=xml.evaluate(path, xml, null, XPathResult.ANY_TYPE,null);
var result=nodes.iterateNext();

while(result)
  {
  document.write(result.childNodes[0].nodeValue);
  document.write("<br />");
  result=nodes.iterateNext();
  }
}


</script>
DaveyErwin is offline   Reply With Quote
Users who have thanked DaveyErwin for this post:
spiderplant0 (10-08-2010)
Old 10-08-2010, 06:39 PM   PM User | #3
spiderplant0
New to the CF scene

 
Join Date: Oct 2010
Posts: 9
Thanks: 3
Thanked 0 Times in 0 Posts
spiderplant0 is an unknown quantity at this point
Thanks DaveyErwin. However I can only get it to work with valid XML/XHTML. If I use it on other HTML fragments (e.g. containing <br> rather than <br />) then it doesnt work. There doesnt seem to be a mime type for HTML for this function. (I'm using Firefox only).

I managed to find a solution for CSS selectors but it seems strange there is not an equivalent for xPath. See below..
Code:
    var stg = "<div>The best-laid<br> schemes o' <span>mice</span> an' men</div>";
    var newDiv = document.createElement("div");
    newDiv.innerHTML = stg;

    //this works
    console.log(newDiv.querySelectorAll("span")[0].textContent);

    //this doesnt work
    var recordNodes = newDiv.evaluate(
        ".//span",
        newDiv,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null );

     console.log(recordNodes.snapshotLength);
The mozilla function nsIScriptableUnescapeHTML.parseFragment() sounds like it might do the job but I cant get .evaluate() to work on it either (and the documentation is pretty slim).

Last edited by spiderplant0; 10-08-2010 at 06:46 PM..
spiderplant0 is offline   Reply With Quote
Old 10-08-2010, 08:37 PM   PM User | #4
rnd me
Senior Coder

 
rnd me's Avatar
 
Join Date: Jun 2007
Location: Urbana
Posts: 3,469
Thanks: 9
Thanked 466 Times in 450 Posts
rnd me is a jewel in the roughrnd me is a jewel in the roughrnd me is a jewel in the rough
Code:
 var stg = "<div>The best-laid<br> schemes o' <br /><span>mice</span> an' men</div>";
    var newDiv = document.createElement("div");
    newDiv.innerHTML = stg;

      //this DOES work
    var recordNodes = document.evaluate(
        ".//span",
        newDiv,
        null,
        XPathResult.ORDERED_NODE_SNAPSHOT_TYPE,
        null );

     console.log(recordNodes.snapshotLength);
__________________
my site (updated 5/13)
STATS (2013/5) HTML5:90.2% MOB:14% IE7:0.5% IE8:8.6% IE9:9.8% IE10:10%
rnd me is offline   Reply With Quote
Users who have thanked rnd me for this post:
spiderplant0 (10-08-2010)
Reply

Bookmarks

Tags
createelement, documentfragment, evaluate, node, xpath

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 06:38 AM.


Advertisement
Log in to turn off these ads.