Dr. Alexandra I. Cristea XML.

Slides:



Advertisements
Similar presentations
XML-XSL Introduction SHIJU RAJAN SHIJU RAJAN Outline Brief Overview Brief Overview What is XML? What is XML? Well Formed XML Well Formed XML Tag Name.
Advertisements

Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: XPath, NameSpaces.
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C2.
XML: Extensible Markup Language
XML Craig Stewart Dr. Alexandra I. Cristea
Dr. Alexandra I. Cristea XPath and Namespaces.
SPECIAL TOPIC XML. Introducing XML XML (eXtensible Markup Language) ◦A language used to create structured documents XML vs HTML ◦XML is designed to transport.
An Introduction to XML Based on the W3C XML Recommendations.
XQuery John Annechino Steven Pow. Agenda What is XQuery? Uses of XQuery XQuery vs. XSLT Syntax –Built-In Functions –FLWOR –if-then-else –User-Defined.
XQuery Or, what about REAL databases?. XQuery - its place in the XML team XLink XSLT XQuery XPath XPointer.
XQUERY. What is XQuery? XQuery is the language for querying XML data The best way to explain XQuery is to say that XQuery is to XML what SQL is to database.
1 XQuery Web and Database Management System. 2 XQuery XQuery is to XML what SQL is to database tables XQuery is designed to query XML data What is XQuery?
IS 373—Web Standards Todd Will
Sistemi basati su conoscenza XML Prof. M.T. PAZIENZA a.a
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C1.
1 COS 425: Database and Information Management Systems XML and information exchange.
Sistemi basati su conoscenza XML Prof. M.T. PAZIENZA a.a
Introduction to XML: Yong Choi CSU Bakersfield.
Dr. Alexandra I. Cristea CS 253: Topics in Database Systems: C2.
Introduction to XML This material is based heavily on the tutorial by the same name at
Manohar – Why XML is Required Problem: We want to save the data and retrieve it further or to transfer over the network. This.
Introduction to XPath Bun Yue Professor, CS/CIS UHCL.
ECA 228 Internet/Intranet Design I Intro to XML. ECA 228 Internet/Intranet Design I HTML markup language very loose standards browsers adjust for non-standard.
Pemrograman Berbasis WEB XML part 2 -Aurelio Rahmadian- Sumber: w3cschools.com.
XML introduction to Ahmed I. Deeb Dr. Anwar Mousa  presenter  instructor University Of Palestine-2009.
What is XML? XML stands for EXtensible Markup Language
CREATED BY ChanoknanChinnanon PanissaraUsanachote
1Computer Sciences Department Princess Nourah bint Abdulrahman University.
Pemrograman Berbasis WEB XML -Aurelio Rahmadian- Sumber: w3cschools.com.
TDDD43 XML and RDF Slides based on slides by Lena Strömbäck and Fang Wei-Kleiner 1.
CSE3201/CSE4500 Information Retrieval Systems
XP 1 CREATING AN XML DOCUMENT. XP 2 INTRODUCING XML XML stands for Extensible Markup Language. A markup language specifies the structure and content of.
1 XPath XPath became a W3C Recommendation 16. November 1999 XPath is a language for finding information in an XML document XPath is used to navigate through.
1 © Netskills Quality Internet Training, University of Newcastle Introducing XML © Netskills, Quality Internet Training University.
1 CIS336 Website design, implementation and management (also Semester 2 of CIS219, CIS221 and IT226) Lecture 6 XSLT (Based on Møller and Schwartzbach,
What is XML?  XML stands for EXtensible Markup Language  XML is a markup language much like HTML  XML was designed to carry data, not to display data.
 XML is designed to describe data and to focus on what data is. HTML is designed to display data and to focus on how data looks.  XML is created to structure,
Processing of structured documents Spring 2002, Part 2 Helena Ahonen-Myka.
XML TUTORIAL Portions from w3 schools By Dr. John Abraham.
How do I use HTML and XML to present information?.
XML eXtensible Markup Language. Topics  What is XML  An XML example  Why is XML important  XML introduction  XML applications  XML support CSEB.
Windows Presentation Foundation (WPF) Chapter 16 Dr. Abraham.
1 Dr Alexiei Dingli XML Technologies XML. 2 XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry.
Waqas Anwar Next SlidePrevious Slide. Waqas Anwar Next SlidePrevious Slide XML XML stands for EXtensible Markup Language.
WEB APPLICATION DEVELOPMENT For More visit:
Introduction to XML This presentation covers introductory features of XML. What XML is and what it is not? What does it do? Put different related technologies.
An Introduction to XML Sandeep Bhattaram
XML Introduction. What is XML? XML stands for eXtensible Markup Language XML stands for eXtensible Markup Language XML is a markup language much like.
What it is and how it works
XML Introduction. Markup Language A markup language must specify What markup is allowed What markup is required How markup is to be distinguished from.
XPath Presented by Kushan Athukorala. 2 Agenda XPath XPath Terminology Selecting Nodes Predicates.
Dr. Alexandra I. Cristea XML.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 4 1COMP9321, 15s2, Week.
Submitted To: Ms. Poonam Saini, Asst. Prof., NITTTR Submitted By: Rohit Handa ME (Modular) CSE 2011 Batch.
IS444: Modern software development tools Dr. Azeddine Chikh.
IS432 Semi-Structured Data Lecture 4: XPath Dr. Gamal Al-Shorbagy.
XML. HTML Before you continue you should have a basic understanding of the following: HTML HTML was designed to display data and to focus on how data.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
1 XPath. 2 Agenda XPath Introduction XPath Nodes XPath Syntax XPath Operators XPath Q&A.
C Copyright © 2011, Oracle and/or its affiliates. All rights reserved. Introduction to XML Standards.
 XML derives its strength from a variety of supporting technologies.  Structure and data types: When using XML to exchange data among clients, partners,
Jackson, Web Technologies: A Computer Science Perspective, © 2007 Prentice-Hall, Inc. All rights reserved Chapter 7 Representing Web Data:
XML Introduction to XML Extensible Markup Language.
XML Schema – XSLT Week 8 Web site:
1 XSL Transformations (XSLT). 2 XSLT XSLT is a language for transforming XML documents into XHTML documents or to other XML documents. XSLT uses XPath.
XML Notes taken from w3schools. What is XML? XML stands for EXtensible Markup Language. XML was designed to store and transport data. XML was designed.
XML. What is XML? XML stands for EXtensible Markup Language XML is a markup language much like HTML XML was designed to carry data, not to display data.
Extensible Markup Language (XML) Pat Morin COMP 2405.
Unit 4 Representing Web Data: XML
Chapter 7 Representing Web Data: XML
Presentation transcript:

Dr. Alexandra I. Cristea XML

2 XML history Inception: circa 1996 The Extensible Markup Language (XML) became a W3C Recommendation 10. February It’s being used currently in very many places – see HESAHESA

3 What is XML? XML stands for EXtensible Markup Language XML was designed to describe data XML is more of a standard and supporting structure than a standalone programming language XML is a markup language much like HTML – wrong!: meta-language

4 How does XML work? XML tags are not predefined. You must define your own tags XML uses a Document Type Definition (DTD) or an XML Schema to describe the data XML with a DTD or XML Schema is designed to be self-descriptive

5 XML is Free and Extensible XML tags are not predefined. You must "invent" your own tags. The tags used to mark up HTML documents and the structure of HTML documents are predefined. The author of HTML documents can only use tags that are defined in the HTML standard (like,, etc.). XHTML is XML but not vice-versa.

6 XML does not DO anything XML was created to structure, store and to send information John Jane Reminder Don't forget the book!

7 Main Difference XML, HTML XML was designed to carry data. XML is not a replacement for HTML. XML and HTML were designed with different goals: –XML was designed to describe data and to focus on what data is. –HTML was designed to display data and to focus on how data looks. HTML is about displaying information, while XML is about describing information. Syntax: XML is well formed, just like XHTML

8 XML is a Complement to HTML XML is not a replacement for HTML. –In Web development XML is used to describe the data, while HTML is used to format and display the same data. XML is a cross-platform, software and hardware independent tool for transmitting information.

9 Benefits XML extensibility and structured nature of XML allows it to be used for communication between different systems from one source of XML-based information you can format and distribute it via a multitude of different channels – XSL files act as templates, allowing a single stylesheet to be used to format multiple pages or the same content for multiple distribution channels

10 XML in Web Development XML is everywhere. the XML standard has been developed quickly and a large number of software vendors have adopted it. XML might be the most common tool for all data manipulation and data transmission.

11 XML Can be Used to Create New Languages XML is the mother of WAP and WML. –WAP: standard for web browser for mobile devices –The Wireless Markup Language (WML), used to markup Internet applications for handheld devices like mobile phones, is written in XML. And many others … search for more as homework

12 Question: When should I use XML? Answer: When you need a buzzword in your resume.

13 Viewing XML to view XML documents hierarchically or view their output, you need an XML parser and processor. there are a number of these tools available: See examples at: Please note, however: XML was not designed to display data.

14 The basic XML flow

XML-based languages RSS Twitter API MathML SVG SOAP WSDL Microsoft Office (pptx, docx, xlsx) Open Office XML SMIL RDF 15

16 XML Rules 1.Every start-tag must have a matching end-tag. 2.Tags cannot overlap. Proper nesting is required. 3.XML documents can only have one root element. 4.Element names must obey the following XML naming conventions: a)Names must start with letters or the "_" character. Names cannot start with numbers of punctuation characters. b)After the first character, numbers and punctuation characters are allowed.

