\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 (xquery.pdf) /Creator (Cheatography) /Author (BenHuf) /Subject (XQuery 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}{C24444} \definecolor{LightBackground}{HTML}{FBF3F3} \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{XQuery Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{BenHuf} via \textcolor{DarkBackground}{\uline{cheatography.com/163984/cs/34363/}}} \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 27th 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*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Overview}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{XQuery is {\bf{{\emph{the}}}} language for querying XML data} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{XQuery for XML is like SQL for databases} \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{XQuery is built on XPath expressions} \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{XQuery is supported by all major databases} \tn % Row Count 4 (+ 1) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{XQuery is a W3C Recommendation} \tn % Row Count 5 (+ 1) % Row 5 \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{XQuery is a language for finding and extracting elements and attributes from XML documents.} \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{Here is an example of what XQuery could solve: \newline \newline "Select all CD records with a price less than \$10 from the CD collection stored in cd\_catalog.xml"} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.36 cm} x{6.64 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{FLWOR Expressions}} \tn % Row 0 \SetRowColor{LightBackground} For & Selects a sequence of nodes \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} Let & Binds a sequence to a variable \tn % Row Count 2 (+ 1) % Row 2 \SetRowColor{LightBackground} Where & Filters the nodes \tn % Row Count 3 (+ 1) % Row 3 \SetRowColor{white} Order by & Sorts the nodes \tn % Row Count 5 (+ 2) % Row 4 \SetRowColor{LightBackground} \seqsplit{Return} & What to return (gets evaluated once for every node) \tn % Row Count 7 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{{\bf{Example:}} \newline `for \$x in \seqsplit{doc("books.xml")/bookstore/book`} \newline `where \$x/price\textgreater{}30` \newline `order by \$x/title` \newline `return \$x/title`} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{The For Clause}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{To loop a specific number of times in a for clause, you may use the {\bf{to}} keyword. \newline % Row Count 2 (+ 2) {\bf{This}} \newline % Row Count 3 (+ 1) `for \$x in (1 to 5)` \newline % Row Count 4 (+ 1) `return \textless{}test\textgreater{}\{\$x\}\textless{}/test\textgreater{}` \newline % Row Count 5 (+ 1) {\bf{Returns}} \newline % Row Count 6 (+ 1) `\textless{}test\textgreater{}1\textless{}/test\textgreater{}` \newline % Row Count 7 (+ 1) `\textless{}test\textgreater{}2\textless{}/test\textgreater{}` \newline % Row Count 8 (+ 1) `\textless{}test\textgreater{}3\textless{}/test\textgreater{}` \newline % Row Count 9 (+ 1) `\textless{}test\textgreater{}4\textless{}/test\textgreater{}` \newline % Row Count 10 (+ 1) `\textless{}test\textgreater{}5\textless{}/test\textgreater{}` \newline % Row Count 11 (+ 1) To count the iteration use the {\bf{at}} keyword \newline % Row Count 12 (+ 1) {\bf{This}} \newline % Row Count 13 (+ 1) `for \$x at \$i in \seqsplit{doc("books.xml")/bookstore/book/title`} \newline % Row Count 15 (+ 2) `return \textless{}book\textgreater{}\{\$i\}. \{data(\$x)\}\textless{}/book\textgreater{}` \newline % Row Count 16 (+ 1) {\bf{Returns}} \newline % Row Count 17 (+ 1) `\textless{}book\textgreater{}1. Everyday Italian\textless{}/book\textgreater{}` \newline % Row Count 18 (+ 1) `\textless{}book\textgreater{}2. Harry Potter\textless{}/book\textgreater{}` \newline % Row Count 19 (+ 1) `\textless{}book\textgreater{}3. XQuery Kick Start\textless{}/book\textgreater{}` \newline % Row Count 20 (+ 1) `\textless{}book\textgreater{}4. Learning XML\textless{}/book\textgreater{}` \newline % Row Count 21 (+ 1) it is also allowed with more than one expression in the for clause. Use comma to separate each in expression. \newline % Row Count 24 (+ 3) {\bf{This}} \newline % Row Count 25 (+ 1) `for \$x in (10,20), \$y in (100,200)` \newline % Row Count 26 (+ 1) `return \textless{}test\textgreater{}x=\{\$x\} and y=\{\$y\}\textless{}/test\textgreater{}` \newline % Row Count 27 (+ 1) {\bf{Returns}} \newline % Row Count 28 (+ 1) `\textless{}test\textgreater{}x=10 and y=100\textless{}/test\textgreater{}` \newline % Row Count 29 (+ 1) `\textless{}test\textgreater{}x=10 and y=200\textless{}/test\textgreater{}` \newline % Row Count 30 (+ 1) } \tn \end{tabularx} \par\addvspace{1.3em} \vfill \columnbreak \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{The For Clause (cont)}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{`\textless{}test\textgreater{}x=20 and y=100\textless{}/test\textgreater{}` \newline % Row Count 1 (+ 1) `\textless{}test\textgreater{}x=20 and y=200\textless{}/test\textgreater{}`% Row Count 2 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{The Let Clause}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{This}} \newline % Row Count 1 (+ 1) `let \$x := (1 to 5)` \newline % Row Count 2 (+ 1) `return \textless{}test\textgreater{}\{\$x\}\textless{}/test\textgreater{}` \newline % Row Count 3 (+ 1) {\bf{Returns}} \newline % Row Count 4 (+ 1) `\textless{}test\textgreater{}1 2 3 4 5\textless{}/test\textgreater{}`% Row Count 5 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The let clause allows variable assignments and it avoids repeating the same expression many times. The let clause does not result in iteration.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{The Where Clause}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{This}} \newline % Row Count 1 (+ 1) where \$x/price\textgreater{}30 and \$x/price\textless{}100 \newline % Row Count 2 (+ 1) {\bf{Returns}} Nodes only where the price is between 30 and 100% Row Count 4 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The where clause is used to specify one or more criteria for the result.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{The order by Clause}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{This}} \newline % Row Count 1 (+ 1) `for \$x in \seqsplit{doc("books.xml")/bookstore/book`} \newline % Row Count 2 (+ 1) `order by \$x/@category, \$x/title` \newline % Row Count 3 (+ 1) `return \$x/title` \newline % Row Count 4 (+ 1) {\bf{Returns}} \newline % Row Count 5 (+ 1) `\textless{}title lang="en"\textgreater{}Harry Potter\textless{}/title\textgreater{}` \newline % Row Count 6 (+ 1) `\textless{}title lang="en"\textgreater{}Everyday Italian\textless{}/title`\textgreater{} \newline % Row Count 7 (+ 1) `\textless{}title lang="en"\textgreater{}Learning XML\textless{}/title\textgreater{}` \newline % Row Count 8 (+ 1) `\textless{}title lang="en"\textgreater{}XQuery Kick Start\textless{}/title\textgreater{}`% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The order by clause is used to specify the sort order of the result.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{The Return Clause}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{This}} \newline % Row Count 1 (+ 1) `for \$x in \seqsplit{doc("books.xml")/bookstore/book`} \newline % Row Count 2 (+ 1) `return \$x/title` \newline % Row Count 3 (+ 1) {\bf{Returns}} \newline % Row Count 4 (+ 1) `\textless{}title lang="en"\textgreater{}Everyday Italian\textless{}/title\textgreater{}` \newline % Row Count 5 (+ 1) `\textless{}title lang="en"\textgreater{}Harry Potter\textless{}/title\textgreater{}` \newline % Row Count 6 (+ 1) `\textless{}title lang="en"\textgreater{}XQuery Kick Start\textless{}/title\textgreater{}` \newline % Row Count 7 (+ 1) `\textless{}title lang="en"\textgreater{}Learning XML\textless{}/title\textgreater{}`% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{The return clause specifies what is to be returned.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{1.292 cm} x{3.116 cm} x{3.192 cm} } \SetRowColor{DarkBackground} \mymulticolumn{3}{x{8.4cm}}{\bf\textcolor{white}{XQuery Basics}} \tn % Row 0 \SetRowColor{LightBackground} doc() & \seqsplit{doc("books.xml")} & Used to open a file \tn % Row Count 2 (+ 2) % Row 1 \SetRowColor{white} Path \seqsplit{Expressions} & doc("books.xml"){\bf{/bookstore/book/title}} & Used to navigate through elements in an XML document \tn % Row Count 6 (+ 4) % Row 2 \SetRowColor{LightBackground} \seqsplit{Predicates} & doc("books.xml")/bookstore/book{\bf{{[}price\textless{}30{]}}} & Used to limit the extracted data \tn % Row Count 9 (+ 3) \hhline{>{\arrayrulecolor{DarkBackground}}---} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{XQuery Basic Syntax Rules}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{XQuery is case-sensitive} \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{XQuery elements, attributes, and variables must be valid XML names} \tn % Row Count 3 (+ 2) % Row 2 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{An XQuery string value can be in single or double quotes} \tn % Row Count 5 (+ 2) % Row 3 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{An XQuery variable is defined with a \$ followed by a name, e.g. \$bookstore} \tn % Row Count 7 (+ 2) % Row 4 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{XQuery comments are delimited by (: and :)\{\{nl\}\}`(: XQuery Comment :)`} \tn % Row Count 9 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{4 cm} x{4 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{Path Expressions vs. FLWOR Expressions}} \tn % Row 0 \SetRowColor{LightBackground} Path Expression & FLWOR Expression \tn % Row Count 1 (+ 1) % Row 1 \SetRowColor{white} `doc("books.xml")/bookstore/book{[}price\textgreater{}30{]}/title` & `for \$x in doc("books.xml")/bookstore/book\{\{nl\}\}where \$x/price\textgreater{}30\{\{nl\}\}return \$x/title` \tn % Row Count 6 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{These expressions yield the same result.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{p{0.8 cm} p{0.8 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{XQuery Comparisons}} \tn % Row 0 \SetRowColor{LightBackground} \mymulticolumn{2}{x{8.4cm}}{General Comparisons: =, !=, \textless{}, \textless{}=, \textgreater{}, \textgreater{}=\{\{nl\}\}\{\{nl\}\}`\$bookstore//book/@q \textgreater{} 10`\{\{nl\}\}\{\{nl\}\}Returns true if any q attributes have a value greater than 10} \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} \mymulticolumn{2}{x{8.4cm}}{Value Comparisons: eq, ne, lt, le, gt, ge\{\{nl\}\}\{\{nl\}\}`bookstore//book/@q gt 10`\{\{nl\}\}\{\{nl\}\}Returns true if there is only one q attribute returned by the expression and its value is greater than 10. If more than one 1 is returned, and error occurs} \tn % Row Count 9 (+ 5) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Examples of Function Calls}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{{\bf{Example 1: In an element}} \newline % Row Count 1 (+ 1) `\textless{}name\textgreater{}\{upper-case(\$booktitle)\}\textless{}/name\textgreater{}` \newline % Row Count 2 (+ 1) {\bf{Example 2: In the predicate of a path expression}} \newline % Row Count 4 (+ 2) `doc("books.xml")/bookstore/book{[}substring(title,1,5)='Harry'{]}` \newline % Row Count 6 (+ 2) {\bf{Example 3: In a let clause}} \newline % Row Count 7 (+ 1) `let \$name := (substring(\$booktitle,1,4))`% Row Count 8 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{A call to a function can appear where an expression may appear.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{User-Defined Functions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{If you cannot find the XQuery function you need, you can write your own. User-defined functions can be defined in the query or in a separate library. \newline % Row Count 3 (+ 3) {\bf{Syntax}} \newline % Row Count 4 (+ 1) `declare function \seqsplit{prefix:function\_name(\$parameter} as datatype)` \newline % Row Count 6 (+ 2) `as returnDatatype` \newline % Row Count 7 (+ 1) `\{` \newline % Row Count 8 (+ 1) ` ...function code here...` \newline % Row Count 9 (+ 1) `\};` \newline % Row Count 10 (+ 1) {\bf{Example: Declared in the Query}} \newline % Row Count 11 (+ 1) `declare function local:minPrice(\$p as xs:decimal?,\$d as xs:decimal?)` \newline % Row Count 13 (+ 2) `as xs:decimal?` \newline % Row Count 14 (+ 1) `\{` \newline % Row Count 15 (+ 1) `let \$disc := (\$p * \$d) div 100` \newline % Row Count 16 (+ 1) `return (\$p - \$disc)` \newline % Row Count 17 (+ 1) `\};` \newline % Row Count 18 (+ 1) {\bf{How to call the function}} \newline % Row Count 19 (+ 1) `\textless{}minPrice\textgreater{}\{local:minPrice(\$book/price,\$book/discount)\}\textless{}/minPrice\textgreater{}`% Row Count 21 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note:}} \newline Use the declare function keyword. \newline The name of the function must be prefixed. \newline The data type of the parameters are mostly the same as the data types defined in XML Schema. \newline The body of the function must be surrounded by curly braces.} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{x{2 cm} x{6 cm} } \SetRowColor{DarkBackground} \mymulticolumn{2}{x{8.4cm}}{\bf\textcolor{white}{XQuery Terminology}} \tn % Row 0 \SetRowColor{LightBackground} Nodes & In XQuery there are 7 kinds: element, attribute, text, namespace, processing-instruction, comment, and document (root) \tn % Row Count 4 (+ 4) % Row 1 \SetRowColor{white} Atomic Values & Atomic values are nodes with no children or parent \tn % Row Count 6 (+ 2) % Row 2 \SetRowColor{LightBackground} Items & Items refer to nodes and atomic values \tn % Row Count 8 (+ 2) % Row 3 \SetRowColor{white} Parent & Each element and attribute has one parent \tn % Row Count 10 (+ 2) % Row 4 \SetRowColor{LightBackground} Children & Element nodes may have 0, 1, or more children. \tn % Row Count 12 (+ 2) % Row 5 \SetRowColor{white} Siblings & Nodes that have the same parent \tn % Row Count 14 (+ 2) % Row 6 \SetRowColor{LightBackground} Ancestors & A node's parent, parent's parent, etc. \tn % Row Count 16 (+ 2) % Row 7 \SetRowColor{white} \seqsplit{Descendants} & A node's children, children's children, etc. \tn % Row Count 18 (+ 2) \hhline{>{\arrayrulecolor{DarkBackground}}--} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{XQuery Conditional Expressions}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{If-Then-Else expressions are allowed in XQuery \newline % Row Count 1 (+ 1) `for \$x in \seqsplit{doc("books.xml")/bookstore/book`} \newline % Row Count 2 (+ 1) `return if \seqsplit{(\$x/@category="children")`} \newline % Row Count 3 (+ 1) `then \textless{}child\textgreater{}\{data(\$x/title)\}\textless{}/child\textgreater{}` \newline % Row Count 4 (+ 1) `else \textless{}adult\textgreater{}\{data(\$x/title)\}\textless{}/adult\textgreater{}`% Row Count 5 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note:}} "if-then-else" syntax: parentheses around the if expression are required. else is required, but it can be just else ().} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Returning HTML}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{This}} \newline for \$x in \seqsplit{doc("books.xml")/bookstore/book} \newline where \$x/price\textgreater{}30 \newline order by \$x/title \newline return \$x/title \newline \newline {\bf{Yields}} \newline \textless{}ul\textgreater{} \newline \textless{}li\textgreater{}\textless{}title lang="en"\textgreater{}Everyday Italian\textless{}/title\textgreater{}\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}\textless{}title lang="en"\textgreater{}Harry Potter\textless{}/title\textgreater{}\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}\textless{}title lang="en"\textgreater{}Learning XML\textless{}/title\textgreater{}\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}\textless{}title lang="en"\textgreater{}XQuery Kick Start\textless{}/title\textgreater{}\textless{}/li\textgreater{} \newline \textless{}/ul\textgreater{} \newline \newline =================================================== \newline \newline {\bf{This}} \newline \textless{}ul\textgreater{} \newline \{ \newline for \$x in \seqsplit{doc("books.xml")/bookstore/book/title} \newline order by \$x \newline return \textless{}li\textgreater{}\{data(\$x)\}\textless{}/li\textgreater{} \newline \} \newline \textless{}/ul\textgreater{} \newline \newline {\bf{Yeilds}} \newline \textless{}ul\textgreater{} \newline \textless{}li\textgreater{}Everyday Italian\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}Harry Potter\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}Learning XML\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}XQuery Kick Start\textless{}/li\textgreater{} \newline \textless{}/ul\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{Note:}} To eliminate the title element and show only data inside the title element use data(\$x)} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Adding Elements and Attributes to the Result}} \tn \SetRowColor{LightBackground} \mymulticolumn{1}{x{8.4cm}}{{\bf{This}} \newline \textless{}html\textgreater{} \newline \textless{}body\textgreater{} \newline \newline \textless{}h1\textgreater{}Bookstore\textless{}/h1\textgreater{} \newline \newline \textless{}ul\textgreater{} \newline \{ \newline for \$x in \seqsplit{doc("books.xml")/bookstore/book} \newline order by \$x/title \newline return \textless{}li\textgreater{}\{data(\$x/title)\}. Category: \{data(\$x/@category)\}\textless{}/li\textgreater{} \newline \} \newline \textless{}/ul\textgreater{} \newline \newline \textless{}/body\textgreater{} \newline \textless{}/html\textgreater{} \newline \newline {\bf{Yields}} \newline \textless{}html\textgreater{} \newline \textless{}body\textgreater{} \newline \newline \textless{}h1\textgreater{}Bookstore\textless{}/h1\textgreater{} \newline \newline \textless{}ul\textgreater{} \newline \textless{}li\textgreater{}Everyday Italian. Category: COOKING\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}Harry Potter. Category: CHILDREN\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}Learning XML. Category: WEB\textless{}/li\textgreater{} \newline \textless{}li\textgreater{}XQuery Kick Start. Category: WEB\textless{}/li\textgreater{} \newline \textless{}/ul\textgreater{} \newline \newline \textless{}/body\textgreater{} \newline \textless{}/html\textgreater{} \newline \newline ======================================= \newline \newline {\bf{This}} \newline \textless{}html\textgreater{} \newline \textless{}body\textgreater{} \newline \newline \textless{}h1\textgreater{}Bookstore\textless{}/h1\textgreater{} \newline \newline \textless{}ul\textgreater{} \newline \{ \newline for \$x in \seqsplit{doc("books.xml")/bookstore/book} \newline order by \$x/title \newline return \textless{}li class="\{data(\$x/@category)\}"\textgreater{}\{data(\$x/title)\}\textless{}/li\textgreater{} \newline \} \newline \textless{}/ul\textgreater{} \newline \newline \textless{}/body\textgreater{} \newline \textless{}/html\textgreater{} \newline \newline {\bf{Yields}} \newline \textless{}html\textgreater{} \newline \textless{}body\textgreater{} \newline \textless{}h1\textgreater{}Bookstore\textless{}/h1\textgreater{} \newline \newline \textless{}ul\textgreater{} \newline \textless{}li class="COOKING"\textgreater{}Everyday Italian\textless{}/li\textgreater{} \newline \textless{}li class="CHILDREN"\textgreater{}Harry Potter\textless{}/li\textgreater{} \newline \textless{}li class="WEB"\textgreater{}Learning XML\textless{}/li\textgreater{} \newline \textless{}li class="WEB"\textgreater{}XQuery Kick Start\textless{}/li\textgreater{} \newline \textless{}/ul\textgreater{} \newline \newline \textless{}/body\textgreater{} \newline \textless{}/html\textgreater{}} \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}