1 00:00:00,260 --> 00:00:01,760 This video gives an introduction to 2 00:00:01,880 --> 00:00:03,800 the XQuery query language for XML. 3 00:00:04,540 --> 00:00:05,890 As a reminder, querying XML 4 00:00:06,490 --> 00:00:08,870 is not nearly as mature as querying relational databases. 5 00:00:09,540 --> 00:00:10,090 It's much newer. 6 00:00:10,760 --> 00:00:12,230 And there is no clean underlying algebra 7 00:00:12,770 --> 00:00:14,080 that is equivalent to the relational algebra. 8 00:00:15,100 --> 00:00:16,500 The sequence of development of XML 9 00:00:16,970 --> 00:00:18,400 querying which is started with 10 00:00:18,870 --> 00:00:20,210 XPath, which we've already covered. 11 00:00:21,180 --> 00:00:22,390 Then Access LT, which we'll 12 00:00:22,510 --> 00:00:23,860 be covering later; and in this 13 00:00:24,080 --> 00:00:25,510 video and the subsequent demo, 14 00:00:25,960 --> 00:00:27,130 we'll be covering the XQuery language. 15 00:00:28,650 --> 00:00:30,050 XQuery is an expression language, 16 00:00:30,700 --> 00:00:32,000 also known as a compositional language. 17 00:00:32,480 --> 00:00:34,310 And we've seen that already with relational algebra. 18 00:00:35,110 --> 00:00:36,000 What that means is, when we 19 00:00:36,110 --> 00:00:37,120 run an expression of the language 20 00:00:37,700 --> 00:00:38,980 on a type of data the 21 00:00:39,220 --> 00:00:42,030 answer to that expression or query is the same type of data. 22 00:00:42,390 --> 00:00:43,720 So let me just draw the picture here. 23 00:00:43,860 --> 00:00:44,670 We have some data. 24 00:00:45,600 --> 00:00:47,070 We run an expression or 25 00:00:47,220 --> 00:00:48,540 query over that data 26 00:00:49,340 --> 00:00:50,470 and we get get a result back 27 00:00:51,300 --> 00:00:52,630 and that result is also going 28 00:00:52,840 --> 00:00:54,140 to be in the same type of data. 29 00:00:55,320 --> 00:00:56,130 That means we can run another 30 00:00:56,460 --> 00:00:58,100 query or expression over 31 00:00:58,390 --> 00:00:59,300 the same type of data 32 00:00:59,580 --> 00:01:00,860 that was the result of the previous one. 33 00:01:01,130 --> 00:01:03,720 And again, we'll again, we get a result of the same type of data. 34 00:01:04,640 --> 00:01:07,170 Now that also means that we can sort of put these together.And 35 00:01:08,080 --> 00:01:09,100 when we put them together, that 36 00:01:09,280 --> 00:01:10,960 will give us additional expressions or 37 00:01:11,040 --> 00:01:12,460 queries that are valid in the language. 38 00:01:13,870 --> 00:01:15,730 When we talked about compositionality in 39 00:01:15,840 --> 00:01:17,140 the relational model, the types 40 00:01:17,490 --> 00:01:18,640 of data we were using were relations. 41 00:01:19,530 --> 00:01:20,470 We would have maybe some relations 42 00:01:21,040 --> 00:01:22,150 here, we run a query over 43 00:01:22,330 --> 00:01:23,590 those relations the result itself 44 00:01:23,990 --> 00:01:25,130 is a relation and we can 45 00:01:25,280 --> 00:01:26,370 run further queries over that 46 00:01:27,440 --> 00:01:28,200 relation that's the answer. 47 00:01:29,040 --> 00:01:29,820 And then when we looked at relational 48 00:01:30,270 --> 00:01:31,290 algebra, we saw a 49 00:01:31,660 --> 00:01:32,640 composing the expressions of relational 50 00:01:33,080 --> 00:01:34,440 algebra into more complicated 51 00:01:35,090 --> 00:01:36,520 expressions and everything worked together. 52 00:01:38,590 --> 00:01:40,090 In XML, it's similar, except the 53 00:01:40,470 --> 00:01:41,810 expressions operate on and return 54 00:01:42,050 --> 00:01:43,490 what are known as sequences of elements. 55 00:01:44,100 --> 00:01:46,250 Now we're not going to go into a great detail of this model. 56 00:01:46,950 --> 00:01:48,310 As we mentioned for X Path, 57 00:01:49,010 --> 00:01:51,580 the sequences of elements can be generated from an XML document.They 58 00:01:53,870 --> 00:01:55,210 can also be generated from an 59 00:01:55,290 --> 00:01:56,780 XML stream and the 60 00:01:57,420 --> 00:01:58,450 interpretation is quite natural. 61 00:02:00,190 --> 00:02:01,030 Now let me mention that one 62 00:02:01,210 --> 00:02:04,020 of the basic types of expressions in XQuery is XPath. 63 00:02:04,460 --> 00:02:05,500 So every XPath expression 64 00:02:06,080 --> 00:02:07,230 is in fact an XQuery 65 00:02:07,450 --> 00:02:09,290 expression or a valid query in XQuery. 66 00:02:09,930 --> 00:02:11,290 Then we can use XPath and 67 00:02:11,540 --> 00:02:12,590 other constructs of X query 68 00:02:13,180 --> 00:02:14,880 to put together into more complicated queries. 69 00:02:16,330 --> 00:02:17,440 Now, one of the most commonly used 70 00:02:17,800 --> 00:02:19,110 expressions in XQuery is 71 00:02:19,220 --> 00:02:20,350 what's known as the FLWOR expression, 72 00:02:21,390 --> 00:02:22,950 and it looks closest to sequel 73 00:02:23,180 --> 00:02:24,990 of any of the constructs in X query. 74 00:02:25,730 --> 00:02:26,740 Let me explain how it works. 75 00:02:28,610 --> 00:02:30,050 We have up to five clauses in 76 00:02:30,250 --> 00:02:31,330 that FLWOR expression and that's 77 00:02:31,520 --> 00:02:33,490 where the FLWOR comes from. 78 00:02:34,260 --> 00:02:35,900 The first, the for clause, 79 00:02:36,520 --> 00:02:38,150 sets up what are known as iterator variables. 80 00:02:39,480 --> 00:02:40,730 So the idea here is that 81 00:02:41,230 --> 00:02:42,570 in the for clause, this expression 82 00:02:43,140 --> 00:02:44,870 is evaluated and it will 83 00:02:45,120 --> 00:02:47,350 produce potentially a set result. 84 00:02:47,840 --> 00:02:49,230 And then the variable will 85 00:02:49,360 --> 00:02:50,260 be backed Count to each element 86 00:02:50,800 --> 00:02:51,690 of the set one at a 87 00:02:51,830 --> 00:02:52,810 time and then the 88 00:02:52,880 --> 00:02:55,980 rest of the expression will be evaluated for each element. 89 00:02:56,570 --> 00:02:58,810 So in other words if this produces a set of end results. 90 00:02:59,550 --> 00:03:02,070 Then we will effectively evaluate the rest of the queries N times. 91 00:03:02,400 --> 00:03:04,550 We will see that much more clearly when we do the demonstration. 92 00:03:06,090 --> 00:03:08,300 The let clause is more of a typical assignment. 93 00:03:09,010 --> 00:03:12,180 So it's only run once, each time the rest of the query is run. 94 00:03:12,850 --> 00:03:13,920 And so this expression is evaluated 95 00:03:14,580 --> 00:03:15,990 and even if it's a set, it's 96 00:03:16,360 --> 00:03:17,680 assigned once to this variable. 97 00:03:18,230 --> 00:03:20,200 So it's not iterating now, it's just doing an assignment. 98 00:03:20,820 --> 00:03:23,300 Again, it'll become quite clear when we do the demonstration. 99 00:03:24,680 --> 00:03:26,310 The where clause specifies a 100 00:03:26,380 --> 00:03:27,650 condition and it's a filter very 101 00:03:28,010 --> 00:03:30,070 similar to the filters that we've seen in SQL. 102 00:03:30,820 --> 00:03:33,180 The order by is also sort of similar to SQL. 103 00:03:33,940 --> 00:03:35,230 It sorts the results so 104 00:03:35,400 --> 00:03:37,260 that you can get the result in a particular order. 105 00:03:38,990 --> 00:03:40,300 And finally the return clause says 106 00:03:40,510 --> 00:03:42,870 what we want to actually get in the result of our query. 107 00:03:43,570 --> 00:03:45,160 And just as a reminder, when we 108 00:03:45,290 --> 00:03:46,260 start the query with the 109 00:03:46,440 --> 00:03:47,750 for, if we are effectively 110 00:03:48,450 --> 00:03:50,410 executing the query N times 111 00:03:51,010 --> 00:03:52,150 then each one of those 112 00:03:52,450 --> 00:03:54,000 N executions may result 113 00:03:54,420 --> 00:03:55,560 in one of the elements in the result. 114 00:03:56,890 --> 00:03:58,240 The FLWOR expression has up 115 00:03:58,390 --> 00:03:59,690 to five clauses, but actually 116 00:04:00,200 --> 00:04:02,530 all of the clauses except the return clause are optional. 117 00:04:02,840 --> 00:04:03,770 The return is the only one 118 00:04:04,000 --> 00:04:06,710 that's required to have a well-formed query and to get a result. 119 00:04:07,700 --> 00:04:08,620 I also want to mention that 120 00:04:08,750 --> 00:04:10,110 the for and let clause can 121 00:04:10,350 --> 00:04:11,940 be repeated multiple times if 122 00:04:12,070 --> 00:04:12,740 we want to set up multiple 123 00:04:13,160 --> 00:04:16,250 variables or multiple assignments and they can be interleaved with each other. 124 00:04:16,450 --> 00:04:17,800 They don't need to be in a particular order. 125 00:04:19,200 --> 00:04:20,380 A next query, it's possible to 126 00:04:20,650 --> 00:04:22,390 mix query evaluation with 127 00:04:22,640 --> 00:04:24,890 simply writing down the XML data that we want in the result. 128 00:04:25,550 --> 00:04:25,960 And here's an example. 129 00:04:26,790 --> 00:04:28,040 In this case, we're writing the 130 00:04:28,300 --> 00:04:29,460 opening and closing tags that 131 00:04:29,580 --> 00:04:30,970 we want to be in the result of our query. 132 00:04:31,800 --> 00:04:33,180 And then inside curly braces, 133 00:04:33,960 --> 00:04:34,810 we write the query itself. 134 00:04:35,050 --> 00:04:36,810 And what these curly braces 135 00:04:37,290 --> 00:04:39,230 effectively say are evaluate me. 136 00:04:39,570 --> 00:04:40,830 The curly braces are an 137 00:04:41,210 --> 00:04:42,270 indicator to the XQuery processor 138 00:04:43,160 --> 00:04:44,450 that what's inside them needs to 139 00:04:44,560 --> 00:04:45,650 be run as a query, 140 00:04:45,980 --> 00:04:48,070 is replaced with the XML that's the query result, 141 00:04:48,880 --> 00:04:50,420 and the final output is 142 00:04:50,550 --> 00:04:51,890 our opening and closing tags with 143 00:04:52,130 --> 00:04:53,600 the XML query result inside. 144 00:04:54,970 --> 00:04:56,520 At this point, let's move ahead to the demo. 145 00:04:57,300 --> 00:04:58,310 We'll once again be using our 146 00:04:58,430 --> 00:04:59,500 bookstore data, and we'll do 147 00:04:59,680 --> 00:05:02,230 a sequence of XQuery examples over that data. 148 00:05:03,020 --> 00:05:03,940 Again we are not going to be comprehensive. 149 00:05:04,630 --> 00:05:05,360 XQuery is a big 150 00:05:05,620 --> 00:05:06,880 language but will show the 151 00:05:07,050 --> 00:05:08,150 flavor of the language and a 152 00:05:08,290 --> 00:05:09,690 lot of its most useful constructs and 153 00:05:09,830 --> 00:05:11,500 we'll be able to write some quite powerful queries.