diff --git a/docs/topics/selectors.rst b/docs/topics/selectors.rst index 87bc3f84a..0728922e5 100644 --- a/docs/topics/selectors.rst +++ b/docs/topics/selectors.rst @@ -173,3 +173,36 @@ The ``x()`` selector method returns a list of selectors, so you can call the Link number 3 points to url [u'image4.html'] and image [u'image4_thumb.jpg'] Link number 4 points to url [u'image5.html'] and image [u'image5_thumb.jpg'] +Working with relative XPaths +---------------------------- + +Keep in mind that if you are nesting XPathSelectors and use an XPath that +starts with ``/``, that XPath will be absolute to the document and not relative +to the ``XPathSelector`` you're calling it from. + +For example, suppose you want to extract all ``
`` elements inside ``
`` elements from the document, not only those +inside ``
from the whole document + >>> print p.extract() + +This is the proper way to do it (note the dot prefixing the ``.//p`` XPath):: + + >>> for p in divs.x('//p') # extracts all
inside + >>> print p.extract() + +Another common case would be to extract all direct ``
`` children:: + + >>> for p in divs.x('p') + >>> print p.extract() + +For more details about relative XPaths see the `Location Paths`_ section in the +XPath specification. + +.. _Location Paths: http://www.w3.org/TR/xpath#location-paths