Presentation is loading. Please wait.

Presentation is loading. Please wait.

XML Data: Part 2 เอกสารภาค บรรยาย ดร. มารุต บูรณรัช : หัวข้อพิเศษด้านเทคโนโลยีสารสนเทศขั้นสูง - เทคโนโลยีเว็บเชิงความหมาย.

Similar presentations


Presentation on theme: "XML Data: Part 2 เอกสารภาค บรรยาย ดร. มารุต บูรณรัช : หัวข้อพิเศษด้านเทคโนโลยีสารสนเทศขั้นสูง - เทคโนโลยีเว็บเชิงความหมาย."— Presentation transcript:

1 XML Data: Part 2 เอกสารภาค บรรยาย ดร. มารุต บูรณรัช marut.bur@nectec.or.th marut.bur@nectec.or.th 269618: หัวข้อพิเศษด้านเทคโนโลยีสารสนเทศขั้นสูง - เทคโนโลยีเว็บเชิงความหมาย (Special Topics in Advanced Information Technology – Semantic Web Technology) ภาควิชาวิทยาการคอมพิวเตอร์และเทคโนโลยีสารสนเทศ คณะวิทยาศาสตร์ มหาวิทยาลัยนเรศวร ภาคการศึกษาที่ 2 ปีการศึกษา 2557

2 2 Outlines Namespace XML Schema XPath XQuery Native XML Databases

3 Namespace

4 4 Namespace Example Without namespace Programming XML in Java Using namespace Programing XML in Java

5 5 XML Namespace It is possible that one document may use more than one DTD  One document uses element tags defined in multiple DTDs When a document contain element tags from many DTDs, there can be name clashes  The same name is defined in different DTDs with different meanings  The processing program can not determine which DTDs should be used to interpret these tags

6 6 XML Namespace (2) XML applications sometimes requires unique names for element tags Using XML namespace, an element tag can be uniquely identified via a URI/URL  Namespace + local name Example  The element name “http://www.w3.org/TR/WD- xsl#template” Namespace: http://www.w3.org/TR/WD-xsl Local name: template

7 7 XML Namespace (3) However, it is not practical to type a full URL every time a tag is used The solution is to define an abbreviation for the URI/URL and then use this abbreviation as a prefix for the element name For example,  The abbreviation “xsl” can be defined for the namespace http://www.w3.org/TR/WD-xsl  Then the element name “xsl:template” can be used to refer to http://www.w3.org/TR/WD-xsl#template

8 8 Declaration of namespace A namespace can be defined in a start tag using prefix “xmlns”, e.g. Example … The abbreviation “xsl” for the namespace “http://www.w3.org/TR/WD-xsl” is defined for the entire element

9 9 Declaration of namespace (2) Namespace declarations are inherited to the nested elements  So namespace declaration is usually put in the root element It is possible to define more than one namespace in one tag, e.g. <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" xmlns:html="http://www.w3.org/TR/REC-html40" version="1.0">

10 10 Declaration of namespace (3) Default namespace  The attribute “xmlns” without suffix means default namespace, e.g. <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl" xmlns="http://www.w3.org/TR/REC-html40" version="1.0">  With default namespace defined, all element types without namespace prefix are automatically assigned to this namespace

11 XML Schema

12 12 Introduction XML Schema provides a better support for database applications than DTD Some limitations of DTD  Limited data type #PCDATA, CDATA, NMTOKEN, etc. No support for number data types  Limited extensibility No user-defined data type  Not compatible with namespaces

13 13 XML Schema Example Example XML Schema Example XML document Hello World!

14 14 DTD vs. XML Schema Example DTD Example XML document This is an article This is the story in this article

15 15 DTD vs. XML Schema (2) Example XML Schema

16 16 Creating XML Schema document XML Schema is defined in XML syntax  An XML Schema document is an XML document XML Schema document must have the following root element...

17 17 Creating XML Schema document (2) The children elements of the root element (“schema”) declare elements and their data types Data types in XML Schema  Simple Types  Complex Types

18 18 Simple vs. Complex Types Simple types are types for values  numbers: integers, floats, doubles  strings, tokens, names  dates, times, etc. Complex types are element structure  content models  attributes

19 19 Simple Type Examples of simple data types in XML Schema  string: “This is a text"  integer: 32, -45  decimal: 12.4, -2.3  float: 12.4, 3.6E-5  boolean: true, false  time: 15:23:00.000-04:00  date: 2001-02-18  ID: same as in DTD  NMTOKEN: same as in DTD

20 20 Simple Type (2) A new simple type can also be defined by user using ‘restriction’ Example

21 21 Simple Type (3) Element declaration  Element that does not contain other elements or attributes has simple type  Examples Attribute declaration  Example

22 22 Complex Type Element that contains other elements has complex type  Children elements are nested under ‘sequence’ element Example