17 XML Rules (cont.) c)Names cannot contain spaces. d)Names should not contain the ":" character as it is a "reserved" character. e)Names cannot start with the letters "xml" in any combination of case. f)The element name must come directly after the "<" without any spaces between them. 5.XML is case sensitive. 6.XML preserves white space within text. 7.Elements may contain attributes. If an attribute is present, it must have a value, even if it is an empty string "".

18 Spot the error! Tove Jani

19 Spot the error! Tove Jani

20 With XML, CR / LF is converted to LF Windows: CR + LF Unix: LF Macintosh: CR

21 There is Nothing Special About XML plain text w XML tags Software that can handle plain text can also handle XML. In an XML-aware application, the XML tags can be handled specially: –Visibility, –Functional meaning, etc.

22 Is this an error? Tove Jani Don't forget me this weekend! Reminder

23 XML Elements have Relationships Elements are related as parents and children. Root element / Parents Children / Siblings

24 Elements An element consists of all the information from the beginning of a start-tag to the end of an end-tag including everything in between. E.g. from (X)HTML, all of the following would be the equivalent of one element, named h1: This is a heading. –Where, is the start tag, is the end tag, and the content is in between. Each XML document has a root element within which all other elements are nested.

25 Examples See at: – urses/CS253/2009/books.xmlhttp:// urses/CS253/2009/books.xml – –Search more by yourself and familiarize yourself with the syntax!

