summaryrefslogtreecommitdiffstats log msg author committer range
path: root/Report/Chapter4.tex
blob: dcb2b021324cdb4b290b7c78d2873dcfbe0cce44 (plain)
 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51  \section{XQuery and XPath} The XQuery and XPath queries used in the Pottepei application gather and update the information stored in the XML database. This chapter deals with those queries and explains how they are build up and what the reproduce. \\ \subsection{Retrieve data} To retrieve data about any liquor or recipe the application needs to perform a query that gathers all items in the database. From the previous chapter we already saw that the database contains an element "items". In this element all the liquor information of the original RSS feed is stored. \\ The query for this is therefor very simple: $$//\text{item}$$ This query gets all the "items" from the database. We know that there is only one "items" node so the application walks trough all child-nodes of the result. The code snippet below show a brief example on how to retrieve the title of all items. \begin{verbatim} $query = '//item';$nodes = $xpath->query($query); foreach ($nodes as$node) { // get title $item =$node->getElementsByTagName("title"); $title =$item->item(0)->nodeValue; } \end{verbatim} %% \footnote[1]{In PHP, variables are indicated by a prefix dollar-sign.} The \textit{\$query} keyword denotes a variable which, in this case, is our query to retrieve the items from our database. The \textit{\$nodes = \$xpath$\rightarrow$query(\$query)} line actually performs the query and retrieves the nodes from the database. \\ We then go through each item with the \textit{foreach} statement. Then, from that element, we get the title with the \textit{getElementsByTagName()} function. This gives us again a XML tree with nodes. We know that there is only one title, so we get the value of that node with \textit{\$item$\rightarrow$item(0)$\rightarrow$nodeValue}. \subsection{Search queries} The search the database for your favorite beer, you can fill in a small search-form. The PHP script will then return all items that apply yo the search query. Such a query is shown below. $$\textit{//item[contains(title/text(), "bier") or contains(descr/text(), "bier")]}$$ $$\textit{[LiquorTypeID/text()="2"][NewsCategoryID/text()="2"]}$$ The query begins the same as that of the previous query. The main difference here are the extra filters applied to the query. It checks if either the title or description of the news-item contains a string with the XPath function$ contains $which returns a boolean value. It also check the type of liquor and news category. \subsection{Update information} The user can also update the description, category and liquor type of a news-item. This is also done with XPath. The following code snippet shows how the description of an item can be changed. \begin{verbatim}$item = $node->getElementsByTagName("description");$item->item(0)->nodeValue = "Changed your value... Joink!"; \end{verbatim} As you can see, the nodeValue can easily be changed. Once the right news-item is retrieved, it's values can be changed with an assignment statement. \\