Generating an XML schemaTo create the XML schema file for a table, simply follow these steps: ? Open the Data perspective.. ? While connected to the EJBBANK sample database select the Co
Trang 1Generating an XML schema
To create the XML schema file for a table, simply follow these steps:
Open the Data perspective
While connected to the EJBBANK sample database (select the Con2 connection and Reconnect from the context menu), open the context menu of the table ITSO.ACCOUNT in the DB Servers view and select Generate XML Schema
Select the folder Web Content/WEB-INF/xml of the ItsoProGuideXmlWeb project, leave the suggested name ACCOUNT.xsd in the File name field and click Finish to create the XML schema file
The source editor opens the newly created schema file ACCOUNT.xsd (Example 11-1)
Example 11-1 XML schema file ACCOUNT.xsd
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.ibm.com/EJBBANK/ITSO"
xmlns:EJBBANKITSO="http://www.ibm.com/EJBBANK/ITSO">
<element name="ACCOUNT_TABLE">
<complexType>
<sequence>
<element ref="EJBBANKITSO:ACCOUNT" minOccurs="0"
maxOccurs="unbounded"/>
</sequence>
</complexType>
<key name="ACCOUNT_PRIMARYKEY">
<selector xpath="EJBBANKITSO:ACCOUNT"/>
<field xpath="ACCID"/>
</key>
</element>
<element name="ACCOUNT">
<complexType>
<sequence>
<element name="ACCID">
Trang 2</restriction>
</simpleType>
</element>
<element name="DISCRIMINATOR">
<simpleType>
<restriction base="string">
<length value="1"/>
</restriction>
</simpleType>
</element>
<element name="OVERDRAFT" type="decimal"/>
<element name="MINAMOUNT" type="decimal"/>
</sequence>
</complexType>
</element>
</schema>
Close the Data perspective and switch back to the XML perspective as we continue with the sample using the XML features
XML schema editor
Application Developer allows you to view and edit XML and XML related files in different views For an XML schema you can use the Outline view and the XML schema editor with three tabs:
Design
Source
Graph
The Graph view is used for working with schema files and provides a graphical way to browse and edit your schema You can click the + or - signs to navigate through the schema and learn more about its structure (Figure 11-8)
You can use the pop-up menu for each node to add content that is appropriate
Trang 3Figure 11-8 Graph view
XML generators
Application Developer provides a number of XML generators Using these tools, you can generate XML schemas, XML files from other components (for example, JavaBeans or an SQL statement), and also generate components from XML files and XML schemas Here is a list of XML generators:
Generating an XML schema from a DTD
Generating JavaBeans from a DTD
Generating an HTML form from a DTD
Trang 4In the following sections we demonstrate a few of these generators.
Generating a DTD from XSD
Application Developer offers the developer the ability to transform your XSD file into a DTD file or a DTD file into an XSD file
We use the new XSD file to generate a DTD Having the ACCOUNT.xsd file, we can generate the corresponding DTD file using the generator To demonstrate how to create a DTD file from an existing XSD file, follow these steps:
Select the file ACCOUNT.xsd
Select Generate -> DTD from the context menu
Select the folder to store the file In this example we use the Web Content\WEB-INF\xml folder in our project
Click Finish The DTD file ACCOUNT.dtd is created and opened in editor Based on a DTD file, you can create XML, HTML, XML schema files, and also JavaBeans Figure 11-9 shows the code of the DTD file ACCOUNT.dtd
Figure 11-9 DTD file ACCOUNT.dtd
Validate XML files
Another useful feature is the incremental validation feature At any point during your development of an XML document, you can invoke the validate process to validate the file
Validating files means that Application Developer determines whether the current state of the file is semantically valid Any errors will be displayed in the Tasks
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT ACCOUNT_TABLE (ACCOUNT*)>
<!ELEMENT ACCOUNT (ACCID,BALANCE,INTEREST,ACCTYPE,DISCRIMINATOR,OVERDRAFT,MINAMOUNT)>
<!ELEMENT BALANCE (#PCDATA)>
<!ELEMENT INTEREST (#PCDATA)>
<!ELEMENT OVERDRAFT (#PCDATA)>
<!ELEMENT MINAMOUNT (#PCDATA)>
<!ELEMENT ACCID (#PCDATA)>
<!ELEMENT ACCTYPE (#PCDATA)>
<!ELEMENT DISCRIMINATOR (#PCDATA)>
Trang 5view Application Developer provides the following validators for the XML technology:
DTD validator
XML schema validator
XML validator
XSL validator You could change the validation settings of Application Developer by selecting Window -> Preferences and select Validation from the left pane A new page displays where you can change options regarding validation (Figure 11-10)
Trang 6Figure 11-11 Validate XML schema
A new dialog box opens and informs you if the file is valid or not
Generating an XML file from an XML schema
To generate an XML file from an XML schema, open the context menu from the XML schema file ACCOUNT.xsd and select Generate -> XML File
A new dialog comes up where you can change the location and the file name for the XML file Select the WEB-INF\xml folder and use the suggested name ACCOUNT.xml and click Next to open the Create XML File dialog (Figure 11-12)
Trang 7Figure 11-12 Create XML File from XML schema dialog Select ACCOUNT_TABLE as the root element of the XML file and select Create required and optional content to create optional information in addition to the minimum amount of information
The XML schema information section contains information about the target namespace of the XML schema, its prefix, and the schema location
Click Finish to create the XML file The XML file is created and opened in the editor Figure 11-13 shows the source of the file
<?xml version="1.0" encoding="UTF-8"?>
<EJBBANKITSO:ACCOUNT_TABLE xmlns:EJBBANKITSO="http://www.ibm.com/EJBBANK/ITSO" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ibm.com/EJBBANK/ITSO WEB-INF/xml/ACCOUNT.xsd "> <EJBBANKITSO:ACCOUNT>
Trang 8After creating the XML file, the Tasks view shows three errors, because the values of the elements ACCID, ACCTYPE, and DICRIMINATOR are not valid
The reason for this is that the default values added to the new XML file do not match with the restrictions defined in the XML schema file ACCOUNT.xsd
Change the values of the three elements as shown in Figure 11-14 and save the file The changed lines are highlighted in bold
Figure 11-14 ACCOUNT.xml file: valid
After you have generated an XML file you could, based on that file, generate an XML application (for example an XML validation application by using the context menu of the XML file and select Generate -> XML Application), a DTD or XML schema file, and even data for an existing databases
<?xml version="1.0" encoding="UTF-8"?>
<EJBBANKITSO:ACCOUNT_TABLE
xmlns:EJBBANKITSO="http://www.ibm.com/EJBBANK/ITSO"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.ibm.com/EJBBANK/ITSO
WEB-INF/xml/ACCOUNT.xsd "> <EJBBANKITSO:ACCOUNT>
<ACCID>101-1001</ACCID>
<BALANCE>0.0</BALANCE>
<INTEREST>0</INTEREST>
<ACCTYPE>CHECKING</ACCTYPE>
<DISCRIMINATOR>C</DISCRIMINATOR>
<OVERDRAFT>0.0</OVERDRAFT>
<MINAMOUNT>0.0</MINAMOUNT>
</EJBBANKITSO:ACCOUNT>
</EJBBANKITSO:ACCOUNT_TABLE>
Note: Application Developer 5 also introduces the XML Signature wizard You
can create a digital signature for your XML file using the XML Signature wizard You must either use an existing certificate or create a new certificate to create a digital signature To launch the XML Signature wizard, open the context menu of an XML file and select XML Security -> Digital Signature
Trang 9Creating an XSL file
An XSL file is a style sheet that can be used to transform XML documents into other document types and to format the output
Next we create a new XSL file from scratch and transform the XML document ACCOUNT.xml by using the XSL style sheet to generate an HTML file
To create a new XSL file, follow these instructions:
In the Workbench, select File -> New -> Other -> XML -> XSL and click Next
A new dialog comes up where you select the project and folder for the new XSL file and enter a file name (Account.xsl) in the appropriate field as shown
in Figure 11-15 Note, that the name of the XSL file must have a xsl extension
Trang 10Figure 11-16 Select XML file for XSL dialog
Click Finish to create the file The new XSL file is opened in the XSL editor Figure 11-17 shows the skeleton of the file
Figure 11-17 Skeleton of a new XSL file
One way to add code to the XSL file is to type it directly into the source editor, but you can also use the XSL wizards to add code to the XSL These wizards can be found by expanding the XSL menu in Application Developer as shown in
Figure 11-18
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:xalan="http://xml.apache.org/xslt">
</xsl:stylesheet>