23 23 Cardinality Cardinality of an element can be represented using attributes ‘minOccurs’ and ‘maxOccurs’  minOccurs=“0”: optional element  maxOccurs=“unbounded” : no maximum limit Examples

24 24 Converting DTD content model Example DTD content model (title?,firstname*,lastname+) Example XML Schema <xsd:element name="lastname" minOccurs=“1” maxOccurs="unbounded"/>

25 25 Sequences The sequence element specifies an exact sequence of children elements Example In the above example, ‘ref’ attribute references to the element that is already declared

26 26 Choices The choice element specifies an set of choices Example In the above example, only one of the choice elements can appear at a time

27 27 Attributes Attributes can be declared in the ‘complexType’ element using the ‘attribute’ element Example

28 28 Empty Element Example of declaration for empty element

29 XPath

30 30 Introduction XPath provides a syntax for addressing parts of an XML document XPath has a directory-path syntax A single “/” (slash) represents the root element Subsequent name in path expressions represent children element  E.g. /doc/title – select the ‘title’ element which is a child of the element ‘doc’

31 31 XPath Example Example XML data 021234567 012783345 021227895 Example XPath expression /CONTACTS/PHONE This will return the three ‘phone’ elements

32 32 Selecting Attributes Attribute can be referenced using ‘@’ Example XML data 021234567 012783345 021227895 Example XPath expression /CONTACTS/PHONE/@type This will return the attribute ‘type' (and its value) of the three elements

