\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{BenHuf} \pdfinfo{ /Title (xpath-cheat-sheet.pdf) /Creator (Cheatography) /Author (BenHuf) /Subject (XPath Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{297B8F} \definecolor{LightBackground}{HTML}{F1F6F8} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{XPath Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{BenHuf} via \textcolor{DarkBackground}{\uline{cheatography.com/163984/cs/34359/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}BenHuf \\ \uline{cheatography.com/benhuf} \\ \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Not Yet Published.\\ Updated 25th September, 2022.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{3} \begin{tabularx}{5.377cm}{x{0.9954 cm} x{3.9816 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{XPath}} \tn % Row 0 \SetRowColor{LightBackground} {\emph{nodename}} & Selects all nodes with the name "{\emph{nodename}}" \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} / & Selects from the root node \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} // & Selects nodes in the document from the current node that match the selection no matter where they are \tn % Row Count 7 (+ 4) % Row 3 \SetRowColor{white} . & Selects the current node \tn % Row Count 8 (+ 1) % Row 4 \SetRowColor{LightBackground} .. & Selects the parent of the current node \tn % Row Count 10 (+ 2) % Row 5 \SetRowColor{white} @ & Selects attributes \tn % Row Count 11 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Wildcards and Multiple Paths}} \tn % Row 0 \SetRowColor{LightBackground} * & Matches any element node \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} @* & Matches any attribute node \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} node() & Matches any node of any kind \tn % Row Count 6 (+ 2) % Row 3 \SetRowColor{white} | & Place between paths to select several paths \tn % Row Count 8 (+ 2) % Row 4 \SetRowColor{LightBackground} /bookstore/* & Selects all the child element nodes of the bookstore element \tn % Row Count 11 (+ 3) % Row 5 \SetRowColor{white} //* & Selects all elements in the document \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} //title{[}@*{]} & Selects all title elements which have at least one attribute of any kind \tn % Row Count 17 (+ 4) % Row 7 \SetRowColor{white} //book/title | //book/price & Selects all the title AND price elements of all book elements \tn % Row Count 20 (+ 3) % Row 8 \SetRowColor{LightBackground} //title | //price & Selects all the title AND price elements in the document \tn % Row Count 23 (+ 3) % Row 9 \SetRowColor{white} \seqsplit{/bookstore/book/title} | //price & Selects all the title elements of the book element of the bookstore element AND all the price elements in the document \tn % Row Count 29 (+ 6) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.74195 cm} x{3.23505 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Location Step Examples}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{\{\{ac\}\}The syntax for a location step is: \{\{nl\}\}`axisname::nodetest{[}predicate{]}`} \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} child::book & Selects all book nodes that are children of the current node \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} \seqsplit{attribute::lang} & Selects the lang attribute of the current node \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} child::* & Selects all element children of the current node \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} attribute::* & Selects all attributes of the current node \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} child::text() & Selects all text node children of the current node \tn % Row Count 13 (+ 2) % Row 6 \SetRowColor{LightBackground} child::node() & Selects all children of the current node \tn % Row Count 15 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{descendant::book} & Selects all book descendants of the current node \tn % Row Count 17 (+ 2) % Row 8 \SetRowColor{LightBackground} \seqsplit{ancestor::book} & Selects all book ancestors of the current node \tn % Row Count 19 (+ 2) % Row 9 \SetRowColor{white} \seqsplit{ancestor-or-self::book} & Selects all book ancestors of the current node - and the current node as well if it is a book node \tn % Row Count 23 (+ 4) % Row 10 \SetRowColor{LightBackground} \seqsplit{child::*/child::price} & Selects all price grandchildren of the current node \tn % Row Count 25 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{p{0.4577 cm} x{2.10542 cm} x{2.01388 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{5.377cm}}{\bf\textcolor{white}{XPath Operators}} \tn % Row 0 \SetRowColor{LightBackground} | & Computes two node-sets & //book | //cd \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} + & Addition & 6 + 4 \tn % Row Count 3 (+ 1) % Row 2 \SetRowColor{LightBackground} - & Subtraction & 6 - 4 \tn % Row Count 4 (+ 1) % Row 3 \SetRowColor{white} * & Multiplication & 6 * 4 \tn % Row Count 5 (+ 1) % Row 4 \SetRowColor{LightBackground} div & Division & 8 div 4 \tn % Row Count 6 (+ 1) % Row 5 \SetRowColor{white} = & Equal & price=9.80 \tn % Row Count 7 (+ 1) % Row 6 \SetRowColor{LightBackground} != & Not equal & price!=9.80 \tn % Row Count 8 (+ 1) % Row 7 \SetRowColor{white} \textless{} & Less than & price\textless{}9.80 \tn % Row Count 9 (+ 1) % Row 8 \SetRowColor{LightBackground} \textless{}= & Less than or equal to & price\textless{}=9.80 \tn % Row Count 11 (+ 2) % Row 9 \SetRowColor{white} \textgreater{} & Greater than & price\textgreater{}9.80 \tn % Row Count 12 (+ 1) % Row 10 \SetRowColor{LightBackground} \textgreater{}= & Greater than or equal to & price\textgreater{}=9.80 \tn % Row Count 14 (+ 2) % Row 11 \SetRowColor{white} or & or & price=9.80 or price=9.70 \tn % Row Count 16 (+ 2) % Row 12 \SetRowColor{LightBackground} and & and & price\textgreater{}9.00 and price\textless{}9.90 \tn % Row Count 18 (+ 2) % Row 13 \SetRowColor{white} mod & Modulus & 5 mod 2 \tn % Row Count 19 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.28942 cm} x{2.68758 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Predicates}} \tn % Row 0 \SetRowColor{LightBackground} /bookstore/book{[}1{]} & Selects the first book element that is the child of the bookstore element. \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} /bookstore/book{[}last(){]} & Selects the last book element that is the child of the bookstore element \tn % Row Count 8 (+ 4) % Row 2 \SetRowColor{LightBackground} /bookstore/book{[}last()-1{]} & Selects the last but one book element that is the child of the bookstore element \tn % Row Count 12 (+ 4) % Row 3 \SetRowColor{white} /bookstore/book{[}position()\textless{}3{]} & Selects the first two book elements that are children of the bookstore element \tn % Row Count 16 (+ 4) % Row 4 \SetRowColor{LightBackground} //title{[}@lang{]} & Selects all the title elements that have an attribute named lang \tn % Row Count 20 (+ 4) % Row 5 \SetRowColor{white} //title{[}@lang='en'{]} & Selects all the title elements that have a "lang" attribute with a value of "en" \tn % Row Count 24 (+ 4) % Row 6 \SetRowColor{LightBackground} /bookstore/book{[}price\textgreater{}35.00{]} & Selects all the book elements of the bookstore element that have a price element with a value greater than 35.00 \tn % Row Count 30 (+ 6) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{2.28942 cm} x{2.68758 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{Predicates (cont)}} \tn % Row 7 \SetRowColor{LightBackground} /bookstore/book{[}price\textgreater{}35.00{]}/title & Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00 \tn % Row Count 7 (+ 7) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Note:}} In IE 5,6,7,8,9 first node is{[}0{]}, but according to W3C, it is {[}1{]}. To solve this problem in IE, set the SelectionLanguage to XPath. \newline \newline In JavaScript: \seqsplit{xml.setProperty("SelectionLanguage"},"XPath");} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{2.18988 cm} x{2.78712 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{XPath Examples}} \tn % Row 0 \SetRowColor{LightBackground} bookstore & Selects all nodes with the name "bookstore" \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} /bookstore & Selects the root element bookstore \tn % Row Count 4 (+ 2) % Row 2 \SetRowColor{LightBackground} bookstore/book & Selects all book elements that are children of bookstore \tn % Row Count 7 (+ 3) % Row 3 \SetRowColor{white} //book & Selects all book elements no matter where they are in the document \tn % Row Count 10 (+ 3) % Row 4 \SetRowColor{LightBackground} bookstore//book & Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element \tn % Row Count 16 (+ 6) % Row 5 \SetRowColor{white} //@lang & Selects all attributes that are named lang \tn % Row Count 18 (+ 2) % Row 6 \SetRowColor{LightBackground} \seqsplit{/bookstore/book/title} & Selects all title nodes that are descendants of book that are descendants of bookstore \tn % Row Count 22 (+ 4) % Row 7 \SetRowColor{white} /bookstore/book{[}1{]}/title & Selects the title of the first book node under the bookstore element \tn % Row Count 26 (+ 4) % Row 8 \SetRowColor{LightBackground} /bookstore/book/price{[}text(){]} & Selects the text from all bookstore/book/price nodes \tn % Row Count 29 (+ 3) % Row 9 \SetRowColor{white} /bookstore/book{[}price\textgreater{}35{]}/price & Selects all the bookstore/book/price nodes with a price greater than 35 \tn % Row Count 33 (+ 4) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{5.377cm}}{{\bf{Note:}} If the path started with a slash ( / ) it always represents an absolute path to an element!} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{XPath Axes}} \tn % Row 0 \SetRowColor{LightBackground} ancestor & Selects all ancestors of the current node \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} \seqsplit{ancestor-or-self} & Selects all ancestors of the current node and the current node itself \tn % Row Count 5 (+ 3) % Row 2 \SetRowColor{LightBackground} attribute & Selects all attributes of the current node \tn % Row Count 7 (+ 2) % Row 3 \SetRowColor{white} child & Selects all children of the current node \tn % Row Count 9 (+ 2) % Row 4 \SetRowColor{LightBackground} descendant & Selects all descendants of the current node \tn % Row Count 11 (+ 2) % Row 5 \SetRowColor{white} \seqsplit{descendant-or-self} & Selects all descendants of the current node and the current nod itself \tn % Row Count 14 (+ 3) % Row 6 \SetRowColor{LightBackground} following & Selects everything in the document after the closing tag of the current node \tn % Row Count 17 (+ 3) % Row 7 \SetRowColor{white} \seqsplit{following-sibling} & Selects all siblings after the current node \tn % Row Count 19 (+ 2) % Row 8 \SetRowColor{LightBackground} namespace & Selects all namespace nodes of the current node \tn % Row Count 21 (+ 2) % Row 9 \SetRowColor{white} parent & Selects the parent of the current node \tn % Row Count 23 (+ 2) % Row 10 \SetRowColor{LightBackground} preceding & Selects all nodes that appear before the current node in the document, except ancestors, attribute nodes, and namespace nodes \tn % Row Count 28 (+ 5) % Row 11 \SetRowColor{white} \seqsplit{preceding-sibling} & Selects all siblings before the current node \tn % Row Count 30 (+ 2) \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{5.377cm}{x{1.54287 cm} x{3.43413 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{5.377cm}}{\bf\textcolor{white}{XPath Axes (cont)}} \tn % Row 12 \SetRowColor{LightBackground} self & Selects the current node \tn % Row Count 1 (+ 1) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}