26 XML Attributes XML elements can have attributes. From HTML you will remember this: The SRC attribute provides additional information about the IMG element.

27 Attributes versus Elements Anna Smith female Anna Smith

28 Comments same as in any other languages with line(s) of code whose sole purpose is to provide the developer, and anyone reading the code in the future, information about the code.

29 XML Validation: Well Formed-ness An XML document is well formed, if all the XML rules are obeyed. (with 7 XML rules as defined in slides 16-17)

30 XML declaration Every XML document begins with a declaration (not mandatory, good practice) Or, using optional attributes:

31 Document Type Definition (DTD) which tags and attributes are allowed, where they can be placed, and whether or not they can be nested within a given document.

32 Document Type Declaration (DOCTYPE) Root document element URL to DTD (external subset via a system identifier)

33 Internal vs External DTD declaration Internal: ]> External, public:

34 Valid XML Documents A "Valid" XML document is a "Well Formed" XML document, which also conforms to the rules of a Document Type Definition (DTD): Tom Jane Reminder Don't forget me this weekend!

35 Validators Also at:

36 Internal DTD <!DOCTYPE note [ ]> Tove Jani Reminder Don't forget me this weekend!

37 External DTD >> saved as file Note.dtd

Attributes in DTDs Syntax: Example: Usage (in XML doc): 38 Attribute-value can be an actual value, or #REQUIRED or #IMPLIED or even #FIXED value

