Building Oracle XML Applications phần 7 potx

89 373 0
Building Oracle XML Applications phần 7 potx

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

emps emp_list; Holds a list of Employees emp emp_t; Holds a single Employee BEGIN Insert the master INSERT INTO dept( deptno, dname ) VALUES (:new.deptno, :new.dname ); Insert the details, using value of :new.deptno as the DEPTNO emps := :new.employees; FOR i IN 1 emps.COUNT LOOP emp := emps(i); INSERT INTO emp(deptno, empno, ename, sal) VALUES (:new.deptno, emp.empno, emp.ename, emp.sal); END LOOP; END; This trigger inserts the :new.deptno and :new.dname values into the dept table, then loops over the collection of <Employees> being inserted and inserts the values of each one as a new row in the emp table, using the value of :new.deptno as the DEPTNO foreign key value in the emp table. With the department view in place, and the <DepartmentList> datagram transformed into canonical format in the deptemp-transformed.xml file, we can insert the transformed document using OracleXML like this: java OracleXML putXML -user xmlbook/xmlbook -fileName deptemp-transformed.xml department This successfully inserts two rows into the department object view, with each row inserted in the object view translating into one row in the dept table and one or more rows added to the emp table. 12.2.6 Inserting Datagrams with Document Fragments The <product-list> datagram in Example 12.25 contains the nested structure for <weight>, <image>, and <dimensions>, as well as an embedded document fragment of well-formed HTML in its <features> element. Example 12.25. Product List Datagram with Nested Structure <product-list> <product> <sku>1234567</sku> <class>CD Player</class> <manufacturer>Sony</manufacturer> <model>D-F411</model> <weight> <units>g</units> <amount>260</amount> </weight> <image> <small>df-411.gif</small> <large>df-411-large.gif</large> </image> <dimensions> <units>mm</units> <width>133</width> <length>152</length> <height>33</height> </dimensions> <price>159.95</price> <features> <ul> <li> <b>SteadySound</b>, The Next Generation Of Skip Protection, surpasses Sony's current 20-second Buffer Memory system. </li> <li> <b>Synthesized Digital AM/FM Stereo Tuner</b> precisely locks in the most powerful signal for accurate, drift-free reception. </li> <li> New <b>Compact Design</b> has attractive yet functional styling </li> </ul> </features> </product> </product-list> Since we've dealt with some of these situations in previous examples, we'll move a little faster through this example to get to the new nuance—the handling of the nested document fragment. To capture the nested structure of <weight>, <image>, and <dimensions>, we create three appropriate user-defined types: CREATE TYPE weight_type as object( units VARCHAR2(5), amount NUMBER ); CREATE TYPE dimension_type as object( units VARCHAR2(5), width NUMBER, length NUMBER, height NUMBER ); CREATE TYPE imagegroup_type as object( small VARCHAR2(200), large VARCHAR2(200) ); Then we make use of these types in appropriate column names of a table to store the product datagrams we receive: CREATE TABLE product( sku NUMBER PRIMARY KEY, class VARCHAR2(80), manufacturer VARCHAR2(80), model VARCHAR2(80), weight weight_type, image imagegroup_type, dimensions dimension_type, price NUMBER, features VARCHAR2(4000) /* Could be a CLOB if you need > 4K */ ); If we can make the assumption that the features blurb of well-formed HTML will not exceed 4096 characters (4K), then we can use a VARCHAR2(4000) column to store these. If this is not a safe assumption, then it would be better to use a CLOB column that can store up to two gigabytes of document fragment. For this example, we'll stick with the VARCHAR2(4000) column for features. Since we want to store the <features> element's nested well-formed HTML content as a textual document fragment, we must transform the resulting element tree: <ul> <li> <b>SteadySound</b>, The Next Generation Of Skip Protection, surpasses Sony's current 20-second Buffer Memory system. </li> <li> <b>Synthesized Digital AM/FM Stereo Tuner</b> precisely locks in the most powerful signal for accurate, drift-free reception. </li> <li> New <b>Compact Design</b> has attractive yet functional styling </li> </ul> by parsing the <product-list> datagram into a single text string. We can do that by leveraging the xmlMarkup XSLT extension function that we will build in Chapter 16 , as part of an identity transformation on the original <product-list> datagram. For now, suffice it to say that an XSLT transformation can invoke methods on user-defined Java objects when some functionality is more easily implemented in Java than in pure XSLT. Example 12.26 shows the insert-product.xsl XSLT transformation we need. Example 12.26. Transforming Document Fragment to Literal XML Markup <! insert-product.xsl > <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ext="http://www.oracle.com/XSL/Transform/java/MarkupExtensions" exclude-result-prefixes="ext"> <! Start with the identity Transformation > <xsl:include href="identity.xsl"/> <! | Use our xmlMarkup( ) extension function to write | out the features nested XML content as literal | XML text markup. + > <xsl:template match="features"> <features> <xsl:value-of select="ext:xmlMarkup(*)"/> </features> </xsl:template> </xsl:stylesheet> This example uses <xsl:include> to include the template for the identity transformation and augments this with an <xsl:template> that matches the <features> element: <xsl:template match="features"> <features> <xsl:value-of select="ext:xmlMarkup(*)"/> </features> </xsl:template> This template outputs the <features> element of each <product> as a textual "printout" of the nested elements within it. So, the result of transforming the <product-list> datagram using the insert-product.xsl stylesheet: oraxsl product-list.xml insert-product.xsl product-to-insert.xml is a product-to-insert.xml file that looks like Example 12.27 . Example 12.27. Results of Quoting the XML Markup for Product Features <?xml version = '1.0' encoding = 'UTF-8'?> <product-list> <product> <sku>1234567</sku> <class>CD Player</class> <manufacturer>Sony</manufacturer> <model>D-F411</model> <weight> <units>g</units> <amount>260</amount> </weight> <image> <small>df-411.gif</small> <large>df-411-large.gif</large> </image> <dimensions> <units>mm</units> <width>133</width> <length>152</length> <height>33</height> </dimensions> <price>159.95</price> <features>&#60;ul> &#60;li> &#60;b>SteadySound&#60;/b>, The Next Generation Of Skip Protection surpasses Sony's current 20-second Buffer Memory system. &#60;/li> &#60;li> &#60;b>Synthesized Digital AM/FM Stereo Tuner&#60;/b> precisely locks in the most powerful signal for accurate, drift-free reception. &#60;/li> &#60;li> New &#60;b>Compact Design&#60;/b> has attractive yet functional styling &#60;/li> &#60;/ul> </features> </product> </product-list> In this example, all the opening angle bracket characters in the content of the <features> element have been replaced by the numerical character entity &#60; to represent a literal less-than sign, which is synonymous with the named character entity &lt;. Now we can use the following command to insert the transformed document into the product table: java OracleXML putXML -user xmlbook/xmlbook -fileName product-to-insert.xml -rowTag product -ignoreCase product To complete the round-trip into the database and back out, let's look at how we would dynamically serve a <product-list> datagram out of our product table for a product with a particular SKU number on request over the Web. Just as we used an XSLT transformation to convert the nested content of the <features> element to a text fragment of XML markup on the way into the database, we'll use XSLT again on the way out to turn the text fragment back into nested elements of the datagram we serve. We'll use a similar transformation that performs the identity transformation on all elements of the document except <features>, which we'll handle in a special way. Example 12.28 shows the required transformation. Example 12.28. Transforming Document Fragment Text into Elements <! features-frag-to-elts.xsl > <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes"/> <xsl:include href="identity.xsl"/> <! | <features> is a column with embedded XML markup in its | corresponding column in the database. By disabling | the output escaping it will be included verbatim | (i.e. angle-brackets intact instead of &lt; and &gt;) | in the resulting document + > <xsl:template match="features"> <features> <xsl:value-of select="." disable-output-escaping="yes"/> </features> </xsl:template> </xsl:stylesheet> Example 12.28 uses the disable-output-escaping="yes" attribute on <xsl:value-of> to request that the XSLT Processor include the text of the features document fragment verbatim in the transformed output instead of "escaping" the less-than signs in the text with a character entity like lt; or #60;. To serve the <product-list> datagram, we just create a simple XSQL page with a SELECT * FROM PRODUCT query, and associate it with the features-frag-to-elts.xsl transformation in its <?xml-stylesheet?> instruction: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="features-frag-to-elts.xsl"?> <xsql:query xmlns:xsql="urn:oracle-xsql" connection="xmlbook" rowset-element="product-list" row-element="product" id-attribute="" tag-case="lower"> SELECT * FROM product WHERE sku = {@sku} </xsql:query> When a request comes in for this XSQL page with an appropriate sku parameter like: http://yourserver/product.xsql?sku=1234567 this delivers a <product-list> datagram that looks exactly like what we started with at the beginning of this section. You can use the output of one XSQL page as the input of another to format a dynamic XML datagram including document fragments as an attractive web page. Assuming that show-product.xsl is an XSLT stylesheet that transforms a <product-list> datagram into eye-catching HTML, you can use the <xsql:include-xsql> tag to include the output of the product.xsql page as part of the input data for another XSQL page called show-product.xsql. Simply create a show-product.xsql page that looks like: <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="show-product.xsl"?> <xsql:include-xsql href="product.xsql?sku={@sku}" xmlns:xsql="urn:oracle-xsql" /> This associates the show-product.xsl stylesheet to the page. Now a web request for: http://yourserver/show-product.xsql?sku=1234567 uses the show-product.xsl stylesheet to transform the output of the product.xsql page, a <product-list> datagram, into a lovely web page. 12.3 Storing Posted XML Using XSQL Servlet We've seen that the general steps for inserting XML into the database are as follows: 1. Choose the table or view you want to use for inserting the XML information. 2. Create an XSL transformation that transforms the inbound document into the canonical format for this table or view. 3. Transform the inbound document into the canonical format for the table or view into which it will be inserted. 4. Insert the transformed document into your table or view with the OracleXML utility. The Oracle XML SQL Utility works well for inserting XML documents you have in front of you in operating system files. However, if you need to have other computers post live XML information to your web site for insertion into your database, you'll need to use a slight twist on this approach. 12.3.1 Storing Posted XML Using XSQL Pages The Oracle XSQL Servlet supports the <xsql:insert-request> action element, which you can include in any XSQL page to automate these steps: 1. Read a posted XML document from the HTTP request. 2. Transform it into the canonical format for insertion using any XSLT transformation you provide. 3. Insert the transformed document into the table or view of your choice. 4. Indicate the status of the operation by replacing the <xsql:insert-request> action element with an <xsql-status> element to show how many rows were inserted or to report an error. Behind the scenes, the insert_request action handler makes programmatic use of the Oracle XSLT Processor to do the transformation and the Oracle XML SQL Utility to do the insert, so everything we've learned earlier applies here. Given the name of the table or view to use as the insert target and the name of the XSLT transformation to use, you add the following tag anywhere in your XSQL page: <xsql:insert-request table="table_or_view_name" transform="transformname.xsl"/> to transform and insert the posted XML document. For example, recall the Moreover.com news feed from Example 12.3 and the moreover-to-newsstory.xsl transformation we created in Example 12.5 . The XSQL page in Example 12.29 is all you need to insert posted XML news stories instead over the Web into your newsstory table. Example 12.29. XSQL Page to Insert Posted XML News Stories <?xml version="1.0"?> <! SimpleNewsInsert.xsql > <xsql:insert-request connection="xmlbook" xmlns:xsql="urn:oracle-xsql" table="newsstory" transform="moreover-to-newsstory.xsl"/> One tag, that's it! No custom servlet to write, no XML to parse, and no transformation to do manually. Deploying a new XSQL page to insert posted XML is as easy as copying the .xsql file to a directory on your web server. We can test the SimpleNewsInsert.xsql page by using the XSQL command-line utility with the command: xsql SimpleNewsInsert.xsql posted-xml=SomeFileToPost.xml or, if the source of XML is coming directly from a URL, we can do this: xsql SimpleNewsInsert.xsql posted-xml=http://example.com/someurl Setting the posted-xml command-line parameter to the name of an XML file or URL returning XML causes the XSQL page processor to treat that XML source as the posted XML source for insert. We can also test SimpleNewsInsert.xsql using any client program that can post an XML document full of news stories. One approach is to use JavaScript in an HTML page to post some XML to the server. In our example, we'll post XML the user types into a <TEXTAREA> so you can see what's going on, but the technique used in the example applies to any XML. The Internet Explorer 5.0 browser includes support for an XMLHttpRequest object that makes quick work of the task from the browser client. Example 12.30 shows the JavaScript code of the PostXMLDocument() function, which does the job of posting any XML document you pass in to the URL you pass as a parameter. Example 12.30. Function to Post XML Document to a Web Server from IE5 // PostXMLDocument.js // Uses HTTP POST to send XML Document "xmldoc" to URL "toURL" function PostXMLDocument (xmldoc, toURL) { // Create a new XMLHttpRequest Object (IE 5.0 or Higher) var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP"); // Open a synchronous HTTP Request for a POST to URL "toUrl" xmlhttp.open("POST", toURL , /* async = */ false ); // Could set HTTP Headers Here (We don't need to in this example) // xmlhttp.setRequestHeader("some-header-param","some value"); // Send the request with in-memory XML Document "xmldoc" as body xmlhttp.send(xmldoc); // Return the response from the request (assumes it is an XML Doc) return xmlhttp.responseXML; } The function does the following: 1. Creates an XMLHttpRequest Object 2. Opens the request, indicating a method of POST 3. Sends the request, passing the XML document as the request body 4. Returns the XML document sent back by the server as a response If the XSQL Servlet encounters an <xsql:insert-request> action element and there is no posted XML document in the current request, it will replace the action element in the data page with the following innocuous <xsql-status> element: <xsql-status action="xsql:insert-request" [...]... claim 778 04 .xml document in Example 13.1 like this: oraxsl claim 778 04 .xml search-cause.xsl we'll get the result: Document Contains "brakes" inside the Cause On the other hand, applying the stylesheet to the following claim document, claim 770 85 .xml, produces no output, because "brakes" is not found within the element nested inside the 778 05 .xml > 77 805... automate these steps since both the Oracle XSLT Processor (oraxsl) and the Oracle XML SQL Utility (OracleXML) can be used programmatically by any Java program The API for the Oracle XSLT Processor comprises two simple-to-use objects, XSLStylesheet and XSLProcessor, and the API for the Oracle XML SQL Utility is even simpler The OracleXMLSave object takes care of inserting XML into the database for us Here... // Create a new XML Parser Object var xmldoc = new ActiveXObject ("Microsoft.XMLDOM"); // Do the parsing synchronously xmldoc.async = false; // Parse the text in the TEXTAREA as XML xmldoc.loadXML(xmldocText.value); // Post the parsed XML document to the SimpleNewsInsert.xsql Page var response = PostXMLDocument(xmldoc, "SimpleNewsInsert.xsql"); // Display the XML text of the response in the "StatusArea"... StatusArea.innerText = response.documentElement .xml; } to Type in an XML Document in Moreover.com News Format to Post: http://technet .oracle. com/tech /xml Oracle Releases XML Parser Oracle ... processor.processXSL(transform, moreoverNewsDoc); // Create a new XML Document and append the fragment to it Document result = new XMLDocument( ); result.appendChild(df); // Pass the transformed document (now in canonical format) to OracleXMLSave Connection conn = Examples.getConnection( ); OracleXMLSave oxs = new OracleXMLSave(conn,"newsstoryview"); int rowsInserted = oxs.insertXML( result ); conn.commit( ); conn.close(... Inserting News Stories import oracle. xml. parser.v2.*; import java.io.InputStream; import org.w3c.dom.*; import java.net.URL; import java.sql.Connection; import oracle. xml. sql.dml.OracleXMLSave; import Examples; public class MoreoverIntoNewsstory { public static void main( String[] arg ) throws Exception { String theNews = "http://www.moreover.com/cgi-local/page?index _xml+ xml", theXSL = "moreover-to-newsstory.xsl";... StatusArea.innerText = "Your XML document is not well-formed.\n" + err.srcText + "\n" + "Line " + err.line + ", Pos " + err.linepos + "\n" + err.reason; return; } just after the line that reads: xmldoc.loadXML(xmldocText.value); If the user makes a mistake in the XML document, a helpful error is displayed in the status area on the browser, as shown in Figure 12 .7 Figure 12 .7 XML parse error displayed... Create a new XML Document and append the fragment to it 19 Document result = new XMLDocument( ); result.appendChild(df); 20 Constructs an OracleXMLSave object, passing it a JDBC connection and the name of the newsstoryview we want to use for the insert operation: 21 // Pass the transformed document (now in canonical format) to OracleXMLSave 22 Connection conn = Examples.getConnection( ); OracleXMLSave oxs... ctxsys.auto_section_group'); We can use this same technique to create an XML document search index over the xmldoc column in our xml_ documents table from Chapter 5, and Chapter 6 This enables fast XML searches over the XML documents stored there using the techniques in this chapter The syntax is: CREATE INDEX xmldoc_idx ON xml_ documents(xmldoc) INDEXTYPE IS ctxsys.context PARAMETERS ('section group ctxsys.auto_section_group');... to posting news stories directly from the Moreover.com XML news feed Using the XSQL command-line utility we can insert the entirety of the live XML news feed using the command: xsql SimpleNewsInsert.xsql posted -xml= http://www.moreover.com/cgi-local/ page?index _xml+ xml This will treat the XML document retrieved from the provided URL as the posted XML to the SimpleNewsInsert.xsql page, transform the results . deptemp-transformed .xml file, we can insert the transformed document using OracleXML like this: java OracleXML putXML -user xmlbook/xmlbook -fileName deptemp-transformed .xml department This. product-list .xml insert-product.xsl product-to-insert .xml is a product-to-insert .xml file that looks like Example 12. 27 . Example 12. 27. Results of Quoting the XML Markup for Product Features < ?xml. xmldoc.loadXML(xmldocText.value); If the user makes a mistake in the XML document, a helpful error is displayed in the status area on the browser, as shown in Figure 12 .7 . Figure 12 .7. XML

Ngày đăng: 08/08/2014, 18:21

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan