Você está na página 1de 46

Introduction to XML

XML Path

Nhóm thực hiện: XQUERY in Oracle


Nguyễn Minh Hoàng 08520132
Trịnh Thanh Long 08520210
Nguyễn Thanh Tâm 08520329
Trần Ngọc Thắng 08520594

GVHD: Th.S Nguyễn Thị Kim Phụng


Introduction to XML
Introduction to XML

1 History of XML

2 What is XML?

3 What can I do with XML?

4 Oracle provide XML technology

5 Benefits of XML?
History of XML
 By the World Wide Web Consortium’s (W3C) XML
1.0 specification.
 Come from the Standard Generalized Markup

Language (SGML)
 Enables users to define their own markup

languages,display within browsers, exchange between


applications across Internet.
What is XML?

 EXtensible Markup Language like HTML.


 Was designed to carry data, not to display data.
 XML is not a replacement for HTML.
 You must define your own tags.
What is difference between XML and HTML?

They we designed for different goal:


 XML: transport and store data, with focus on what

data is.
 HTML: display data, with focus on how data looks.
A XML document
<?xml version="1.0" encoding="ISO-8859-1"?>
<transaction>
<account>123456</account>
<buy shares = "100">
<ticker>WEBM</ticker>
</buy>
<sell shares = "30">
<ticker>INTC</ticker>
</sell>
</transaction>
A XML document(cont)
 Tags: every tag represent one piece of data, can contain other
tags, simple text or a mixture of both
<ticker>WEBM</ticker>
 Contents: information is represent by tags

<ticker>WEBM</ticker>
 Attributes: supply more information about tag, has a name

and value.
<buy shares = "100">
 Comments: add a comment to the document.

<!--This is a comment-->
XML syntax

<root> //must have root element


<child>
<subchild>.....</subchild>
</child>
</root> //XML element must have closing tag

//Tags are case-sensitive


//XML Elements Must be Properly Nested
<b><i>This text is bold and italic</i></b> : TRUE
<b><i>This text is bold and italic</b></i>: FALSE
Namespaces
 <?xml version="1.0"?>
 <BookOrder OrderNo="1234">
 <OrderDate>2001-01-01</OrderDate>
 <Customer>
 <Title>Mr.</Title>
 <FirstName>Graeme</FirstName>
 <LastName>Malcolm</LastName>
 </Customer>
 <Book>
 <Title>Treasure Island</Title>
 <Author>Robert Louis Stevenson</Author>
 <Book>
 </BookOrder>
 Misunderstand between <Title></Title> of
Customer(Mr, Mrs, Ms…) and
<Title></Title> of Book(Title of book)?
 To avoid this situation, we you Namespace to

know their parent.


Solution
 <?xml version="1.0"?>
 <BookOrder xmlns="http://www.northwindtraders.com/order"

xmlns:cust="http://www.northwindtraders.com/customer"
xmlns:book="http://www.northwindtraders.com/book"
OrderNo="1234">
◦ <OrderDate>2001-01-01</OrderDate>
◦ <cust:Customer>
 <cust:Title>Mr.</cust:Title> <cust:FirstName>Graeme</cust:FirstName>
<cust:LastName>Malcolm</cust:LastName>
◦ </cust:Customer>
◦ <book:Book>
 <book:Title>Treasure Island</book:Title>
 <book:Author>Robert Louis Stevenson</book:Author>
◦ </book:Book>
 </BookOrder>
Entity Rerferences
Some characters have a special meaning in XML.
&lt; < less than
&gt; > greater than
&amp; & ampersand
&apos; ' apostrophe
&quot; " quotation mark
What can I do with XML?

Send and receive XML over the web


What can I do with XML?(cont)

Parse XML to access its information set


What can I do with XML?(cont)

Transform XML Using XSLT


What can I do with XML?(cont)

The XSLT processor transforms source tree to result tree


Oracle provide XML technology ?
 XML Parser: Parses, constructs, and validates XML documents
 XPath Engine: Searches in-memory XML documents
declaratively
 XSLT Processor: Transforms XML documents
 XML SQL Utility: Produces XML from SQL and inserts XML into
tables
 XSQL Pages: interMedia Text
 Object views: Serve rich XML-enabled views of flat relational
data
JServer Java VM: Reduces network traffic by running Java in
the Database
 Jdeveloper: Creates, syntax-checks, and debugs Java, XML,
 XSLT, and XSQL
 Advanced Queuing: Queues and dequeues XML messages
Benefits of XML?
 XML Separates Data from HTML
 XML Simplifies Data Sharing
 XML Simplifies Data Transport
 XML Simplifies Platform Changes
 XML Makes Your Data More Available
 XML is Used to Create New Internet Languages
Query using XPATH
 XPath is a language for finding information in
an XML document.
 XPath queries select information from the

logical tree-structured data model presented


by an XML document's infoset, not from its
raw text representation.
 XPath includes over 100 built-in functions,

uses path expressions to select nodes or


node-sets in an XML document.
Path expression

nodename Selects all child nodes of the named node


/ Selects from the root node
// Selects nodes in the document from the
current node that match the selection no
matter where they are
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
Some function in Xpath:
 ExtractValue() return value of node and
attribute, can only return a the value of a
single node or attribute value.
 Extract() is used when the XPath expression

will result in a collection of nodes being


returned
 Existsnode() to check whether a node exists

or not.
Example
 <?xml version="1.0"?>
 <Order OrderNo="1047">
◦ <OrderDate>2002-03-26</OrderDate> <Customer>John
Costello</Customer>
<Item>
<Product ProductID="1" UnitPrice="70">Chair</Product>
<Quantity>6</Quantity>
</Item>
<Item>
<Product ProductID="2" UnitPrice="250">Desk</Product>
<Quantity>1</Quantity>
</Item>
 </Order>
XML likes a tree towads XPATH
Absolute Location PATH
 Begin with Context node=Root node

Standard form Short form


  /child::Order /Order
/child::Order/child::Customer /Order/Customer
/child::Order/attribute::OrderNo /Order/@OrderNo
/child::Order/descendant::Product /Order//Product
Relative Location PATH
 Begin with Context node=Current Node
 For example, Context Node=Item(1)

Standard form Short form


child::Quantity Quantity
child::Product/attribute::ProductID Product/@ProductID
 parent::Order/attribute::OrderNo ../@OrderNo
parent::*/attribute::OrderNo ../@OrderNo
XQUERY in Oracle
XQUERY in Oracle
1 Overview of the XQuery Language

2 SQL Functions XMLQuery and XMLTable

3 Oracle XQuery Extension Functions

4 Xquery command
Xquery Langague
 XQuery 1.0 is the W3C language designed
for querying XML data.
 It is similar to SQL in many ways
 XQuery is designed especially for querying

semistructured, XML data from a variety of


data sources.
Xquery Langague
 You can use XQuery to query XML data
wherever it is found, whether, it is stored in
database tables, available through Web
Services, or otherwise created on the fly.
 XQuery is a functional language. It consists of

a set of possible expressions that are


evaluated and return values.
Xquery Langague
 XQuery Expressions:
◦ Primary expression: A variable name starts with
dollar-signs ($).
◦ XPath expression.
◦ FLWOR expression(for, let, where , order by, return):
The most important XQuery expression.
◦ XQuery sequence: The comma (,) constructor
creates sequences. All XQuery sequences are
effectively flat.
◦ Direct (literal) constructions:XQuery expression
<a>33</a> constructs the XML element
<a>33</a>.
Xquery Langague
 XQuery Expressions:
◦ Computed (dynamic) constructions: Braces ({, }) are used to
mark off an XQuery expression to be evaluated.
Example: expression:
<foo>{attribute toto {2+3}, element bar {"tata", "titi"}, text
{" why? "} constructs XML data:
<foo toto="5"><bar>tata titi</bar> why? </foo>.
◦ Conditional expression:
Example:
if (something < somethingElse) then expression1 else
expression2.
◦ Arithmetic, relational expression:As usual, but remember
that each relational expression returns a Boolean value.
Xquery Langague
 XQuery Expressions:
◦ Regular expression.
◦ Type expression: An XQuery expression that
represents an XQuery type.
Examples: item(), node(), attribute(), element(),
document-node(), namespace(), text(), xs:integer,
xs:string.
Type expression – An XQuery expression that
represents an XQuery type. Examples: item(),
node(), attribute(), element(), document-node(),
namespace(), text(), xs:integer, xs:string
FLWOR Expressions
 FLWOR is the most general expression syntax in Xquery.
 A FLWOR expression has at least one for or let clause and a return
clause; single where and order by clauses are optional.
◦ For: Bind one or more variables each to any number of values, in
turn. At each iteration, the variables are bound in the order they
appears.
Example: for $i in (3, 4), $j in ($i, 2+$i).
◦ Let: Bind one or more variables. Bind a variable to a computed
value.
◦ Where: Filter the for and let variable bindings according to some
condition.
◦ order by: Sort the result of where filtering.
◦ return – Construct a result from the ordered, filtered values. This
is the result of the FLWOR expression as a whole. It is a flattened
sequence.
SQL Functions XMLQuery and XMLTable

 SQL functions XMLQuery and XMLTable are


defined by the SQL/XML standard as a
general interface between the SQL and
XQuery languages, XMLQuery and XMLTable
let you take advantage of the power and
flexibility of both SQL and XML.
XMLQuery Examples:
 Ex.1:
◦ select xmlquery(‘for $i in (1 to 10) return $i’
returning content) from dual;

Result(SQL*plus):
XMLQUERY('FOR$IIN(1TO10)RETURN$I'RETURNINGCONTENT)
---------------------------------------------------
1 2 3 4 5 6 7 8 9 10
Oracle XQuery Extension Functions
 Oracle XML DB adds some XQuery
functions to those provided in the
W3C standard.
 These additional functions are in

the Oracle XML DB namespace,


http://xmlns.oracle.com/xdb
Oracle XQuery Extension Functions
 ora:contains:
◦ ora:contains (input_text, text_query [, policy_name] [,
policy_owner])
◦ Function ora:contains returns a positive integer when the
input_text matches text_query(the higher the number, the
more relevant the match), and zero otherwise.
◦ When used in an XQuery expression, the XQuery return
type is xs:integer(); when used in an XPath expression
outside of an XQuery expression, the XPath return type is
number.
 ora:matches:
◦ ora:matches (target_string, match_pattern [,
match_parameter])
◦ XQuery function ora:match lets you use a regular
expression to match text in a string. It returns true() if its
target_string argument matches its regular-expression
match_pattern argument and false() otherwise.
◦ If target_string is the empty sequence, false() is returned.
Optional argument match_parameter is a code that
qualifies matching: case-sensitivity and so on.
Oracle XQuery Extension Functions
 ora:replace
◦ ora:replace (target_string, match_pattern, replace_string [,
match_parameter]).
◦ XQuery function ora:replace lets you use a regular
expression to replace matching text in a string.
◦ Each occurrence in target_string that matches regular-
expression match_pattern is replaced by replace_string.
◦ It returns the new string that results from the replacement.
If target_string is the empty sequence, then the empty
string ("") is returned. Optional argument match_parameter
is a code that qualifies matching: case-sensitivity and so
on.
Oracle XQuery Extension Functions
 ora:sqrt
◦ ora:sqrt (number)
◦ XQuery function ora:sqrt returns the square root of
its numerical argument, which can be of XQuery
type xs:decimal, xs:float, or xs:double. The
returned value is of the same XQuery type as the
argument.
Oracle XQuery Extension Functions
 ora:view
◦ ora:view ([db-schema STRING,] db-table STRING) RETURNS
document-node(element())
◦ XQuery function ora:view lets you query existing database
tables or views inside an XQuery expression, as if they were
XML documents. In effect, ora:view creates XML views over
the relational data, on the fly. You can thus use ora:view to
avoid explicitly creating XML views on top of relational data.
◦ The input parameters are as follows:
 db-schema – An optional string literal that names a
database schema.
 db-table – A string literal naming a database table or view.
If db-schema is present, then db-table is in database
schema db-schema.
Xquery command
 We can enter an Xquery expression directly at
the SQL*plus command line, by using XQuery
command.
 Oracle Database treats XQuery expressions

submitted with this command the same way it


treats Xquery expressions in SQL functions
XMLQuery and XMLTable.

Você também pode gostar