39 XML Schema (XSD) XML Schema is an XML based alternative to DTD. W3C supports an alternative to DTD called XML Schema:

40 Displaying your XML Files with CSS? It is possible to use CSS to format an XML document. Example: XML file: The CD catalogThe CD catalog style sheet: The CSS fileThe CSS file product: The CD catalog formatted with the CSS fileThe CD catalog formatted with the CSS file Below is a fraction of the XML file. The second line,, links the XML file to the CSS file

41 Displaying XML with XSL XSL is the preferred style sheet language of XML. XSL (the eXtensible Stylesheet Language) is far more sophisticated than CSS. examples: –View the XML file, the XSL style sheet, and View the result.View the XML filethe XSL style sheetView the result

42 XML Conclusions We have learned: –XML history –What it is –How it works –Differences to (X)HTML –XML flow –XML Rules –XML Elements, Relationships, Attributes, Comments –Well-formed-ness concept –XML supporting frame: XML Schema or DTD –Generics on displaying XML

43 Why an XML Editor? XML Schema to define XML structures and data types XSLT to transform XML data SOAP to exchange XML data between applications WSDL to describe web services RDF to describe web resources XPath and XQuery to access XML data SMIL to define graphics Altova's XMLSpy –30 days free trial – –

44 Next: –We look at how to access elements and attributes inside the XML –This can be done via … –XPATH

45 Previously we looked at: –XML Next: –XPath –Namespaces

Validity versus well-formedness Checking for well-formedness: – – Checking for validity: – As a homework, check the validity and well- formedness of various XML documents in this course and on the web. Pay attention to the difference. 46

47 XPath

48 XPath XPath is a syntax for defining parts of an XML document XPath uses path expressions to navigate in XML documents XPath contains a library of standard functions XPath is a major element in XSLT XPath is a W3C recommendation, thus a Standard (16. November 1999; newest Dec )

49 XPath Path Expressions Uses path expressions to select nodes or node-sets in an XML document. –These path expressions look very much like the expressions you see when you work with a traditional computer file system.

50 XPath Standard Functions over 100 built-in functions. –string values, –numeric values, –date and time comparison, –node and QName manipulation, –sequence manipulation, –Boolean values, –and more.

51 XPath Terminology Nodes Atomic values Items (atomic values or nodes) Relationships of nodes –Parent –Children –Siblings –Ancestors –Descendants

52 XPath Nodes 7 kinds of nodes: 1.element, 2.attribute, 3.text, 4.namespace, 5.processing-instruction, 6.comment, and 7.document (root) nodes. XML documents are treated as trees of nodes. The root of the tree is called the document node (or root node).

53 Nodes Examples Harry Potter J K. Rowling Document (root) nodeElement node Attribute node

54 Atomic values Examples* Harry Potter J K. Rowling *nodes with no children or parent

55 Selecting nodes ExpressionDescription nodenameSelects all child nodes with this name / Selects from the root node // Selects nodes in the document from the current node down that match the selection no matter where they are. Selects the current node.. Selects the parent of the current Selects attributes

56 Examples of selecting nodes Path ExpressionResult bookstoreSelects all the bookstore elements /bookstoreSelects the root element bookstore Note: If the path starts with a slash ( / ) it always represents an absolute path to an element! bookstore/bookSelects all book elements that are children of bookstore //book bookstore//book all attributes that are named lang Selects all book elements no matter where they are in the document Selects all book elements that are descendant of the bookstore element, no matter where they are under the bookstore element

57 Predicates Predicates are used to find a specific node or a node that contains a specific value. Predicates are always embedded in square brackets.

58 Example predicates Path ExpressionResult /bookstore/book[1]Selects the first book element that is the child of the bookstore element /bookstore/book[last()]Selects the last book element that is the child of the bookstore element /bookstore/book[last()-1] /bookstore/book[position()<3] Selects the last but one book element that is the child of the bookstore element Selects the first two book elements that are children of the bookstore element

