<xsd:attributeGroup ref="productDetails"/> </xsd:complexType> </xsd:element> <xsd:attributeGroup name="productDetails"> <xsd:attribute name="productID" use="required" type="xsd:ID"/> <xsd:attribute name="name" use="required" type="xsd:string"/> <xsd:attribute name="description" use="required" type="xsd:string"/> <xsd:attribute name="unit" type="xsd:positiveInteger"/> <xsd:attribute name="price" use="required" type="xsd:decimal"/> <xsd:attribute name="stock" type="xsd:integer"/> </xsd:attributeGroup> You can enforce uniqueness within a document instance by using xsd:unique: <xsd:unique name="uniqueProductID"> <xsd:selector xpath="tc:Product/"/> <xsd:field xpath="tc:@productID"/> </xsd:unique> Uniqueness is declared at the same level as the complex type definition for the “Product” element. The tc: namespace is for the schema you are creating and is declared in the xsd:schema element. The xsd:selector value specifies the elements to constrain (the “Product” elements), and xsd:field specifies the attribute to constrain (productID). You can use xsd:key and xsd:keyref to create references between elements. For example, you can create a “Customer” element and then reference it from multiple “Invoice” elements: <xsd:key name="customerID"> <xsd:selector xpath="tc:SalesData/"/> <xsd:field xpath="tc:Customer/@CustID"/> </xsd:key> <xsd:keyref name="item" refer="customerID"> <xsd:selector xpath="tc:SalesData/"/> <xsd:field xpath="tc:Invoice/@CustID"/> </xsd:keyref> To allow an element to be NULL, use the nillable attribute: <xsd:element name="FlightDinner" nillable="true"/> Using XSU to Select, Insert, Update, and Delete XML Values You can use Oracle’s XML-SQL Utility (XSU) to convert the result of a SQL query to XML. XSU supports dynamic generation of DTDs and the insertion of XML into database tables. You can also use XSU to update or delete rows from database objects. 928 Part VIII: Hitchhiker’s Guides ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:928 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:44 PM Color profile: Generic CMYK printer profile Composite Default screen Chapter 47: The Hitchhiker’s Guide to XML in Oracle 929 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:929 You can access XSU via Java and PL/SQL APIs (application programming interfaces). The XSU Java classes are part of the Oracle kernel software. The PL/SQL API is a wrapper that publishes the Java classes to PL/SQL (see Chapter 39). Because the XSU classes are stored in the database, you can write new stored procedures to directly access XSU’s Java classes. Because a PL/SQL API is available, XSU’s functionality can be accessed directly through SQL. For example, you can generate the XML version of the RATING table via the XSU PL/SQL procedures. The following listing creates a procedure named PRINTCLOBOUT that will be called as part of the data-output process. The data will be read in as a CLOB. create or replace procedure PRINTCLOBOUT(result IN OUT NOCOPY CLOB) is xmlstr varchar2(32767); line varchar2(2000); begin xmlstr := dbms_lob.SUBSTR(result,32767); loop exit when xmlstr is null; line := substr(xmlstr,1,instr(xmlstr,chr(10))-1); dbms_output.put_line('| '||line); xmlstr := substr(xmlstr,instr(xmlstr,chr(10))+1); end loop; end; / Now that the PRINTCLOBOUT procedure exists, run the following anonymous PL/SQL block. This block queries the RATING table, so that table must be accessible from your schema (see the accompanying CD-ROM). NOTE You must set serveroutput on prior to executing this PL/SQL block. declare queryCtx DBMS_XMLquery.ctxType; result CLOB; begin set up the query context queryCtx := DBMS_XMLQuery.newContext('select * from rating'); get the result result := DBMS_XMLQuery.getXML(queryCtx); Now you can use the result to put it in tables/send as messages printClobOut(result); DBMS_XMLQuery.closeContext(queryCtx); you must close the query end; / P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:45 PM Color profile: Generic CMYK printer profile Composite Default screen The result is the RATING table, formatted with XML tags: | <?xml version = '1.0'?> | <ROWSET> | <ROW num="1"> | <RATING>1</RATING> | <RATINGDESCRIPTION>ENTERTAINMENT</RATINGDESCRIPTION> | </ROW> | <ROW num="2"> | <RATING>2</RATING> | <RATINGDESCRIPTION>BACKGROUND INFORMATION</RATINGDESCRIPTION> | </ROW> | <ROW num="3"> | <RATING>3</RATING> | <RATINGDESCRIPTION>RECOMMENDED</RATINGDESCRIPTION> | </ROW> | <ROW num="4"> | <RATING>4</RATING> | <RATINGDESCRIPTION>STRONGLY RECOMMENDED</RATINGDESCRIPTION> | </ROW> | <ROW num="5"> | <RATING>5</RATING> | <RATINGDESCRIPTION>REQUIRED READING</RATINGDESCRIPTION> | </ROW> | </ROWSET> PL/SQL procedure successfully completed. Insert, Update, and Delete Processing with XSU To insert a document into a table or view, use the DBMS_XMLSave package. XSU will parse the document and create an insert statement that inserts the values into all the columns of the table or view. An absent element is treated as a NULL value. create or replace procedure INSPROC(xmlDoc IN CLOB, tableName IN VARCHAR2) is insCtx DBMS_XMLSave.ctxType; rows number; begin insCtx := DBMS_XMLSave.newContext(tableName); get the context rows := DBMS_XMLSave.insertXML(insCtx,xmlDoc); insert the doc DBMS_XMLSave.closeContext(insCtx); close the handle end; / You can now pass in XML documents as CLOB input values to the INSPROC procedure, along with the table name. INSPROC will take the XML document and insert its values into the table. Updates can be executed via calls to a separate procedure, UPDATEPROC, shown in the following listing: 930 Part VIII: Hitchhiker’s Guides ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:930 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:45 PM Color profile: Generic CMYK printer profile Composite Default screen create or replace procedure UPDATEPROC ( xmlDoc IN clob) is updCtx DBMS_XMLSave.ctxType; rows number; begin updCtx := DBMS_XMLSave.newContext('RATING'); get context DBMS_XMLSave.clearUpdateColumnList(updCtx); clear settings DBMS_XMLSave.setKeyColumn(updCtx,'RATING'); set RATING as key rows := DBMS_XMLSave.updateXML(updCtx,xmlDoc); update DBMS_XMLSave.closeContext(updCtx); close context end; / In this example, the Rating column is the key column used for the update’s where clause: DBMS_XMLSave.setKeyColumn(updCtx,'RATING'); set RATING as key The new values are passed in as an XML document in CLOB format, and the RATING table values are updated based on the contents of the XML document. Delete operations function in much the same way. The following procedure takes an XML document as its input, establishes the Rating column as the key for the RATING table, and performs the delete based on the key values in the input document. Note that newContext takes the table name RATING as its input value, whereas setKeyColumn takes the key column name (which in this case happens to also be Rating). create or replace procedure DELETEPROC(xmlDoc IN clob) is delCtx DBMS_XMLSave.ctxType; rows number; begin delCtx := DBMS_XMLSave.newContext('RATING'); DBMS_XMLSave.setKeyColumn(delCtx,'RATING'); rows := DBMS_XMLSave.deleteXML(delCtx,xmlDoc); DBMS_XMLSave.closeContext(delCtx); end; / XSU and Java If you prefer, you can perform the XSU DML operations via calls to Java classes instead of the PL/ SQL procedures. In place of the DBMS_XMLQuery package, you should execute the oracle.xml.sql .query.OracleXMLQuery class. In place of the DBMS_XMLSave package, use oracle.xml.sql.dml .OracleXMLSave. For example, the Oracle-provided sample Java program for XML queries is customized slightly in the following listing. It relies on connection management in a fashion very similar to that shown in the JDBC examples (see Chapter 38). The program imports the oracle.xml.sql.query.OracleXMLQuery class, enabling it to get the XML string; Java’s System.out.println method is then called to display the output. import oracle.xml.sql.query.OracleXMLQuery; import java.lang.*; Chapter 47: The Hitchhiker’s Guide to XML in Oracle 931 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:931 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:46 PM Color profile: Generic CMYK printer profile Composite Default screen create function GET_XML (in_SQL varchar2) return CLOB is queryCtx DBMS_XMLQuery.ctxType; result CLOB; begin queryCtx := DBMS_XMLQuery.newContext(in_SQL); result := DBMS_XMLQuery.getXML(queryCtx); DBMS_XMLQuery.closeContext(queryCtx); return result; end; / The data will be returned in CLOB format, so you can use the set long command to set the maximum display length (the default is 80 characters). You can call GET_XML from within SQL queries, as shown in the following listing: set pagesize 100 long 2000 select GET_XML('select * from category') from DUAL; GET_XML('SELECT*FROMCATEGORY') <?xml version = '1.0'?> <ROWSET> <ROW num="1"> <CATEGORYNAME>ADULTREF</CATEGORYNAME> <PARENTCATEGORY>ADULT</PARENTCATEGORY> <SUBCATEGORY>REFERENCE</SUBCATEGORY> </ROW> <ROW num="2"> <CATEGORYNAME>ADULTFIC</CATEGORYNAME> <PARENTCATEGORY>ADULT</PARENTCATEGORY> <SUBCATEGORY>FICTION</SUBCATEGORY> </ROW> <ROW num="3"> <CATEGORYNAME>ADULTNF</CATEGORYNAME> <PARENTCATEGORY>ADULT</PARENTCATEGORY> <SUBCATEGORY>NONFICTION</SUBCATEGORY> </ROW> <ROW num="4"> <CATEGORYNAME>CHILDRENPIC</CATEGORYNAME> <PARENTCATEGORY>CHILDREN</PARENTCATEGORY> <SUBCATEGORY>PICTURE BOOK</SUBCATEGORY> </ROW> <ROW num="5"> <CATEGORYNAME>CHILDRENFIC</CATEGORYNAME> <PARENTCATEGORY>CHILDREN</PARENTCATEGORY> <SUBCATEGORY>FICTION</SUBCATEGORY> </ROW> <ROW num="6"> <CATEGORYNAME>CHILDRENNF</CATEGORYNAME> <PARENTCATEGORY>CHILDREN</PARENTCATEGORY> Chapter 47: The Hitchhiker’s Guide to XML in Oracle 933 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:933 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:46 PM Color profile: Generic CMYK printer profile Composite Default screen 934 Part VIII: Hitchhiker’s Guides ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:934 <SUBCATEGORY>NONFICTION</SUBCATEGORY> </ROW> </ROWSET> Using XMLType You can use XMLType as a datatype in your tables. XMLType can be used to store and query XML data in the database. As a type, XMLType has member functions (see Chapter 33) to access, extract, and query XML data using a class of operations known as Xpath expressions. The SYS_ XMLGEN, SYS_XMLAGG, and DBMS_XMLGEN packages create XMLType values from existing object-relational data. When you designate a column as using the XMLType datatype, Oracle will store the data internally in a CLOB datatype. The following listing shows the creation of a table using the XMLType datatype: create table MY_XML_TABLE (Key1 NUMBER, Xml_Column SYS.XMLTYPE); If you describe the table, you will see its columns: desc MY_XML_TABLE Name Null? Type KEY1 NUMBER XML_COLUMN SYS.XMLTYPE If you increase the describe depth, you will see the member functions for the XMLType column as well. The following listing shows the first few of the many functions and methods available: set describe depth 5 desc MY_XML_TABLE Name Null? Type KEY1 NUMBER XML_COLUMN SYS.XMLTYPE METHOD STATIC FUNCTION CREATEXML RETURNS XMLTYPE Argument Name Type In/Out Default? XMLDATA CLOB IN METHOD MEMBER FUNCTION EXTRACT RETURNS XMLTYPE Argument Name Type In/Out Default? XPATH VARCHAR2 IN P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:47 PM Color profile: Generic CMYK printer profile Composite Default screen Chapter 47: The Hitchhiker’s Guide to XML in Oracle 935 ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:935 METHOD MEMBER FUNCTION EXISTSNODE RETURNS NUMBER Argument Name Type In/Out Default? XPATH VARCHAR2 IN METHOD MEMBER FUNCTION ISFRAGMENT RETURNS NUMBER METHOD MEMBER FUNCTION GETCLOBVAL RETURNS CLOB METHOD MEMBER FUNCTION GETSTRINGVAL RETURNS VARCHAR2 METHOD MEMBER FUNCTION GETNUMBERVAL RETURNS NUMBER You can now insert values into MY_XML_TABLE by calling XMLType’s CREATEXML method: insert into MY_XML_TABLE (Key1, Xml_Column) values (1, SYS.XMLTYPE.CREATEXML ('<book> <title>MY LEDGER</title> <chapter num= "1 "> <title>Beginning</title> <text>This is the ledger of G.B. Talbot</text> </chapter> </book>')); You can query the data by calling the GETCLOBVAL method. For example, the query select M.Xml_Column.GETCLOBVAL() as XML_Data from MY_XML_TABLE M where Key1 = 1; returns this data: XML_DATA <book> <title>MY LEDGER</title> <chapter num="1"> <title>Beginning</title> <text>This is the ledger of G.B. Talbot</text> </chapter> |</book> P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:48 PM Color profile: Generic CMYK printer profile Composite Default screen NOTE You can create functional indexes on columns created with the XMLType datatype to improve the performance of queries. Other Features This chapter has shown a very high-level view of the interaction of XML data with the Oracle database. There are many other possibilities, including the use of style sheets for data formatting, and creating XSQL pages. You can even create text indexes on XMLType columns: create index XML_INDEX on MY_XML_TABLE (Xml_Column) indextype is ctxsys.context; NOTE See Chapter 25 for further details on text indexes. Oracle provides the following packages to interact with XML data: DBMS_XMLGEN Converts the results of SQL queries to canonical XML format, returning them as a CLOB. DBMS_XMLGEN is implemented in C and compiled in the database kernel. DBMS_XMLGEN is similar in functionality to the DBMS_XMLQuery package. SYS_XMLGEN Generates XML within SQL queries. DBMS_XMLGEN and other packages operate at a query level, giving aggregated results for the entire query. SYS_ XMLGEN operates on a single argument inside a SQL query and converts the result to XML. SYS_XMLGEN takes in a scalar value, object type, or an XMLType instance to be converted to an XML document. It also takes an optional XMLGenFormatType object to specify formatting options for the result. SYS_XMLGEN returns an XMLType. SYS_XMLAGG Aggregates over a set of XMLTypes. SYS_XMLAGG aggregates all the input XML documents/fragments and produces a single XML document by concatenating XML fragments and adding a top-level tag that defaults to ROWSET. Oracle provides utilities to support additional uses, such as oraxml (a command-line interface to parse an XML document) and oraxsl (a command-line interface used to apply a style sheet on multiple XML documents). A full discussion of every utility and data-access method is beyond the scope of this book; see the XML documentation provided with Oracle Database 10 g for additional options and sample programs. See the Alphabetical Reference for descriptions of functions that act on XMLType instances, including EXTRACT, EXTRACTVALUE, SYS_XMLAGG, SYS_XMLGEN, UPDATEXML, XMLAGG, XMLCONCAT, and XMLELEMENT. 936 Part VIII: Hitchhiker’s Guides ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:936 P:\010Comp\Oracle8\351-7\CD\Ventura\book.vp Friday, August 13, 2004 1:53:49 PM Color profile: Generic CMYK printer profile Composite Default screen ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 1 Blind Folio 1:937 PART IX Alphabetical Reference Copyright © 2003, 2004, Oracle Corporation. All rights reserved. This appendix contains Oracle Corporation User Documentation reproduced herein with permission of Oracle Corporation, and offers brief summaries of the most common SQL commands. The commands are arranged alphabetically. This material is extracted from Oracle Database SQL Reference Release 10 g, SQL*Plus User’s Guide and Reference, and Oracle Database Utilities 10 g Release 1. P:\010Comp\Oracle8\351-7\Part9A.vp Friday, August 13, 2004 3:12:22 PM Color profile: Generic CMYK printer profile Composite Default screen [...]... screen 9 38 Part IX: ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 1 Blind Folio 1:9 38 Alphabetical Reference T his chapter contains references for most major Oracle commands, keywords, features, and functions, with cross-referencing of topics The reference is intended for use by both developers and users of Oracle, but assumes some familiarity with the products Reading the. .. name, and %ROWTYPE with the table name, and then select a row into the record Because the variable has the same structure as the table, you can reference the fields as variable.field, using notation similar to P:\010Comp \Oracle8 \351-7\Part9A.vp Friday, August 13, 2004 3:12:27 PM Color profile: Generic CMYK printer profile Composite Default screen ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney /... Default screen 9 58 Part IX: ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 1 Blind Folio 1:9 58 Alphabetical Reference startup_clauses::= recovery_clauses::= general_recovery::= P:\010Comp \Oracle8 \351-7\Part9A.vp Friday, August 13, 2004 3:12: 48 PM Color profile: Generic CMYK printer profile Composite Default screen ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney /... Default screen ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 1 Blind Folio 1:939 Other Formatting Guidelines SEE ALSO suggests other topics that are closely related to the keyword, or gives the chapter or chapters in the book that give detailed descriptions of how the keyword is used in practice Occasionally you will be referred to an Oracle manual or other reference book... real command The style of the examples is not the same as the style of the FORMAT section Instead, it follows the style of the first part of this book (described in Chapter 3), since that style is more typical of real coding practice The Order of the Keywords This reference is in alphabetical order, with all entries that begin with symbols coming before the first entry beginning with the letter A Words... instances, the text refers you to the Oracle manual in which you can find the needed information General Format of Entries Entries in this reference are typically either definitions of terms or descriptions of functions, commands, and keywords These are usually structured in seven sections: the keyword, its type, the products in which it is used, a SEE ALSO cross -reference, the format in which the keyword... for the value of the variable HIDE suppresses the user’s entry, and is valuable for passwords and the like EXAMPLE The following prompts the user with “How hot?” and puts the user’s entry in a number variable named Temperature: P:\010Comp \Oracle8 \351-7\Part9A.vp Friday, August 13, 2004 3:12:42 PM Color profile: Generic CMYK printer profile Composite Default screen ORACLE Series TIGHT / Oracle Database. .. where they appear, as must words shown in uppercase Standard Usages for Variables Some standard usages for variables follow: This Variable Indicates column The name of a column database The name of a database link The name of a link in Oracle Net password A password segment The name of a segment table The name of a table tablespace The name of a tablespace user The name of a user or owner view The name... value2 For CONTEXT indexes, a – tells the text search of two terms to subtract the score of the second term’s search from the score of the first term’s search before comparing the result to the threshold score For CTXCAT indexes, a – tells the text search engine to exclude the row from the result set if the term following the – is found - (Hyphen [Form 2]) SEE ALSO FORMAT Chapter 6 command text text... this is another comment where Section = 'F'; (Period or Dot [Form 1]) (SQL*Plus) FORMAT &variable.suffix P:\010Comp \Oracle8 \351-7\Part9A.vp Friday, August 13, 2004 3:12:33 PM 947 Color profile: Generic CMYK printer profile Composite Default screen 9 48 Part IX: ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 1 Blind Folio 1:9 48 Alphabetical Reference DESCRIPTION The is a . to display the output. import oracle. xml.sql.query.OracleXMLQuery; import java.lang.*; Chapter 47: The Hitchhiker’s Guide to XML in Oracle 931 ORACLE Series TIGHT / Oracle Database 10g: TCR /. column database The name of a database link The name of a link in Oracle Net password A password segment The name of a segment table The name of a table tablespace The name of a tablespace user The. from database objects. 9 28 Part VIII: Hitchhiker’s Guides ORACLE Series TIGHT / Oracle Database 10g: TCR / Loney / 225351-7 / Chapter 47 Blind Folio 47:9 28 P:10Comp Oracle8 351-7CDVenturaook.vp Friday,