1. Trang chủ
  2. » Công Nghệ Thông Tin

Oracle XSQL- P20 pdf

20 115 0

Đ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

Thông tin cơ bản

Định dạng
Số trang 20
Dung lượng 211,55 KB

Nội dung

string The string function converts an object to a string. string string(node_set_to_convert) string string(number_to_convert) string string(boolean_to_convert) string string(string_to_convert) string string(object_to_convert) string string() Table 13.43 lists the parameters. string-length The string-length function returns the number of characters in a string. number string-length(target_string) number string-length() If a string is passed to the function, its length will be returned; otherwise, the string value of the context node is returned. Table 13.43 string Parameters ARGUMENT DESCRIPTION node_set_to_convert The string value is returned to the first node in document order of the node set. number_to_convert A number is converted as normal, with the following exceptions: an infinite number, which is returned as the string “Infinity” or “–Infinity”, and “not a number”, which is returned as “NaN”. boolean_to_convert If true, the string “true” is returned. If false, the string “false” is returned. string_to_convert If a string is passed, it will be returned without modification. object_to_convert If some other type of object is passed, its conversion will be object-dependent and not defined by XPath. 360 Chapter 13 Substring The substring function returns a substring of a string. string substring(superstring, beginning_pos, end_pos) string substring(superstring, beginning_pos) Table 13.44 lists the arguments. The numbering of characters is different than it is in Java and ECMAScript, where the position of the first character is 0. For this function, the first character is at position 1 and the last character’s position is equivalent to the length of the string. If you pass 0 as the second argument, it will resolve as 1. If you pass a noninteger, it will be rounded to an integer and the integer will be evaluated. The function will return the empty string if the arguments don’t make sense, because the third argument is less than the second argument, an argument is negative, an argument is infinity, or an argument is NotANumber. substring-before The substring-before function returns a substring of a string that precedes a given token. It can be used to tokenize a string based on a delimiter. string substring-before(superstring, delimiter_string) Table 13.45 lists the arguments. If the superstring doesn’t contain the delimiter_string, the empty string will be returned. The delimiter will not be included in the returned string. Table 13.44 Substring Arguments ARGUMENT DESCRIPTION superstring The string from which the substring is pulled. beginning_pos The position of the first character of the substring, where the first character is numbered as 1. end_pos If present, the position of the last character of the desired substring. If not present, the last character of the substring is the last character of the superstring. XSLT In-Depth 361 Table 13.45 Substring-before Arguments ARGUMENT DESCRIPTION superstring The string from which the substring is pulled. delimiter_string The delimiter that should mark the end of the substring that you desire. substring-after The substring-after function returns a substring of a string that follows a given token. It can be used to tokenize a string based on a delimiter. string substring-after(superstring, delimiter_string) Table 13.46 lists the arguments. If the superstring doesn’t contain the delimiter_string, the empty string will be returned. The delimiter will not be included in the returned string. translate The translate function translates the target_string by interchanging characters of the base_string with characters of the source_string. The function can also be used to remove characters from the target_string. string translate(target_string, base_string, source_string) Table 13.47 lists the arguments. Each character in the base_string that has a character in the source_string at the same position is replaced in the target_string. If the source_string is shorter than the base_string (or empty), the trailing characters (or all the characters) in the base_string are eliminated from the target_string. Table 13.46 Substring-after Arguments ARGUMENT DESCRIPTION superstring The string from which the substring is pulled. delimiter_string The delimiter that should precede the substring that you desire. 362 Chapter 13 Table 13.47 translate Arguments ARGUMENT DESCRIPTION target_string The string to be translated. base_string The base_string, which contains the list of characters that should be translated. source_string The source_string, which is the source of characters that should replace the base-string characters. boolean Functions The following functions return true and false. They have theoretical importance, but probably the only one you will regularly use is the not function. boolean The boolean function returns a boolean value based on the argument boolean boolean(node-set) boolean boolean(string) boolean boolean(number) boolean boolean(boolean) boolean boolean(object) Table 13.48 lists the arguments. Table 13.48 boolean Arguments ARGUMENT DESCRIPTION node-set True if the node set is not empty. string True if the length is greater than 0. number True if it isn’t 0 or NotANumber. boolean True if true. object The boolean value of objects of a type other than the basic type is dependent on that type and isn’t defined by XPath. XSLT In-Depth 363 false The false function returns false. boolean false() lang The lang function determines whether a particular lang is the language or whether a sublanguage specified by the argument is the same as the context node. boolean lang(lang_string) The lang value of the context node is determined by the xml:lang attribute of the context node or the xml:lang attribute of the nearest ancestor that has an xml:lang attribute. If no xml:lang attribute can be found, the function returns false. not The not function inverses the boolean value of its argument. True returns false and false returns true. boolean not(boolean_value) true The true function returns true. boolean true() Number Functions ceiling The ceiling function returns the next highest integer compared to the number _argument or the number_argument itself if it is an integer. number ceiling(number_argument) floor The floor function returns the next-lowest integer compared to the number_argument or the number_argument itself if it is an integer. 364 Chapter 13 number The number function converts its argument to a number. number number(node_set_as_single_node) number number(string) number number(number) number number(boolean) number number(object) Table 13.49 lists the arguments. round The round function rounds a number to the nearest integer or returns a number if it is an integer, a positive infinity, a negative infinity, or a NotANumber. number round(number) sum The sum function converts the string value of each node in a node set to a number, adds them, and returns the result. number sum(node-set) Table 13.49 number Arguments ARGUMENT DESCRIPTION Node_set_as_single_node If a node set is passed, the first node in document order is converted to a string; then the string is converted to a number. string A string that represents a number is converted to a number. If the string can’t be interpreted as a number, then NotANumber will be returned. boolean True is converted to 1 and false is converted to 0. object An object of a type other than the basic type is converted in a way that is dependent on that type and not defined by XPath. XSLT In-Depth 365 Moving On This chapter covered all the details of XSLT. At this point, you now have learned all the core technologies that you need for developing XSQL applications. In the next chapter, you’ll put XSLT, SQL, and XSQL to use when you develop a real-world application. 366 Chapter 13 367 You’ve covered a lot of ground in the past few chapters. You’ve seen all of the syntax and elements for XSQL, a large percentage of SQL and PL/SQL, and you’ve learned XSLT. You know all the parts—now it is time to put them together. This chapter focuses on using the knowledge that you’ve acquired to efficiently create XSQL applications. The first step is to examine how to create an architecture of XSQL applications. We’ve touched on this subject before, but this time you can focus on how to make spe- cific architectural decisions, such as “Should I sort in SQL or in XSLT?” We’ll also dis- cuss how Java action handlers come into play. Though you haven’t learned those at an implementation level yet, it is important to understand the opportunities that they make available to you. Often, action handlers can greatly simplify a task that would be quite hard to accomplish in SQL and PL/SQL. From here, you’ll walk through the process of designing and implementing a simple application—an online product catalog for the Mom N’ Pup store. It has two parts: (1) a public part that allows you to browse and search the catalog, and (2) a simple data editor that allows you to input new entries. This example will be used to demonstrate a process for designing an XSQL application architecture followed by the core code for the public catalog part of the application. Based on this core code, you’ll extend the application to include pagination, where large queries are separated into more man- ageable pages. Then, you’ll develop the data editor, and that application will be used to show how to integrate XSLT and JavaScript. The chapter ends by looking at how to handle errors. Building XSQL Web Applications CHAPTER 14 Application Architecture XSQL is more of a framework than a language. It allows you to easily leverage the power of the database and XSLT. If your database is well suited to your application, you can have a simple query-only application in place in a matter of minutes and hours instead of days and weeks. For applications that are more complex, you can extend the basic model with action handlers and PL/SQL procedures. Even then, you’ll still hope- fully find yourself writing far less code than you would in a traditional n-tier develop- ment model. “Hopefully” is the key word here. The chief challenge of XSQL development isn’t to figure out how to manipulate strings or write the most efficient loops in XSQL—you can’t do those things in XSQL anyway! Rather, it is to make the best decisions regard- ing where to put the logic. This section looks in detail at this question. Because each of the individual technologies is so powerful, a lot of the decisions are trade-offs. For instance, both XPath and SQL are searching languages—when should you use each? Should you extend the framework with stored procedures or Java action handlers? NOTE In Chapter 9, “PL/SQL,” you learned about PL/SQL stored procedures. You can also create stored procedures in Java. This, of course, means yet another decision: Should you write stored procedures in Java or PL/SQL? This discussion focuses on stored procedures of either language as a building block and considers how they relate to the other building blocks. Your first step is to examine the simple XSQL model that uses only SQL and XSLT. From there you’ll look at how to consider how stored procedures and action handlers fit in. Then, we’ll talk about the various ways to extend the simple model. Before launching into the various architectures, you may wish to review the high-level architecture figures earlier in the book that describe the various components. In this section, you’ll see how to think about the different components in terms of application development. The real challenge facing us: Which component should do what? There are no absolute answers, but the next few pages should give you some guidelines for exam- ining this question. The way you answer this question will have a lot to do with your preferences and the preferences of your team and management. For instance, if you’re a Java guru, you may find that action handlers are the primary building blocks for your solutions. If your management prefers to have everything in PL/SQL, then you’ll find that many of the action handler duties can be handled in PL/SQL stored procedures. Perhaps the best way to approach this section is by focusing on the parts that you don’t know so well. You may find that you can more efficiently handle a task with PL/SQL in spite of being a Java master or that you can win over your team of Java action han- dlers and change the policy. 368 Chapter 14 The Simple XSQL Architecture The challenge for a lot of individual Web pages is straightforward: Display the data that resides in the database in an intuitive, pleasing manner to your audience. If you then consider that a lot of pages are simply static, you’ll find that a large majority of your pages can be handled solely with the XSQL query action and XSLT stylesheets. Even as you move into the Web services arena, the same premise holds true. Instead of pretty HTML, you use XSQL and XSLT to create XML that can be consumed by the Web service client. At some point, you’ll want to gather information from your audience. Maybe you want their feedback or you want to get some basic information from them. Maybe a Web service client wishes to provide you with data. In many of these cases, the simple xsql:dml action will handle your needs. In these cases, you are basically providing a pretty front end to SQL. The stylesheets handle the transformations and create the user interface. The challenges of storing and retrieving the data are left to the database. Query pages are represented by one or more SELECT statements, and input pages are represented by one or more DML statements, using either the xsql:dml, xsql:insert-request, xsql:update-request, or xsql:delete-request actions. These last three actions assume that the data is going to be provided as XML. Though you can use them in conjunction with traditional Web sites, they are generally more useful in Web services. For now, let’s focus only on the xsql:query and xsql:dml actions. On the surface, the xsql:dml appears more flexible. You can put any number of statements that you wish into the action, whereas the xsql:query action is restricted to one SELECT statement. This restriction seems quite limiting at first, and it might make you doubt the earlier claim that a large majority of database-driven Web pages can be developed with the simple XSQL architecture. To back up that claim, consider a couple of points: ■■ You can have more than one xsql:query action per page. ■■ You have access to all of the built-in SQL functions and PL/SQL functions. ■■ You can use the searching capabilities of Oracle Text inside SQL queries. ■■ You can use cursors inside SELECT statements to provide a deeper result set of data. All of these combine to give your xsql:query actions a lot of power. Your stylesheets don’t care how many xsql:query actions you have in a page—they just translate the XML. There are lots of SQL functions and Oracle-provided PL/SQL func- tions that can help you process the data in the database and provide better data in the output. Perhaps most important, you can use cursors inside a SQL statement so that the result is multileveled. These are especially useful to XSLT stylesheets. You can’t do everything with a SELECT statement or two, but you can do a lot. Two obvious cases in which you need to move beyond the xsql:query action are (1) when Building XSQL Web Applications 369 [...]... action handler, you just need to create a script to load the data as needed into your table What if you have more than one database that you need to access? First, it doesn’t matter if the databases are Oracle or not—they just have to be reachable by Java Database Connectivity (JDBC) The problem is that any given XSQL page can only connect to one particular database There are two ways to Building XSQL... you specify in each xsql:include-xsql action can attach to other databases It is also possible to link the databases together at the database level This is especially easy if all of the databases are Oracle You create a distributed database that is composed of several databases but appears to your SQL statements to be one 5 Create the SQL SELECT statements that you need to create the dynamic data parts... Web Browsing,” you’ll see the details of programmatic invocation, and you’ll see how to use it to augment the basic XSQL architecture Stylesheet Extensions Our last topic is stylesheet extensions The Oracle XSLT processor allows you to call Java methods from inside a stylesheet You do this by declaring the Java class that you wish to use and then calling the method inside a value-of element The most... options at the database level to ease the implementation of your application Specifically, you can database views, and triggers can make your job easier The details of these were covered in Chapter 8, Oracle SQL.” Here, they are considered as valuable pieces in the overall puzzle First, let’s consider views If you find that the SQL statements that you formulated in steps 5 and 6 are overly complex, . of the built-in SQL functions and PL/SQL functions. ■■ You can use the searching capabilities of Oracle Text inside SQL queries. ■■ You can use cursors inside SELECT statements to provide a deeper. xsql:query actions you have in a page—they just translate the XML. There are lots of SQL functions and Oracle- provided PL/SQL func- tions that can help you process the data in the database and provide. have more than one database that you need to access? First, it doesn’t matter if the databases are Oracle or not—they just have to be reachable by Java Database Connectivity (JDBC). The problem is

Ngày đăng: 03/07/2014, 08:20