Figure 11-18 XSL menuTo prepare the style sheet that can be used to generate a HTML file, we have to add some HTML code.. Note that Application Developer immediately adds the HTML code t
Trang 1Figure 11-18 XSL menu
To prepare the style sheet that can be used to generate a HTML file, we have to add some HTML code First we add an HTML template This basically generates
an HTML code skeleton and add it to the XSL file
To use the HTML Template wizard, place the cursor in a new line right after the stylesheet tag (Figure 11-19) and select XSL -> HTML Template from the menu bar Note that Application Developer immediately adds the HTML code to the file without bringing up a dialog
Figure 11-19 Place cursor in XML editor
A new code fragment has been added to the XSL file, as shown in Figure 11-20
XSL menu with code wizards
Trang 2Figure 11-20 HTML template code in XSL file
Next, delete the <xsl:apply-templates/> line from the code and place the cursor
in this line (Figure 11-21)
Figure 11-21 XSL code ready for insert
Note: In the absence of a select attribute, the xsl:apply-templates
instruction processes all of the children of the current node including text nodes Next we add a select attribute, which can be used to process nodes selected by an expression instead of all children Therefore we remove this instruction
Trang 3In the menu bar select XSL -> HTML Table to bring up the HTML Table wizard to add a HTML table that displays the content of an XML file in a table when applying the XSL to the XML file (Figure 11-22)
Figure 11-22 Add a table to the XSL
Expand EJBBANKITSO:ACCOUNT_TABLE and select the EJBBANKITSO:ACCOUNT node
We want to display all accounts later when we apply this style sheet Also select the check box Include Header This option includes a for each element inside the content node
Note: As we have specified the corresponding XML file, when creating the
style sheet, Application Developer does not bring up the XML file selection dialog This would be the case if no XML file had been selected when creating new style sheets
Trang 4Click Next to open the dialog where you can change the properties of the HTML table You can change properties like color, border, and width of the table In this example we are not doing this, and we click Finish to add the table to the XSL file Figure 11-23 shows the code which is added to the XSL file
Figure 11-23 Added HTML table to style sheet
Now we finished creating a simple XSL style sheet which is based on the ACCOUNT.xml file By applying this file to the XML file, a list of all accounts found in the XML file is generated
Note: By selecting the nodes in the XSL tree, the preview pane at the bottom
of the dialog changes accordingly There you can verify the HTML code which would be added to the XSL file when confirming the dialog
<table>
<tr>
<th>ACCID</th>
<th>BALANCE</th>
<th>INTEREST</th>
<th>ACCTYPE</th>
<th>DISCRIMINATOR</th>
<th>OVERDRAFT</th>
<th>MINAMOUNT</th>
</tr>
<xsl:for-each select="/EJBBANKITSO:ACCOUNT_TABLE/EJBBANKITSO:ACCOUNT">
<tr>
<td><xsl:value-of select="ACCID"/></td>
<td><xsl:value-of select="BALANCE"/></td>
<td><xsl:value-of select="INTEREST"/></td>
<td><xsl:value-of select="ACCTYPE"/></td>
<td><xsl:value-of select="DISCRIMINATOR"/></td>
<td><xsl:value-of select="OVERDRAFT"/></td>
<td><xsl:value-of select="MINAMOUNT"/></td>
</tr>
</xsl:for-each>
</table>
Note: As this is just a simple example of a style sheet, it has not been nicely
formatted You can do this by changing the HTML tags in the Source view appropriate to your needs
Trang 5Transforming XML files
You can apply an XSL file to an XML file and create either a new XML file, a new HTML file, or a new text file
If you select only an XSL file or XML file, and select Apply XSL from the pop-up menu, a wizard will open, prompting you to select an XML or XSL file from the Workbench or from the Internet using HTTP
If you select an XML or XSL from HTTP, you must specify its URI Only an XSL transformation will be run on the selected file Because the file is not available in the Workbench, the debugger cannot be opened It can only be transformed and the results displayed
To apply an XSL file to an XML file and create a new HTML file, select an XML and an XSL file (press the CTRL key to select both files) and Apply XSL -> As XML (context)
Figure 11-24 shows the selected files and the context menu where you can transform the file
Figure 11-24 Apply XSL as HTML
Note: You should only apply an XSL file to an XML file and attempt to create
an HTML file if the style sheet you are using is designed to provide HTML output Otherwise, an empty or incorrect HTML file will be created
Trang 6The XSL Debug perspective opens and now you can either view the generated file or debug the XSL style sheet
If you now go back to the XML perspective, you see that Application Developer has created a new HTML file and named it ACCOUNT_Acount_transform.html When you open the HTML file in the Design tab it should look like Figure 11-25
Figure 11-25 HTML file design
By changing the HTML tags in the style sheet, you can format this HTML file more nicely
Debugging XSL
Application Developer’s XSL debugging and transformation tool records the transformation that is generated by the Xalan processor The Xalan processor is
an XSLT processor that transforms XML files into HTML, text, or other XML file types
For more information on the Xalan processor, refer to the following Apache Web site:
http://xml.apache.org/xalan-j/
The XSL debugging and transformation tool enables you to visually step through
an XSL transformation script Figure 11-26 shows the XSL Debug perspective with its views
Trang 7Figure 11-26 XSL Debug perspective
The Sessions view displays any sessions you have run (that is, any XSL files you have applied to XML files) You can use the Sessions view to step through an XSL transformation script, highlighting the transformation rules as they are fired The Sessions view provides the following actions:
To step forward, click the Step forward icon
To step backwards, click the Step backward icon
To reset the file, click the Restart from beginning icon
To run to breakpoint, click the Run to breakpoint icon
To open the transformation output, which is the result file, in a Web browser, click the Open the browser on the transformation result icon
You can click the Globe icon to view the result HTML file Again, if you switch back to the XML perspective you notice that a new file named, in this case, named ACCOUNT_Account_transform.html has been created
Trang 8Generating JavaBeans from DTD or XSD
In this section we generate JavaBeans from the existing XML schema file To do this, open the context menu of the ACCOUNT.xsd file and select Generate -> Java Beans (Figure 11-27)
Figure 11-27 Generate Java Beans dialog
The Java root folder has to be specified in the Container field In the Package field we enter itso.xml as we would like to store the JavaBeans in this package
In the Root element field select ACCOUNT and make sure the check box Generate sample test program is selected This generates a runnable JavaBean which shows you how to use the beans you have created
Click Finish to generate the JavaBeans The following files are created in the itso.xml package:
ACCOUNT_TABLE.java—represents the complex type <ACCOUNT_TABLE>
ACCOUNTFactory.java—provides methods for creating Java beans for elements in the XML document
ACCOUNT.java—represents the complex type <ACCOUNT>
Sample.java—creates and saves an XML document, loads the XML document and prints its content in the Console view
Next, switch to the Java perspective and modify the createACCOUNT method of the Sample.java file and change the parameter of the setACCID, setACCTYPE, and setDISCRIMINATOR statements as shown in Figure 11-28
Trang 9Without the changes you get three errors displayed in the Tasks View when running this sample class, because these elements (ACCID, ACCTYPE, and DISCRIMINATOR) would not match with the corresponding schema file
Figure 11-28 createACCOUNT method of the Sample.java file
Now you can run the sample class See “Running your programs” on page 103 for more information about how to run a Java application
Note that after running the sample application a new file, ACCOUNTSample.xml, is created in the Web Content\WEB-INF\xml folder This file contains the element values specified in the createACCOUNT method of the sample Java file
Summary
In this chapter we have explained and demonstrated some XML tools of Application Developer As the XML topic is to complex to be covered in only one chapter, we could not demonstrate everything Application Developer provides
We have shown how to create a new XML file, a new XSL style sheet, and how to generate an XML schema based on a relational database table
More information
In the IBM Redbook The XML Files: Development of XML/XSL Applications Using WebSphere Studio Version 5, SG24-6586, more detailed information is provided regarding developing XML/XSL applications using Application Developer It also contains examples using Application Developer Version 5
void createACCOUNT() {
iACCOUNT.setACCID("101-1001");
iACCOUNT.setBALANCE("0.0");
iACCOUNT.setINTEREST(0);
iACCOUNT.setACCTYPE("Checking");
iACCOUNT.setDISCRIMINATOR("C");
iACCOUNT.setOVERDRAFT("0.0");
iACCOUNT.setMINAMOUNT("0.0");
}
Tip: You have to select the xml folder and Refresh (context) to see the generated file
Trang 10Chapter 12. Developing EJB applications
This chapter introduces the Enterprise JavaBeans and shows Application Developer’s support for creating, maintaining, and testing such components
In this chapter we develop a few entity beans, relationships between the entity beans, a session bean, and a front-end Web application
This chapter provides only a condensed description of the EJB architecture and
a few examples For complete coverage of EJBs, refer to the IBM Redbook, EJB 2.0 Development with WebSphere Studio Application Developer, SG24-6819
12