Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Semestr2 / 1 - Oracle / Oracle selected docs / SQL reference.pdf
Скачиваний:
24
Добавлен:
12.05.2015
Размер:
11.92 Mб
Скачать

XMLFOREST

<salary>12000</salary> <Hiredate>07-JUN-94</Hiredate>

</Emp>

XMLFOREST

Syntax

XMLForest::=

 

 

,

 

 

 

AS

c_alias

XMLFOREST

(

value_expr

)

Purpose

XMLForest converts each of its argument parameters to XML, and then returns an XML fragment that is the concatenation of these converted arguments.

If value_expr is a scalar expression, then you can omit the AS clause, and Oracle uses the column name as the element name.

If value_expr is an object type or collection, then the AS clause is mandatory, and Oracle uses the specified c_alias as the enclosing tag.

If value_expr is null, then no element is created for that value_expr.

Examples

The following example creates an Emp element for a subset of employees, with nested employee_id, last_name, and salary elements as the contents of Emp:

SELECT XMLELEMENT("Emp",

XMLFOREST(e.employee_id, e.last_name, e.salary)) "Emp Element"

FROM employees e WHERE employee_id = 204;

Emp Element

----------------------------------------------------------------

<Emp> <EMPLOYEE_ID>204</EMPLOYEE_ID> <LAST_NAME>Baer</LAST_NAME> <SALARY>10000</SALARY>

</Emp>

Functions 6-217

XMLSEQUENCE

See Also: the example for XMLCOLATTVAL on page 6-212 to compare the output of these two functions

XMLSEQUENCE

Syntax

XMLSequence::=

 

 

XMLType_instance

 

 

XMLSEQUENCE

(

,

fmt

)

 

 

 

 

 

sys_refcursor_instance

 

 

Purpose

XMLSequence has two forms:

The first form takes as input an XMLType instance and returns a varray of the top-level nodes in the XMLType.

The second form takes as input a REFCURSOR instance, with an optional instance of the XMLFormat object, and returns as an XMLSequence type an XML document for each row of the cursor.

Because XMLSequence returns a collection of XMLType, you can use this function in a TABLE clause to unnest the collection values into multiple rows, which can in turn be further processed in the SQL query.

See Also: Oracle9i XML API Reference - XDK and Oracle XML DB for more information on this function

Examples

The following example shows how XMLSequence divides up an XML document with multiple elements into VARRAY single-element documents. In this example, the TABLE keyword instructs Oracle to consider the collection a table value that can be used in the FROM clause of the subquery:

SELECT EXTRACT(warehouse_spec, ’/Warehouse’) as "Warehouse"

FROM warehouses WHERE warehouse_name = ’San Francisco’;

6-218 Oracle9i SQL Reference

XMLTRANSFORM

Warehouse

------------------------------------------------------------

<Warehouse>

<Building>Rented</Building>

<Area>50000</Area>

<Docks>1</Docks> <DockType>Side load</DockType> <WaterAccess>Y</WaterAccess> <RailAccess>N</RailAccess> <Parking>Lot</Parking> <VClearance>12 ft</VClearance>

</Warehouse>

1 row selected.

SELECT VALUE(p)

FROM warehouses w,

TABLE(XMLSEQUENCE(EXTRACT(warehouse_spec, ’/Warehouse/*’))) p WHERE w.warehouse_name = ’San Francisco’;

VALUE(P)

----------------------------------------------------------------

<Building>Rented</Building>

<Area>50000</Area>

<Docks>1</Docks> <DockType>Side load</DockType> <WaterAccess>Y</WaterAccess> <RailAccess>N</RailAccess> <Parking>Lot</Parking> <VClearance>12 ft</VClearance>

8 rows selected.

XMLTRANSFORM

Syntax

XMLTransform::=

XMLTRANSFORM ( XMLType_instance , XMLType_instance )

Functions 6-219

XMLTRANSFORM

Purpose

XMLTransform takes as arguments an XMLType instance and an XSL style sheet, which is itself a form of XMLType instance. It applies the style sheet to the instance and returns an XMLType.

This function is useful for organizing data according to a style sheet as you are retrieving it from the database.

See Also: Oracle9i XML API Reference - XDK and Oracle XML DB for more information on this function

Examples

The XMLTransform function requires the existence of an XSL style sheet. Here is an example of a very simple style sheet that alphabetizes elements within a node:

CREATE TABLE xsl_tab (col1 XMLTYPE);

INSERT INTO xsl_tab VALUES ( XMLTYPE.createxml( ’<?xml version="1.0"?>

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > <xsl:output encoding="utf-8"/>

<!-- alphabetizes an xml tree --> <xsl:template match="*">

<xsl:copy>

<xsl:apply-templates select="*|text()">

<xsl:sort select="name(.)" data-type="text" order="ascending"/> </xsl:apply-templates>

</xsl:copy>

</xsl:template> <xsl:template match="text()">

<xsl:value-of select="normalize-space(.)"/> </xsl:template>

</xsl:stylesheet> ’));

1 row created.

The next example uses the xsl_tab XSL style sheet to alphabetize the elements in one warehouse_spec of the sample table oe.warehouses:

SELECT XMLTRANSFORM(w.warehouse_spec, x.col1).GetClobVal()

FROM warehouses w, xsl_tab x

WHERE w.warehouse_name = ’San Francisco’;

6-220 Oracle9i SQL Reference

Соседние файлы в папке Oracle selected docs