tags that are
+ children of the tag)
+
+This will match only ``
A very short ebook to demonstrate the use of XPath.
``
+in the `Sample ebook`_ but not any of the other ``
`` tags.
+
+Now suppose you want to select both ``
`` and ```` tags. To do that,
+we need a XPath construct called *predicate*. A :dfn:`predicate` is simply
+a test that is used to select tags. Tests can be arbitrarily powerful and as
+this tutorial progresses, you will see more powerful examples. A predicate
+is created by enclosing the test expression in square brackets::
+
+//*[name()='h1' or name()='h2']
+
+There are several new features in this XPath expression. The first is the use
+of the wildcard ``*``. It means *match any tag*. Now look at the test expression
+``name()='h1' or name()='h2'``. :term:`name()` is an example of a *built-in function*.
+It simply evaluates to the name of the tag. So by using it, we can select tags
+whose names are either `h1` or `h2`. XPath has several useful built-in functions.
+A few more will be introduced in this tutorial.
+
+Selecting by attributes
+-----------------------
+
+To select tags based on their attributes, the use of predicates is required::
+
+ //*[@style] (Select all tags that have a style attribute)
+ //*[@class="chapter"] (Select all tags that have class="chapter")
+ //h1[@class="bookTitle"] (Select all h1 tags that have class="bookTitle")
+
+Here, the ``@`` operator refers to the attributes of the tag. You can use some
+of the `XPath built-in functions`_ to perform more sophisticated
+matching on attribute values.
+
+
+Selecting by tag content
+------------------------
+
+Using XPath, you can even select tags based on the text they contain. The best way to do this is
+to use the power of *regular expressions* via the built-in function :term:`re:test()`::
+
+ //h2[re:test(., 'chapter|section', 'i')] (Selects tags that contain the words chapter or
+ section)
+
+Here the ``.`` operator refers to the contents of the tag, just as the ``@`` operator referred
+to its attributes.
+
+
+Sample ebook
+------------
+
+.. literalinclude:: xpath.xhtml
+ :language: html
+
+XPath built-in functions
+------------------------
+
+.. glossary::
+
+ name()
+ The name of the current tag.
+
+ contains()
+ ``contains(s1, s2)`` returns `true` if s1 contains s2.
+
+ re:test()
+ ``re:test(src, pattern, flags)`` returns `true` if the string `src` matches the
+ regular expression `pattern`. A particularly useful flag is ``i``, it makes matching
+ case insensitive. A good primer on the syntax for regular expressions can be found
+ at `regexp syntax `_
+
diff --git a/src/calibre/manual/xpath.xhtml b/src/calibre/manual/xpath.xhtml
new file mode 100644
index 0000000000..7468e3d856
--- /dev/null
+++ b/src/calibre/manual/xpath.xhtml
@@ -0,0 +1,19 @@
+
+
+ A very short ebook
+
+
+
+ A very short ebook
+ Written by Kovid Goyal
+
+
A very short ebook to demonstrate the use of XPath.
+
+
+ Chapter One
+ This is a truly fascinating chapter.
+
+ Chapter Two
+ A worthy continuation of a fine tradition.
+
+