You are here: Reference > JavaScript > client-side > HTML DOM > methods > evaluate (document, XMLDocument)
evaluate method (document, XMLDocument)
Evaluates an XPath expression and creates an XPathResult object that represents the result.
The evaluate method makes it possible to search within the HTML or XML document by complex search conditions.
Syntax:
You can find the related objects in the Supported by objects section below.
Parameters:
String that specifies the XPath expression to evaluate. For more information, please visit the Location Paths (W3C) page. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Reference to the node in which the search will take place. The node can be the document or any element in the document. | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Reference to an XPathNSResolver object or a function that can resolve namespaces.
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Integer that specifies the type of the result. Predefined constants are available for the possible values of this parameter, in the scope of the XPathResult object. For further details, see the page for the XPathResult object.
The value can be one of the following values (the value of a predefined constant appears in parentheses after the constant):
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Specifies an existing XPathResult object to use for the results. Set to null if you want to create a new XPathResult object. |
Return value:
Returns the created or referred (by the result parameter) XPathResult object.
Example HTML code 1:
This example shows how to get all span elements that are immediate children of a given element:
|
||||
<head> <style> .level1 {color:red;} .level2 {color:blue;} .level3 {color:green;} </style> <script type="text/javascript"> function GetSpanElements () { var message = ""; if (document.evaluate) { // Firefox, Opera, Google Chrome and Safari var container = document.getElementById ("container"); var xPathRes = document.evaluate ( 'span', container, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); message += "The contents of the span elements:<br />"; var actualSpan = xPathRes.iterateNext (); while (actualSpan) { message += "<b>" + actualSpan.innerHTML + "</b><br />"; actualSpan = xPathRes.iterateNext () } } else { // Internet Explorer message = "Your browser does not support the evaluate method!"; } var info = document.getElementById ("info"); info.innerHTML = message; } </script> </head> <body onload="GetSpanElements ()"> <span class="level1">Span 1</span> <div id="container"> <span class="level2">Span 2</span> <div> <span class="level3">Span 3</span> <span class="level3">Span 4</span> </div> <span class="level2">Span 5</span> <span class="level2">Span 6</span> </div> <span class="level1">Span 7</span> <br /><br /> <div id="info" style="background-color:#e0b0b0; width:400px"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 2:
This example shows how to get all span elements that are descendants of a given element:
|
||||
<head> <style> .level1 {color:red;} .level2 {color:blue;} .level3 {color:green;} </style> <script type="text/javascript"> function GetSpanElements () { var message = ""; if (document.evaluate) { // Firefox, Opera, Google Chrome and Safari var container = document.getElementById ("container"); var xPathRes = document.evaluate ( './/span', container, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); message += "The contents of the span elements:<br />"; var actualSpan = xPathRes.iterateNext (); while (actualSpan) { message += "<b>" + actualSpan.innerHTML + "</b><br />"; actualSpan = xPathRes.iterateNext () } } else { // Internet Explorer message = "Your browser does not support the evaluate method!"; } var info = document.getElementById ("info"); info.innerHTML = message; } </script> </head> <body onload="GetSpanElements ()"> <span class="level1">Span 1</span> <div id="container"> <span class="level2">Span 2</span> <div> <span class="level3">Span 3</span> <span class="level3">Span 4</span> </div> <span class="level2">Span 5</span> <span class="level2">Span 6</span> </div> <span class="level1">Span 7</span> <br /><br /> <div id="info" style="background-color:#e0b0b0; width:400px"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 3:
This example shows how to get all span elements that are descendants but not immediate children of a given element:
|
||||
<head> <style> .level1 {color:red;} .level2 {color:blue;} .level3 {color:green;} </style> <script type="text/javascript"> function GetSpanElements () { var message = ""; if (document.evaluate) { // Firefox, Opera, Google Chrome and Safari var container = document.getElementById ("container"); var xPathRes = document.evaluate ( '*//span', container, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); message += "The contents of the span elements:<br />"; var actualSpan = xPathRes.iterateNext (); while (actualSpan) { message += "<b>" + actualSpan.innerHTML + "</b><br />"; actualSpan = xPathRes.iterateNext () } } else { // Internet Explorer message = "Your browser does not support the evaluate method!"; } var info = document.getElementById ("info"); info.innerHTML = message; } </script> </head> <body onload="GetSpanElements ()"> <span class="level1">Span 1</span> <div id="container"> <span class="level2">Span 2</span> <div> <span class="level3">Span 3</span> <span class="level3">Span 4</span> </div> <span class="level2">Span 5</span> <span class="level2">Span 6</span> </div> <span class="level1">Span 7</span> <br /><br /> <div id="info" style="background-color:#e0b0b0; width:400px"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 4:
This example shows how to get all span elements in the document that are immediate children of an li element:
|
||||
<head> <style> .level1 {color:red;} .level2 {color:blue;} .level3 {color:green;} </style> <script type="text/javascript"> function GetSpanElements () { var message = ""; if (document.evaluate) { // Firefox, Opera, Google Chrome and Safari var xPathRes = document.evaluate ( "//li/span", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); message += "The contents of the span elements:<br />"; var actualSpan = xPathRes.iterateNext (); while (actualSpan) { message += "<b>" + actualSpan.innerHTML + "</b><br />"; actualSpan = xPathRes.iterateNext () } } else { // Internet Explorer message = "Your browser does not support the evaluate method!"; } var info = document.getElementById ("info"); info.innerHTML = message; } </script> </head> <body onload="GetSpanElements ()"> <span class="level1">Span 1</span> <ul> <li> <span class="level2">Span 2</span> </li> <li> <div> <span class="level3">Span 3</span> </div> </li> <li> <span class="level2">Span 4</span> <span class="level2">Span 5</span> </li> <li> <div> <span class="level3">Span 6</span> </div> <span class="level2">Span 7</span> </li> </ul> <span class="level1">Span 8</span> <br /><br /> <div id="info" style="background-color:#e0b0b0; width:400px"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 5:
This example shows how to get all span elements in the document that are descendants of an li element:
|
||||
<head> <style> .level1 {color:red;} .level2 {color:blue;} .level3 {color:green;} </style> <script type="text/javascript"> function GetSpanElements () { var message = ""; if (document.evaluate) { // Firefox, Opera, Google Chrome and Safari var xPathRes = document.evaluate ( "//li//span", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); message += "The contents of the span elements:<br />"; var actualSpan = xPathRes.iterateNext (); while (actualSpan) { message += "<b>" + actualSpan.innerHTML + "</b><br />"; actualSpan = xPathRes.iterateNext () } } else { // Internet Explorer message = "Your browser does not support the evaluate method!"; } var info = document.getElementById ("info"); info.innerHTML = message; } </script> </head> <body onload="GetSpanElements ()"> <span class="level1">Span 1</span> <ul> <li> <span class="level2">Span 2</span> </li> <li> <div> <span class="level3">Span 3</span> </div> </li> <li> <span class="level2">Span 4</span> <span class="level2">Span 5</span> </li> <li> <div> <span class="level3">Span 6</span> </div> <span class="level2">Span 7</span> </li> </ul> <span class="level1">Span 8</span> <br /><br /> <div id="info" style="background-color:#e0b0b0; width:400px"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 6:
This example shows how to get all span elements in the document that have a 'class' attribute with a value of 'level2':
|
||||
<head> <style> .level1 {color:red;} .level2 {color:blue;} .level3 {color:green;} </style> <script type="text/javascript"> function GetSpanElements () { var message = ""; if (document.evaluate) { // Firefox, Opera, Google Chrome and Safari var xPathRes = document.evaluate ( "//span[@class='level2']", document, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null); message += "The contents of the span elements:<br />"; var actualSpan = xPathRes.iterateNext (); while (actualSpan) { message += "<b>" + actualSpan.innerHTML + "</b><br />"; actualSpan = xPathRes.iterateNext () } } else { // Internet Explorer message = "Your browser does not support the evaluate method!"; } var info = document.getElementById ("info"); info.innerHTML = message; } </script> </head> <body onload="GetSpanElements ()"> <span class="level1">Span 1</span> <ul> <li> <span class="level2">Span 2</span> </li> <li> <div> <span class="level3">Span 3</span> </div> </li> <li> <span class="level2">Span 4</span> <span class="level2">Span 5</span> </li> <li> <div> <span class="level3">Span 6</span> </div> <span class="level2">Span 7</span> </li> </ul> <span class="level1">Span 8</span> <br /><br /> <div id="info" style="background-color:#e0b0b0; width:400px"></div> </body> |
||||
|
||||
Did you find this example helpful?
|
Example HTML code 7:
This example illustrates the use of the XPathNSResolver object for the evaluate method:
|
|||||||
<head> <script type="text/javascript" src="ajax.js"></script> <script type="text/javascript"> var httpRequest = null; function SendRequest () { if (!httpRequest) { httpRequest = CreateHTTPRequestObject (); // defined in ajax.js } if (httpRequest) { // The requested file must be in the same domain that the page is served from. var url = "ns.xml"; httpRequest.open ("GET", url, true); // async httpRequest.onreadystatechange = OnStateChange; httpRequest.send (null); } } function OnStateChange () { if (httpRequest.readyState == 0 || httpRequest.readyState == 4) { if (IsRequestSuccessful (httpRequest)) { // defined in ajax.js GetParamTags (); } else { alert ("Operation failed."); } } } function NSResolver (nsPrefix) { if (nsPrefix == "dotto") { return "http://help.dottoro.com/NS"; } return null; } function GetParamTags () { var xmlDoc = ParseHTTPResponse (httpRequest); // defined in ajax.js if (!xmlDoc) return; if (xmlDoc.createNSResolver) { var nsResolver = xmlDoc.createNSResolver (xmlDoc.documentElement); var params = xmlDoc.evaluate("//dotto:param", xmlDoc.documentElement, nsResolver, XPathResult.ANY_TYPE, null); var param = params.iterateNext(); while (param) { var paramName = param.getAttribute ("name"); var paramValue = param.getAttribute ("value"); alert ("<dotto:param name='" + paramName + "' value='" + paramValue + "' />"); param = params.iterateNext(); } } else { alert ("Your browser doesn't support the createNSResolver method!"); } } </script> </head> <body> <button onclick="SendRequest ()">Get param tags with 'dotto' namespaces</button> </body> |
|||||||
|
|||||||
Did you find this example helpful?
|
Example HTML code 8:
This example is equivalent to the previous one, but it implements a namespace resolver function for the evaluate method:
|
|||||||
<head> <script type="text/javascript" src="ajax.js"></script> <script type="text/javascript"> var httpRequest = null; function SendRequest () { if (!httpRequest) { httpRequest = CreateHTTPRequestObject (); // defined in ajax.js } if (httpRequest) { // The requested file must be in the same domain that the page is served from. var url = "ns.xml"; httpRequest.open ("GET", url, true); // async httpRequest.onreadystatechange = OnStateChange; httpRequest.send (null); } } function OnStateChange () { if (httpRequest.readyState == 0 || httpRequest.readyState == 4) { if (IsRequestSuccessful (httpRequest)) { // defined in ajax.js GetParamTags (); } else { alert ("Operation failed."); } } } function NSResolver (nsPrefix) { if (nsPrefix == "dotto") { return "http://help.dottoro.com/NS"; } return null; } function GetParamTags () { var xmlDoc = ParseHTTPResponse (httpRequest); // defined in ajax.js if (!xmlDoc) return; if (xmlDoc.createNSResolver) { var params = xmlDoc.evaluate("//dotto:param", xmlDoc.documentElement, NSResolver, XPathResult.ANY_TYPE, null); var param = params.iterateNext(); while (param) { var paramName = param.getAttribute ("name"); var paramValue = param.getAttribute ("value"); alert ("<dotto:param name='" + paramName + "' value='" + paramValue + "' />"); param = params.iterateNext(); } } else { alert ("Your browser doesn't support the createNSResolver method!"); } } </script> </head> <body> <button onclick="SendRequest ()">Get param tags with 'dotto' namespaces</button> </body> |
|||||||
|
|||||||
Did you find this example helpful?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments