Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 DEEL II XML.

Similar presentations


Presentation on theme: "1 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 DEEL II XML."— Presentation transcript:

1 1 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 DEEL II XML

2 2 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Table of Contents 1. Semistructured Data 2. Introduction to XML 3. Querying XML 4. XPath 5. XQuery 6. Typing in XQuery 7. Document Type Definitions 8. XML Schema 9. Light XQuery 10. XSLT

3 3 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 References I. XML [1] www.w3.org/TR/xmlschema-0 [2] www.w3.org/TR/xmlschema-1 [3] www.w3.org/TR/xpath [4] P.M. Lewis, A. Bernstein, M. Kifer, Databases and Transaction Processing, Addison Wesley, Chapter 17, 2002 [5] D. Chamberlin, XQuery, a query language for XML, Web, June 2003 [6] www.w3.org/TR/xquery [7] www.w3.org/XML/Query

4 4 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 References [8] www.w3.org/TR/xslt20 [9] Draper et al, XQuery 1.0 and XPath 2.0 Formal Semantics, www.w3.org/TR/xquery-semantics/, 2003 [10] M. Brundage, XQuery, Add. Wesley, 2004 [11] J. McGovern, P. Bothner, K. Cagle, J. Linn, V. Nagarajan, XQuery, Sams Publ., 2004 [12] H. Katz, XQuery from the Experts, Add. Wesley, 2004

5 5 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 1. Semistructured Data [4] Web data for human consumption –HTML (Cfr. next slide) –Self describing: attribute names are included but not explicitly separated from data values Web data for machine consumption –Characteristics of Semistructured Data: object-like schemaless self-describing –XML (optional) structure descr.: DTD, XML Schema

6 6 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Student List ListName: Students Name: Jan Vijs Id: 11 Address: Number: 123 Street: Turnstreet Name: Jan De Moor Id: 66 Address: Number: 4 Street: Hole Rd HTML-document

7 7 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 2. Introduction to XML [4] Why is XML important? –simple open non-proprietary widely accepted data exchange format XML is like HTML but –no fixed set of tags X = “extensible” –no fixed semantics (c.q. representation) of tags representation determined by separate ‘stylesheet’ semantics determined by application –no fixed structure user-defined schemas

8 8 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Jan Vijs 11 123 Turnstreet 66 4 Hole Rd XML-document – Running example 1

9 9 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Global structure –First line is mandatory; –Tags are chosen by author; –Opening tag must have a matching closing tag; –Only one root element PersonList ; – … ; a is the name of the element, content, child, descendant, parent, ancestor, sibling; – Type is name of the attribute of element PersonList ; the value of the attribute is “Student” ; all attribute values must be quoted;

10 10 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 –empty elements: –processing instruction: –comment: –mixed data-text: Jan lives in Q Street number 123 –elements are ordered:

123 Q Street Q Street 123
are different –weak facilities for constraints

11 11 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 XML Attributes An element can have any number of attributes; the order of the attributes does not matter; an attribute can only occur once within an element; attribute values can only be strings; the following have the same semantics (except for the ordering of and ): 2 Jan

12 12 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Well-formedness We call an XML-document well-formed iff –it has one root element; –elements are properly nested; –any attribute can only occur once in a given opening tag and its value must be quoted;

13 13 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Types of nodes: document, element, text, attribute Root Students CrsTaken Student Name FirstLast CrsTaken StudId Semester CrsCode Semester CrsCode Root Students Semester siblings are ordered John Doe U4 … Data Model “dr” “John”“Doe” “U4”

14 14 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 A value is an ordered sequence of zero or more items; An item is a node or an atomic value; There are four kinds of nodes: Document Node Element Node Attribute Node Text Node Children are element or text nodes (no attribute nodes) Examples of values 47 (1, 2, 3) (47,, "Hello") () An XML document An attribute standing by itself

15 15 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 There is no distinction between an item and a sequence of length one; There are no nested sequences; There is no null value; A sequence can be empty; Sequences can contain heterogeneous values; All sequences are ordered; Nodes have identity (atomic values don't); Element and attribute nodes have a type annotation; (not considered here) May be a complex type such as PurchaseOrder; (not considered here) Type may be unknown ("anyType"); Each node has a typed value; (not considered here) There is a document order among nodes; Ordering among documents and constructed nodes is implementation-defined but stable;

16 16 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Cfr. C ++ Building vocabularies to prevent naming conflicts; uniform use of tag-names; general form of a tag: or in principle nothing to do with Internet. use different URIs(URLs) for different domains; “http://www.acmeinc.com/jp#students” for students “http://www.acmeinc.com/jp#toys” for toys synonyms for URIs (URLs) can be declared; called namespaces default namespace; Namespaces

17 17 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 < item xmlns=“http://www.acmeinc.com/jp#supplies” xmlns:toy=“http://www.acmeinc.com/jp#toys”> backpack cyberpet notebook sticker the default namespace is declared by the attribute xmlns the other namespaces are declared by xmlns : the outermost, the first and belong to default namespace “http://www.acmeinc.com/jp#supplies” and the first belong to the namespace “http://www.acmeinc.com/jp#toys” the innermost and the second belong to the default namespace “http://www.acmeinc.com/jp#supplies2” the second belongs to the namespace “http://www.acmeinc.com/jp#toys2”

18 18 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Jan Vijs U2 Jan De Moor U3 Bart Simpson U4 Running example 2

19 19 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 CS308 S2002 CS308 F2003 MAT123 F2003 Databases Algebra

20 20 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 3. Querying XML Why query XML-documents? –special XML databases –major DBMSs “speak” XML; Does the world need a new query language? Most of the world's business data is stored in relational databases; The relational language SQL is mature and well- established; Can SQL be adapted to query XML data? –Leverage existing software –Leverage existing user skills

21 21 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 How is XML data different from relational data? Relational data is "flat”: rows and columns; XML data is nested: and its depth may be irregular and unpredictable; Relations can represent hierarchic data by foreign keys or by structured datatypes; In XML it is natural to search for objects at unknown levels of the hierarchy: "Find all the red things“;

22 22 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Relational data is uniform and repetitive; All bank accounts are similar in structure; Metadata can be factored out to a system catalog; XML data is highly variable; Every web page is different; Each XML object needs to be self-describing; Metadata is distributed throughout the document; Queries may access metadata as well as data: "Find elements whose name is the same as their content“: //*[name(.) =string(.)]

23 23 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Relational queries return uniform sets of rows; The results of an XML query may have mixed types and complex structures; "Red things": a flag, a cherry, a stopsign,... Elements can be mixed with atomic values; XML queries need to be able to perform structural transformations; Example: invert a hierarchy;

24 24 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The rows of a relation are unordered; Any desired output ordering must be derived from values; The elements in an XML document are ordered; Implications for query: Preserve input order in query results Specify an output ordering at multiple levels; "Find the fifth step“; "Find all the tools used before the hammer“;

25 25 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Relational data is "dense“; Every row has a value in every column; A "null" value is needed for missing or inapplicable data; XML data can be "sparse“; Missing or inapplicable elements can be "empty" or "not there“; This gives XML a degree of freedom not present in relational databases;

26 26 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Unabbreviated syntax A location path transforms a document tree and one of its nodes as a context node to a sequence of distinct nodes in document order. child::a selects the element children with name a of the context node child::* selects all element children of the context node child::text() selects all text children of the context node child::node() selects all the (element or text) children of the context node attribute::a selects the attribute with name a of the context node attribute::* selects all the attributes of the context node descendant::a selects the element descendants with name a of the context node descendant-or-self::a selects the element descendants with name a of the context node and, if the context node has name a, the context node as well ancestor::a selects all ancestors with name a of the context node ancestor-or-self::a selects the ancestors with name a of the context node and, if the context node is an element with name a, the context node as well self::a selects the context node if it is has the name a, and otherwise selects nothing child::chapter/descendant::a selects the element descendants with name a of the element children with name chapter of the context node child::*/child::a selects all element grandchildren with name a of the context node / selects the document root 4 XPath [3,5]

27 27 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 /descendant::a selects all the elements with name a in the same document as the context node /descendant::a/child::b selects all the elements with name b that have a parent with name a and that are in the same document as the context node child::a[position()=1] selects the first child with name a of the context node child::a[position()=last()] selects the last child with name a of the context node child::a[position()=last()-1] selects the last but one child with name a of the context node child::a[position()>1] selects all the children with name a of the context node other than the first child with name a of the context node following-sibling::chapter[position()=1] selects the next chapter sibling of the context node preceding-sibling::chapter[position()=1] selects the prev. chapter sibling of the context node /descendant::figure[position()=42] selects the forty-second element with name figure in the document child::a[attribute::type=“warning”] selects all the children with name a of the context node that have an attribute with name type and value “warning” child::a[attribute::type=“warning”][position()=5] selects the fifth child with name a of the context node that has an attribute with name type and value “warning” child::a[position()=5][attribute::type="warning”] selects the fifth child with name a of the context node if that child has an attribute with name type and value “warning” child::chapter[child::title] selects the chapter children of the context node that have one or more children with name title child::*[self::chapter or self::appendix] selects the chapter and appendix children of the context node child::*[self::chapter or self::appendix][position()=last()] selects the last chapter or appendix

28 28 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 General form of a location path: relative lp : step 1 / step 2 / … / step n (n > 0) child::*/child::name Each step in turn selects a sequence of nodes relative to a context node. An initial sequence of steps is composed together with a following step as follows: the initial sequence of steps selects a sequence of nodes relative to a context node. Each node in that sequence is used as a context node for the following step. The sequences of nodes identified by that step are unioned together. The sequence of nodes identified by the composition of the steps is this union, ordered in document order, without duplicates. The example selects the union of all name children of children of the context node. absolute lp : / step 1 / step 2 / … / step n (n  0) /child::*/child::name The initial / selects the root node of the document containing the context node. Then apply step 1 / step 2 / … / step n

29 29 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 General form of step : axis :: node-test [predicate 1 ] … [predicate n ] (n  0) child::name[attribute::type=“warning”][position()=5] The node-sequence selected by the step is the node-sequence that results from generating an initial node-sequence from the axis, filtering it by the node-test, and then filtering that node-sequence by each of the predicates in turn. the child axis contains the element and text children of the context node; the descendant axis contains the element and text descendants of the context node; the parent axis contains the parent of the context node; the ancestor axis contains the ancestors of the context node; if the context node is an element or text node the following-sibling axis contains all the following element or text siblings of the context node; if the context node is an attribute node, the following-sibling axis is empty; if the context node is an element or text node the preceding-sibling axis contains all the preceding element or text siblings of the context node; if the context node is an attribute node, the preceding-sibling axis is empty;

30 30 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 the following axis contains all element or text nodes in the same document as the context node that are after the context node in document order (preorder), excluding any descendants; the preceding axis contains all element or text nodes in the same document as the context node that are before the context node in document order, excluding any ancestors; the attribute axis contains the attribute children of the context node; the self axis contains just the context node itself; the descendant-or-self axis contains the context node and the descendants of the context node; the ancestor-or-self axis contains the context node and the ancestors of the context node; Remark that the ancestor, descendant, following, preceding and self axis partition the element nodes of a document. parent::*/parent::*/child::*/following-sibling::*/preceding-sibling::*/ following::*/descendant::*/ancester::*/preceding::*

31 31 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Node-test has the form: label : filters the nodes with that label * : filters all element nodes node() : filters all nodes text() : filters the text nodes Predicate has (until now) the form: [position()=i] The ancestor, ancestor-or-self, preceding, and preceding-sibling axes are reverse axes; all other axes are forward axes. The proximity position of a node of a node-sequence with respect to an axis is defined to be the position of the node in the node-sequence ordered in document order if the axis is a forward axis and ordered in reverse document order if the axis is a reverse axis. The first position is 1. [position()=i] filters those nodes whose proximity position is equal to i. parent::*/parent::*/child::*/following-sibling::*/ preceding-sibling::*[position()=1]/following::*/descendant::*/ ancestor::*[position()=1]/preceding::*

32 32 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Abbreviated Syntax in red More used than the unabbreviated syntax. child:: is omitted child::name name attribute:: is abbreviated to @ attribute::name @name /descendant-or-self::node()/ is abbreviated to // child::aa/descendant-or-self::node()/child::bb aa//bb self::node() is abbreviated to. self::node()/descendant-or-self::node()/child::name.//name parent::node() is abbreviated to.. parent::node()/child::title../title position()= is omitted [position()=1] [1]

33 33 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 a selects the element children with name a of the context node * selects all element children of the context node text() selects all text children of the context node node() selects all the element and text children of the context node @a selects the attribute with name a of the context node @* selects all the attributes of the context node */a selects all element grandchildren with name a of the context node a[1] selects the first element child with name a of the context node a[last()] selects the last element child with name a of the context node /doc/chapter[5]/section[2] is an abbreviation of /child::doc/child::chapter[position()=5]/child::section[position()=2] chapter//a is an abbreviation of chapter/descendant-or-self::node()/child::a //a is an abbreviation of /descendant-or-self::node()/child::a./a is an abbreviation of self::node()/child::a, which is equivalent to child::a chapter/./a is an abbreviation of child::chapter/self::node()/child::a, which is equivalent to child::chapter/child::a or chapter/a */.. is an abbreviation of child::*/parent::node() selects the context node, if it has children, otherwise nothing is selected //a[1] is an abbreviation of /descendant-or-self::node()/child::a[position()=1] and selects the name descendant elements of the root that are the first name child of their parent /descendant::a[1] is an abbreviation of /descendant::a[position()=1] and selects the first name descendant of the root

34 34 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Predicates [3] Predicates can be Boolean expression [a/b/c/text()=5] 5 belongs to the result sequence of the location path [a/b/c=d/e] intersection of the 2 result sequences is not empty location path [a/b/c] result sequence of location path is not empty number [5] means [position()=5] combinations using and, or, not

35 35 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Note the difference between [a/text() = 12] selects a node if it has an a child with a text child equal to 12; [not(a/text() != 12)] selects a node if all the text children of all its a childeren are equal to 12; (Cfr. Data Model p.12) //Students/Student/@StudId[../Name/First[string(text())=“John”] or../CrsTaken[@Semester=“2”]] //Students/Student[Name/First[string(text())=“John”] or CrsTaken[@Semester=“2”]] //Students/Student/Status[../Name/First[string(text())=“John”] or../CrsTaken[@Semester=“2”]] //Students/Student/Name[First[string(text())=“John”] or../CrsTaken[@Semester=“2”]] //Students/Student/Name/First[string(text())=“John” or../../CrsTaken[@Semester=“2”]] //a/b[2] selects 2nd and 4th b (//a/b)[2] selects 2nd b a a b b b b

36 36 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Alice Smith 123 Maple Street Mill Valley CA 90952 Robert Smith 8 Oak Avenue Old Town PA 95819 Hurry, my lawn is going wild! Lawnmower 1 148.95 Confirm this is electric Baby Monitor 1 39.98 2004-12-21 Document on file ‘po.xml’, running example 3 Xpath as a Query Language for XML

37 37 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Select the date of the purchase order if it is shipped to Belgium: document(“po.xml”)/purchaseOrder/@orderDate[../shipTo/@country=“Belgium”] document(“po.xml”)/purchaseOrder[shipTo/@country=“Belgium”]/@orderDate Select the items that are shipped to Mechelen and whose price is greater than 40: document(“po.xml”)//item[USPrice/text()>40 and (../../billTo/city/text()="Mechelen")] Select the purchase order if all its items cost more than 40: document(“po.xml”)/purchaseOrder[not(.//USPrice/text()<=40)] Select the purchase order if some of its items cost more than 40: document(“po.xml”)/purchaseOrder[.//USPrice/text>40] Select the purchase order if it contains at least 2 items: document(“po.xml”)/purchaseOrder[items/item[2]] Given an item, select the preceding item of the same purchase order: preceding-sibling::*[1] Select the dates on which there is an order that is shipped and billed in the same city: document(“po.xml”)/purchaseOrder/@orderDate [../shipTo/city/text()=../billTo/city/text()]

38 38 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 5. XQuery [4,5,6] Principles of XQuery Design A set of operators that are closed under the data model; Every expression has a value and no side effects; Expressions can be composed with full generality; Use the type system of XML Schema; XPath compatibility. Adopt XPath as a syntactic subset; Roughly equivalent to "relational completeness“; No formal standard exists for hierarchic languages; XQuery is a case-sensitive language Keywords are in lower-case Expressions can raise errors

39 39 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Recursive Functions; Conciseness - Simplicity Static Analysis optional static analysis phase before query execution type inference rules based on XML Schema early detection of some kinds of errors optimization Comments look like this: (: Houston, we have a problem :)

40 40 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 For overview: http://www.w3.org/XML/Query Demos X-Hive's XQuery demoXQuery demo Software AG's Tamino XML Query DemoTamino XML Query Demo Free and/or Open Source Fernandez/Simeon's Galax. Open-source.Galax Saxonica's Saxon. Available in a schema-aware version as a commercial product, and without schema support as open source.Saxon Sourceforge's eXist. Open-source.eXist Ispras Modis' Sedna. Open-source. … and many more …Sedna Commerical BEA's Liquid DataLiquid Data X-Hive; Software AG’s Tamino; Microsoft's SQL Server 2005 Express, with XQuery supportSQL Server 2005 Express Oracle's Xquery Technology - Preview … and many many more …Xquery Technology - Preview Interesting Implementations

41 41 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Literals: "Hello" 47 4.7 4.7E-2 Constructed values: true() false() date("2002-03-15") Variables: $x Constructed sequences $a, $b is the same as ($a, $b) (1, (2, 3), (), (4)) is the same as 1, 2, 3, 4 5 to 8 is the same as 5, 6, 7, 8 Functions XQuery functions have expressions for bodies and may be recursive Function calls: two-argument-function(1, (2,3)) Functions are not overloaded (except certain built-ins) Subtype substitutability in function arguments XQuery Expressions

42 42 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Functions on sequences union intersect except (infix) – only on sequences of nodes; result in doc. order without dupl. empty() count() Location paths of XPath abbreviated and non-abbreviated; examples: book[author/text() = "Mark Twain "] chapter[2] book[appendix] person[@married] //book[author/text() = "Mark Twain"]/chapter[2] (1 to 100)[. mod 5=0] Arithmetic operators: + - * div idiv mod Extract typed value from node Multiple values => error If operand is (), return () Supported for numeric and date/time types

43 43 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Comparison operators eq ne gt ge lt le compare single atomic values = != > >= < <= implied existential semantics is, is not compare two nodes based on identity > compare two nodes based on document order

44 44 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 FLWOR Expression A FLWOR expression binds some variables, applies a predicate and constructs a new result. for var in expr let var := expr where expr order by exprreturn expr

45 45 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Market Analysis Adrian Jones Electronic Circuits David Jones Database Systems Mary Doe Examples in Galax transcripts.xml classes.xml

46 46 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 –FOR clause for $c in document(“classes.xml”)//Class, $t in document(“transcripts.xml”)//Transcript specify documents used in the query declare variables and bind them to a range result is a list of bindings –LET clause let $sn := $t/Student/@Name, $cn := $c/CrsName bind variables to a value –WHERE clause where $c/@CrsCode = $t/CrsTaken/@CrsCode and $c/@Semester = $t/CrsTaken/@Semester selects a sublist of the list of bindings –RETURN clause return $cn $sn construct result for every selected binding

47 47 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 { for $t in document(“transcripts.xml”)//Transcript let $s := $t/Student where $t/CrsTaken/@CrsCode = “CS308” return {$s/@Name} } q01.xq

48 48 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Element Constructor Huckleberry Finn The result of an element constructor is a new element node, with its own node identity. All the attribute and descendant nodes of the new element node are also new nodes with their own identities, even if they are copies of existing nodes.

49 49 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 If the content of an element or attribute must be computed, use a nested expression enclosed in { } {$b/title} AA $i//t BB {$i//t} The above query might generate the following result: AA $i//t BB CC {1, 2, 3} The constructed element node has one child, a text node containing the value "1 2 3". I saw {5 + 3} cats. The constructed element node has three children: a text node containing " I saw ", a child element node named howmany, and a text node containing " cats. ". The child element node in turn has a single text node child containing the value " 8 ".

50 50 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 doc1.xml v3 v4 v6 q14.xq for $e in document("doc1.xml")//el return $e, for $e in document("doc1.xml")//el return $e/@a1, for $e in document("doc1.xml")//el return {$e/@a1}, for $e in document("doc1.xml")//el return, for $e in document("doc1.xml")//el return, for $e in document("doc1.xml")//el return {string($e/@a1)}, for $e in document("doc1.xml")//el for $s in $e/sub return {data($s)}, for $e in document("doc1.xml")//el for $s in $e/sub return {$s/text()} Result v3 v4, v6,, $e/@a1, $e/@a1, $e/@a1,,,,,,,,,, v1, v5, v7, v3, v4, v6, v3, v4, v6 The functions data() and string() give the content of their arguments (Cfr. later)

51 51 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 If both the name and the content must be computed, use a computed constructor: element{name-expr}{content-expr} attribute{name-expr}{content-expr} The first enclosed expression after the element keyword generates the name of the element, and the second enclosed expression generates the content and attributes: element {string( nnn )} {string( sss )}, let $dict := Adresse Indirizzo let $e:= 123 Roosevelt Ave. Flushing, NY 11368 return element {string($dict/entry[@word=name($e)]/variant[@lang="Italian"])} {$e/@*, string($e)} results in sss, 123 Roosevelt Ave. Flushing, NY 11368 q15.xq

52 52 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 for $c in document("classes.xml")//Class, $t in document("transcripts.xml")//Transcript where $c/@CrsCode = $t/CrsTaken/@CrsCode and $c/@Semester = $t/CrsTaken/@Semester return {$c/CrsName} {$t/Student/@Name} Market Analysis, Electronic Circuits, Database Systems, Database Systems Flat Join (wrong result cfr. next slide) q02.xq

53 53 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Market Analysis Adrian Jones Electronic Circuits David Jones Database Systems Mary Doe Why Market Analysis, is in the result transcripts.xml classes.xml

54 54 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 for $c in document("classes.xml")//Class, $t in document("transcripts.xml")//Transcript, $ct in $t/CrsTaken where $c/@CrsCode = $ct/@CrsCode and $c/@Semester = $ct/@Semester return {$c/CrsName} {$t/Student/@Name} Database Systems, Database Systems Order of variables in FOR-clause matters. Classes with no transcripts are omitted. q03.xq Flat join

55 55 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 for $c in document("classes.xml")//Class order by ($c/CrsName/text()) return ( { for $t in document("transcripts.xml")//Transcript for $ct in $t/CrsTaken where ($c/@CrsCode = $ct/@CrsCode and $c/@Semester = $ct/@Semester) return {$t/Student/@Name} } ),, Nested Join q04.xq

56 56 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Group students per course code and semester on the basis of Transcripts alone let $trs := document("transcripts.xml")//Transcript let $ct := $trs/CrsTaken for $c in $ct return { for $t in $trs where (($c/@CrsCode = $t/CrsTaken/@CrsCode) and ($c/@Semester = $t/CrsTaken/@Semester)) order by ($t/Student/@StudId) return $t/Student } q05.xq , , , , ,

57 57 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 User-defined functions local: is only necessary in GALAX, not necessary in XQuery; Count number of descendants declare function local:countElemNodes($e) { if (empty($e/*)) then 0 else local:countElemNodes($e/*) + count($e/*) }; local:countElemNodes(document("transcripts.xml")//Transcripts) Result : 10 Input and output are typed Body can be any XQuery expression, recursion is allowed XPath core functions: sum(), count() Automatic generalization of local:countElemNodes() to collection q06.xq

58 58 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Group students per course code and semester on basis of Transcripts alone q13.xq declare function local:extractClasses($e) { for $c in $e//CrsTaken return }; let $trs := document("transcripts.xml")/Transcripts for $c in local:extractClasses($trs) return { $c/@CrsCode, $c/@Semester } { for $t1 in $trs//Transcript[CrsTaken/@CrsCode=$c/@CrsCode and CrsTaken/@Semester=$c/@Semester] order by ($t1/Student/@StudentId) return $t1/Student }

59 59 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Result , , , , ,

60 60 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Give all the elements in classes that contain somewhere “ys” and whose elementname ends with “ses” { for $t in document("classes.xml")//*[contains(string(.), "ys") and ends-with(name(.),"ses")] return $t } Market Analysis Adrian Jones Electronic Circuits David Jones Database Systems Mary Doe Cfr. www.w3.org/TR/xquery-operators q17.xq

61 61 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 >>, << document order { for $c1 in document("transcripts.xml")//CrsTaken[@Semester > "F1994"] for $c2 in document("transcripts.xml")//CrsTaken[@Semester > "F1994"] where (($c1 << $c2) and not($c1/@Grade = $c2/@Grade)) return {$c1, $c2} } q18.xq

62 62 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 let $doc := aa 1 2 bb 3 4 5 let $i1 := $doc//b[2] for $i2 in $doc//c[. >> $i1][position()<=2] return $i2/text() text {“ 3 ”}, text {“ 4 “} let $doc := 1 2 bb 3 4 5 for $i2 in $doc//c where not(some $i1 in $doc//b satisfies ($i1 << $i2)) return $i2/text() text {“ 1 ”}, text {“ 2 “} let $doc := 1 2 bb 3 4 5 for $i2 in $doc//c where not(some $i1 in $doc//b satisfies ($i1 << $i2)) return $i2/text() text {“ 1 “} q19.xq q21.xq q20.xq

63 63 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Find everything between two nodes declare function local:btween($seq, $start, $enda) { let $nodes := for $n in $seq except $start//node() where $n >> $start and $n << $enda return $n return ($nodes except $nodes//node()) }; { let $proc := ( 1 fff 2 bb 3 4 5 ), $first := $proc/c[1], $second := $proc/c[last()] return local:btween($proc//node(), $first, $second) } 2 bb 3 4 q22.xq

64 64 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 { let $proc := ( cxcxc 1 fffc 2 cbb 3 4 c5 ) return $proc//node()[contains(.,"c")] } cxcxc 1 fffc fffc 2 cbb cbb c5 q23.xq

65 65 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 declare function local:one_level($l, $p) { { for $s in $l//part where $s/@partof = $p/@partid return local:one_level($l,$s) } }; let $list := return ( { for $p in $list//part[empty(@partof)] return local:one_level($list,$p) } ) q24.xq

66 66 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Grouping and aggregation Count courses per student for $t in document("transcripts.xml")//Transcript, $s in $t/Student let $c := $t/CrsTaken return , q08.xq

67 67 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Compute average grade per class q09.xq declare function local:numericGrade($a) { let $grade := data($a) return if ($grade = "A") then 10 else if ($grade = "B") then 8 else if ($grade = "C") then 6 else if ($grade = "D") then 4 else if ($grade = "E") then 2 else 0 }; for $c in document("classes.xml")//Class let $g := ( for $ct in document("transcripts.xml")//CrsTaken where $ct/@CrsCode = $c/@CrsCode and $ct/@Semester = $c/@Semester return local:numericGrade($ct/@Grade) ) order by ($c/@CrsCode) return , ,

68 68 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Quantification –Existential quantification: some $Var in Expr satisfies Cond –Universal quantification: every $Var in Expr satisfies Cond Select courses that were followed by some student q10.xq for $c in document("classes.xml")//Class where ( some $t in document("transcripts.xml")//CrsTaken satisfies ($c/@CrsCode = $t/@CrsCode and $c/@Semester = $t/@Semester) ) return $c/CrsName Database Systems

69 69 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Select classes in which every student took MAT123 for $c in document("classes.xml")//Class let $g := ( for $t in document("transcripts.xml")//Transcript let $tt := for $b in $t/CrsTaken where $b/@CrsCode = $c/@CrsCode and $b/@Semester = $c/@Semester return $b where not(empty($tt)) return $t ) where every $tr in $g satisfies not(empty($tr[CrsTaken/@CrsCode = "MAT123"])) order by ($c/CrsCode) return $c Market Analysis Adrian Jones, Electronic Circuits David Jones q11.xq

70 70 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Order sorts a list … of elements on their names. declare function local:Car($x) {if (empty($x/*)) then else for $y at $z in $x/* where $z=1 return $y}; declare function local:Cdr($x) {if (empty($x/*)) then else {for $y at $z in $x/* where 1 lt $z return $y} }; declare function local:Cons($x, $y) { {$x, $y/*} }; declare function local:Decomp($x) {if (empty($x/*)) then {$x, $x} else if (count($x/*) eq 1) then ( {$x} ) else {local:Decomp(local:Cdr($x))/*[2]} {local:Car($x), local:Decomp(local:Cdr($x))/*[1]/*} }; declare function local:Merge($x, $y) {if (empty($x/*)) then $y else if (empty($y/*)) then $x else if (name($x/*[1]) lt name($y/*[1])) then local:Cons($x/*[1], local:Merge(local:Cdr($x), $y)) else local:Cons($y/*[1], local:Merge(local:Cdr($y), $x))}; declare function local:Order($x) {if (count($x/*) lt 2) then $x else let $t := local:Decomp($x) let $t1 := local:Car($t) let $t2 := local:Car(local:Cdr($t)) return local:Merge(local:Order($t1),local:Order($t2))}; let $l5 := return local:Order($l5) q16.xq The function name($e) gives the name of the element $e (Cfr. later)

71 71 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 XQuery Data Model Sequences are list of 0 or more items; an item is a node or an atomic value; a sequence of one item is equiv. with that item; a sequencetype consists of a typename and an occurrence indicator; xs:integer* xs:integer + xs:integer? 6 Typing in XQuery [10]

72 72 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Atomic Types Built-in atomic types are defined in two namespaces: xs: (http://www.w3.org/2001/XMLSchema) xdt: (http://www.w3.org/2003/11/xpath-datatypes) xdt:anyAtomicType xdt:untypedAtomicxs:boolean xs:doublexs:decimal xs:integer xs:float xs:string

73 73 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 xdt:untypedAtomic numbers are double, rest are string avoids casting xs:boolean true(), false() numerical types xs:double, xs:decimal, xs:integer, xs:float Type constructors are used to create values of that type (complex rules for errors) xs:integer("12") => 12 xs:integer(56) => 56 xs:boolean(“true”) => true() xs:boolean(“1”) => true() xs:boolean(“false”) => false() xs:boolean(“0”) => false() xs:boolean(other) => error 12 => 12 xs:integer( 78 ) => 78 xs:integer(“4.5”) => error xs:float("2.88") => xs:float(“2.88”) xs:float("567") => xs:float(“567”) xs:float(567) => xs:float(“567”) xs:decimal(3.8) => 3.8 xs:decimal("3.8") => 3.8 xs:decimal(3) => 3 xs:decimal("3") => 3

74 74 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Node kinds Nodes are part of a document or a fragment (whose root is not a document node). We consider 4 node kinds: element, attribute text, document-node also comment, namespace, processing-instruction. Every node has type node() ; other types are item(), element(), attribute(), document-node(), text()

75 75 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 name() is a function that give the name (type string) of a node; string() takes a node and gives the concatenation of the string values of all its descendants; data() takes a sequence of items (with each node having a single atomic value) and gives the sequence of the typed atomic values of the items. boolean() takes a sequence of items and returns a boolean value; empty sequence, empty string, 0, false() => false() other sequences => true() instance of takes a value and a type and verifies whether the value is of that type;

76 76 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 v cast as t has the same meaning as t(v); v castable as t => true() iff v cast t gives no error; name( ), string( cdf 3 4 aa 1q2 ; ), data((1, 3.4, 3.4E2, 34, 34 )), boolean(""), boolean(123), boolean((0,0)) => “a”, “ cdf 3 4 aa1q2;”, 1, 3.4, 340, xdt:untypedAtomic(“34”), xs:integer(“34”), false(), true(), true() !not in Galax! instance of element(), 1 instance of xs:integer, 1 instance of item()*, (1,2,7) instance of xs:integer*, (1,2,7) instance of xs:integer+, (1,2,7) instance of xs:integer?, (let $s := "Antwerp" return $s instance of xs:string) => true(), true(), true(), true(), true(), false(), true() “2” cast as xs:integer => 2 “2” castable as xs:integer => true()

77 77 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Function declarations with types type of the arguments and type of the result are mentioned: declare function f(p as t1, p2 as t2) as t3 { … };

78 78 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 declare function local:seconditin($seq as item()*) as xs:integer (: gives the second item of a sequence :) { $seq[2] }; declare function local:secondinin($seq as xs:integer*) as xs:integer (: gives the second item of a sequence :) { $seq[2] }; declare function local:seconditit($seq as item()*) as item() (: gives the second item of a sequence :) { $seq[2] }; local:seconditin((3, 7, 45, 6.7)) => 7 local:seconditin((3, 45, 6.7)) => 45 local:seconditin((3, gg, 6.7)) => text: “gg” is not an integer local:secondinin((3, 7, 45, 6)) => 7 local:secondinin((3, 7, 45, 6.7)) => cannot promote xs:decimal to xs:integer local:secondinin((3, 7, gg, 6.7)) => text: “gg” is not an integer local:seconditit((3, 7, 45, 6.7)) => 7 local:seconditit((3, 45, 6.7)) => 45 local:seconditit((3, gg, 6.7)) => gg local:seconditit((3, 7, 45, 6)) => 7 local:seconditit((3, 7, 45, 6.7)) => 7 local:seconditit((3, 7, gg, 6.7)) => 7

79 79 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Convert all attributes to elements declare function local:convertAttribute($a as attribute()*) as element()* { for $attrib in $a let $name := name($attrib) return {data($attrib)} }; declare function local:convertElement($e as element()*) as element()* { for $el in $e let $name := name($el) return { local:convertAttribute($el/@*), if (empty($el/*)) then $el/text() else local:convertElement($el/*) } }; local:convertElement(document("transcripts.xml")//Transcript) Fails for elements with mixed (elements & text) content. q07.xq

80 80 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 111111111 John Doe CS308 F1990 B MAT123 F1997 B EE101 F1997 A CS305 F1995 A, 987654321 Bart Simpson CS305 F1995 C CS308 F1994 B

81 81 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Convert all nodes to elements q12.xq declare function local:convertNode($n as node()) as element() { typeswitch ($n) case attribute() return { data($n) } case element() return { for $attr in $n/@* return local:convertNode($attr), for $child in $n/node() return local:convertNode($child) } case text() return { data($n) } default return { data($n) } }; local:convertNode(document("mixed.xml")/mixed) Can be used with user-defined types in imported schemas Supports mixed content model

82 82 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Mixed Content Example This paragraph contains mixed content which is no more than a piece of data mixed with some tags. becomes next slide

83 83 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Mixed Content Example center This paragraph contains mixed content which is no more than a piece of data mixed with some tags.

84 84 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 7 Document Type Definitions [4] DTD is a grammar that specifies valid XML-documents; XML-documents do not need to have a DTD, nor do they need to be valid;

85 85 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 An attribute can be declared of type CDATA, ID, IDREF or IDREFS; – if attr1 and attr2 are declared of type ID then and cannot occur in the same document; – an attribute of type IDREF must refer to an ID-value in the same document; if there is an a with and a1 of type IDREF then there is a b with and a2 of type ID – an attribute of type IDREFS represents a space-separated list of references to ID-values in the same document; if there is an a with and a1 of type IDREFS then there is a b and a c with and a2 and a3 of type ID

86 86 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 ]> the order of the subelements has to be obeyed; * [0,  [, + [1,  [, ?[0,1], | alternatives; #PCDATA : (Parsed Character Data) : character string for elements (unquoted); CDATA : (Character Data) : character string for attributes (quoted); IMPLIED : optional; REQUIRED : mandatory;

87 87 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 DTD for running example 2

88 88 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 8 XML Schema [1,2] XML Schema is as a DDL for XML-documents; it describes the structure of other instance XML-documents; Advantages over DTD : –uses the same syntax as XML-documents; –integrates namespace mechanism; –built-in types; –complex types can be built from simple types; –references can be typed; –supports keys and referential integrity constraints; –same element name can have different types depending where the element name is nested; –XML data do not need to be ordered;

89 89 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 elements and attributes have types; elements that contain subelements have complex types; elements with attributes have complex types; other elements have simple types; attributes have simple types; types are given names or are anonymous; schema is defined in a schema XML document; we presume (for the moment (Cfr. later)) that the instance document is not referring to the schema document;

90 90 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Alice Smith 123 Maple Street Mill Valley CA 90952 Robert Smith 8 Oak Avenue Old Town PA 95819 Hurry, my lawn is going wild! Lawnmower 1 148.95 Confirm this is electric Baby Monitor 1 39.98 2004-12-21 Document on file ‘po.xml’, running example 3

91 91 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Purchase order schema for Example.com. Copyright 2000 Example.com. All rights reserved. Schema document on file ‘po.xsd’, running example 3

92 92 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009

93 93 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 is the XML Schema namespace; annotation gives info for human readers: Purchase order schema for Example.com. Copyright 2000 Example.com. All rights reserved. complex type example (“sequence” Cfr. later) : This is an XML element with 2 subelements. It specifies the type ‘USAddress’. All elements in the Instance document with type “USAddress’ must have a value ‘satisfying’ this type declaration. must have 5 subelements in the specified order; may have a ‘country’ attribute;

94 94 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 References an existing element ‘comment’ that must be declared as a global element (ie. children of the element). There may be comment element in the instance document ( minOccurs=“0” ). has attributes name, type, ref, minOccurs, maxOccurs ; minOccurs is a nonnegative integer, maxOccurs is a nonnegative integer or unbounded ; their default value is 1; has attributes name, type, use ; use is “required” or “optional” ; default optional

95 95 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Simple Types Built-in XML simple types: “string”, “byte”, “integer”, “long”, “decimal”, “float”, “double”, “boolean”, “dateTime”, “ID”, “IDREF”, “IDREFS”, “anyType”, … “anyType” is the universal type; Restriction of built-in simple types The element “simple type”” has a subelement “restriction” with two subelements (called facets) Regular expression Cfr. [2]

96 96 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Which facets can be combined with which built-in simple types, Cfr. [2]. List types: lists of built-in simple types or restrictions of built-in simple types 20003 15037 95977 95945 is an element of type “listOfMyIntType”.

97 97 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Several facets can be applied to list types: length, minlength, maxlength. PA NY CA NY LA AK is an element of type “SixUSStates”. Remark that a space delimites the elements of a list. Hence Paredaens Van Sant is not an element of type twoNames

98 98 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Union types the value of elements or attributes with a union type has a type drawn from the union of multiple built-in types, restrictions or list types. CA is of type “zipUnion” 95630 95977 95945 is of type “zipUnion” AK 78997 is NOT of type “zipUnion” AK CA is NOT of type “zipUnion”

99 99 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Types can be named (and declared separately) or anonymous. Anonymous Complex Types declaring elements with only attributes: is of the type above. Complex Types

100 100 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Groups of elements “sequence”: an ordered set of elements and choices; “choice”: one element of the given set of elements and sequences; “all”: an unordered set of elements; “sequence” within a “choice” and “choice” within a “sequence” can have “minOccurs” and “maxOccurs” attributes. “sequence”: can only contain elements or choice-groups; they have to occur (taking into account “minOccurs” and “maxOccurs”) in the given order; “minOccurs” must be nonneg. (default 1), “maxOccurs” must be nonneg. or “unbounded” (default 1)

101 101 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009

102 102 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 “choice”: can only contain elements or sequence-groups; only at most one can occur (taking into account “minOccurs” and “maxOccurs”); “minOccurs” must be nonneg. (default 1), “maxOccurs” must be nonneg. or “unbounded” (default 1)

103 103 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 “all”: can only contain elements; they occur in an arbitrary order (taking into account “minOccurs” and “maxOccurs”); “minOccurs” must be 0 or 1 (default 1), “maxOccurs” must be 1 Illegal:

104 104 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 mixed: allows text appearing between elements and their child elements ; Dear Mr. Robert Smith. Your order of 1 Baby Monitor shipped from our warehouse on 2004-12-21..... is declared in the way above.

105 105 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 General form of an anonymous complex type (choice|all|sequence) ( )* … is of the type above.

106 106 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Named Types and Groups …

107 107 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 XML Schema and Namespaces An XML schema-document starts with the declaration of namespaces : –http://www.w3.org/2001/XMLSchema gives the names of the tags, attributes, types in the schema-document ex.: schema, attribute, element, … –target namespace gives the names defined by the schema-document ex.: CrsTaken, Student, Status, …

108 108 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 … running example 3

109 109 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Alice Smith 123 Maple Street Mill Valley CA 90952 Robert Smith 8 Oak Avenue Old Town PA 95819 Hurry, my lawn is going wild! Lawnmower 1 148.95 Confirm this is electric Baby Monitor 1 39.98 2004-12-21 running example 3 In the corresponding XML instance-documents we first declare the target namespace of the schema

110 110 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Key - Refkey......

111 111 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009...

112 112 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Each state, within the document, has a different code child value: element information item: target node set: - elements within the - element key-sequence: for each such state, its - child value The selector, with the element information item as the context node, evaluates to a node-set. Call this the target node set. For each node in the target node set all of the fields, with that node as the context node, evaluate to exactly one member, which must have a simple type. Call the sequence of values of the element and/or attribute information items in those node-sets in order the key-sequence of the node. Key. No two members of the target node set have key- sequences whose members are pairwise equal.

113 113 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Each vehicle within the document has a different state-platenumber pair: element information item: target node set: - elements within the - element key-sequence: for each such vehicle, its “state” and “plateNumber” attribute value Each vehicle has a different platenumber attrib. value within each state: element information item: target node set: - elements within the - element key-sequence: for each such vehicle, its “plateNumber” attribute value

114 114 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Each state attribute value of a vehicle within the document must be the code value of a state within the document: element information item: referenced key: “state” target node set: - elements within the - element key-sequence: for each such vehicle, its “state” attribute value target node set of “state” : - elements within the - element key-sequence: for each such state, its - child value Keyref. For each member m of the target node set, there must be a member m r in the target set of the referenced key with key-sequence of m = key-sequence of m r.

115 115 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Each (regState attribute, regPlate attribute) value of a car within the document, must be a (state attribute, plateNumber attribute) value of a car within the document: element information item: referenced key: “regKey” target node set: - elements within the - element key-sequence: for each such car, its “regState” and “regPlate” attribute value target node set of “regKey” : - elements within the - element key-sequence: for each such vehicle, its “state” and “plateNumber” attribute value

116 116 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Running Example 2

117 117 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009

118 118 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 …

119 119 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009

120 120 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009

121 121 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Abstract Example

122 122 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 E1 { E2 E6 String E7 { E8 String } > } The following are legal key declarations of E1 The following are illegal key declarations of E1 not one member the value of E3 has no simple type the selector-path does not start in E1 the field-path does not start in E2

123 123 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 E1 { E2 E6 String E7 { E8 String } > } The following are legal foreign key declarations of E1 The following are illegal foreign key declarations of E1 E4 has no simple type the field-path does not start in E2

124 124 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 9 Light XQuery Concise backwards compatible sublanguage of XQuery Complete formal description in a couple of pages More info at: http://www.adrem.ua.ac.be/~lixquery http://www.adrem.ua.ac.be/~lixquery

125 125 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 9 XSLT eXtensible Stylesheet Language Transformations; XSLT is a stylesheet language; Mainly used to translate XML into HTML; Turing Complete; Templates (procedures) are central; Templates can have (input) parameters; Templates write on a variable or on the output; Templates are called or ‘match’; An XSLT stylesheet is an XML document; Uses namespaces; Syntax is influenced by stylesheet people;

126 126 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Syntax van MiXSLT

127 127 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009

128 128 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 BUT ROME WAS NOT BUILT IN 1 DAY. BUT ROME WAS NOT BUILT IN 1 DAY. Output Document Statements : 1. Text XSLT-file

129 129 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 BUT ROME WAS NOT JUST COULD NOT BUILT IN 1 DAY. BUT ROME WAS NOT JUST COULD NOT BUILT IN 1 DAY. Remark the blanc space! …  … 2. Element

130 130 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Ma Roma non fu fatta in un giorno. non un non un Giovannib Input Document 3. Value-of

131 131 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The xsl:value-of element is instantiated to create a text node in the result tree. The required select attribute is an XPath-expression; this expression is evaluated and the resulting object is converted to a string as if by a call to the string function. The string specifies the string-value of the created text node. If the string is empty, no text node will be created. The created text node will be merged with any adjacent text nodes.

132 132 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 4. Copy-of Ma Roma non fu fatta in un giorno. non un non un b 123

133 133 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The xsl:copy-of element can be used to insert a result tree fragment into the result tree, without first converting it to a string as xsl:value-of does. The required select attribute contains an expression. When the result of evaluating the expression is a result tree fragment, the complete fragment is copied into the result tree. When the result is a node-set, all the nodes in the set are copied in document order into the result tree; copying an element node copies the attribute nodes and children of the element node as well as the element node itself; a root node is copied by copying its children. When the result is neither a node-set nor a result tree fragment, the result is converted to a string and then inserted into the result tree, as with xsl:value-of.

134 134 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 5. For-each Ma Roma non fu fatta in un giorno. AA BB ababccaccbccb AA BB Ma BB Roma non fu fatta in un BB giorno. BB Ma Roma non fu fatta in un giorno. Current node!

135 135 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The xsl:for-each instruction contains a template, which is instantiated for each node selected by the expression specified by the select attribute. The select attribute is required. The expression must evaluate to a node-set. The template is instantiated with the selected node as the current node, and with a list of all of the selected nodes as the current node list.

136 136 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 6. Copy Ma Roma non fu fatta in un giorno. -- --

137 137 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The xsl:copy element provides an easy way of copying the current node. Instantiating the xsl:copy element creates a copy of the current node. The attributes and children of the node are not automatically copied. The content of the xsl:copy element is a template for the attributes and children of the created node; the content is instantiated only for nodes of types that can have attributes or children (i.e. root nodes and element nodes).

138 138 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 7. If No else ‘ … ‘ within “ … “ current node! Ma Roma non fu fatta in un giorno. 123 234 345 456 567 678 789 890 901 012 123 345 678 789 901 012

139 139 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The xsl:if element has a test attribute, which specifies an XPath-expression. The content is a template. The expression is evaluated and the resulting object is converted to a boolean as if by a call to the boolean function. If the result is true, then the content template is instantiated; otherwise, nothing is created.

140 140 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 8. Variable 123 ABC Roma non fu fatta in un 123 ABC Roma non Roma non fu fatta in un 123 ABC Roma non Roma in ABC non

141 141 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 ,,,,, 100, 200, 100, 120, 100, Ma Roma non fu fatta in un giorno.

142 142 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 A variable is a name that may be bound to a value. For these variable-binding elements, there is a region of the stylesheet tree within which the binding is visible; within this region, any binding of the variable that was visible on the variable-binding element itself is hidden. Thus, only the innermost binding of a variable is visible. The set of variable bindings in scope for an expression consists of those bindings that are visible at the point in the stylesheet where the expression occurs.

143 143 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 9. Call-template Ma Roma non fu fatta in un giorno. CCDD EEFF GGHH AABB CCDD AABB EEFF Ma GGHH Ma

144 144 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Templates can be invoked by name. An xsl:template element with a name attribute specifies a named template. An xsl:call-template element invokes a template by name; it has a required name attribute that identifies the template to be invoked. An xsl:call-template does not change the current node nor the current node list.

145 145 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 10. Parameters Ma Roma non fu fatta in un giorno. CCDD GGHH EEFF MMNN OOPP { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/8/2174677/slides/slide_145.jpg", "name": "145 Deel II XML © Prof.dr. J. Paredaens 2008 - 2009 10.", "description": "Parameters Ma Roma non fu fatta in un giorno. CCDD GGHH EEFF MMNN OOPP

146 146 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 A parameter is a name that may be bound to a value. Parameters are passed to templates using the xsl:with-param element. The required name attribute specifies the name of the parameter. Every parameter in an xsl:with-param has to be declared by an xsl:param in the called template. The value of the parameter is specified in xsl:with-param in the same way as in xsl:variable. The current node and current node list used for computing the value specified by xsl:with-param element is the same as that used for the xsl:apply-templates or xsl:call-template element within which it occurs. It is not an error to pass a parameter x to a template that does not have an xsl:param element for x; the parameter is simply ignored. The parameters are only visible within the template in which they are declared.

147 147 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Global product Cannot be done by a for-each 1]"/> 288

148 148 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 11. Template - Apply-templates M4M M7M M6M M8M NN k4NN k5NN k7NN M8M

149 149 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 A template has a name-attribute or a match-attribute. Those with a name are called with call-template. The others are called with apply-templates. The value of the match-attribute of a template is a pattern p. It indicates that the template can only be called for nodes n for which there is a node n’ such that n belongs to the sequence that results from the application of p to n’. It is an error for the value of the match attribute to contain a variable. The content of the template element is instantiated when the template is called.

150 150 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The value of the select attribute of apply-templates is an XPath-expression. The expression must evaluate to a node-set. This set is processed in document order. To process a node n means that n becomes the current node and that a template with a match-attribute is called for n. If no such template exists, all the children of n are processed in document order, if n is not a leaf. If n is a leaf its content is instantiated. If more such templates exist, the template with the highest priority is selected. The default priority is 0. If more then one template has the highest priority there is an ambiguity. If the apply-template has a mode-attribute, only templates are called with a mode-attribute with the same value. If the apply-template does not have a mode-attribute, only templates are called with no mode-attribute.

151 151 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 M4M M7M M6M M8M NN LL KK k4NN k5NN k7LL M8M k2KK M6M k8KK

152 152 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Ma Roma non fu fatta in un giorno. AA BB CC AA BB AA BB CC CC AA CC CC BB CC CC BB 12. Choose – when - otherwise

153 153 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The xsl:choose element selects one among a number of possible alternatives. It consists of a sequence of xsl:when elements followed by an optional xsl:otherwise element. Each xsl:when element has a single attribute, test, which specifies an expression. The content of the xsl:when and xsl:otherwise elements is a template. When an xsl:choose element is processed, each of the xsl:when elements is tested in turn, by evaluating the expression and converting the resulting object to a boolean as if by a call to the boolean function. The content of the first, and only the first, xsl:when element whose test is true is instantiated. If no xsl:when is true, the content of the xsl:otherwise element is instantiated. If no xsl:when element is true, and no xsl:otherwise element is present, nothing is created.

154 154 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 13. Key Ma un Roma non si un uno noon fu un non fatta in un uni non un si un giorno. CC DD EE FF GG HH II AA BB { "@context": "http://schema.org", "@type": "ImageObject", "contentUrl": "http://images.slideplayer.com/8/2174677/slides/slide_154.jpg", "name": "154 Deel II XML © Prof.dr. J. Paredaens 2008 - 2009 13.", "description": "Key Ma un Roma non si un uno noon fu un non fatta in un uni non un si un giorno. CC DD EE FF GG HH = 3 and position() <= 4]) /> II AA BB

155 155 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 AAun BB CC AAun BB DD AAun BB AARoma BB AAfu BB AAun BB AAin BB AAun BB AAuni BB EE AAsi BB FF AAsi BB GG AAsi BB AAfu BB HH AAun BB AARoma BB AAnon BB AAfu BB AAun BB AAin BB AAun BB AAuni BB AAnon BB AAun BB II

156 156 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 The xsl:key element is used to declare keys. The name attribute specifies the name of the key. The match attribute is a Pattern; the use attribute is an expression specifying the values of the key; the expression is evaluated once for each node that matches the pattern. It is an error for the value of either the use attribute or the match attribute to contain a VariableReference. The first argument of the key function specifies the name of the key. The value of the argument must be a name. The second argument is converted to a set of strings; the key function returns a node-set containing the nodes in the same document as the context node that have a value for the named key equal to one of these strings.

157 157 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 Examples 1. Composition C) of R in Tables.in.xml. --> D) of T in Tables.in.xml. -->

158 158 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 .

159 159 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009.

160 160 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 . 1]"/> 2. Two heads

161 161 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009...

162 162 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 3. return value of a template

163 163 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009


Download ppt "1 Deel II XML © Prof. dr. J. Paredaens 2008 - 2009 DEEL II XML."

Similar presentations


Ads by Google