| |
Building your First XML Document
Module Objectives
Purpose
This module describes how to create an XML document,
access data from the database and format the output of the data.
Objectives
Upon completion of this module, you should be able to:
Prerequisites
The prerequisites for this lesson are:
Reference Material
The following list represents some useful reference material should you
need additional information on the topics in this module:
 |
Documentation: Application Developers Guide - XML
|
Overview
What is XML?
XML stands for eXtensible Markup Language. It is
a markup language similar to HTML. XML is a markup language for structuring
data rather than formatting information.You use XML to create a document
that contains structured data that can be used or intrepreted by other
applications. The format or structure is straightforward and can be used
by any person or program that can read text.
The Benefits of Using XML
One significant benefit of XML is that you can exchange data between
applications in a simple, nonproprietary format. Because the language
is extensible, you create tags that are specific to your business. For
example, your document may contain tags to structure information about
books. Those tags may include <title>, <author>,and <desc>.
With these tags, other applications can parse the document for specific
information, ignore some information, or even search for a specific title.
<?xml version='1.0'?> <LIBRARY> <BOOK> <TITLE>Is This Your Child</TITLE> <AUTHOR>Doris Rapp M.D.</AUTHOR> <DESC>Discovering and treating unrecognized allergies in children and adults.</DESC> </BOOK> </LIBRARY>
What is the Difference Between XML and HTML?
There are many differences between XML and HTML as outlined in the following
table:
| XML |
HTML |
| Markup language for structuring data rather than formatting
information. |
Markup language primarily used for formatting and displaying
text and images in a browser. |
| Maintains context of the data |
Data has no context |
| You create your own tags as you need them |
Has a set of predefined formatting tags that you can
use, but you cannot create your own. |
| Can parse the document for specific information, ignore
some information, or even search for a specific title |
Can search only the entire document for a specific string |
An Example
You will use an XML document named Library-document to demonstrate how
the browser displays XML. Notice that the author, Charles Dickens, contains
several titles. The following image shows how a browser that supports
XML displays XML. Currently the only browser that displays raw XML is
Microsoft Internet Explorer 5. This document is being displayed without
any external formatting commands or tags.
Here is the XML code:
<?xml version='1.0'?> <Library-document> <author name="Charles Dickens"> <title>Great Expectations</title> <title>Oliver Twist</title> <title>A Tale of Two Cities</title> </author> <librarian name="Ben Franklin"/> </Library-document>
Here is what the XML looks like in a browser:

You will see later how you can apply a stylesheet to format the data.
How would this look in HTML?
The HTML document is designed to display the library information in a
browser. As such, the tags in HTML are mostly for formatting, and the
displayed document is easier to read than the nonformatted XML document
on the previous page.
Here is the HTML code
<body>
<dl>
<dt>Charles Dickens</dt>
</dl>
<ul>
<li>Great Expectations</li>
<li>Oliver Twist</li>
<li>A Tale of Two Cities</li>
</ul>
<dl>
<dt>Ben Franklin</dt>
</dl>
Here is what the HTML looks like in a browser:

Elements and Attributes
There are two ways to write XML: using Elements and Attributes. Elements
and attributes accomplish roughly the same thing. Both examples below
are valid and communicate the same basic information about a customer.
It takes some experience to determine which style to use in which case.
In general, it is more robust and flexible to use elements rather than
attributes. The easiest and most consistent approach is to use elements
for data and attributes for metadata. Metadata is information about the
data. Elements should be information that the user of the document will
want to see and use. Metadata is information the creator or owner of the
document may want.
Another tip is that elements are more easily added as your requirements
expand. If you use elements rather than attributes, you simply add new
elements to the structure as needed. Attributes are returned unordered
from the document, whereas elements may be ordered. Attributes cannot
be nested that is, they cannot have attributes of their own.
| Elements |
Attributes |
<customer>
<person>
<name>Jane Daux</name>
<address>
<street>100 Main St.</street>
<city>Anytown</city>
<state>Texas</state>
<zipcode>77034</zipcode>
</address>
</person>
</customer>
|
<customer>
<person name="Jane Daux"
street="100 Main St."
city="Anytown"
state="Texas"
zipcode="77034"/>
</customer>
|
Setting Up the XDK Environment
You will need to perform the following steps to setup the XDK environment:
| 1. |
Download the XDK
from OTN and unzip into d:\xdk.
|
| 2. |
Copy the following files from d:\xdk\lib
to d:\jdev902\j2ee\home\lib:
xmlparserv2.jar
xsu12.jar
oraclexsql.jar
xsqlserializers.jar