33 33 Wildcards The ‘*’ (asterisk) can be used to match any character sequence in names Using the above example data, the XPath expression: /CONTACTS/* This will return all the children elements under the ‘CONTACTS’ element /CONTACTS/PHONE/@* This will return all the attributes and their values of the ‘PHONE’ elements

34 34 Context Node Evaluation of XPath expression always references to a context node The context node can be referenced by ‘.’ (period) Example  The following XPath expression returns all the attributes of the context node./@* The context node is implicit, e.g.  the following expressions are equivalent CONTACTS/PHONE./CONTACTS/PHONE

35 35 Parents and Ancestors From the context node, parent and ancestor nodes can be referenced Similar to directory path, ‘..’ represents the parent node, e.g.../FIRSTNAME Expression can referenced back as many levels, e.g.../../NAME/FIRSTNAME Both select the ‘FIRSTNAME' element that is in the same level as the context node

36 36 Conditional Matching Matching conditions can be specified Matching conditions are wrapped in square brackets ('[' and ']') Example /CONTACTS/PHONE[@type='mobile'] This will select the ‘PHONE' elements where the ‘type' attribute has value 'mobile‘  Using the above example, the element for the phone number ‘021234567’ would be returned

37 37 Skipping Levels Elements that are not direct children can be referenced with ‘//’ (double forward slash) For example /DEPARTMENT//FIRSTNAME This will match all ‘FIRSTNAME' elements that are below the ‘DEPARTMENT’ element The following expression will match all ‘FIRSTNAME' elements in the document //FIRSTNAME

38 38 Special Functions There are some special functions that can be used to step through paths For example, /PERSON/node() will match all children nodes of ‘PERSON’ element (including elements, comment, text, etc.) FunctionResult node() text() processing-instruction() comment() Match any node Match text Match a processing-instruction Match a comment

39 XQuery

40 40 Introduction XML query languages  The goal is to allow query of XML data similarly to querying databases  Some XML query languages are XML-QL, XQL and XQuery XQuery is an W3C effort to standardize query language for XML  It is dependent on XPath 1.0 standard

41 41 Introduction (2) Some requirements for XQuery:  Operate on A single XML document Collection of XML documents  Select entire documents or parts of documents that match conditions  Construct new documents based on what has been selected

42 42 XQuery Example  Find books that have price higher than average for $a in document("books.xml")//book let $b := avg(document(“books.xml")//book/@price) where $a/@price> $b return $a/title

43 43 XQuery (2) XQuery syntax  XQuery is case-sensitive Keywords are in lower case, e.g. for, let, return, etc.  Comment has the form (: … :) E.g. (: this is a comment :)  XQuery allows various forms of expressions, e.g. variables, numbers, string, functions, etc.

44 44 XQuery Expressions String  e.g. “Hello” Number  e.g. 4, 4.8, 9.5E-2 Constructed values  e.g. true(), false(), date(“2005-09-11”) Variables  e.g. $x Constructed sequences  e.g. $a, $b or ($a, $b)  1 to 3 (the same as 1, 2, 3) Path expressions  e.g. /STUDENT/NAME/FIRSTNAME

45 45 XQuery Path Expressions XQuery uses path expression syntax from XPath Result of a path expression is ordered list of nodes (and their children nodes)  Result of path expression may contain duplicate values Each step in path expression represents movement through document Each step can eliminate nodes by applying predicates

46 46 Predicates Predicate is an evaluation of expression Predicate is enclosed in square bracket (‘[ ’, ‘]’)  Boolean expressions, e.g. book[author=“John Smith]  Number expressions, e.g. chapter[3]  Existence test, e.g. book[author] person[@fax] Predicates can be used in path expressions //book[author=“John Smith”]/chapter[3]

47 47 Operators Comparison operators  =, !=, >, >=, <, <=  eq, ne, gt, ge, lt, le  is, is not Compare two nodes based on identity  > Compare two nodes based on order in the document (before & after) Logical operators  and, or, not( )

48 48 Path Expression: Example Find phone number of the first staff member in our “staff_list.xml” document doc(“staff_list.xml”)/STAFFLIST/STAFF[1]//PHONE Steps:  Open the file “staff_list.xml”  Select /STAFFLIST element (root element)  Select the first STAFF element under STAFFLIST (first child of the root element)  Select all PHONE elements occurring under this STAFF element

49 49 Path Expression: Example (2) If the XML document has the following structure: John Smith 0123456789 0234567890... Other possible path expressions to access phone numbers of staff 1 doc(“staff_list.xml”)/STAFFLIST/STAFF[1]/CONTACTS/PHONE doc(“staff_list.xml”)//STAFF[1]//PHONE

50 50 XQuery query: FLWOR A query in XQuery has the form “for, let, where, order by, return” (FLWOR) for in let := where order by return

51 51 XQuery query: FLWOR (2) An XQuery query:  Start with any number of FOR or LET (or both) clause  WHERE clause is optional  ORDER BY clause is optional  RETURN clause is required FOR and LET clauses assign values to variables  Values are obtained using expressions (e.g. path expressions)  FOR is used for iteration, associating each specified variable with each value returned by expression  LET clause is also used to assign value to variable but without iteration

52 52 XQuery query: FLWOR (3) WHERE clause specifies conditions that filter data generated by FOR and LET RETURN and ORDER BY specifies the format of the output data Together “FOR-LET-WHERE-RETURN” is similar to “SELECT-FROM-WHERE” in SQL

53 53 Example of query in XQuery Example of basic ‘for-where-return’ query for $st in document(“student-list.xml")//STUDENT where $st/LASTNAME = “Smith" return { $st/FIRSTNAME, $st/LASTNAME, $st/GRADE }

54 54 Example of query in XQuery (2) ‘@’ represents attribute Example for $st in document(“student-list.xml")//STUDENT where $st/@stud_id < 500 return $st/FIRSTNAME

55 55 Example of query in XQuery (3) Nested ‘for’ can also be used Example for $a in document(“student-list.xml") for $b in $a/STUDENT-LIST for $c in $b/STUDENT return { $c/FIRSTNAME }

56 56 Example of query in XQuery (4) Join operation can be used for multiple XML files Example for $a in document(“website1-products.xml")//product, $b in document(“website2-products.xml")//product where $a/name = $b/name return { $a/name } { $a/price/text() } { $b/price/text() }

57 57 Example of updates in XQuery Examples of XQuery extensions for updates FOR $e IN /emp INSERT count($e/skill) BEFORE $e/skill[1] FOR $e IN /emp WHERE $e/empno = "1234” REPLACE $e/job WITH "Broom Tester" FOR $e IN /emp/[job = "Programmer"], $s IN $e/skill WHERE $s/rating < 4 OR $s/cert_date < date("1995-01-01") DELETE $s Insert Replace Delete

58 58 XML data vs. Relational data Structure of XML data is variable  Relational data has homogenous structure, e.g. Table data must conform to database schema Each row must have the same structure  XML data is semi-structured XML document may or may not use DTD or XML Schema Structure of XML document is more flexible XML data is ordered  Relational data is unordered Order of rows are not important in relational model  The elements in XML document are ordered

59 59 XML Databases Some possible methods for storing XML data in relational databases  Store entire XML data as an attribute value in a table CLOB data type may be used for the columns storing XML data Can not fully benefit from query/update feature  Parse XML elements and store them in tables Database schema must be designed to match with structure of the stored XML data Relational DB must have uniform structure, however the structure of XML data is more flexible (“semi-structured data”)

60 60 XML Databases (2) Native XML Databases  DBMS designed specially to support XML data  Some features of native XML databases Collection Query languages Update and delete Transaction and Concurrency Control Indexes, etc.

61 61 XML Databases (3) Types of native XML databases  Text-based Store XML data as text May use file systems or relational DBMS to store XML data  Model-based Storing structure of XML data, e.g. in tree format May be built on relational DBMS or object-oriented DBMS or use proprietary format


Download ppt "XML Data: Part 2 เอกสารภาค บรรยาย ดร. มารุต บูรณรัช : หัวข้อพิเศษด้านเทคโนโลยีสารสนเทศขั้นสูง - เทคโนโลยีเว็บเชิงความหมาย."

Similar presentations


Ads by Google