59 Example predicates – cont. Path ExpressionResult all the title elements that have an attribute named lang all the title elements that have an attribute named lang with a value of 'eng' /bookstore/book[price>35.00] /bookstore/book[price>35.00]/title Selects all the book elements of the bookstore element that have a price element with a value greater than Selects all the title elements of the book elements of the bookstore element that have a price element with a value greater than 35.00

60 Selecting Unknown Nodes WildcardDescription *Matches any element any attribute node node()Matches any node of any kind

61 Example: selecting several paths Path ExpressionResult //book/title | //book/price Selects all the title as well as price elements of all book elements //title | //price /bookstore/book/title | //price Selects all the title as well as price elements in the document Selects all the title elements of the book element of the bookstore element as well as all the price elements in the document

62 XPath Axes self childparent ancestordescendant ancestor-or-selfdescendant-or- self preceding-siblingfollowing-sibling precedingfollowing attribute namespace

63 axisname::nodetest[predicate] //DDD/parent::*

64 axisname::nodetest[predicate] //BBB/child::* Note: /AAA is equivalent to /child::AAA

65 More examples Check basics, //, *, predicates, attributes, functions (new ones: count, name, normalize-space, starts- with, contains, string-length, floor, ceiling), axes, operators (mod)predicatesattributescountnamenormalize-spacestarts- withcontainsstring-lengthfloorceilingaxes operators (mod) –Note: The ancestor, descendant, following, preceding and self axes partition a document (ignoring attribute and namespace nodes): they do not overlap and together they contain all the nodes in the document. (see example)example

XPath Try-out XPath online: tester.html#ad-output tester.html#ad-output You can use an existing XML: – 3/2008/books.xmlhttp:// 3/2008/books.xml Use XPath snippets from module or create your own: –//title | //price –/bookstore/book[last()] –/bookstore/book[last()-1] –/bookstore/book[position()<3] etc. 66

67 XPath Conclusion We have learned: –XPath definition –Path expressions –Standard functions –Terminology –Predicates –Location paths –Axes –Some operators

68 Before we go on, one more thing about XML: XML Namespaces

69 Naming ambiguity

