Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 20 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
20
Dung lượng
237,35 KB
Nội dung
Your users do not need to see the results in this form, of course. If you would just like to say “Thanks,” you can do so with this very simple stylesheet. It hides the raw XML. <?xml version=”1.0”?> <xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0”> <xsl:template match=”/”> <html> <h1>Thanks!</h1> </html> </xsl:template> </xsl:stylesheet> To use the stylesheet, you need to add the following line as the second line in your insert-request.xsql file: <?xml-stylesheet type=”text/xsl” href=”thanks.xsl” ?> Using xsql:update-request The use of xsql:update-request is very similar to the use of xsql:insert -request. The general process is exactly the same. First, you create your HTML form and set the action to an XSQL page. When the form is submitted, the XSQL servlet will pass the parameters as XML to the XSQL page processor. You need a stylesheet to transform the passed XML to the canonical XML schema. The key difference really relates to the difference between SQL inserts and updates. An insert adds an entirely new row to your table. An update, on the other hand, mod- ifies an existing row. To perform an update, XSQL needs to know which row (or rows) in the database will be modified. This is done through the key-columns attribute of the xsql:update-request element. It lists the columns that have to match for a par- ticular row of data to be updated. First things first. For this example, you can use the exact same form as was used for xsql:insert-request. Assume that the e-mail address is staying the same and that the user wants to modify the organization and name. It would be simple enough to populate the fields in the form, but you aren’t quite ready to do that yet. For now, life is hard for your users. Here is the modified HTML form that you should save as newsletter-update.html: <html> <body> <h1>Sign Up For Newsletter</h1> <form action=”update-request.xsql” method=”post”> <table border=”0”> <tr> 120 Chapter 7 <td>Name:</td> <td><input type=”text” name=”name”/></td> </tr> <tr> <td>Email:</td> <td><input type=”text” name=”email”/></td> </tr> <tr> <td>Organization:</td> <td><input type=”text” name=”org”/></td> </tr> <tr> <td colspan=”2” align=”center”> <input type=”submit” value=”Sign Me Up!” /> </td> </tr> </table> </form> </body> </html> Now you are ready to code your update-request.xsql file. It looks very much like the insert-request.xsql file that you coded before. The difference is the key-columns attribute. This attribute tells XSQL that you want to make changes in any of the rows where the email field matches the e-mail address that is entered. <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”thanks.xsl” ?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:update-request table=”newsletter” key-columns=”email” transform=”transform-request.xsl”/> </page> After saving this file as update-request.xsql, you should get the same “Thanks!” screen when you fill out the form of newsletter-update.html. You can see that this XSQL page is linked to the same thanks.xsl file as the insert -request.xsql file. There is another interesting similarity: Both XSQL pages use the same transform-request.xsl file. You are able to do this because the names of the form parameters are the same for both pages. Coincidence? Somewhat. The lesson to learn here is that with a little discipline, you can save yourself some recoding. Using xsql:delete-request The purpose of this request is to remove data from the database. All of the steps are the same as with the xsql:update-request. The main difference is that you only need to submit the data that matches the key-columns that you specify. This data is used to determine which rows should be deleted. Database Modifications with XSQL 121 Your first step is to create the HTML form. This is the same form as in the other two examples here, except that only the email field is needed. This is the key to tell you when to delete rows. <html> <body> <h1>Sign Up For Newsletter</h1> <form action=”delete-request.xsql” method=”post”> <table border=”0”> <tr> <td>Email:</td> <td><input type=”text” name=”email”/></td> </tr> <tr> <td colspan=”2” align=”center”> <input type=”submit” value=”Delete Me!” /> </td> </tr> </table> </form> </body> </html> Your next step is to create the delete-request.xsql file. It has only one differ- ence from the update-request.xsql file. Instead of using the xsql:update -request action, the xsql:delete-request action is used. <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”thanks.xsl” ?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:delete-request table=”newsletter” key-columns=”email” transform=”transform-request.xsl”/> </page> Modifying with xsql:dml Using the built-in actions covered in the preceding section is easy—if your form para- meters map easily to the underlying table. However, the required stylesheet compli- cates things a bit, especially if you are already familiar with SQL. In this section, you will use the xsql:dml action to complete the same tasks as in the preceding examples. These examples assume that you have some knowledge of SQL. If you don’t, you’ll learn all about SQL in Chapter 8. You can reuse the newsletter-insert.html form almost entirely. Only one change is necessary—the action should point at your new dml-insert.xsql. Here is line that needs to be changed: <form action=”dml-insert.xsql” method=”post”> 122 Chapter 7 Here is the dml-insert.xsql file that you need to create. The form parameters are interpolated directly to the SQL statement. This is your first glimpse at parameter handling with XSQL. You will see more of this in the next section of this chapter. <?xml version=”1.0”?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:dml> BEGIN INSERT INTO newsletter (email,name,organization) VALUES(‘{@email}’,’{@name}’,’{@org}’); COMMIT; END; </xsql:dml> </page> If you are familiar with SQL, this is far simpler than creating a XSLT stylesheet to transform the request XML. If you are not familiar with SQL, then just wait until Chapter 8, you are sure to find the xsql:dml action to be the simpler route for your data modification needs. To complete the example, you need to cook up a dml -update.xsql. Here it is. <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” ?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql”> <xsql:update-request table=”newsletter” key-columns=”email” transform=”include-request.xsl”/> </page> The last example is deletion. Again, it has the same pattern as the other examples. <?xml version=”1.0”?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:dml> BEGIN UPDATEnewsletter SET email=’{@email}’,name=’{@name}’,organization’{@org}’; COMMIT; END; </xsql:dml> </page> Handling XML Documents In the preceding section, you learned how to use the built-in action handlers with built-in HTML forms. You performed the appropriate action by interpreting an XML Database Modifications with XSQL 123 document containing the form parameters. In conjunction with HTML forms, their use probably seemed a bit clumsy. However, in conjunction with Web services and browsers capable of sending XML, their use is easier and starts to make more sense. You will see how this works in this section. In addition to seeing how the xsql:insert-request, xsql:update-request, and xsql:delete-request actions work with XML documents, you will learn about the xsql:insert -request-parameter action. This action is used when a parameter has, as its value, an XML document. Handling Posted XML Before beginning with the xsql:insert-request action, your first step is to under- stand how XML is posted. The action itself works the same—you specify the table and a stylesheet to transform the XML to the canonical form. What works differently is how the data arrives. With HTML forms, the key-value pairs arrive contained in the XML request element. In this case, the XML document arrives within the XML request. To understand the difference, it is important to understand what an HTTP POST looks like. When you hit Submit on an HTML form, your Web browser starts an HTTP transaction. There isn’t much mysterious about the transaction. In plain text, it sends the following message to the Web server: POST /xsql/insert-request.xsql HTTP/1.0 User-Agent: Mozilla/4.7 (X11; Linux) Accept: */* Content-length: 41 Content-type: application/x-www-form-urlencoded email=someEmail&name=someName&org=someOrg The XSQL servlet receives the parameters and does the translation to XML. The data is inserted, and a result is returned. That is the complete HTTP transaction. If you can type fast enough, you can actually manually execute the exact same insertion that your Web browser performs when you fill out the form. Just enter telnet localhost 80 and start typing! The case you are working at here is one in which you are receiving an XML docu- ment directly as part of the body of the HTTP POST request. In this case the transaction looks like the preceding request. The difference is that the XML is already formatted— the XSQL servlet doesn’t need to conjure up XML out of a list of name-value pairs. As you will see, from here you simply have to transform the XML to the canonical format. If you have been doing Web work for a while, you may be asking yourself: “The old way of submitting forms is great. Why should I bother with this new way?” There are a few reasons why posting XML in this manner is important. First, a lot of work often goes into transforming name-value pairs to a more usable data structure. In a lot of cases, the problem is figuring out how the different name-value pairs are related to each other. XML is simply a more powerful way to represent data than simple name- value pairs. The second most important reason is that a lot of Web services applications work on this principle. The XML is passed from a machine to your application over HTTP, and then you handle the XML. You will learn more about this in Chapter 16. 124 Chapter 7 NOTE You have probably heard a lot about Web services and a lot about Simple Object Access Protocol (SOAP). Per the preceding description, you can consider any two applications communicating over HTTP to be a Web service. They tend to exchange XML because it is the best format for the job. SOAP is a protocol that encapsulates this basic architecture. You’ll learn more about Web services in Chapter 16. At present, not all Web browsers will allow you to submit XML in HTTP POST requests. Microsoft Internet Explorer will, so it is required for these examples. The first step is to construct the HTML form. Note that it invokes a Javascript function on submit. <HTML> <BODY> <SCRIPT> function PostOrder (xmldoc) { var xmlhttp = new ActiveXObject (“Microsoft.XMLHTTP”); xmlhttp.open(“POST”, “insert-request-xml.xsql”,false); xmlhttp.send(xmldoc); return xmlhttp; } function submitInfo(){ var xmldoc = new ActiveXObject (“Microsoft.XMLDOM”); xmldoc.async = false; xmldoc.loadXML(xmldocText.value); var response = PostOrder(xmldoc); document.write(response.responseText); window.location.reload( false ); } </SCRIPT> <b>Type in an XML Document to Post:<b><br> <TEXTAREA rows=”12” style=”width:100%” cols=”70” NAME=”xmldocText”> <request> <parameters> <email>email10</email> <name>name10</name> <org>org10</org> </parameters> </request> </TEXTAREA> <P> <INPUT TYPE=”button” Value=”Post XML Document” onclick=”submitInfo()”> </BODY> </HTML> Database Modifications with XSQL 125 When the form is loaded in the browser, it looks like a normal form, as shown in Figure 7.4. It behaves differently, though. The Microsoft.XMLHTTP ActiveX object is created to handle the transaction. In the present case, all of the data from the textarea element is simply passed along. However, you aren’t limited to only text areas. You can have any HTML form elements and then just assemble them into the XML document as you like. For this first example, you are using the exact same XML schema that you had to use in the preceding form examples. This makes the insert-request.xsql document very simple. Note that it uses the exact same stylesheet as was used in the earlier example. <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”thanks.xsl” ?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:insert-request table=”newsletter” transform=”insert- request.xsl”/> </page> This XML schema is overkill, though. The request and parameters elements are unnecessary for the purposes of this example. Instead, you can simplify the schema so that it looks like the following code. (At this point, you might also want to delete or modify the default for the text area in the HTML page.) <newsletter-member> <email>email value</email> <name>name value</name> <org>org value</org> </newsletter-member> Your new, simpler XML schema means that you need a new stylesheet. The stylesheet that follows will do the trick. The only change required in the XSQL document is the transform attribute. That should be set to xml-transform.xsl or whatever name you choose. <?xml version = ‘1.0’?> <ROWSET xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” xsl:version=”1.0”> <ROW> <email> <xsl:value-of select=”/newsletter-member/email”/> </email> <name> <xsl:value-of select=”/newsletter-member/name”/> </name> <organization> <xsl:value-of select=”/newsletter-member/org”/> </organization> </ROW> </ROWSET> 126 Chapter 7 Figure 7.4 XML-enabled form. From here, updates and deletions can be created with the same steps. Your XML schema hasn’t changed, so you don’t need to create a new stylesheet. All that is needed is to create the update-request-xml.xsql and delete-request-xml.xsql files. Here is the update-request-xml.xsql file: <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”thanks.xsl” ?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:update-request table=”newsletter” key-columns=”email” transform=”xml-transform.xsl”/> </page> Here is the delete-request-xml.sql file: <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”thanks.xsl” ?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:delete-request table=”newsletter” key-columns=”email” transform=”xml-transform.xsl”/> </page> Database Modifications with XSQL 127 Handling XML Parameters XSQL also makes it possible to insert XML documents that are values of HTML form parameters. This is different from the work you did with HTML forms before. As with the examples with posted XML documents, you can use any schema that you want. Unlike with the posted XML documents, this works with any Web browser. The first step is to create the HTML form. This one looks like the last one, but with- out any Javascript and ActiveX: <HTML> <BODY> <b>Type in an XML Document to Post:<b> <br> <FORM action=”” method=”POST”> <TEXTAREA rows=”12” style=”width:100%” cols=”70” NAME=”xmldocText”> <ROWSET> <ROW> <EMAIL>email</EMAIL> <NAME>name</NAME> <ORGANIZATION>org</ORGANIZATION> </ROW> </ROWSET> </TEXTAREA> <P> <INPUT TYPE=”submit” Value=”Post XML Document”> </FORM> </BODY> </HTML> The next step is to create the XSQL page. It looks exactly like the insert -request-xml.xsql page, except that you use the xsql:insert-param action. The only difference between the xsql:insert-param action and the xsql:insert -request action is that you must specify the parameter element with the xsql :insert-param action. The value of that parameter will be treated as an XML document. <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”thanks.xsl” ?> <page connection=”momnpup” xmlns:xsql=”urn:oracle-xsql”> <xsql:insert-param name=”xmldocText” table=”newsletter”/> </page> In this example, the input is not transformed. As you can see in the default value for xmldocText, the input is expected to be in the canonical schema format. If you expect the data to be in a different format, you will need to transform it. You do this just as you 128 Chapter 7 do with the xsql:insert-request action—by specifying a stylesheet with the transform attribute: <xsql:insert-param table=”newsletter”_transform=”insert-request.xsl”/> You probably won’t expect the user to enter an XML parameter correctly, as in this example. Instead, you could use this action in conjunction with Javascript to assemble one or several XML documents. Then you would use the xsql:insert-param action to get the data into the database. Inserting XML as XML The final case is the simplest. What if you have an XML document that you want to store in the database as XML? Instead of dividing it up into different columns and so forth, you just want to put the entire document in as text and be able to retrieve it as such. This is easy and can be done with xsql:dml. You will use the following HTML page: <HTML> <BODY> <FORM action=”” method=”POST”> <b>Document name:</b> <br> <input type=”text” name=”name” /> <b>Type in an XML Document to Post:</b> <br> <TEXTAREA rows=”12” style=”width:100%” cols=”70” NAME=”xmldocText”> <newsletter-member> <email>email13</email> <name>name13</name> <org>org13</org> </newsletter-member> </TEXTAREA> <P> <INPUT TYPE=”submit” Value=”Post XML Document”> </FORM> </BODY> </HTML> Your parameter that has the XML is xmldocText. To insert it into the database, just use the xsql:dml action. Database Modifications with XSQL 129 [...]... conjunction with XSQL Oracle SQL and related database technologies are covered first, followed by XSLT At the end of the book, you’ll return to XSQL to see how it can be extended with action handlers and serializers CHAPTER 8 Oracle SQL The key of any XSQL system is the Oracle database This chapter covers how to access and input data in Oracle, focusing on SQL, the predominant language for Oracle However,... the Oracle database itself The BFILE data type points to a file outside the database Table 8.2 Other Number Types TYPE DEFINITION DECIMAL(p,s) NUMBER(p,s) This can specify only fixed-point numbers DOUBLE PRECISION NUMBER The precision is 126 FLOAT(b) NUMBER The binary precision is specified by the argument INT NUMBER(38) INTEGER NUMBER(38) NUMERIC(p,s) NUMBER(p,s) REAL FLOAT(63) SMALLINT NUMBER(38) Oracle. .. character data, Oracle won’t convert raw data when moving it between the user session and the database, the latter possibly having different character sets Also, raw data is imported and exported without conversion The raw type is a variable-length column, like VARCHAR2, with a maximum size of 2,000 bytes Other Types There are several other data types that are available in a default Oracle install PL/SQL... data—REF, ROWID, and UROWID— You can also create your own types by defining object types In addition to the builtin types, Oracle provides several object types The XMLType is an example of one and is specifically covered at the end of this chapter The following is a list of some other Oracle- provided key object types XMLType Used to store XML data “Any” Types Used when the data type isn’t known or when... manipulate audio, video, image, and other media data Oracle SQL Table 8.5 Arithmetic and Concatenation Operators OPERATOR DESCRIPTION EXAMPLE + Addition of the operands a+b - Subtract the second operand from the first, or negate a sole operand a(b; (a * Multiply the first operand by the second a*b / Divide the first operand by the second a/b Operators Oracle operators are used in expressions to either... covers how to access and input data in Oracle, focusing on SQL, the predominant language for Oracle However, SQL isn’t procedural, so there’s a lot you can’t do with it You fill in the gap with PL/SQL Oracle Text, which allows you to store, search, and retrieve text data and XML, is also covered here It gives you a powerful way to search text and serves as a great basis for search engines Chapter 11... to allow storage of large amounts of data in a table, but you should use the newer types described here instead The LONG type stores a smaller amount of data, and you can only have one LONG per table Oracle plans to desupport creation of the LONG data type and the LONG RAW data types Neither should be created, and exisitng columns of these types should be converted Table 8.4 defines the various data... special characters Data Types SQL has many built-in data types, and you can also create your own A data type is a fixed set of properties that a value is guaranteed to have If a column is declared with Oracle SQL a NUMBER data type, you know that you will get a number back and you are only allowed to put a NUMBER in The most common data types are NUMBER, VARCHAR2, and DATE Most data types are used to... purposes of the first three are pretty obvious, the last two might not be The large object types give you the ability to store large amounts of data—up to 4 GB The raw types are used when you don’t want Oracle to perform implicit character conversions The following paragraphs outline each of the different groups of data types String Types The string types—CHAR, NCHAR, VARCHAR2, and NVARCHAR2—are used... can range from (-84 to +127 A negative number given for the scale is rounded to the specified number of places to the left of the decimal place The data types in Table 8.2 are also recognized so that Oracle SQL is compatible with the American National Standards Institute (ANSI) SQL However, all of these are aliases to the number type Date Types The date types represent dates and times In many cases, . 7 131 The key of any XSQL system is the Oracle database. This chapter covers how to access and input data in Oracle, focusing on SQL, the predominant language for Oracle. However, SQL isn’t procedural,. built- in types, Oracle provides several object types. The XMLType is an example of one and is specifically covered at the end of this chapter. The following is a list of some other Oracle- provided. section of this chapter. <?xml version=”1.0”?> <page connection=”momnpup” xmlns:xsql=”urn :oracle- xsql”> <xsql:dml> BEGIN INSERT INTO newsletter (email,name,organization) VALUES(‘{@email}’,’{@name}’,’{@org}’); COMMIT;