Schema registration - Web view4 5 The . at. keyword can be used to count the iteration: ... Returns an XML element in an XMLType when given the XML element name,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Using of Oxygen XML editorfor XML document creation
XML file<?xml version="1.0"?><SERVICES><SERVICE><SERVICE_DESCRIPTION>Coloring: short</SERVICE_DESCRIPTION><SERVICE_PRICE>16 eur</SERVICE_PRICE><SERVICE_DURATION>40</SERVICE_DURATION></SERVICE>
XQuery is the language for querying XML dataXQuery is built on XPath expressionsXQuery is supported by all major databases
16
FLWOR?
FLWOR (pronounced "flower") is an acronym for "For, Let, Where, Order by, Return".For - selects a sequence of nodesLet - binds a sequence to a variableWhere - filters the nodesOrder by - sorts the nodesReturn - what to return (gets evaluated once for every node)
for $x in doc("books.xml")/bookstore/bookwhere $x/price>30order by $x/titlereturn $x/title
The at keyword can be used to count the iteration:for $x at $i in doc("books.xml")/bookstore/book/titlereturn <book>{$i}. {data($x)}</book> <book>1. Everyday Italian</book><book>2. Harry Potter</book><book>3. XQuery Kick Start</book><book>4. Learning XML</book>
17
for $x in (10,20), $y in (100,200)return <test>x={$x} and y={$y}</test>
<test>x=10 and y=100</test><test>x=10 and y=200</test><test>x=20 and y=100</test><test>x=20 and y=200</test>
The let clause allows variable assignments and it avoids repeating the same expression many times. The let clause does not result in iteration.let $x := (1 to 5)return <test>{$x}</test>
<test>1 2 3 4 5</test>
The where clause is used to specify one or more criteria for the result:where $x/price>30 and $x/price<100
The order by clause is used to specify the sort order of the result. Here we want to order the result by category and title:
for $x in doc("books.xml")/bookstore/bookorder by $x/@category, $x/titlereturn $x/title
User-defined functions can be defined in the query or in a separate library.
declare function prefix:function_name($parameter as datatype)as returnDatatype{ ...function code here...};
Notes on user-defined functions: Use the declare function keywordThe name of the function must be prefixedThe data type of the parameters are mostly the same as the data types defined in XML SchemaThe body of the function must be surrounded by curly braces
Example of a User-defined Function Declared in the Query
declare function local:minPrice($p as xs:decimal?,$d as xs:decimal?)as xs:decimal?{let $disc := ($p * $d) div 100return ($p - $disc)};
Below is an example of how to call the function above:<minPrice>{local:minPrice($book/price,$book/discount)}</minPrice>
BEGIN OPEN S_CURSOR; LOOP FETCH S_CURSOR INTO S_DATA; EXIT WHEN S_CURSOR%NOTFOUND; DBMS_OUTPUT.PUT_LINE(S_DATA.getStringVal()); END LOOP; CLOSE S_CURSOR;END;
Function DescriptionxmlElement() Returns an XML element in an XMLType when given the
XML element name, an optional list of XML attributes (XMLATTRIBUTES()), and an optional list of values as the content of the new element. XMLELEMENT() can also contain other XML elements or XML fragments (XMLFOREST() ) as its children.
xmlAttributes() Used within XMLELEMENT() to specify attributes for the element.
xmlForest() Returns an XML fragment in an XMLType when given a list of named expressions for the XML elements. Each expression specifies the name of an XML element and its content.
XMLCONCAT() Returns an XML fragment in an XMLType by concatenating a list of XML elements/values.
XMLAGG() Returns an XML fragment in an XMLType by aggregating XML fragments, with the option of XML element sorting.
SYS_xmlGen() Generates an XML document with the <?XML?> prolog from one scalar type, a user-defined object type, or an instance of XMLType.
XMLSEQUENCE() Returns a collection of XMLTypes in an XMLSEQUENCEType, which is a VARRAY of XMLType instances in the database.
SYS_XMLAGG() Aggregates XML elements from one scalar type, a user-defined object type, or an instance of XMLType.
XMLCOLATTVAL() Generates a set of <column/> elements with the name attributes specifying the column names or the name aliases.
UPDATEXML() Updates XML documents in XMLTypes using XPath expressions.
XMLTRANSFORM() Applies XSL transformation on XML documents in XMLTypes.
EXTRACTVALUE() Returns scalar content, such as numbers or strings, when passed an XPath expression pointing to an XML element with only a single text child.
setlinesize 500;
26
setserveroutputon;
select XMLELEMENT("SERVICE", XMLATTRIBUTES( a.SERVICE_ID as "ID"), XMLFOREST(a.SERVICE_DESCRIPTION as "DESCRIPTION", a.SERVICE_PRICE as "PRICE", a.SERVICE_DURATION as "DURATION ")).extract('/*') as xmlfrom SERVICES_REL a;