Displaying the FAQ and Answers
In a previous section, you saw how the link is created to invoke the showanswer.xsql page that displays the FAQ. Now we will discuss how this actually works. This page is invoked by passing the selected FAQ id as a parameter in the following URL:
http://localhost:8988/xdkus/app_faq/showAnswer.xsql?id=16
Figure 14-2 shows the XML transformed into the HTML display of the output.
Figure 14-2: FAQ answer in HTML
The id is then passed into the following query to retrieve the FAQ data:
<xsql:include-xml>
<![CDATA[
select xmlelement("FAQ",
xmlforest(extract(value(x),'/FAQ/ANSWER') as answer,
extract(value(x),'/FAQ/TITLE/text()') as title,
extract(value(x),'/FAQ/QUESTION') as question,
extract(value(x),'/FAQ/@id') as id)).getClobVal()
from faq x
where extractValue(value(x),'/FAQ/@id')='{@id}'
]]>
</xsql:include-xml>
If you examine this query, you’ll see that it uses the SQL/XML extensions in the database to construct a new XML document. To simply include the XML output from SQL/XML queries, you can use <xsql:include-xml> instead of <xsql:query> as it doesn’t add the <ROWSET> and <ROW> elements. The following is an example of this document before it is transformed into HTML by the showAnswer.xsl stylesheet:
<page id="1">
<FAQ>
<ANSWER>
<ANSWER>
<PARAGRAPH> You should be able to set the
<KEYWORD>encoding</KEYWORD> declaration in XML PI using the
DBMS_XMLQUERY.setEncodingTag() procedure,
which will not do the encoding conversion.</PARAGRAPH>
<PARAGRAPH> As data is returned in <KEYWORD>CLOB</KEYWORD>,
the XML data will always be converted to <KEYWORD>UCS2</KEYWORD>
( <KEYWORD>AL16UTF16</KEYWORD> ) encoding by database if the
database is in multibyte or the database character set if the
database is in single byte. When you store the query result
to files, you should take care of the characterset conversion
to make sure there is no conflict between the actual coding and
encoding declaration. </PARAGRAPH>
</ANSWER>
</ANSWER>
<TITLE>How can I set the encoding
the output of the XSU query?</TITLE>
<QUESTION>
<QUESTION>
<PARAGRAPH>One of my customers executes a query using XSU on a
table with diacritical characters, gets the result in a CLOB,
and puts it in a file. It works fine, but he needs to modify the
encoding, modify the line:</PARAGRAPH>
<CODE><?xml version="1.0" ?></CODE> to <CODE><?xml version="1.0"
encoding="iso-8859-1" ?></CODE>
<PARAGRAPH>How can he do that?</PARAGRAPH>
</QUESTION>
</QUESTION>
<ID>2</ID>
</FAQ>
Note that the four top-level elements, <ANSWER>, <TITLE>, <QUESTION>, and <ID>, were retrieved using the extract() function passing in the respective XPaths.
To display this FAQ in HTML, you need to apply a stylesheet. This is done for you by including a stylesheet processing instruction in showanswer.xsql and having the XSQL Servlet apply it for you. The following is in our example:
<?xml-stylesheet type="text/xsl" href=" ?>
This stylesheet is quite long because it sets up tables, but it is useful to examine the templates that actually operate over the input FAQ XML document. First, the <PARAGRAPH> sections need to be formatted. This is done with the identity transform template, introduced in Chapter 3, as follows:
<xsl:template match="PARAGRAPH">
<p>
<xsl:apply-templates select="*|@*|comment()|processing-instruction()
|text()"/>
</p>
</xsl:template>
Next, the <CODE> elements need to be presented in a fixed font. This is accomplished with the following template:
<xsl:template match="CODE">
<pre>
<xsl:value-of select="."/>
</pre>
</xsl:template>
Next, turning to the main sections of the answer, these will be formatted in the main template of the stylesheet. First, we see that the FAQ title has special formatting. This is done with the following section, which uses a CSS class to provide the special formatting:
<tr valign="top">
<th class="portletTitle">
<xsl:value-of disable-output-escaping="yes" select="page/FAQ/TITLE"/>
</th>
</tr>
Note | Throughout these examples, you will see that CSS classes are used to provide formatting. You should adopt this technique, because it makes your stylesheets easier to build, and the formatting is reusable. |
Next, we see that the answer is identified with Answer: and a different background color. The following section shows how this is done:
<tr bgcolor="#F0F0F0">
<td width="100%">
<b>Answer: </b>
<br/>
<xsl:apply-templates select="page/FAQ/ANSWER/ANSWER"/>
</td>
</tr>
Finally, you need to add navigational controls to this page, such as Back and Close. You can do this by adding JavaScript commands to the template as follows:
<tr>
<td>
<img src="/image/library/english/10158_r_arrow.gif"
width="8" height="9"/>
<a href="#" onClick="history.back()">Back</a>
</td>
<td align="right">
<img src="/image/library/english/10158_r_arrow.gif"
width="8" height="9"/>
<a href="#" onClick="self.close()">Close</a>
</td>
</tr>
Note that both the text of the commands and an arrow graphic are inserted when the template is applied.