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

Oracle XSQL- P17 ppsx

20 266 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

Cấu trúc

  • Oracle. XSQL Combining SQL, Oracle Text,XSLT, and Java to Publish Dynamic Web Content

    • Cover

  • Contents

  • About the Author

  • Chapter 1 Introducing Oracle XSQL

  • Chapter 2 Getting Started with XSQL

  • Chapter 3 Hello, XSQL!

  • Chapter 4 XSQL Architecture

  • Chapter 5 Writing XSQL Pages

  • Chapter 6 XSQL Parameters

  • Chapter 7 Database Modifications with XSQL

  • Chapter 8 Oracle SQL

  • Chapter 9 PL/SQL

  • Chapter 10 Using Oracle Text 253

  • Chapter 11 Retrieving XML

  • Chapter 12 XSLT

  • Chapter 13 XSLT In-Depth

  • Chapter 14 Building XSQL Web Applications

  • Chapter 15 Command Line Utility 443

  • Chapter 16 Web Services with XSQL

  • Chapter 17 XSQL Beyond Web Browsing

  • Chapter 18 Custom Action Handlers

  • Chapter 19 Serializers

  • Appendix A Resources

  • Appendix B Related Standards

  • Index

  • Team DDU

Nội dung

Unlike our previous examples, you are now using a template element. This element must be defined at the top level—a direct child of the root. For this reason, all the HTML code should be included in a template. If you don’t include all this code, the XSLT processor won’t find it. Generally, a template that matches the root of your XSQL docu- ment includes scripts, as well as a head section and any header and footer information The next example includes the remaining data from the query. Instead of a bulleted list, the data is nicely formatted in a table. It also uses a call-template element to put a standard break wherever necessary. <?xml version=”1.0”?> <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”page”> <html> <head><title>Simple Stylesheet</title></head> <body> <h1>Simple Stylesheet</h1> <table border=”0”> <th></th><th>Name</th><th>Job</th><th>Salary</th> <xsl:apply-templates select=”ROWSET/*”/> </table> </body> </html> </xsl:template> <xsl:template match=”ROW”> <tr> <td><xsl:apply-templates select=”@num”/></td> <td><xsl:apply-templates select=”ENAME”/></td> <td><xsl:apply-templates select=”JOB”/></td> <td><xsl:apply-templates select=”SAL”/></td> <xsl:call-template name=”break”/> </tr> </xsl:template> <xsl:template name=”break”> <tr><td height=”4” colspan=”4”><hr/></td></tr> </xsl:template> <xsl:template match=”JOB | SAL”> <xsl:apply-templates/> </xsl:template> <xsl:template match=”ENAME”> <b><xsl:apply-templates/></b> </xsl:template> </xsl:stylesheet> 300 Chapter 13 Figure 13.1 Output of stylesheet. The template produces the output seen in Figure 13.1. The JOB and SAL elements share a template, while ENAME gets its own. The only named template, break, is refer- enced by xsl:call-template. (In practice, xsl:call-template is used in con- junction with parameters. Examples are found later in this chapter.) Also, notice that the num attribute from the ROW element is in our output. This was accomplished with <xsl:apply-templates select=”@num”/>. Templates work against nodes, not elements, so you can use these template elements in conjunction with attributes. This example is a classic example of a push stylesheet. In the coming section on loops, you’ll see how to get the same output by taking a pull approach. Value Selection You can’t write a meaningful template without selecting values from the source XML at some point. Previously, to perform this task you used the xsl:apply-templates element. It was a rather blunt instrument, though. The value-of element introduced here gives you better control. This section shows you the syntax of this element and works though some examples. XSLT In-Depth 301 Table 13.9 xsl:value-of Attributes ATTRIBUTE DESCRIPTION select Any XPath expression. It can describe an element, in which case the text node of that value will be copied, or an attribute, in which case the attribute’s value will be copied. disable-output-escaping If set to “yes”, XML special characters such as & and < won’t be escaped. By default, this is set to “no”. xsl:value-of Syntax The xsl:value-of element copies text from the XML document to the output. Gen- erally, the text copied is value of a node, though it can also be an attribute of a node. <xsl:value-of select = “XPath-expression” disable-output-escaping = “yes” | “no”./> Table 13.9 lists the attributes. Table 13.10 lists the parent-child relationships. Table 13.10 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl attribute xsl comment xsl copy xsl element xsl fallback xsl for-each xsl if xsl message xsl otherwise xsl param xsl processing-instruction xsl template xsl variable xsl:when 302 Chapter 13 Examples If there is no template associated with a particular node, xsl:apply-templates will provide the same functionality by default. However, it’s better to use xsl:value-of for a couple of reasons. First, xsl:apply-templates will apply any template that matches, which may not be what you want. There often might be a matching template, but you really just want the value. Also, with xsl:value-of, you can disable output escaping. The first example shows the places in our earlier stylesheet where xsl:value-of should be used instead of xsl:apply-templates. <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”page”> <html> <head><title>Simple Stylesheet</title></head> <body> <h1>Simple Stylesheet</h1> <table border=”0”> <th></th><th>Name</th><th>Job</th><th>Salary</th> <xsl:apply-templates select=”ROWSET/*”/> </table> </body> </html> </xsl:template> <xsl:template match=”ROW”> <tr> <td><xsl:value-of select=”@num”/></td> <td><xsl:apply-templates select=”ENAME”/></td> <td><xsl:apply-templates select=”JOB”/></td> <td><xsl:apply-templates select=”SAL”/></td> <xsl:call-template name=”break”/> </tr> </xsl:template> <xsl:template name=”break”> <tr><td height=”4” colspan=”4”><hr/></td></tr> </xsl:template> <xsl:template match=”JOB | SAL”> <xsl:value-of select=”.”/> </xsl:template> <xsl:template match=”ENAME”> <b><xsl:value-of select=”.”/></b> </xsl:template> </xsl:stylesheet> XSLT In-Depth 303 The xsl:value-of element also gives you the ability to disable output escaping. If you know that your query is going to return special characters, such as & and <, and you don’t want them to be escaped, set disable-output-escaping=”yes”. Here is an example XSQL page that returns an &: <?xml version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”value-of.xsl”?> <page connection=”demo” xmlns:xsql=”urn:oracle-xsql”> <xsql:query> select chr(‘38’) AS ampersand from dual </xsql:query> </page> The ASCII code for the ampersand is 38, so this query will return a single ampersand character. The following stylesheet demonstrates the difference between the default behavior and the behavior when disable-output-escaping is set to “yes”. <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”page”> <html> <head><title>Value-of Escape Example</title></head> <body> <h1>Value-of Escape Example</h1> <ul> <li>Default: <xsl:value-of select=”.”/></li> <li>Output Escaping disabled: <xsl:value-of select=”.” disable- output-escaping=”yes”/></li> </ul> </body> </html> </xsl:template> </xsl:stylesheet> Upon invoking the XSQL page through a browser, you’ll probably see an amper- sand in both cases. But if you look at the source of the resultant HTML page, you’ll see that the first ampersand is &amp; the second, &. Iteration XSLT allows you to loop through a set of nodes with the xsl:for-each element. On each iteration, the context-node changes to the next node in the set. Within the loop, you can do a series of operations on the context-node, such as selecting data. Many of the same goals that can be met with the xsl:for-each element can also be met with the xsl:apply-templates element. Iteration is more familiar to a procedural programmer than the tree-based recursive approach that xsl:apply-templates entails. However, looping can lead to difficult-to-reuse XSLT code. 304 Chapter 13 Xsl:for-each Syntax The xsl:for-each element declares the beginning of a loop. The loop iterates over all the nodes in the node set described by the value of the select attribute, which is an XPath-expression. <xsl:for-each select = “XPath_expression”> any XML </xsl:for-each> The sole attribute—select—describes a node set over which the iteration should occur. Table 13.11 lists the parent-child relationships. Table 13.11 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:attribute xsl:apply-imports xsl:comment xsl:apply-templates xsl:copy xsl:attribute xsl:element xsl:call-template xsl:fallback xsl:choose xsl:for-each xsl:comment xsl:if xsl:copy xsl:message xsl:copy-of xsl:otherwise xsl:element xsl:param xsl:fallback xsl:processing-instruction xsl:for-each xsl:template xsl:if xsl:variable xsl:message xsl:when xsl:number xsl:processing-instruction xsl:sort xsl:text xsl:value-of xsl:variable XSLT In-Depth 305 The for-each element can be used almost anywhere in XSLT except as a top-level element. One xsl:for-each element can be nested inside another element, and mul- tiple levels of nesting are permitted. Examples In our example of xsl:apply-templates and xsl:value-of, you saw how to create a stylesheet by matching templates to nodes and then using xsl:apply -templates to match the input XML nodes with the templates in the stylesheet. This is the push approach described earlier. For this example, you’ll see how to get the same stylesheet using iteration instead. <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> <xsl:template match=”page”> <html> <head><title>Simple Stylesheet</title></head> <body> <h1>Simple Stylesheet</h1> <table border=”0”> <th></th><th>Name</th><th>Job</th><th>Salary</th> <xsl:for-each select=”ROWSET/ROW”> <tr> <td><xsl:value-of select=”@num”/></td> <td><b><xsl:value-of select=”ENAME”/></b></td> <td><xsl:value-of select=”JOB”/></td> <td><xsl:value-of select=”SAL”/></td> <xsl:call-template select=”break”/> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> <xsl:template name=”break”> <tr><td height=”4” colspan=”4”><hr/></td></tr> </xsl:template> </xsl:stylesheet> In this example, the formatting of the individual data elements is done within the ele- ments. If you wish, you can still use templates in conjunction with an xsl:for-each loop: <xsl:stylesheet version=”1.0” xmlns:xsl=”http://www.w3.org/1999/XSL/Transform”> 306 Chapter 13 <xsl:template match=”page”> <html> <head><title>Simple Stylesheet</title></head> <body> <h1>Simple Stylesheet</h1> <table border=”0”> <th></th><th>Name</th><th>Job</th><th>Salary</th> <xsl:for-each select=”ROWSET/ROW”> <tr> <td><xsl:value-of select=”@num”/></td> <td><xsl:apply-templates select=”ENAME”/></td> <td><xsl:apply-templates select=”JOB”/></td> <td><xsl:apply-templates select=”SAL”/></td> <xsl:call-template name=”break”/> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> <xsl:template name=”break”> <tr><td height=”4” colspan=”4”><hr/></td></tr> </xsl:template> <xsl:template match=”JOB | SAL”> <xsl:value-of select=”.”/> </xsl:template> <xsl:template match=”ENAME”> <b><xsl:value-of select=”.”/></b> </xsl:template> </xsl:stylesheet> In this case, the three apply-templates elements are applied to each ROW element that iterates through the list. Likewise, any of those templates could have its own for-each element inside of it. Conditional Logic As with most computer programming languages, you can use conditional logic to decide what the output should be based on the input. You have the xsl:if element and the xsl:choose/xsl:when/xsl:otherwise elements at your disposal. These last three elements are grouped together because they are used collectively for if-else-if-else processing. However, the xsl:if element adds a set of actions only if its condition is true. Unlike most programming languages, it can’t be used XSLT In-Depth 307 in conjunction with an else or else if construct. This section looks at the syntax of these four elements and provides examples of how to use them in templates. Before you cover the element syntax, you need to understand a boolean expression as it is defined by XSLT. boolean Expressions Used by conditional logic elements. boolean expressions evaluate how XSLT should proceed. If the expression evaluates to true, the XSLT inside the element will be output and the other text output as appropriate. If the expression evaluates to false, the ele- ment will be skipped. XSLT determines truth and falseness by evaluating the expres- sion and then using the rules of the XPath boolean()function on the results. These boil down to the following: If the expression evaluates to a boolean expression, that value will be used to determine truth and falseness. This is the simplest case. If the expression evaluates to a number, it will be true if it is neither 0 nor NotANumber. If the expression evaluates to a node set, it will be true if it isn’t empty. If the expression evaluates to a string, it will be true if it isn’t empty. You’ll be learning more about XPath as you progress through the chapter. XPath provides a variety of functions that can be used not only for boolean expressions but for all types of expressions. xsl:if Syntax The xsl:if element allows you to conditionally include the contents of the element. <xsl:if test = “boolean_expression”> XML XSLT Template Elements </xsl:if> It has one attribute, test, which is a boolean expression that evaluates to either true or false. As discussed in an earlier section, this doesn’t mean that the expression must evaluate to either true or false. If it evaluates to a node set, a number, or a string, the rules described in the earlier section on boolean expressions will apply. Table 13.12 lists the parent-child relationships. If the boolean expression evaluates to true, any text or XML contained in the ele- ment will be output after any XSLT elements have been evaluated. There is no “else” element that can be used with xsl:if. If you need to do compound conditional pro- cessing, you should use the xsl:choose element instead. 308 Chapter 13 Table 13.12 Parent-Child Relationships CAN BE A CHILD OF . . . CAN BE A PARENT OF . . . xsl:attribute xsl:apply-imports xsl:comment xsl:apply-templates xsl:copy xsl:attribute xsl:element xsl:call-template xsl:fallback xsl:choose xsl:for-each xsl:comment xsl:if xsl:copy xsl:message xsl:copy-of xsl:otherwise xsl:element xsl:param xsl:fallback xsl:processing-instruction xsl:for-each xsl:template xsl:if xsl:variable xsl:message xsl:when xsl:number xsl:processing-instruction xsl:text xsl:value-of xsl:variable Xsl:choose, xsl:when, and xsl:otherwise Syntax The xsl:choose provides conditional processing similar to the xsl:if element. However, xsl:choose gives you the ability to select one of many possible alternatives delineated with the xsl:when element. You can also provide a catchall if none of the alternative conditions is met with the xsl:otherwise element. The syntax is as follows: <xsl:choose> <xsl:when test=”boolean_expression”> XML text Template XSLT Elements </xsl:when> XSLT In-Depth 309 . version=”1.0”?> <?xml-stylesheet type=”text/xsl” href=”value-of.xsl”?> <page connection=”demo” xmlns:xsql=”urn :oracle- xsql”> <xsql:query> select chr(‘38’) AS ampersand from dual </xsql:query>

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