Detailed usage of document. Evaluate

Source: Internet
Author: User
Tags xpath functions

The evaluate function is the most powerful tool used when greasemonkey is used. By using the XPath query language, it can be used to find elements, attributes, and text in the page.

For example, if you want to obtain all the links on a page. You may want to use document. getelementsbytagname ('A'); but if you want to continue to check whether each link has the href attribute, because <A> can also be used as the anchor name, you need to use the built-in XPath support of Firefox to obtain all <A> elements with the href attribute.

Example: Get all links on the page

VaR alllinks, thislink; alllinks = document. evaluate ('// A [@ href]', document, null, xpathresult. unordered_node_snapshot_type, null); For (VAR I = 0; I <alllinks. snapshotlength; I ++) {thislink = alllinks. snapshotitem (I);/do something with thislink}

Here, document. Evaluate is a key part. It uses the XPath query statement as a string, and other parameters will be explained later. This XPath query statement can find all <A> elements with the href attribute and return them in random order. (This means that the first element to be returned must be the first one on the page .) Then, you can use the alllinks. snapshotitem (I) function to access each element.

What an XPATH expression can do is even surprising. See the following example to obtain all the elements with the title attribute.

Example: obtain all elements with the title attribute

VaR allelements, thiselement; allelements = document. evaluate ('// * [@ title]', document, null, xpathresult. unordered_node_snapshot_type, null); For (VAR I = 0; I <allelements. snapshotlength; I ++) {thiselement = allelements. snapshotitem (I); Switch (thiselement. nodename. touppercase () {Case 'A':/This is a link, do something break; Case 'img ':/This is an image, do something else break; default: /Do something with other kinds of HTML elements }}

If you have referenced an element (for example, the preceding thiselement), you can use thiselement. nodename to replace the tag name corresponding to it on the HTML page. If the accessed page is executed by the server in text/html mode, the tag name is always returned in uppercase, no matter how it is defined on the original page. If the page is in the application/XHTML + XML format, the tag name is returned in lowercase. In either case, I always use thiselement. nodename. touppercase () to get an upper-case tag name.

This is another XPath query, which returns a special class in Div.

Example: retrieve the soredlink class in the DIV

VaR alldivs, thisdiv; alldivs = document. evaluate ("// Div [@ class = 'sortedlink']", document, null, xpathresult. unordered_node_snapshot_type, null); For (VAR I = 0; I <alldivs. snapshotlength; I ++) {thisdiv = alldivs. snapshotitem (I);/do something with thisdiv}

Note that I use double quotation marks outside the XPath query statement, so that single quotation marks can be used inside the statement.

The document. Evaluate function has many parameters. The second parameter (in the first two examples, docoment) can be an element. XPath queries only return elements contained in this element. Therefore, if you have referenced an element (for example, using document. getelementbyidx or by document. getelementsbytagname is an element in the array), you can restrict the query to return only the child elements of this element.

The third parameter is a reference to a function called namespace resolver, which is only valid in user scripts that work on pages of the application/XHTML + XML type. It doesn't matter even if you don't know it very well, because there are not many pages of that type, and you may not encounter it once. If you want to know how it is used, see Mozilla XPath documentation (Http://www-jcsu.jesus.cam.ac.uk /~ Jg307/Mozilla/xpath-tutorial.html), Which explains its usage.

The fourth parameter is the return method of the result. In the previous two examples, xpathresult. unordered_node_snapshot_type is used to return the result randomly. I use this method almost all. However, for some reason, you can use xpathresult. ordered_node_snapshot_type to return the results in the order they appear on the page. Mozilla XPath documentation (Http://www-jcsu.jesus.cam.ac.uk /~ Jg307/Mozilla/xpath-tutorial.html) Other use cases are provided.

The fifth parameter is used to merge the results of two XPath queries. After obtaining the results obtained by calling document. Evaluate for the first time, it returns the results of the two queries together. In the first two examples, null is used for this parameter, which means we only want to obtain the results of this query.

Do you understand now? XPath can be both simple and difficult, depending on how you use it. Here I strongly recommend that you read this excellent XPath tutorial (Http://www.zvon.org/xxl/XPathTutorial/General/examples.html) To learn more about the XPath syntax. I almost never use other parameters of the document. Evaluate function. In fact, you can define a function to encapsulate them.

Example: Custom XPath Functions

Function XPath (query) {return document. Evaluate (query, document, null, xpathresult. unordered_node_snapshot_type, null );}

After defining this function, you can call XPath ('// A [@ href]') to obtain all the links on a page, alternatively, you can call XPath ('// * [@ title]') to obtain the element with the title attribute. You still need to use the snapshotitem function to access each item in the result. The type of this function is not the javas of a rule.Struct array.

# Javas Examples/ajax Columns

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.