Building Oracle XML Applications phần 5 doc

89 321 0
Building Oracle XML Applications phần 5 doc

Đ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

SELECT * FROM doctor WHERE id = '{@id}' </xsql:query> This parameterized XSQL page can be used to retrieve information about doctors, given the value of their email id in the requesting URL like this: http://xmlapps/examples/DoctorInfo.xsql?id=bproto which returns the datagram: <?xml version = '1.0'?> <ROWSET> <ROW num="1"> <ID>bproto</ID> <FIRSTNAME>Bryan</FIRSTNAME> <LASTNAME>Proto</LASTNAME> <HOMEOFFICE>French44</HOMEOFFICE> </ROW> </ROWSET> Parameter references like {@paramname} in an XSQL page's <xsql:query> are substituted with their appropriate parameter values from the query string whenever the page is requested. This means you can build a page that uses parameters to provide parts of the query statement, like this: <?xml version="1.0"?> <xsql:query connection="xmlbook" select="*" where="1=1" order="1" xmlns:xsql="urn:oracle-xsql"> SELECT {@select} FROM {@from} WHERE {@where} ORDER BY {@order} </xsql:query> This XSQL page allows the requester to control all the parts of the syntax of the SELECT statement. You can use XML attributes with the same names as the parameters to provide defaults in case the requester does not provide a specific value. Taking the last example a step further, you can build a page that uses a parameter to represent the entire SQL statement: <?xml version="1.0"?> <xsql:query connection="xmlbook" xmlns:xsql="urn:oracle-xsql"> SELECT * FROM ( {@sql} ) </xsql:query> Such a page allows any requester with the authorization to access the page the ability to return XML datagrams for any SQL statement on your server. This obviously is not something you would want to use on a production system without appropriate authentication. Since XSQL pages can be secured using standard web page security mechanisms, this access can be set up so it is not abused. 8.1.4 Customizing the XSQL Results The most basic way to get XML results from queries into your XSQL page is to include the text of the desired SQL query between the <xsql:query> and </xsql:query> tags. Using this technique, each query in the page will produce the following structure by default: • A <ROWSET> element containing the query's results • A <ROW> element and a row identifier, for each row in the result set • A child element for each non-NULL column in each <ROW> The element names for each column in the result set are derived from the column names or column aliases if provided. 8.1.4.1 Changing element names by aliasing columns You can use the standard SQL columnname AS alias syntax to rename a column included in the SELECT list or to specifically provide a column alias for a SQL expression like this: <?xml version="1.0"?> <xsql:query connection="scott" xmlns:xsql="urn:oracle-xsql"> SELECT dept.deptno AS department, SUM(sal) AS "TOTAL-SALARIES" FROM emp,dept WHERE dept.deptno = emp.deptno GROUP BY dept.deptno ORDER BY 1 </xsql:query> If you include the column alias in double quotes, the alias will be used verbatim; otherwise, names are converted to uppercase. In this case, the double quotes are required, since a name with a dash in it, like TOTAL-SALARIES in the following example, is not a valid SQL column name: <?xml version = '1.0'?> <ROWSET> <ROW num="1"> <DEPARTMENT>10</DEPARTMENT> <TOTAL-SALARIES>8750</TOTAL-SALARIES> </ROW> <ROW num="2"> <DEPARTMENT>20</DEPARTMENT> <TOTAL-SALARIES>10875</TOTAL-SALARIES> </ROW> <ROW num="3"> <DEPARTMENT>30</DEPARTMENT> <TOTAL-SALARIES>9400</TOTAL-SALARIES> </ROW> </ROWSET> 8.1.4.2 Tailoring XML query results In addition to using column aliasing and parameter values to control the query's XML output, you can also specify values for any combination of the <xsql:query> tag attributes in Table 8.1. This allows you to further refine each query's XML results. Table 8.1. xsql:query Attributes to Control XML Output Attribute Name Description rowset-element Element name to use as a parent for the rowset of query results. The default name is <ROWSET>. Set equal to the empty string to suppress printing an element for the rowset. row-element Element name to use as a parent element for each row in the query results. The default name is <ROW>. Set equal to the empty string to suppress printing a row element. max-rows Maximum number of rows to fetch from the query. Useful in combination with an ORDER BY in your query for fetching the top-N rows. When used in combination with skip-rows, it's easy to implement getting the next-N rows from a query result to page through a set of query results. The default is to fetch all rows. skip-rows Number of rows to skip over before returning the query results. The default is not to skip any rows. id-attribute Attribute name for the row identifier attribute for each row in the query result. The default is num. id-attribute-column Column name to use to supply the value of the row identifier attribute for each row in the query result. The default is to use the row count as the value. null-indicator If set to y or yes, causes a null-indicator attribute to be used on the element for any column whose value is NULL. The default is to omit the element in the result for any column with a NULL value. tag-case If set to upper, causes the element names for columns in the query result to be in uppercase. If set to lower, causes the element names for columns in the query result to be in lowercase. The default is to use the case of the column name (or column aliases if provided) from the query. fetch-size Explicitly sets the number of records retrieved in each round-trip to the database for this query. This overrides the default fetch size set in the XSQL configuration file. For example, using rowset-element and row-element you can serve a datagram for a list of department numbers and a sum of all the salaries in each department as a <DEPARTMENT-LIST> document with nested <DEPARTMENT> elements. The XSQL page for this looks like the following: <?xml version="1.0"?> <xsql:query connection="xmlbook" xmlns:xsql="urn:oracle-xsql" rowset-element="DEPARTMENT-LIST" row-element="DEPARTMENT"> SELECT dept.deptno AS "NUMBER", sum(sal) AS "TOTAL-SALARIES" FROM emp,dept WHERE dept.deptno = emp.deptno GROUP BY dept.deptno ORDER BY 1 </xsql:query> Requesting this page produces the result: <?xml version = '1.0'?> <DEPARTMENT-LIST> <DEPARTMENT num="1"> <NUMBER>10</NUMBER> <TOTAL-SALARIES>8750</TOTAL-SALARIES> </DEPARTMENT> <DEPARTMENT num="2"> <NUMBER>20</NUMBER> <TOTAL-SALARIES>10875</TOTAL-SALARIES> </DEPARTMENT> <DEPARTMENT num="3"> <NUMBER>30</NUMBER> <TOTAL-SALARIES>9400</TOTAL-SALARIES> </DEPARTMENT> </DEPARTMENT-LIST> To suppress the row identifier attribute, and force all of the XML element names to lowercase, we can add the id-attribute="" and tag-case="lower" attributes: <?xml version="1.0"?> <xsql:query connection="xmlbook" id-attribute="" tag-case="lower" rowset-element="DEPARTMENT-LIST" row-element="DEPARTMENT" xmlns:xsql="urn:oracle-xsql"> SELECT dept.deptno AS "NUMBER", sum(sal) AS "TOTAL-SALARIES" FROM emp,dept WHERE dept.deptno = emp.deptno GROUP BY dept.deptno ORDER BY 1 </xsql:query> This produces the expected changes in the resulting datagram: <?xml version = '1.0'?> <department-list> <department> <number>10</number> <total-salaries>8750</total-salaries> </department> <department> <number>20</number> <total-salaries>10875</total-salaries> </department> <department> <number>30</number> <total-salaries>9400</total-salaries> </department> </department-list> You may set either or both rowset-element and row-element to the empty string to suppress their output. Doing so can be useful when you know that the results of the query will produce a single row or a single column and you don't need the extra levels of <rowset> and <row> elements in the resulting document. As an example, the following XSQL page: <?xml version="1.0"?> <xsql:query connection="xmlbook" id-attribute="" xmlns:xsql="urn:oracle-xsql" rowset-element="" row-element=""> SELECT sum(sal) AS "TOTAL" FROM emp,dept WHERE dept.deptno = emp.deptno AND dept.deptno = {@dept} GROUP BY dept.deptno ORDER BY 1 </xsql:query> produces this one-element document containing only the <TOTAL> tag generated for the TOTAL column in the results: <?xml version = '1.0'?> <TOTAL>10875</TOTAL> When suppressing rowset-element or row-element in an XSQL page comprised of only a single query tag, you must ensure that your results still generate a well-formed XML document. For example, this XSQL page, which specifies both rowset-element="" and row-element="": <?xml version="1.0"?> <xsql:query connection="xmlbook" rowset-element="" row-element="" xmlns:xsql="urn:oracle-xsql"> /* This query returns one row, two columns */ SELECT 1 AS VALUE1, 2 AS VALUE2 FROM DUAL </xsql:query> would generate the following result: <?xml version = '1.0'?> <VALUE1>1</VALUE1> <VALUE2>2</VALUE2> This is not a well-formed XML document, since it has two top-level elements instead of the single document element required by the XML specification. Similarly, an example like this: <?xml version="1.0"?> <xsql:query connection="xmlbook" rowset-element="" row-element="PERSON" xmlns:xsql="urn:oracle-xsql"> /* This query returns two rows of one column each */ SELECT 'Emma' AS NAME FROM DUAL UNION SELECT 'Amina' AS NAME FROM DUAL </xsql:query> returns two rows but suppresses the rowset-element: <?xml version = '1.0'?> <PERSON> <NAME>Emma</NAME> </PERSON> <PERSON> <NAME>Amina</NAME> </PERSON> This again, is not well-formed XML. In both cases, rather than returning an invalid page, the XSQL page processor will return an error: Oracle XSQL Command Line page processor XSQL-014: Resulting page is an empty document or had multiple document elements. If you are building a page with a query that you think should return a single row, you can guarantee that only a single row will be returned by using the max-rows="1" attribute on your query tag to avoid this situation. 8.1.4.3 Using parameters in <xsql:query> tag attributes In addition to using parameters in the SQL statement for an <xsql:query> tag, the values of <xsql:query> tag attributes can also reference parameters. This makes it easy for the requester to pass in values for the attributes that control the query's XML output. For example, a page like this: <?xml version="1.0"?> <xsql:query connection="xmlbook" cat="news" skip="0" max-rows="2" skip-rows="{@skip}" xmlns:xsql="urn:oracle-xsql"> SELECT title,url FROM site_entry WHERE categories like '%'||UPPER('{@cat}')||'%' ORDER BY id desc </xsql:query> uses the following <xsql:query> tag attributes: cat="news" Specifies news as the default category for requested headlines skip="0" Defaults the value of the skip parameter to zero max-rows="2" Shows at most two rows of results skip-rows="{@skip}" Skips the number of rows specified in the skip parameter before returning data So the following URL: http://xmlapps/examples/BrowseHeadlines.xsql produces an XML datagram showing the first two stories from the news category: <?xml version = '1.0'?> <ROWSET> <ROW num="1"> <TITLE>New XML Content on Oracle TechNet</TITLE> <URL>http://technet.oracle.com/tech/xml</URL> </ROW> <ROW num="2"> <TITLE>XML.org Premieres As Repository For Schemas</TITLE> <URL>http://www.infoworld.com/cgi-bin/displayStory.pl?990526.ice.htm</URL> </ROW> </ROWSET> If we add the URL parameter skip=2, then the skip-rows="{@skip}" attribute on the query tag will evaluate to 2, overriding the default value of zero. So the result of requesting: http://xmlapps/examples/BrowseHeadlines.xsql?skip=2 is the XML datagram showing just the third and fourth rows of the results: <?xml version = '1.0'?> <ROWSET> <ROW num="3"> <TITLE>Microsoft's Maritz at TechEd on XML</TITLE> <URL>http://www.infoworld.com/cgi-bin/displayStory.pl?990524.icmaritz.htm</URL> </ROW> <ROW num="4"> <TITLE>IBM XML Translator Generator</TITLE> <URL>http://www.alphaworks.ibm.com/tech/xmltranslatorgenerator</URL> </ROW> </ROWSET> Providing a value for the cat parameter in the URL allows the requester to request a different category of stories. The following request: http://xmlapps/examples/BrowseHeadlines.xsql?cat=software&skip=4 shows the fifth and sixth news stories in the software category: <?xml version = '1.0'?> <ROWSET> <ROW num="5"> <TITLE>Beta 1.5 of Microsoft XML Notepad</TITLE> <URL>http://msdn.microsoft.com/xml/notepad/intro.asp</URL> </ROW> <ROW num="6"> <TITLE>SAXON 4.2 Released</TITLE> <URL>http://home.iclweb.com/icl2/mhkay/saxon.html</URL> </ROW> </ROWSET> Here we've seen parameters in use for attribute values to control the behavior of individual <xsql:query> tags, but as a final note to this section, we recall that it's also possible to use a parameter to control the page-level connection. By simply including a parameter in the connection attribute on the document element, you can build a page that allows the requester to provide the name of the named connection as a parameter to the request. So, with a page that begins like this: <page xmlns:xsql="urn:oracle-xsql" connection="{@conn}"> the requester can provide a parameter like conn="prod" or conn="test" to select between the production database whose named definition is prod and the test database instance whose named definition is test. Using the techniques we learned earlier, a default value for the conn attribute can be supplied using an attribute of that name on the document element, like this: <page xmlns:xsql="urn:oracle-xsql" conn="prod" connection="{@conn}"> This causes the page to default to the connection named prod, but allows the conn="test" parameter to be supplied to override the default value and use the test database connection for a particular request. 8.1.5 Using Multiple Queries on a Page Our examples so far have all used a single <xsql:query> tag as the only element in the XSQL page. However, there is no limit to the number of queries that can appear in a page or to where the <xsql:query> tags can appear in the page. The only overriding rule is that the XSQL page itself must be a well-formed XML document: an <xsql:query> tag can appear anywhere an element can appear in a document: <?xml version="1.0"?> <a-query connection="xmlbook" xmlns:xsql="urn:oracle-xsql"> <can-go-here/> <xsql:query> SELECT * FROM table1 </xsql:query> <or-here> <xsql:query> SELECT * FROM table2 </xsql:query> </or-here> Or even <xsql:query> SELECT * FROM table3 </xsql:query> here. </a-query> Conversely, anywhere it would be illegal to put an XML element in a well-formed document is an illegal place for an <xsql:query> tag. The following page shows three illegal places for an <xsql:query> tag to appear: <?xml version="1.0"?> <a-query connection="xmlbook" xmlns:xsql="urn:oracle-xsql"> <cannot go-here="<xsql:query> SELECT * FROM table1 </xsql:query>"/> <or-here<xsql:query> SELECT * FROM table2</xsql:query>/> <! Ok here, but has no effect in a comment <xsql:query> SELECT * FROM table3 </xsql:query> > </a-query> <but-not-here> <xsql:query> SELECT * FROM table4 </xsql:query> </but-not-here> [...]... the web request: http://xmlapps/examples/Bookmark.xsql The requester won't get the basic raw data page delivered as an XML datagram: < ?xml version = '1.0'?> Free XML Software http://www.stud.ifi.uio.no/~larsga/linker/XMLtools.html Oracle Technet XML Page http://technet .oracle. com/tech /xml ... processing instruction in your document The simplest way to feed a static XML document to the XSQL page processor is to rename it to have an xsql file extension For example, take Jon Bosak's dream .xml, an XML version of Shakespeare's A Midsummer Night's Dream, shown in Example 8.9 Example 8.9 Excerpt from XML Version of A Midsummer Night's Dream < ?xml version="1.0"?> < ?xml- stylesheet type="text/xsl"... transforms the XSQL data page into a valid XBEL document: < ?xml version="1.0"?> < ?xml- stylesheet type="text/xsl" href="XBEL.xsl" ?> SELECT title, url FROM bookmark ORDER BY title We happen to have the XBEL.xsl stylesheet, which does just this, lying around: ... Oracle Technet XML Page Using XSQL and XSLT, any tool or program that understands bookmarks in the XBEL format could import any bookmarks from your database using the dynamically produced XBEL document as the exchange medium 8.2.4 Using a Stylesheet on Static XML Documents You can use the XSQL Servlet... Oracle Data Dictionary views USER_TAB_COLUMNS and USER_TYPE_ATTRS, we can quickly build an XSQL page like the one in Example 8 .5 to examine the structure of any table or type in our schema interactively Example 8 .5 Describe.xsql Explores the Oracle Data Dictionary < ?xml version="1.0"?> ... more dynamic later on, you can substitute the static XML tags in the XSQL page with dynamic XML results using the now familiar tag as shown in Example 8.12 Example 8.12 Querying Web Site Categories from a Table < ?xml version="1.0"?> < ?xml- stylesheet type="text/xsl" href="SiteMenu.xsl"?> < ?xml- stylesheet type="text/xsl" href="Bookmark.xsl"?> SELECT title, url FROM bookmark ORDER BY title This page queries information about web browser bookmarks from the bookmark table, and uses the following Bookmark.xsl stylesheet to format the results: ... Goodnews Bay, Alaska, Usa The requesting agent, which may be a Java program using the Oracle XML Parser or a dynamic HTML page in Internet Explorer 5. 0 with its integrated client-side XML parser, can notice that the document element of the returned XML datagram is and can proceed to collect the list of suggested matches for subsequent processing or display to... 8.2: My Bookmarks My Bookmarks Free XML Software Oracle Technet XMLPage< a> Figure 8.2 HTML list of bookmarks produced using a stylesheet It's easy to change the name of the stylesheet... http://xmlapps/examples/dream.xsql causes the XSQL page processor to transform dream.xsql using the associated shakespeare.xsl stylesheet and return the formatted results to the requester Technically speaking, it would be fine if dream.xsql had no < ?xml- stylesheet?> instruction The net effect would be to serve the XML document for A Midsummer Night's Dream to the requester verbatim Serving a raw XML document . well-formed XML document: an <xsql:query> tag can appear anywhere an element can appear in a document: < ?xml version="1.0"?> <a-query connection="xmlbook" xmlns:xsql="urn :oracle- xsql">. <TITLE>New XML Content on Oracle TechNet</TITLE> <URL>http://technet .oracle. com/tech /xml& lt;/URL> </ROW> <ROW num="2"> <TITLE> ;XML. org Premieres. resulting document. As an example, the following XSQL page: < ?xml version="1.0"?> <xsql:query connection="xmlbook" id-attribute="" xmlns:xsql="urn :oracle- xsql"

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

Từ khóa liên quan

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

Tài liệu liên quan