|
| 3. |
Edit the d:\xdk\xdk\admin\XSQLConfig.xml
in Wordpad. Make the following changes in bold
and save the file
<connection name="employee"> <username>hr</username> <password>hr</password> <dburl>jdbc:oracle:thin:@<hostname>:1521:ORCL</dburl> <driver>oracle.jdbc.driver.OracleDriver</driver> <autocommit>true</autocommit> </connection>

|
| 4. |
Copy the d:\xdk\xdk\admin\XSQLConfig.xml
file to the d:\jdev902\j2ee\home\default-web-app\WEB-INF\classes
directory.
|
| 5. |
Edit the d:\jdev902\j2ee\home\config\global-web-application.xml
and add the following <servlet>
and <servlet-mapping>
entries in bold. When done, save the file.
<servlet>
<servlet-name>xsql</servlet-name> <servlet-class>oracle.xml.xsql.XSQLServlet</servlet-class> </servlet>
<servlet-mapping>
<servlet-name>xsql</servlet-name>
<url-pattern>/*.xsql</url-pattern>
</servlet-mapping>

|
| 6. |
Edit the d:\jdev902\j2ee\home\application-deployments\default\defaultWebApp\orion-web.xml
and add the following entries in bold. When done, save the
file.
<virtual-directory virtual-path="/obe" real-path="/d:/wkdir/" /> <virtual-directory virtual-path="/xsql" real-path="/d:/xdk/xdk/demo/java/xsql/" />

|
Creating a Well-formed XML Document
A well-formed XML document must comply with the following syntax rules:
 |
The XML declaration must begin the document. |
 |
The document must contain one element known as the root that contains
all other elements. |
 |
All elements must have a start and end tag unless they are emply
elements. |
 |
Empty elements must have only one tag of the form <name/>.
|
 |
Attribute values must be quoted. |
You will perform the following steps to create and test a well-formed
XML document using elements:
| 1. |
Open book1.xml
from your working directory in Notepad and type the following
in bold:
<?xml version='1.0'?> <LIBRARY> <BOOK> <TITLE>Is This Your Child</TITLE> <ISBN>0-688-11907-7 </ISBN> <AUTHOR>Doris Rapp M.D.</AUTHOR> <DESC>Discovering and treating unrecognized allergies in children and adults.</DESC> <COPYRIGHT>1991</COPYRIGHT> </BOOK> <PERIODICAL> <TITLE>PCWEEK</TITLE> <PUBLISHER>Ziff Davis Publishing</PUBLISHER> <DATE>April 17, 2000</DATE> <ARTICLE> <TITLE>Auto markets </TITLE> <DESC>Suppliers fear they'll be left in the dust</DESC> </ARTICLE> <ARTICLE> <TITLE>Right time for wireless</TITLE> <DESC>802.11b based wireless networking</DESC> </ARTICLE> </LIBRARY>
|
| 2. |
Save the file.
|
| 3. |
Open IExplorer and open the book1.xml
file to see its contents.

Notice that you get an error because the end tag for PERIODICAL
was left out.
|
| 4. |
Add the </PERIODICAL> as shown below and save your
file again:
<?xml version='1.0'?> <LIBRARY> <BOOK> <TITLE>Is This Your Child</TITLE> <ISBN>0-688-11907-7 </ISBN> <AUTHOR>Doris Rapp M.D.</AUTHOR> <DESC>Discovering and treating unrecognized allergies in children and adults.</DESC> <COPYRIGHT>1991</COPYRIGHT> </BOOK> <PERIODICAL> <TITLE>PCWEEK</TITLE> <PUBLISHER>Ziff Davis Publishing</PUBLISHER> <DATE>April 17, 2000</DATE> <ARTICLE> <TITLE>Auto markets </TITLE> <DESC>Suppliers fear they'll be left in the dust</DESC> </ARTICLE> <ARTICLE> <TITLE>Right time for wireless</TITLE> <DESC>802.11b based wireless networking</DESC> </ARTICLE> </PERIODICAL> </LIBRARY>
|
| 5. |
Switch back to IExplorer and Open book1.xml
again to see if it runs successfully.

|
Applying a Stylesheet to your XML Document
What is XSL?
XSL is an XML-based language used to define stylesheets. XSL can transform
an XML document into HTML, a text document, or a new XML document. You
can use XSL to reorganize the data in an XML document. You can also supress
and order data from an XML document using an XSL stylesheet.
What is a Stylesheet?
A Stylesheet contains instructions used to create a new document based
on information in the source document. The instructions can add, remove,
or reorganize nodes in an XML document. The stylesheet may also include
instructions to help format the resulting new document.
Stylesheet Components
Stylesheets contain a combination of types of XSL elements. The elements
fall into two general categories:
 |
XSL Transformations (XSLT): Commands that transform an XMl
document into a new XML, HTML, or text document. |
 |
non-XSLT: Commands that are sent as text to the target document.
They may include HTML tags. |
Types of XSLT Elements
The following elements exist in a stylesheet:
| xsl:stylesheet |
Defines that the document is indeed a stylesheet. Can
include additional attributes, such as, xmlns:xsl.
This attribute defines the namespace that will be used for this document. |
| xsl:template |
Defines template rules that are the foundation of the
XSLT commands. |
| xsl:apply-templates |
A template that maps to a node in the XML tree. |
| xsl:value-of |
Copies the value of a node element from the source document
to the output document. |
Creating the Stylesheet
You will create a stylesheet to display the book information in the book2.xml
in a table format. Perform the following:
| 1. |
Open a DOS window and execute the following commands:
d:
cd \xdk\bin
env
cd \wkdir
oraxsl -l book2.xml -s books.xsl -r html

|
| 2. |
Open IExplorer and open the file d:\wkdir\book2.xml.html

Notice that the book information is formatted but the Periodical
information is not. You will format the Periodical information next.
|
| 3. |
Open books.xsl
from your working directory in Notepad and type the following
in bold:
<?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output media-type="text/html" method="html" indent="yes"/> <xsl:template match="/"> <html> <body> <xsl:apply-templates select="//BOOK"/> <xsl:apply-templates select="//PERIODICAL"/> </body> </html> </xsl:template> <xsl:template match="BOOK"> <h2>Books</h2> <h3><xsl:value-of select="TITLE"/> , <xsl:value-of select="ISBN"/> (<xsl:value-of select="COPYRIGHT"/>) </h3> <b>Author: </b> <xsl:value-of select="AUTHOR"/> <p><xsl:value-of select="DESC"/></p> </xsl:template> <xsl:template match="PERIODICAL"> <h2>Periodicals</h2> <h3><xsl:value-of select="TITLE"/> -
<xsl:value-of select="DATE"/></h3> <b>Publisher: </b> <xsl:value-of select="PUBLISHER"/><br/> <p><b> Articles </b></p> <table cellpadding="2"> <tr> <th>Title</th> <th>Description</th> </tr> <xsl:for-each select="ARTICLE"> <xsl:apply-templates select="."/> </xsl:for-each> </table> </xsl:template> <xsl:template match="ARTICLE"> <tr> <td><xsl:value-of select="TITLE"/></td> <td><xsl:value-of select="DESC"/></td> </tr> </xsl:template> </xsl:stylesheet>
|
| 4. |
Switch to your DOS window and execute the same oraxsl
command.

|
| 5. |
Switch to IExplorer and open d:\wkdir\book2.xml.html
again or click Refresh.

The Periodical information in the XML file has now been formatted
using the XSL Stylesheet.
|
Validating an XML Document
A well-formed XML document is one that meets all the rules of the XML
specification.
What if just following the syntax rules isn't quite good enough? Your
business needs to validate the actual structure and content of a document.
Your document must not only follow the XML rules, but also satisfy structural
requirements imposed by your business model. For instance, each <book>
element must consist of <title>, <isbn>, <author> and
<copyright>. If a <book> does not have each of these elements,
it is not valid. you can perform validation using a Document Type Definition
(DTD).
What is a DTD?
A DTD provides a list of elements contained in an XML document. It specifies
the structure or schema of a document. When a document is validated against
its DTD, it is guaranteed to be consistent with the definition. This means
that applications using the document such as JavaScript, CGI, servlets,
and others can assume that the data matches the definition. DTD may be
stored in a file separate from the XML document or may be included within
the document. DTDs are simply a set of specific markups that the XML engine
interprets so that it can check the document for validity.
DTD Components
The following elements exist in a DTD:
| Document Type Declaration |
The declaration begins with <!DOCTYPE and ends with ]>. The
declaration of the root element LIBRARY is as follows:
<!DOCTYPE LIBRARY[ ]>
|
| Item Element (s) |
Defines the business rules about the document. The first rule in
our LIBRARY document is not just character data, but rather contains
multiple Item elements. You will define the LIBRARY element to be
a container for item elements. When at the lowest item element level,
you declare the element as character data by using #PCDATA in the
declaration. In this case the syntax is
<!DOCTYPE LIBRARY[ <!ELEMENT LIBRARY (BOOK, PERIODICAL)> <!ELEMENT BOOK (TITLE, ISBN, AUTHOR, DESC, COPYRIGHT)> <!ELEMENT TITLE (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT AUTHOR (#PCDATA)> <!ELEMENT DESC (#PCDATA)> <!ELEMENT COPYRIGHT (#PCDATA)>
]>
|
| Constraints |
A special character suffix that is added to the element definition
to define the cardinality of the element -- that is, how many occurrences
can or must exist within an element. Here are the list of characters
and their meanings:
+ one or more
* zero or more
? zero or one
(none) one and only one
Using the above example as a guide, here is the syntax:
<!DOCTYPE LIBRARY[ <!ELEMENT LIBRARY (BOOK+, PERIODICAL+)> <!ELEMENT BOOK (TITLE, ISBN, AUTHOR+, DESC, COPYRIGHT)> <!ELEMENT TITLE (#PCDATA)> <!ELEMENT ISBN (#PCDATA)> <!ELEMENT AUTHOR (#PCDATA)> <!ELEMENT DESC (#PCDATA)> <!ELEMENT COPYRIGHT (#PCDATA)>
]>
|
Using a DTD
There are two ways to use a DTD. You can put the DTD elements within
the XML document, or you can place them in a separate file referenced
from within the XML document. For simplicity, you will place the DTD statements
within your XML document. Perform the following:
| 1. |
Open book3.xml
from your working directory in Notepad and type the following
in bold:
<?xml version='1.0'?> <!DOCTYPE LIBRARY[ <!ELEMENT LIBRARY (BOOK , PERIODICAL )> <!ELEMENT BOOK (TITLE , ISBN , AUTHOR , DESC , COPYRIGHT )> <!ELEMENT TITLE (#PCDATA )> <!ELEMENT ISBN (#PCDATA )> <!ELEMENT AUTHOR (#PCDATA )> <!ELEMENT DESC (#PCDATA )> <!ELEMENT COPYRIGHT (#PCDATA )> <!ELEMENT PERIODICAL (TITLE , PUBLISHER , DATE , ARTICLE )> <!ELEMENT PUBLISHER (#PCDATA )> <!ELEMENT DATE (#PCDATA )> <!ELEMENT ARTICLE (TITLE , DESC )> ]> <LIBRARY> <BOOK> <TITLE>Is This Your Child</TITLE> <ISBN>0-688-11907-7 </ISBN> <AUTHOR>Doris Rapp M.D.</AUTHOR> <DESC>Discovering and treating unrecognized allergies in children and adults.</DESC> <COPYRIGHT>1991</COPYRIGHT> </BOOK> <PERIODICAL> <TITLE>PCWEEK</TITLE> <PUBLISHER>Ziff Davis Publishing</PUBLISHER> <DATE>April 17, 2000</DATE> <ARTICLE> <TITLE>Auto markets </TITLE> <DESC>Suppliers fear they'll be left in the dust</DESC> </ARTICLE> <ARTICLE> <TITLE>Right time for wireless</TITLE> <DESC>802.11b based wireless networking</DESC> </ARTICLE> </PERIODICAL> </LIBRARY>
|
| 2. |
Switch to your DOS window and execute the following
command:
d:
cd xdk\bin
env
cd \wkdir
oraxml book3.xml

Note that you get errors because you still need to add the constraints.
|
| 3. |
Open book3.xml
again and add the following contraints:
You have more than one book, periodical, author and article.
<?xml version='1.0'?> <!DOCTYPE LIBRARY[ <!ELEMENT LIBRARY (BOOK+ , PERIODICAL+ )> <!ELEMENT BOOK (TITLE , ISBN , AUTHOR+ , DESC , COPYRIGHT )> <!ELEMENT TITLE (#PCDATA )> <!ELEMENT ISBN (#PCDATA )> <!ELEMENT AUTHOR (#PCDATA )> <!ELEMENT DESC (#PCDATA )> <!ELEMENT COPYRIGHT (#PCDATA )> <!ELEMENT PERIODICAL (TITLE , PUBLISHER , DATE , ARTICLE+ )> <!ELEMENT PUBLISHER (#PCDATA )> <!ELEMENT DATE (#PCDATA )> <!ELEMENT ARTICLE (TITLE , DESC )> ]> <LIBRARY> <BOOK> <TITLE>Is This Your Child</TITLE> <ISBN>0-688-11907-7 </ISBN> <AUTHOR>Doris Rapp M.D.</AUTHOR> <DESC>Discovering and treating unrecognized allergies in children and adults.</DESC> <COPYRIGHT>1991</COPYRIGHT> </BOOK> <PERIODICAL> <TITLE>PCWEEK</TITLE> <PUBLISHER>Ziff Davis Publishing</PUBLISHER> <DATE>April 17, 2000</DATE> <ARTICLE> <TITLE>Auto markets </TITLE> <DESC>Suppliers fear they'll be left in the dust</DESC> </ARTICLE> <ARTICLE> <TITLE>Right time for wireless</TITLE> <DESC>802.11b based wireless networking</DESC> </ARTICLE> </PERIODICAL> </LIBRARY>
|
| 4. |
Switch to your DOS window and execute the following
command again:
oraxml book3.xml

Note that your document has been validated.
|
Retrieving Database Data as an XML Document
So far you have seen how to create and manipulate XML documents with
static data. You can also use SQL to produce nonproprietary XML documents
from existing data in a database using the XSQL Servlet.
What are XSQL Pages?
XSQL pages are XML documents with embedded SQL statements. The purpose
of XSQL pages is to support parameterized SQL queries from within an XML
page. This makes it simple to include information stored in a database
in a dynamic XML page. It also cleanly separates the data from any formatting
issues or concerns. The data can be formatted using XSLT into any number
of different formats.
XSQL Page Components
The following components exist in an XSQL page:
| <xsql:query> tag |
This tag indicates that a query will begin.
<xsql:query>
</xsql:query>
|
| <xsql:query> attributes |
The attributes help the XSQL Servlet engine understand which database
to connect to and how to create the XML document.
<xsql:query xmlns:xsql="urn:oracle-xsql"
connection="employee"
rowset-element="OK"
row-element="EMPLOYEE">
</xsql:query>
|
| SQL statement - Simple |
The SQL statement selects all the columns and rows from a particular
table. You can change the element names to reflect the content of
the elements.
<xsql:query xmlns:xsql="urn:oracle-xsql"
connection="employee"
rowset-element="OK"
row-element="EMPLOYEE">
SELECT employee_id "ID"
,last_name "NAME"
,job_id "JOB"
,salary "SALARY"
,commission_pct "COMMISSION"
FROM EMPLOYEES
</xsql:query>
|
| Parameters |
XSQL provides the ability to retrieve HTTP parameters and use them
in the SQL statement. You refer to these lexical-substitution parameters
using the syntax {@paramname}.
The URL could be:
http://localhost:8888/employee1.xsql?empno=104
The xsql query would be:
<xsql:query xmlns:xsql="urn:oracle-xsql"
connection="employee"
rowset-element="OK"
row-element="EMPLOYEE">
SELECT employee_id "ID"
,last_name "NAME"
,job_id "JOB"
,salary "SALARY"
,commission_pct "COMMISSION"
FROM EMPLOYEES
WHERE employee_id={@employee_id}
</xsql:query>
|
| SQL Statement - Complex |
If you need to include special characters in like * and %, then
you need to embed the SQL within <![CDATA[...]]>. This allows
the entire query to be passed to the XSQL Servlet without being
parsed for valid XML characters.
<xsql:query xmlns:xsql="urn:oracle-xsql"
connection="employee"
rowset-element="OK"
row-element="INFO">
<![CDATA[
SELECT count(*) AS "TOTAL"
,job_id AS "JOB"
FROM EMPLOYEES
WHERE job_id LIKE '%'||UPPER('{@cat}')||'%'
]]>
</xsql:query>
|
Building a XSQL Page
You will examine a few ways to select information from the database using
a XSQL page. Perform the following:
| 1. |
Open employee1.xsql
from your working directory in Notepad and type
the following in bold and save the file:
<?xml version='1.0'?> <xsql:query xmlns:xsql="urn:oracle-xsql" connection="employee" rowset-element="OK" row-element="EMPLOYEE"> SELECT employee_id "ID" ,last_name "NAME" ,job_id "JOB" ,salary "SALARY" ,commission_pct "COMMISSION" FROM EMPLOYEES </xsql:query>
|
| 2. |
To test the XSQL page, you will need to start the J2EE Container.
Open a DOS window and execute the following:
d:
cd\jdev902\j2ee\home
java -jar oc4j.jar

|
| 3. |
Open IExplorer and enter the following URL:
http:/<hostname>:8888/obe/employee1.xsql

All of the employees are listed in the XML. You only want a particular
employee. In this case, you would need to specify a parameter.
|
| 4. |
You will add the parameter for employee_id.
Edit the employee1.xsql
file again and add the following in bold and save the file:
<?xml version='1.0'?> <xsql:query xmlns:xsql="urn:oracle-xsql" connection="employee" rowset-element="OK" row-element="EMPLOYEE"> SELECT employee_id "ID" ,last_name "NAME" ,job_id "JOB" ,salary "SALARY" ,commission_pct "COMMISSION" FROM EMPLOYEES WHERE employee_id={@employee_id} </xsql:query>
|
| 5. |
Switch to IExplorer again and change the URL to the following:
http:/<hostname>:8888/obe/employee1.xsql?employee_id=104

Now you only retrieve the employee information for the ID you requested.
|
Module Summary
In this module, you should have learned how to:
Copyright © 2002 Oracle Corporation. All Rights Reserved.
Close Window
|