diff --git a/docs/topics/selectors.rst b/docs/topics/selectors.rst index 39ec9b73c..43370d479 100644 --- a/docs/topics/selectors.rst +++ b/docs/topics/selectors.rst @@ -283,6 +283,40 @@ XPath specification. .. _Location Paths: https://www.w3.org/TR/xpath#location-paths +.. _topics-selectors-xpath-variables: + +Variables in XPath expressions +------------------------------ + +XPath allows you to reference variables in your XPath expressions, using +the ``$somevariable`` syntax. This is somewhat similar to parameterized +queries or prepared statements in the SQL world where you replace +some arguments in your queries with placeholders like ``?``, +which are then substituted with values passed with the query. + +Here's an example to match an element based on its "id" attribute value, +without hard-coding it (that was shown previously):: + + >>> # `$val` used in the expression, a `val` argument needs to be passed + >>> response.xpath('//div[@id=$val]/a/text()', val='images').extract_first() + u'Name: My image 1 ' + +Here's another example, to find the "id" attribute of a ``