70 The Idea to Solve it Assign a URI (~ URL) to every sub- language: –E.g., for XHTML 1.0: Qnames: Qualify element names with URIs: –{ Web Naming and Addressing Overview (URIs, URLs,...)

71 The actual solution Namespace declarations bind URIs to prefixes: Default namespace (no prefix) declared with: xmlns=“…” Lexical Scope Attribute names can also be prefixed

72 Applying namespaces

73 Next we look at how to query XML This can be done, to some extent, as we have seen, within XPath (or XSLT), but the main language developed for this purpose is …

74 XQuery

75 Previously we looked at: –XPath –Namespaces Next: –XQuery

76 What is XQuery? XQuery is the language for querying XML data XQuery for XML is like SQL for databases XQuery is built on XPath expressions XQuery is defined by the W3C XQuery is supported by all the major database engines (IBM, Oracle, Microsoft, etc.) XQuery is a W3C recommendation (Jan 2007; latest 14 Dec 2010) thus a standard

77 XQuery - Examples of Use Extract information to use in a Web Service Generate summary reports Transform XML data to XHTML Search Web documents for relevant information

78 XQuery compared to XPath XQuery 1.0 and XPath 2.0 share the same data model and support the same functions and operators. XQuery 1.0 is a strict superset of XPath 2.0  XPath 2.0 expression is directly an XQuery 1.0 expression (a query) The extra expressive power is the ability to: –Join information from different sources and –Generate new XML fragments

79 XQuery ‘compilers’ Download: Syntax check at: XPath, XQuery tester:

80 XQuery query makeup Prolog –Like XPath, XQuery expressions are evaluated relatively to a context –explicitly provided by a prolog (header) ~ header with definitions Body –The actual query Select Join Generate

81 XQuery Ex.: Prolog + Query

82 XQuery Prolog (i.e., header(s)) Settings define various parameters for the XQuery processor language, such as: xquery version "1.0"; declare base-uri " declare default element namespace " declare namespace xs= " import module " at "logo.xq"; declare variable $x as xs:integer := 7; declare function addLogo($root as node()) as node()*{ }; (: etc :)

Module definition xquery version "1.0"; module namespace mylib = " " ; declare variable $mylib:foo as xs:string := " foo " ; declare function mylib:foobar() as xs:string { concat ($mylib:foo, " bar " ) }; 83

84 Body: Constructors Direct constructors in XQuery: my fragment –Evaluates to the given XML fragment

85 Explicit constructors computed constructors

86 Variable bindings (implicit constructors) {$name} {$job} {$deptno} {$SGMLspecialist }

87 How to Select Nodes with XQuery? Functions –XQuery uses functions to extract data from XML documents. (X)Path Expressions –XQuery uses path expressions to navigate through elements in an XML document. Predicates –XQuery uses predicates to limit the extracted data from XML documents.

88 Functions doc() –function to open a file Example: –doc("books.xml") Note: A call to a function can appear where an expression may appear.

89 Path Expressions Example: select all the title elements in the "books.xml" file: doc("books.xml")/bookstore/book/title

90 Predicates Example: select all the book elements under the bookstore element that have a price element with a value that is less than 30 : doc("books.xml")/bookstore/book[price<30]

91 At a glance: function, path, predicate

92 FLWOR For, Let, Where, Order by, Return = main engine ~ SQL syntax (SFW(GH)O) ~ programs and function calls

93 FLWOR by comparison with Path expressions select all the title elements under the book elements that are under the bookstore element that have a price element with a value that is higher than 30. Path expression : doc("books.xml")/bookstore/book[price>30]/title FLWOR expression : for $x in doc("books.xml")/bookstore/book where $x/price>30 return $x/title

94 Sorting in FLWOR for $x in doc("books.xml")/bookstore/book where $x/price>30 order by $x/title return $x/title

95 Present the Result as HTML List { for $x in doc("books.xml")/bookstore/book/title order by $x return {$x} }

96 Result HTML List Everyday Italian Harry Potter Learning XML XQuery Kick Start

97 Eliminate element (here: title) { for $x in doc("books.xml")/bookstore/book/title order by $x return {data($x)} (: also text{} :) }

98 New result HTML List Everyday Italian Harry Potter Learning XML XQuery Kick Start

99 Another FLWOR Expression { for $s in doc("students.xml")//student let $m := $s/major where count($m) ge 2 order by return { $s/name/text()} }

100 The Difference between for and let

101 The Difference between for and let := in

102 The Difference between for and let

103 The Difference between for and let

104 FLWOR Basic Building Blocks

105 General rules for and let may be used many times in any order only one where is allowed many different sorting criteria can be specified (descending, ascending, etc.)

106 Reversing order Reverses the order of a sequence, for nodes or atomic values reverse (( 1, 2, 3)) -> 321

107 Joining documents for $p in doc(" for $n in doc("neighbors.xml")//neighbor[ssn = $p/ssn] return { $p/ssn } { $n/name } { $p/income }

108 Two-way join in a where Clause for $item in doc(“ord.xml”)//item, $product in doc(“cat.xml”)//product where = $product/number return <item name=“{$product/name}” />

109 Aggregating Make summary calculations on grouped data Functions: –sum, avg, max, min, count

110 Conditionals for $b in doc(“bib.xml”)/book return {$b/title} {if ( count($b/author) and others ) }

111 Nesting Conditional Expressions Conditional expressions can be nested ‘else if’ functionality is provided if ( count($b/author) = 1 ) then $b/author else if (count($b/author) = 2 )then (:.. :) else ( $b/author[1], and others )

112 Logical Expressions and, or operators: –and has precedence over or –Parentheses can change precedence if ($isDiscounted and ($discount > 5 or $discount < 0 ) ) then 5 else $discount not function for negations: if (not($isDiscounted)) then 0 else $discount

113 XQuery Built-in Functions XQuery function namespace URI is: default prefix: fn:. E.g.: fn:string() or fn:concat(). fn: is the default prefix of the namespace, the function names does not need to be prefixed when called.

114 Built-in Functions String-related –substring, contains, matches, concat, normalize- space, tokenize Date-related –current-date, month-from-date, adjust-time-to- timezone Number-related –round, avg, sum, ceiling Sequence-related –index-of, insert-before, reverse, subsequence, distinct-values

115 Built-in Functions (2) Node-related –data, empty, exists, id, idref Name-related –local-name, in-scope-prefixes, QName, resolve- QName Error handling and trapping –error, trace, exactly-one Document and URI-related –collection, doc, root, base-uri

116 Function calls doc("books.xml")//book[substring(title,1,5)='Harry'] let $name := (substring($book/title,1,4)) {upper-case($book/title)}

117 for $x in doc(" stea/courses/CS253/2009/books.xml")// book/title for $y in data($x) for $name in (substring($y,1,4)) return $name

118 User Defined Functions declare function prefix:function_name($parameter AS datatype) AS returnDatatype { (:...function code here... :) };

119 User-defined Functions declare function depth($e AS xsd:integer) AS xsd:integer { if (empty($e/*) then 1 else max(for $c in $e/* return depth($c)) ) +1 }; (: usage :) for $b in doc(“bib.xml”)/book return depth($b)

120 Existential and Universal Quantifiers for $b in doc(“bib.xml”)/book where some $author in $b/author satisfies $author/text() = “Ullman” return $b for $b in doc(“bib.xml”)/book where every $author in $b/author satisfies $author/text() = “Ullman” return $b Return books where all authors are “Ullman” Return books where at least one author is “Ullman”

121 Comments

122 Comparisons Value comparisons Eq, ne, lt, le, gt, ge Used to compare individual values Each operand must be a single atomic value (or a node containing a single atomic value) General comparisons =, !=,, >= Can be used with sequences of multiple items

123 Example

Node comparisons to compare two nodes, by their identity or by their document orderdocument order Example: The following comparison is true only if the left and right sides each evaluate to exactly the same single node: /books/book[isbn=" "] is /books/book[call="QA76.9 C3845"] 124

Remember direct element constructors? element book { attribute isbn {"isbn " }, element title { "Harold and the Purple Crayon"}, element author { element first { "Crockett" }, element last {"Johnson" } } 125

Using direct element constructors interestingly let $e := {5} return element {fn:node-name($e)} 2 * fn:data($e)} 126

Other examples node comparison The following comparison is false because each constructed node has its own identity: 5 is 5 The following comparison is true only if the node identified by the left side occurs before the node identified by the right side in document order: /transactions/purchase[parcel="28-451"] << /transactions/sale[parcel="33-870"] 127

128 XQuery Syntax Declarative, functional language ~ SQL Nested expressions Case sensitive White spaces: –Tabs, space, CR, LF –Ignored between language constructs –Significant in quoted strings No special EOL character

129 Keywords and names Keywords and operators –Case-sensitive, generally lower case –May have several meanings depending on the context E.g. “*” or “in” –No reserved words All names must be valid XML names –variables, functions, elements, attributes –Can be associated with a namespace

130 XQuery gives you a choice: Path Expressions: –If you just want to copy certain elements and attributes as is FLWOR Expressions: –Allow sorting –Allow adding elements/attributes –Verbose, but can be clearer

131 XQuery tools XStylus Studio oad.html (free trial version) oad.html –See also short XQuery intro at:

132 Other info: –XQuery on Distributed Resources –Extensions for generic programming with XML

133 XQuery on Distributed Sources

134

135

136

137

138

139 XML and programming XSLT, XPath and XQuery provide tools for specialized tasks. But many applications are not covered: –domain-specific tools for concrete XML languages –general tools that nobody has thought of yet

140 XML in general-purpose programming languages parse XML documents into XML trees navigate through XML trees construct XML trees output XML trees as XML documents DOM and SAX are corresponding APIs that are language independent and supported by numerous languages. JDOM is an API that is tailored to Java.DOMSAXJDOM

141 XQuery Conclusion We have learned: –XQuery definition –Usage scenarios –Comparison w. XSLT and XPath –Capabilities –Functions, path expressions and predicates –FLWOR