Вы находитесь на странице: 1из 238

Oracle 10g: XML Fundamentals

Volume 2 - Student Guide

n l y
e O
U s
A I
O
l &
D17320GC10
na
Edition 1.0

te r
August 2004
D39788 I n
c l e
r a
O
Author Copyright © 2004, Oracle. All rights reserved.

Glenn Stokol This documentation contains proprietary information of Oracle Corporation. It is


provided under a license agreement containing restrictions on use and disclosure and
is also protected by copyright law. Reverse engineering of the software is prohibited.
Technical Contributors If this documentation is delivered to a U.S. Government Agency of the Department of
and Reviewers Defense, then it is delivered with Restricted Rights and the following legend is
applicable:
Gert Van Barneveld Restricted Rights Legend
Mark Bauer
Use, duplication or disclosure by the Government is subject to restrictions for
Brian Boxx commercial computer software and shall be deemed to be Restricted Rights software
Scott Brewton under Federal law, as set forth in subparagraph (c)(1)(ii) of DFARS 252.227-7013,
Niall Commiskey Rights in Technical Data and Computer Software (October 1988).

Ken Cooper This material or any portion of it may not be copied in any form or by any means
Olivier Le Diouris without the express prior written permission of Oracle Corporation. Any other copying
is a violation of copyright law and may result in civil and/or criminal penalties.
Mark Drake
Shelley Higgins If this documentation is delivered to a U.S. Government Agency not within the
Craig Hollister Department of Defense, then it is delivered with “Restricted Rights,” as defined in
FAR 52.227-14, Rights in Data-General, including Alternate III (June 1987).
Taj-ul Islam
Peter Laseau The information in this document is subject to change without notice. If you find any
problems in the documentation, please report them in writing to Education Products,
Yi. L. Lu Oracle Corporation, 500 Oracle Parkway, Redwood Shores, CA 94065. Oracle
Jack Melnick Corporation does not warrant that this document is error-free.
Steve Muench All references to Oracle and Oracle products are trademarks or registered trademarks
Valli Pataballa of Oracle Corporation.
Sunitha Patel
All other products or company names are used for identification purposes only, and
Marcus Pecher may be trademarks of their respective owners.
Aniket Raut
Mark Scardina
John Soltani n l y
Priya Vennapusa
Ulrich Vogel
e O
Jinyu Wang
Ted Witiuk
U s
Publisher
A I
Michael Sebastian
O
l &
n a
te r
I n
c l e
r a
O
Contents

Preface

I Introduction
Objectives I-2
Course Objectives I -3
Course Agenda I-4
Extensible Markup Language I-7
Advantages of Using XML I-8
XML Standards I-9
Document Object Model I-10
Simple API for XML I-11
Oracle XML Support I-12
XML in Oracle JDeveloper 10g I-13
Building XML Applications with Oracle Technology I-14
Course Environment and Data I-15
Creating a Workspace and Project in JDeveloper I-16
Creating an XML Document in JDeveloper I-17
Adding an XML Document to a Project I-18
Viewing an XML Document in a Browser I-19
Summary I-20
Practice I: Overview I-21

1 Introducing the XML Document


n l y
Objectives 1-2
Extensible Markup Language 1-3
e O
Example: A Simple XML Page 1-4
XML Document Structure 1-5
U s
The XML Declaration 1-6
Components of an XML Document 1-7
XML Elements 1-8 A I
Markup Rules for Elements 1-9
O
XML Attributes 1-10

l
Using Elements Versus Attributes 1-11&
XML Entities 1-12

n a
XML Comments 1-13

te r
A Well-Formed XML Document 1-14
Class Activity 1-15

I n
Comparing XML and HTML 1-16
XML Development 1-17

l e
Editing XML in Oracle JDeveloper 10g 1-18

c
Summary 1-19

r a
Practice 1: Overview 1-20

O
iii
2 Validating XML with Document Type Definitions (DTDs)
Objectives 2-2
What Is a Document Type Definition? 2-3
Why Validate an XML Document? 2-4
General DTD Rules 2-5
The Contents of a DTD 2-6
Example of a Simple DTD Declaration 2-7
Referencing the DTD 2-8
Element Declarations 2-9
Specifying Cardinality of Elements 2-11
Attribute Declarations 2-12
CDATA and Enumerated Attribute Types 2-13
NOTATION Declaration and Attribute Type 2-14
Specifying Default Attribute Values 2-15
Entities in XML 2-16
General Entity Declarations 2-17
Parameter Entities 2-19
Complete DTD: Example 2-20
Validating XML Against a DTD 2-21
Summary 2-22
Practice 2: Overview 2-23

3 Modularizing XML with Namespaces


Objectives 3-2
What Is an XML Namespace? 3-3
Why Use XML Namespaces? 3-4
n l y
Declaring XML Namespaces 3-5
XML Namespace Prefixes 3-6
e O
Example of XML Namespace Declarations 3-7
Scope of XML Namespace Declarations 3-8
U s
Invoking the ShowXmlNS Tool 3-9
Summary 3-10
Practice 3: Overview 3-11 A I
O
Objectives 4-2
l &
4 Validating XML by Using XML Schema

What Is an XML Schema? 4-3

n a
The Benefits of XML Schemas 4-4

te r
An Example XML Schema Document 4-5
Validating an XML Document with an XML Schema Document 4-6

I n
Referencing an XML Schema with the schemaLocation Attribute 4-7
Components of an XML Schema 4-8

l e
Example of XML Schema Components 4-9

c
The <schema> Declaration 4-10

r a
Global and Local Declarations 4-12
Declaring an Element 4-14
O Built-in XML Schema Data Types 4-15
Declaring a <simpleType> Component 4-17
Using <list> and <union> SimpleTypes 4-19
Declaring <complexType> Components 4-20

iv
Declaring a <sequence> 4-21
Declaring a <choice> 4-22
Declaring an Empty Element 4-23
Using Element Wildcards 4-24
Declaring Attributes 4-25
Attribute Declarations: Example 4-26
Declaring and Referencing an <attributeGroup> 4-27
Documenting the XML Schema 4-28
Creating an XML Schema Document in JDeveloper 4-29
Using the JDeveloper XML Schema Editor 4-30
Using the JDeveloper XML Schema-Aware XML Editor 4-31
Registering an XML Schema with JDeveloper 4-32
Using the XML Schema-Aware XML Editor 4-33
Validating an XML Document with its XML Schema in JDeveloper 4-34
Validating an XML Document with its XML Schema by Using oraxml 4-35
Applications for XML Schema 4-36
XML Schema Versus DTD 4-37
Converting a DTD to an XML Schema 4-38
Summary 4-39
Practice 4: Overview 4-40

5 Navigating XML Documents by Using XPath


Objectives 5-2
What Is XML Path Language? 5-3
The XPath Model 5-4
XPath Expressions 5-5
n l y
The Location Path Expression 5-6
Results of Location Path Expressions 5-7
e O
Location Steps in XPath Expressions 5-8
XPath Axes 5-9
U s
XPath Node Test Types 5-10
Unabbreviated and Abbreviated Expressions 5-11
A I
O
Abbreviated XPath Expression: Examples 5-13
XPath Predicates 5-14

XPath Functions 5-17


l &
Operators in XPath Expressions 5-16

Boolean Functions 5-18


n a
Number Functions 5-19

te
Node-Set Functions 5-20r
I n
String Functions 5-21
XSLT and XPath 5-22

c l e
Testing XPath Expressions 5-23
Using the TestXPath Tool 5-24

r a
Summary 5-25
Practice 5: Overview 5-26

O
6 Transforming XML by Using XSL Transformations
Objectives 6-2
What Is XSL? 6-3
XSL Transformations 6-4
The XSLT Stylesheet 6-5
v
XSLT Stylesheet: Example 6-6
Using an XSLT Stylesheet with an XML Document 6-7
Viewing the Transformed Document 6-8
JDeveloper Support for XSLT 6-9
Creating Template Rules 6-10
Getting Input Text with <xsl:value-of> 6-11
Applying Template Rules 6-12
Controlling Template Activation Order 6-14
Template Rules and Priorities 6-15
Default Template Rules 6-16
Effects of Default Template Rules 6-17
Looping with <xsl:for-each> 6-18
Specifying Output Formats 6-19
Attribute Value Templates 6-20
Creating Elements with Attributes 6-21
Sorting an XML Document 6-22
Conditional Processing with <xsl:if> 6-23
Conditional Processing with <xsl:choose> 6-24
Modes 6-25
Example of Using Modes 6-26
Calling Templates by Name 6-29
Creating and Using Parameters 6-30
Using the oraxsl Utility 6-31
Summary 6-33
Practice 6: Overview 6-34
n l y
7 XSQL Pages Publishing Framework
Objectives 7-2
e O
Web Application Architecture 7-3
XSQL Pages Publishing Framework 7-4
U s
Applications and Benefits of XSQL Pages 7-5
Structure of a Simple XSQL Page 7-6
A I
O
Creating an XSQL Page in JDeveloper 7-7
Creating Database Connections 7-8

Viewing an XSQL Page 7-10


l &
Creating an <xsql:query> Action Element 7-9

a
Customizing the <xsql:query> Output 7-11

n
te r
Using Parameters in XSQL Pages 7-12
Supplying Parameters to XSQL Pages 7-13
Parameters and Order of Precedence 7-14

I n
Providing a Fallback Query 7-15

l e
Results of Fallback Query: Example 7-16
An XSQL Page with Multiple Queries 7-17
c
r a
Transforming an XSQL Page with XSLT 7-18
XSQL Page with XSLT: Example 7-19

O Summary 7-20
Practice 7: Overview 7-21

vi
8 Additional Features in XSQL Pages
Objectives 8-2
Using CURSOR Expressions 8-3
Exchanging Data with XSQL Parameters 8-4
Setting Page Parameters 8-5
Including Parameters in an XSQL Page 8-7
Setting HTTP Cookie Values 8-9
Setting HTTP Session Variables 8-10
Aggregating Information 8-12
Including External Well-Formed XML 8-13
Results of Including XML 8-14
Data Manipulation by Using XSQL 8-15
Results of DML: Example 8-16
Posting XML Documents for DML Operations 8-17
Handling Posted XML Data 8-18
Transforming XML to Canonical Form 8-20
Summary 8-21
Practice 8: Overview 8-22

9 Introduction to Oracle XML DB (XML DB)


Objectives 9-2
What Is Oracle XML DB? 9-3
When to Use Oracle XML DB 9-4
How Is Oracle XML DB Implemented? 9-6
Oracle XML DB: Features 9-7
XMLType 9-10
n l y
When to Use XMLType 9-11
XML Schema Support in Oracle Database 10g 9-12
e O
XMLType and XML Schema 9-15
XML Schema Management 9-17
U s
Registering an XML Schema 9-18
Storage and Access Infrastructure 9-20
Local and Global XML Schemas 9-21 A I
Deleting an XML Schema 9-22
O
Validating XML 9-24
l &
Example of Deleting an XML Schema 9-23

Schema Validation 9-25

n a
te r
Overview of XML Repository 9-28
What Is the Repository? 9-29
XML Repository: Features 9-30

I n
Repository Support for Internet Protocols 9-31

e
Oracle XML DB Versioning 9-32

l
Benefits of Oracle XML DB 9-33
c
Summary 9-34

r a
Practice 9: Overview 9-35

O
vii
10 Generating XML from an Oracle Database 10g
Objectives 10-2
SQL/XML Generation Functions 10-3
The XMLElement() SQL/XML Function 10-4
XML Elements with Attributes 10-5
The XMLForest() SQL/XML Function 10-6
Generating Nested XML Elements 10-7
The XMLSequence() SQL/XML Function 10-8
The XMLConcat() SQL/XML Function 10-9
The XMLAgg() SQL/XML Function 10-11
Generating Master-Detail Content 10-12
A Complex Master-Detail: Example 10-13
The XMLColAttVal() SQL/XML Function 10-14
Native XML Generation 10-15
Using SYS_XMLGEN() 10-16
Using SYS_XMLAGG() 10-18
DBMS_XMLGEN 10-19
Summary 10-20
Practice 10: Overview 10-21

11 Managing XML Data in an Oracle 10g Database


Objectives 11-2
XMLType 11-3
The Benefits of Using XMLType 11-4
Declaring an XMLType 11-5
XMLType Storage Characteristics 11-6
n l y
Unstructured Versus Structured Storage 11-7
CLOB Versus Object-Relational Storage 11-8
e O
Loading Data into XMLType 11-9
Overview of Querying XML 11-10
U s
Subset of XMLType Member Functions 11-11
Querying an XMLType by Using XPath 11-12
Using the existsNode() Function 11-13 A I
Using the extract() Function 11-14
O
l &
Combining extract() and existsNode() 11-16
Using the extractValue() Function 11-17

a
Filtering with extractValue() 11-18

n
Summary 11-19

te r
Practice 11: Overview 11-20

I n
12 Creating XMLType Views
Objectives 12-2

l e
What Are XMLType Views? 12-3

c
Creating XMLType Views 12-4

r a
Creating Non-Schema-Based Views 12-5
Non-Schema XMLType Views with SQL/XML Functions 12-6
O Non-Schema XMLType Views with Object Types and SYS_XMLGEN() 12-7
Creating XML Schema-Based Views 12-8

viii
XML Schema XMLType Views with SQL/XML Functions 12-9
Using Namespaces with SQL/XML Functions 12-11
XML Schema XMLType Views with Object Types 12-12
Creating XMLType Views from XMLType Tables 12-13
DML with XMLType Views 12-14
Example of an Updateable XMLType View 12-15
Summary 12-17
Practice 12: Overview 12-18

13 Executing SQL Operations on XML


Objectives 13-2
Overview of Transforming XML 13-3
Transform Functions 13-4
Using the XMLTransform() Function 13-5
Results of Using the XMLTransform() Function 13-7
Benefits of XML Transformation 13-8
Updating XML 13-9
Using the updateXML() Function 13-10
Piecewise Update of XML 13-11
Deleting Rows Based on XML Contents 13-13
Indexing XML Data 13-14
Function-Based Indexes 13-15
Using a CTXXPATH Index 13-16
Using a Context Index 13-17
Summary 13-18
Practice 13: Overview 13-19
n l y
14 Accessing XML Through URIs
e O
Objectives 14-2
Definitions 14-3
U s
URI Support in Oracle Database 14-4
The DBUri Servlet 14-5
Retrieving Table Data as an XML Document 14-6 A I
Retrieving a Single Row as XML 14-7
O
Retrieving Column Data 14-9
l &
Retrieving Multiple Rows as XML 14-8

a
Retrieving the Column Text 14-10

n
te r
Using Multiple Predicates 14-11
Applying a Stylesheet 14-12
Results After Applying a Stylesheet 14-13

I
Summary 14-14
n
Practice 14: Overview 14-15

c l e
r a
O
ix
Appendix A: Practice Solutions

Appendix B: Table Descriptions and Data

Appendix C: Additional Information About Document Type Definitions


Objectives C-2
Declaration Types in a DTD (Revisited) C-3
Referencing the DTD (Revisited) C-4
Element Declarations (Revisited) C-5
Cardinality of Elements (Revisited) C-7
Attribute Declarations (Revisited) C-8
CDATA and Enumerated Attribute Types C-9
The ENTITY and ENTITIES Attribute Types C-10
The ID Attribute Type C-12
The IDREF and IDREFS Attribute Types C-13
The NMTOKEN and NMTOKENS Attribute Types C-14
Entities in XML (Revisited) C-15
Summary C-16

Appendix D: Using Oracle XML Developer’s Kit


Objectives D-2
Overview of XML Developer’s Kit D-3
XML Trees D-4
XML Trees: Example D-5
XML Developer’s Kit Components D-6
XML Parsers D-8
n l y
XSL Transformation (XSLT) Processors D-9
Oracle XML Schema Processors D-11
Using XML Schema Processors D-12
e O
XML Class Generators D-13
Using XML Class Generators D-14
U s
Oracle XDK for JavaBeans D-15
XML SQL Utility D-17
A I
Applications of XSU D-18
XSU Command-Line Utility D-19 O
Using the OracleXML Utility D-20
l &
n a
Updating and Deleting by Using XSU API D-22
Generating an XML Schema with XSU D-23
TransX Utiliity D-25

te r
Using TransX Utility D-26
Summary D-28
I n
c l e
Appendix E: Managing External Tools in JDeveloper
Objectives E-2

r a
External Tools with JDeveloper E-3
Managing External Tools E-4

O Adding an External Tool E-5


Editing an External Tool E-9
Removing an External Tool E-10
Summary E-11

x
Preface

n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
Profile

Before You Begin This Course


Before you begin this course, you should have thorough knowledge of SQL, and SQL*Plus or
iSQL*Plus. The following Oracle University course is a prerequisite:
• Oracle Database 10g: Introduction to SQL
How This Course Is Organized
Oracle 10g: XML Fundamentals is an instructor-led course featuring lectures and hands-on
exercises. Online demonstrations and practice sessions reinforce the concepts and skills that are
introduced.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O

Preface-3
Related Publications

Oracle Publications
Title Part Number
Oracle® XML DB Developer’s Guide (10g Release 1) B10790-01
Oracle® XML Developer’s Kit Programmer's Guide (10g Release 1) B10794-01
Oracle® Database SQL Reference (10g Release 1) B10759-01
PL/SQL Packages and Types Reference (10g Release 1) B10802-01

Additional Publications
• System release bulletins
• Installation and user’s guides
• Read-me files
• International Oracle Users Group (IOUG) articles
• Oracle Magazine

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O

Preface-4
Typographic Conventions

Typographic Conventions in Text


Convention Element Example
Bold Emphasized words and phrases To navigate within this application, do
in Web content only not click the Back and Forward buttons.

Bold italic Glossary terms (if there is a The algorithm inserts the new key.
glossary)

Brackets Key names Press [Enter].

Caps and Buttons, Click the Executable button.


lowercase check boxes, Select the Registration Required check
triggers, box.
windows
Assign a When-Validate-Item trigger.
Open the Master Schedule window.

Carets Menu paths Select File > Save.

Commas Key sequences Press and release these keys one at a


n l y
time:
[Alt], [F], [D]
e O
U s
A I
O
l &
n a
te r
I n
c le
r a
O

Preface-5
Typographic Conventions (continued)

Typographic Conventions in Text (continued)


Convention Object or Term Example
Courier New, Code output, Code output: debug.seti (‘I’,300);
case sensitive SQL and PL/SQL
SQL code elements: Use the SELECT command to view
code elements, Java
information stored in the last_name column of the emp
code elements,
table.
directory names,
filenames, Java code elements: Java programming involves the
passwords, String and StringBuffer classes.
pathnames, URLs, Directory names: bin (DOS), $FMHOME (UNIX)
user input,
usernames Filenames: Locate the init.ora file.
Passwords: Use tiger as your password.
Pathnames: Open c:\my_docs\projects.
URLs: Go to http://www.oracle.com.
User input: Enter 300.
Usernames: Log on as scott.
Initial cap Graphics labels Customer address (but Oracle Payables)
(unless the term is a
proper noun)
n l y
Italic Emphasized words
and phrases in print
Do not save changes to the database.

e O
For further information, see Oracle7 Server SQL
publications, titles of
books and courses,
Language Reference Manual.

U s
variables

A I
Enter user_id@us.oracle.com, where user_id is
the name of the user.
Plus signs Key combinations
O
Press and hold these keys simultaneously:
[Control] + [Alt] + [Delete]

l &
Quotation
marks
Lesson and chapter
titles in cross
n a
This subject is covered in Unit II, Lesson 3, “Working with
Objects.”

te r
references, interface
elements with long Select the “Include a reusable module component” and

I n
names that have only
initial caps
click Finish.

cl e Use the “WHERE clause of query” property.

r a
O

Preface-6
Typographic Conventions (continued)

Typographic Conventions in Navigation Paths


This course uses simplified navigation paths to direct you through Oracle applications, as in
the following example.
Invoice Batch Summary
(N) Invoice > Entry > Invoice Batches Summary (M) Query > Find (B) Approve
This simplified path translates to the following sequence of steps:
1. (N) From the Navigator window, select Invoice > Entry > Invoice Batches Summary.
2. (M) From the menu, select Query > Find.
3. (B) Click the Approve button.
Notation:
(N) = Navigator (I) = icon
(M) = menu (H) = hyperlink
(T) = tab (B) = button

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O

Preface-7
n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
________
A
Practice Solutions
________

n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
Practice I: Solutions
The goal of this practice is to become familiar with the basic JDeveloper features needed to
create and open XML documents, and to experience the default appearance of an XML
document in an XML-enabled browser.

1. Start Oracle JDeveloper by using the icon on the desktop.

2. Open an existing workspace called XMLFundamentalsWorkspace.jws located in the


E:\labs directory. The workspace contains a project called XMLLabsProject.

3. Create an XML document called employees.xml.

a. Add the following employees XML element after the XML declaration:
<?xml version='1.0' encoding='windows-1252'?>
<employees>
Company Employee Data
</employees>

Note: The first line, containing the XML Declaration, will automatically be inserted for you.

b. Save the file by using the File > Save menu (or the Save toolbar icon).

4. Open and add an existing XML document called regions.xml to your project. The XML
document is located in the E:\labs\lab directory.
n l y
select File > Open and double-click the file name.
e O
Select XMLLabsProject in the Navigator, and click the Add File to Project (+) Icon, or

5. View the XML documents in the Internet Explorer browser.


U s
A I
a. In Windows Explorer, double-click employees.xml file in the E:\labs\lab
directory to display the file in the browser.
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-2
Practice I: Solutions (continued)

b. In JDeveloper, use Tools > Internet Explorer to display regions.xml in the browser.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-3
Practice 1: Solutions
The goal of this practice is to create a well-formed XML document containing employee
information. You use SQL*Plus to execute a query to obtain the content for your document.
After the document is created, you are instructed to force some errors and explain how the
changes violate the rules of a well-formed document. Optionally, you may answer paper-based
questions to facilitate the understanding of an XML document.

1. Generate XML data from the database for an XML document.

a. Start SQL*Plus by double-clicking the SQL*Plus icon.

b. Log in to the database by using your username (orax), password (oracle), and the
database host/connect string. The instructor must provide you with a number x and the
database connect string for the course.

c. Execute the SQL script called E:\labs\lab\lab_01_01.sql to generate a list of


data for employees in department 60. The script saves the data in a file called
E:\labs\lab\lab_01_01.txt.
REM
REM Script: lab_01_01.sql
REM
set pagesize 24 trimspool on feedback off echo off verify off
col first_name format a20 temp
col last_name format a20 temp
define outfile="\labs\lab\lab_01_01.txt"
spool &outfile n l y
select employee_id, first_name, last_name, salary
from employees
e O
where department_id = 60
/
U s
spool off
prompt
A I
prompt NOTE: These results have been saved in the file: "&outfile"
prompt
O
undefine outfile
l &
prompt Optionally Type: exit to terminate SQL*PLus

set feedback on

n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-4
Practice 1: Solutions (continued)

Solution step and result for executing the lab_01_01.sql script is:
SQL> @e:\labs\lab\lab_01_01.sql

EMPLOYEE_ID FIRST_NAME LAST_NAME SALARY


----------- -------------------- -------------------- ----------
103 Alexander Hunold 9000
104 Bruce Ernst 6000
105 David Austin 4800
106 Valli Pataballa 4800
107 Diana Lorentz 4200

NOTE: The results have been saved in the file: "\labs\lab\lab_01_01.txt"

Optionally Type: exit to terminate SQL*PLus


SQL>

2. Modify the employees.xml document in the XMLLabsProject to contain elements for


the employees in department 60.

a. Convert the text Company Employee Data in the <employees> element into an
XML comment.
<?xml version='1.0' encoding='windows-1252'?>
<employees>
n l y
O
<!-- Company Employee Data -->
</employees>

s e
b. For each employee row in the lab_01_01.txt file, add an employee element by using
the following structure:
I U
<employee>
<employee_id><!-- id --></employee_id>
<first_name><!-- first name --></first_name>
O A
<salary><!-- salary --></salary>
l &
<last_name><!-- last name --></last_name>

</employee>

n a
te r
Hint: You can use the file E:\labs\lab\lab_01_02.xml as a template. There
must be five employees.

I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-5
Practice 1: Solutions (continued)

The employees.xml file should now contain the following additional elements (in bold):
<?xml version='1.0' encoding='windows-1252'?>
<employees>
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
<employee>
<employee_id>104</employee_id>
<first_name>Bruce</first_name>
<last_name>Ernst</last_name>
<salary>6000</salary>
</employee>
<employee>
<employee_id>105</employee_id>
<first_name>David</first_name>
<last_name>Austin</last_name>
<salary>4800</salary>
</employee>

y
<employee>
<employee_id>106</employee_id>
<first_name>Valli</first_name>
n l
<last_name>Pataballa</last_name>
<salary>4800</salary>
</employee>
e O
<employee>
<employee_id>107</employee_id>
U s
<first_name>Diana</first_name>
<last_name>Lorentz</last_name>
A I
<salary>4200</salary>
</employee>
O
</employees>

l &
a
c. Save the changes to your document, and check the employees.xml document to see
n
te r
whether it is well-formed. Correct the errors and save the changes.
In the JDeveloper Messages tab window, the following text is the final result if there are

I n
no XML Syntax errors.

l e
Checking XML Syntax...

c
Checking E:\labs\lab\employees.xml

a
Check XML Syntax: 0 errors, 0 warnings.

r
O
Oracle 10g: XML Fundamentals A-6
Practice 1: Solutions (continued)

d. Display the employees.xml file in your browser.

n l y
e O
U s
A I
O
l &
n a
te r
Note: The browser display above shows a subset of the data in the employees.xml file.

I n
3. Add an XML document containing department data to the XMLLabsProject and correct

l e
the errors violating the rules of a well-formed document.

c
Note: After checking and before correcting the syntax errors in JDeveloper, you can

r a
optionally view the XML document in the browser to observe how it reports the syntax
errors.
O
Oracle 10g: XML Fundamentals A-7
Practice 1: Solutions (continued)

a. Add the file E:\labs\lab\departments.xml to your project.

The solution steps are the following:

i. Select the XMLLabsProject project in the Navigator.

ii. Either click File > Open …, or right-click the project name and select
Add to Project ….

iii. Select the E:\labs\lab\departments.xml file and click Open.

b. Check the XML document syntax, and correct the error reported. Repeat this step until all
the syntax errors are removed.
Hint: There are three syntax errors in the document, which are indicated as warnings in
the Structure pane.

XML Syntax Error 1: “>” Missing from end tag for first department element.
Error(9,3): <Line 9, Column 3>: XML-0123: (Fatal Error) '>' Missing from
end tag.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-8
Practice 1: Solutions (continued)

Correction for XML Syntax Error 1: Add the missing “>” character.
<?xml version='1.0' encoding='windows-1252'?>
<departments>
<department>
<department_id>10</department_id>
<department_name>Administration</department_name>
<location_id>1700</location_id>
<manager_id></manager_id>
</department>
<department>
<department_id>20</department_id>
<department_name>Marketing</department_name>
<Manager_id>201</manager_id>
<location_id>Toronto</location_id>
</departmnt>
</departments>

XML Syntax Error 2: End tag does not match the start tag for “Manager_id”.
Error(12,32): <Line 12, Column 32>: XML-0121: (Fatal Error) End tag does
not match start tag 'Manager_id'.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-9
Practice 1: Solutions (continued)

Correction for XML Syntax Error 2: Change the first character of the start tag to
lowercase “m” for the Manager_id element in the second <department> element.
<?xml version='1.0' encoding='windows-1252'?>
<departments>
<department>
<department_id>10</department_id>
<department_name>Administration</department_name>
<location_id>1700</location_id>
<manager_id></manager_id>
</department>
<department>
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</departmnt>
</departments>

XML Syntax Error 3: The end tag for the second <department> element is
misspelled.
Error(14,14): <Line 14, Column 14>: XML-0121: (Fatal Error) End tag does
not match start tag 'department'.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-10
Practice 1: Solutions (continued)

Correction for XML Syntax Error 3: Fix the end tag to read </department>.
<?xml version='1.0' encoding='windows-1252'?>
<departments>
<department>
<department_id>10</department_id>
<department_name>Administration</department_name>
<location_id>1700</location_id>
<manager_id></manager_id>
</department>
<department>
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</department>
</departments>

The departments.xml document is now well-formed, without XML syntax errors.

c. Add an attribute called num, which is assigned a value of 1, to the start tag of the first
<department> element. Do not use quotes around the attribute value.
<?xml version='1.0' encoding='windows-1252'?>
<departments>
n l y
O
<department num=1>
<department_id>10</department_id>
<department_name>Administration</department_name>
<location_id>1700</location_id>
s e
<manager_id></manager_id>
</department>
I U
<department>
<department_id>20</department_id>
<department_name>Marketing</department_name>
O A
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
l &
</department>
</departments>
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-11
Practice 1: Solutions (continued)

d. Check the XML document syntax, and explain and fix the syntax error. Check the
document syntax to confirm your correction, and save changes to the document.

XML Syntax Error: The attribute value must be enclosed in quotes.


Error(3,20): <Line 3, Column 20>: XML-0125: (Fatal Error) Attribute value
should start with quote.

n l y
e O
U s
Correction: Enclose the num attribute value 1 in quotes.
<?xml version='1.0' encoding='windows-1252'?>
A I
<departments>
<department num="1"> O
<department_id>10</department_id>
l &
n
<location_id>1700</location_id>
a
<department_name>Administration</department_name>

<manager_id></manager_id>
</department>
te r
<department>

I n
<department_id>20</department_id>

e
<department_name>Marketing</department_name>

l
<manager_id>201</manager_id>
c
<location_id>Toronto</location_id>

r a
</department>

O
</departments>

Oracle 10g: XML Fundamentals A-12


Practice 1: Solutions (continued)

If you have time:

4. Answer each of the following questions about XML documents: (Solutions are in bold)

a. XML is a markup language for formatting and displaying text and images.
True/False

b. XML affords the simple exchange of data between applications.


True/False

c. Element tags within an XML document permit other applications to parse the document
for specific information.
True/False

d. The following syntax represents a well-formed XML document:


<?XML version="1.0"?>
<customer>
<name>Mary Lewis>
<state>VA</state>
</customer>

True/False. The <name> element is missing the end tag: </name>. Correct syntax is:
<?XML version="1.0"?>
<customer>
n l y
O
<name>Mary Lewis</name>
<state>VA</state>
</customer>

s e
e. A root element is ____.
Required to be the first line of an XML document
I U
Container of all other elements
Not required
O A
Start and end tags
l &
f. Elements that contain data must have ____.

A declaration statement
n a
Attributes

te r
I n
g. Elements are ___________ as requirements expand.
Used for metadata and are helpful

l e
Not easily added

c
Easily added

r a
h. Although not strictly required, including __________ is a good and common practice.

O An XML declaration
A root element
Start and end tags

Oracle 10g: XML Fundamentals A-13


Practice 2: Solutions
The goal of this practice is to create a valid XML document based on DTD declarations. You
also create internal DTD declarations.

1. Modify the employees.xml file by adding a new employee and validate the document by
using a supplied DTD.

a. Add the following new employee as the last <employee> element in the
employees.xml file, just before the </employees> end tag:
<employee>
<emp_id>007</emp_id>
<last_name>Bond</last_name>
<first_name>James</first_name>
</employee>

Hint: Copy the text from E:\labs\lab\lab_02_01_a.xml to save time. Check


the syntax of the employees.xml document and save the changes.
<?xml version='1.0' encoding='windows-1252'?>
<employees>
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>

y
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
n l
</employee>
<employee>
e O
s
<employee_id>104</employee_id>
<first_name>Bruce</first_name>
<last_name>Ernst</last_name>
<salary>6000</salary>
I U
</employee>
<employee>
<employee_id>105</employee_id> O A
<first_name>David</first_name>
<last_name>Austin</last_name>
l &
<salary>4800</salary>

n a
</employee>
<employee>

te r
<employee_id>106</employee_id>

I n
<first_name>Valli</first_name>
<last_name>Pataballa</last_name>

l e
<salary>4800</salary>

c
</employee>

r a
O
Oracle 10g: XML Fundamentals A-14
Practice 2: Solutions (continued)
<employee>
<employee_id>107</employee_id>
<first_name>Diana</first_name>
<last_name>Lorentz</last_name>
<salary>4200</salary>
</employee>
<employee>
<emp_id>007</emp_id>
<last_name>Bond</last_name>
<first_name>James</first_name>
</employee>
</employees>

At this point, the document syntax is well-formed, but the docucment is not valid. The DTD
will assist with validating the document.

b. Add the DTD file E:\lab\labs\employees.dtd to your project and examine the
DTD declarations.
<!ELEMENT employees (employee*)>
<!ELEMENT employee (employee_id, first_name, last_name, salary)>
<!ELEMENT employee_id (#PCDATA)>
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT
<!ELEMENT
last_name (#PCDATA)>
salary (#PCDATA)>
n l y
c. Associate the employees.dtd file with employees.xml by inserting an
e O
U s
appropriate document type declaration using a <!DOCTYPE …> entry. Check the XML
syntax, correct syntax errors, and save the changes to the file.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE employees SYSTEM "employees.dtd">
A I
<employees>
<!-- Company Employee Data --> O
<employee>
<employee_id>103</employee_id>
l &
<first_name>Alexander</first_name>
n a
<salary>9000</salary>
</employee>
te r
<last_name>Hunold</last_name>

...
<employee> I n
l e
<emp_id>007</emp_id>

c
<last_name>Bond</last_name>

r a
<first_name>James</first_name>
</employee>

O
</employees>

Oracle 10g: XML Fundamentals A-15


Practice 2: Solutions (continued)

d. Validate employees.xml using the Tools > OraXML-dtd menu, and check for
errors in the Message Log window.
In the OraXML-dtd tab pane of Message Log window, you should see the following
validation errors:
file:/E:/labs/lab/employees.xml<Line 36, Column 12>: XML-0148: (Error)
Invalid element 'emp_id' in content of 'employee', expected elements
'[employee_id]'.

file:/E:/labs/lab/employees.xml<Line 36, Column 12>: XML-0149: (Error)


Element 'emp_id' used but not declared.

file:/E:/labs/lab/employees.xml<Line 37, Column 15>: XML-0148: (Error)


Invalid element 'last_name' in content of 'employee', expected elements
'[employee_id]'.

file:/E:/labs/lab/employees.xml<Line 38, Column 16>: XML-0148: (Error)


Invalid element 'first_name' in content of 'employee', expected elements
'[employee_id]'.

file:/E:/labs/lab/employees.xml<Line 39, Column 14>: XML-0150: (Error)


Element employee not complete, expected elements '[employee_id]'.

Error while parsing input file: employees.xml(Invalid element 'emp_id' in


content of 'employee', expected elements '[employee_id]'.)
n l y
e O
e. Using information in the Message Log window, make corrections to the

using Tools > OraXML-dtd until the errors are fixed.


U s
employees.xml document, and save your changes. Validate the employees.xml

A I
The corrections needed are in the employee data for James Bond:

O
- The <emp_id> element is invalid and should be <employee_id>.
- Bond’s <last_name> should appear after the <first_name> element.

l &
- Bond <employee> element requires a <salary> child element.

a
The changes for the solution are shown in bold
n
te r
(Note: You can use any salary value.):
<?xml version='1.0' encoding='windows-1252'?>

I n
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees>

l e
<!-- Company Employee Data -->

c
<employee>

r a
<employee_id>103</employee_id>
<first_name>Alexander</first_name>

O<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>

Oracle 10g: XML Fundamentals A-16


Practice 2: Solutions (continued)
...
<employee>
<employee_id>107</employee_id>
<first_name>Diana</first_name>
<last_name>Lorentz</last_name>
<salary>4200</salary>
</employee>
<employee>
<employee_id>007</employee_id>
<first_name>James</first_name>
<last_name>Bond</last_name>
<salary>5000</salary>
</employee>
</employees>

Validating the changes above using Tools > OraXML-dtd, yields the following result:
The input XML file is parsed without errors using DTD validation mode.

2. Associate an external DTD with departments.xml, validate, and correct the document
using an internal DTD declaration.

a. Add the E:\labs\lab\departments.dtd file into the XMLLabsProject, and


add a DOCTYPE reference in departments.xml to use the DTD.

n l y
File: departments.dtd:
<!ELEMENT departments (department)*>
e O
location_id?)>
U s
<!ELEMENT department (department_id, department_name, manager_id?,

<!ATTLIST department num CDATA #REQUIRED>


<!ELEMENT department_id (#PCDATA)>
<!ELEMENT department_name (#PCDATA)>
A I
<!ELEMENT manager_id (#PCDATA)>
O
&
<!ELEMENT location_id (#PCDATA)>

a l
r n
I n te
c l e
r a
O
Oracle 10g: XML Fundamentals A-17
Practice 2: Solutions (continued)

File: departments.xml:
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE departments SYSTEM "departments.dtd">
<departments>
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<location_id>1700</location_id>
<manager_id></manager_id>
</department>
<department>
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</department>
</departments>

b. Validate the departments.xml document using Tools > OraXML-dtd. Explain


any errors reported and correct them, and validate again to confirm the changes you have
made are valid.
Hint: The num attribute is used as a sequence number.

The OraXML-dtd log messages are:


n l y
file:/E:/labs/lab/departments.xml<Line 8, Column 16>: XML-0147: (Error)

e
Invalid element 'manager_id' in content of 'department', expected closing
O
tag.

U s
REQUIRED attribute 'num' is not specified.
A I
file:/E:/labs/lab/departments.xml<Line 10, Column 15>: XML-0138: (Error)

O
Error while parsing input file: departments.xml(Invalid element

&
'manager_id' in content of 'department', expected closing tag.)

l
a
The first error indicates that the <manager_id> element for department 10 is not
n
te r
expected. Consulting the DTD indicates that the <manager_id> element is optional,
and, if present, it must appear before <location_id> element. Move the

n
<manager_id> element for department 10 before its <location_id> element.
I
l e
The second error indicates that the num attribute is missing from department 20. Looking

c
at the DTD indicates that the num attribute is mandatory (#REQUIRED). Add the

r a
attribute num="1" to the start tag of <department> element for department 20.

O
Oracle 10g: XML Fundamentals A-18
Practice 2: Solutions (continued)

The resulting departments.xml file, after corrections (in bold) are made, is:
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE departments SYSTEM "departments.dtd">
<departments>
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
</department>
<department num="2">
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</department>
</departments>

Message when validating, after the changes are made, is:


The input XML file is parsed without errors using DTD validation mode.

c. Modify the DOCTYPE declaration in departments.xml to include an internal DTD


l y
declaration redefining the num attribute to use the #IMPLIED default setting. Save the
changes and validate departments.xml using Tools > OraXML-dtd. n
<?xml version='1.0' encoding='windows-1252'?>
e O
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
]>
U s
<departments>
<department num="1">
A I
<department_id>10</department_id>
O
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
l &
</department>
n a
<department num="2">

te r
<department_id>20</department_id>
<department_name>Marketing</department_name>

I n
<manager_id>201</manager_id>
<location_id>Toronto</location_id>

l
</department>

c
</departments> e
r a
As shown here, the validation results should be positive:
O
The input XML file is parsed without errors using DTD validation mode.

Oracle 10g: XML Fundamentals A-19


Practice 2: Solutions (continued)

d. In the internal DTD, add an external entity declaration called deptData that references
an external file called E:\labs\lab\newdepts.txt. Add newdepts.txt file to
your project to view its contents, and observe that the second <department> element
(for department 40) does not have a num attribute.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
<!ENTITY deptData SYSTEM "newdepts.txt">
]>
<departments>
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
</department>
<department num="2">
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>

y
</department>
</departments>

n l
e O
e. Reference the deptData entity before the end tag of the document root element. Save
the changes to departments.xml, and validate the document using Tools >
OraXML-dtd.

U s
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
A I
<!ENTITY deptData SYSTEM "newdepts.txt">
]> O
<departments>
l &
a
<department num="1">

r n
<department_id>10</department_id>
<department_name>Administration</department_name>

te
<manager_id></manager_id>

n
<location_id>1700</location_id>
</department>

e I
<department num="2">

c l
<department_id>20</department_id>
<department_name>Marketing</department_name>

r a
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
O
</department>
&deptData;
</departments>

Oracle 10g: XML Fundamentals A-20


Practice 2: Solutions (continued)

The validation of departments.xml should be positive as shown below, even though the
<deparment> element for department 40 is missing its num attribute. The reason is that
the internal DTD declaration for the num attribute makes it optional (#IMPLIED), which
overrides the num attribute declaration in the external DTD:
The input XML file is parsed without errors using DTD validation mode.

f. View the document in Internet Explorer to see the results of the entity reference being
replaced by the contents of the newdepts.txt file.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Note: The &deptData; entity reference is replaced by its contents in Internet Explorer.

Oracle 10g: XML Fundamentals A-21


Practice 3: Solutions
The goal of this practice is to add XML Namespaces to an existing XML document and then use
the ShowXmlNS tool to examine the effects of using XML Namespaces.
Note: In this course, the source code for the ShowXmlNS Java class is provided in the
JDeveloper project. Oracle JDeveloper has been configured to have an External Tool command
called ShowXmlNS that can be invoked using the following steps:

i. Right-click the XML document in the Navigator or the XML Editor.

ii. Select ShowXmlNS from the menu.


The ShowXmlNS displays the expanded XML Namespace in the start tag of each element in the
Message Log window.

1. In the XMLLabsProject, edit employees.xml and add a default XML Namespace.

a. In the employees root element add a default XML Namespace with the URN string value
urn:hr:employees, and save the change to the XML document.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees xmlns="urn:hr:employees">
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
n l y
<salary>9000</salary>
</employee>
e O
<employee>
<employee_id>104</employee_id>
U s
I
<first_name>Bruce</first_name>
<last_name>Ernst</last_name>
<salary>6000</salary>
</employee>
<employee> O A
<employee_id>105</employee_id>
l &
a
<first_name>David</first_name>

n
<last_name>Austin</last_name>
<salary>4800</salary>
</employee>
te r
n
...
<employee>

e I
<employee_id>007</employee_id>

c l
<first_name>James</first_name>
<last_name>Bond</last_name>

r a
<salary>5000</salary>
</employee>
O
</employees>

Oracle 10g: XML Fundamentals A-22


Practice 3: Solutions (continued)

b. Run the ShowXmlNS tool and explain the results.

As shown by the sample output results from the ShowXmlNS tool, all elements in the
document are qualified by the default namespace string urn:hr:employees.
<employees xmlns(expanded)="urn:hr:employees">
<employee xmlns(expanded)="urn:hr:employees">
<employee_id xmlns(expanded)="urn:hr:employees">
103
</employee_id xmlns(expanded)="urn:hr:employees">
<first_name xmlns(expanded)="urn:hr:employees">
Alexander
</first_name xmlns(expanded)="urn:hr:employees">
<last_name xmlns(expanded)="urn:hr:employees">
Hunold
</last_name xmlns(expanded)="urn:hr:employees">
<salary xmlns(expanded)="urn:hr:employees">
9000
</salary xmlns(expanded)="urn:hr:employees">
</employee xmlns(expanded)="urn:hr:employees">
<employee xmlns(expanded)="urn:hr:employees">
<employee_id xmlns(expanded)="urn:hr:employees">
104
</employee_id xmlns(expanded)="urn:hr:employees">
<first_name xmlns(expanded)="urn:hr:employees">
n l y
Bruce
</first_name xmlns(expanded)="urn:hr:employees">
<last_name xmlns(expanded)="urn:hr:employees">
e O
Ernst
</last_name xmlns(expanded)="urn:hr:employees">
U s
<salary xmlns(expanded)="urn:hr:employees">
6000
A I
O
</salary xmlns(expanded)="urn:hr:employees">
</employee xmlns(expanded)="urn:hr:employees">
...

l &
<employee xmlns(expanded)="urn:hr:employees">

007
n a
<employee_id xmlns(expanded)="urn:hr:employees">

te r
</employee_id xmlns(expanded)="urn:hr:employees">
<first_name xmlns(expanded)="urn:hr:employees">
James
I n
</first_name xmlns(expanded)="urn:hr:employees">

l e
<last_name xmlns(expanded)="urn:hr:employees">

c
Bond

r a
</last_name xmlns(expanded)="urn:hr:employees">
<salary xmlns(expanded)="urn:hr:employees">

O 5000
</salary xmlns(expanded)="urn:hr:employees">
</employee xmlns(expanded)="urn:hr:employees">
</employees xmlns(expanded)="urn:hr:employees">

Oracle 10g: XML Fundamentals A-23


Practice 3: Solutions (continued)

2. In the XMLLabsProject, edit departments.xml, and add an XML Namespace by


using a namespace prefix.

a. In the <departments> root element, add an XML Namespace by using the dept
prefix, and URL value of http://www.hr.com/departments. Save the changes
and predict the namespace applied to each element in the document. Run ShowXmlNS to
confirm your results.

Changes to departments.xml (shown in bold):


<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
<!ENTITY deptData SYSTEM "newdepts.txt">
]>
<departments xmlns:dept="http://www.hr.com/departments">
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
</department>
<department num="2">
<department_id>20</department_id>
<department_name>Marketing</department_name>
n l y
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
e O
</department>
&deptData;
U s
</departments>

A I
Answer: All elements are not associated with any namespace. This can be seen by the
O
empty string for the expanded namespace value in the output generated by ShowXmlNS:
<departments xmlns(expanded)="">
l &
<department xmlns(expanded)="">

n a
<department_id xmlns(expanded)="">
10

te r
</department_id xmlns(expanded)="">

I n
<department_name xmlns(expanded)="">
Administration

e
</department_name xmlns(expanded)="">

l
<manager_id xmlns(expanded)="">
c
</manager_id xmlns(expanded)="">

r a
<location_id xmlns(expanded)="">

O
1700
</location_id xmlns(expanded)="">
</department xmlns(expanded)="">
...
</departments xmlns(expanded)="">
Oracle 10g: XML Fundamentals A-24
Practice 3: Solutions (continued)

b. Apply the dept namespace prefix to the start and end tags of <department> elements
in the deparments.xml document, and save the changes.
Note: Do not edit the file identified by the &deptData; entity reference.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
<!ENTITY deptData SYSTEM "newdepts.txt">
]>
<departments xmlns:dept="http://www.hr.com/departments">
<dept:department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
</dept:department>
<dept:department num="2">
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</dept:department>
&deptData;
</departments>

n l y
c. Run the ShowXmlNS tool and explain the results.
<departments xmlns(expanded)="">
e O
<department_id xmlns(expanded)="">
U s
<dept:department xmlns(expanded)="http://www.hr.com/departments">

10
</department_id xmlns(expanded)="">
A I
O
<department_name xmlns(expanded)="">
Administration

<manager_id xmlns(expanded)="">
l &
</department_name xmlns(expanded)="">

n a
</manager_id xmlns(expanded)="">
<location_id xmlns(expanded)="">
1700

te r
</location_id xmlns(expanded)="">

I n
</dept:department xmlns(expanded)="http://www.hr.com/departments">
<dept:department xmlns(expanded)="http://www.hr.com/departments">

l e
<department_id xmlns(expanded)="">
20
c
r a
</department_id xmlns(expanded)="">
<department_name xmlns(expanded)="">

O Marketing
</department_name xmlns(expanded)="">
...

Oracle 10g: XML Fundamentals A-25


Practice 3: Solutions (continued)
<manager_id xmlns(expanded)="">
201
</manager_id xmlns(expanded)=""><location_id xmlns(expanded)="">
Toronto
</location_id xmlns(expanded)="">
</dept:department xmlns(expanded)="http://www.hr.com/departments">
<department xmlns(expanded)="">
<department_id xmlns(expanded)="">
30
</department_id xmlns(expanded)="">
<department_name xmlns(expanded)="">
Purchasing
</department_name xmlns(expanded)="">
<manager_id xmlns(expanded)="">
114
</manager_id xmlns(expanded)="">
<location_id xmlns(expanded)="">
1700
</location_id xmlns(expanded)="">
</department xmlns(expanded)="">
<department xmlns(expanded)="">
<department_id xmlns(expanded)="">
40
</department_id xmlns(expanded)="">
<department_name xmlns(expanded)="">
Human Resources
n l y
</department_name xmlns(expanded)="">
<location_id xmlns(expanded)="">
e O
s
2400
</location_id xmlns(expanded)="">
</department xmlns(expanded)="">
</departments xmlns(expanded)="">
I U
O A
As seen by the output from ShowXmlNS, only the <department> elements (including the

l &
start and end tags) are qualified by the namespace prefix dept representing the
http://www.hr.com/departments expanded XML namespace string.

n a
te r
Note: The child elements of the <department> elements, and all elements included by the
&deptData; entity reference are not qualified with the dept namespace prefix. All other

defined.
I n
elements are not qualified with a namespace because a default namespace has not been

c l e
Note: The <department> elements included (after department 20 elements) from the

r a
usage of the &deptData; entity reference are not qualified by the namespace either,
because the prefix has not been applied to the respective elements in the newdepts.txt
O
external file.

Oracle 10g: XML Fundamentals A-26


Practice 3: Solutions (continued)

3. Add the file E:\labs\lab\hr.xml to your XMLLabsProject and examine the effects
of using XML Namespaces without using the ShowXmlNS tool until you have answered the
following questions: What is the expanded namespace for the following:

a. The <hr> element?

Answer: urn:hr:default

b. The <department> element?

Answer: urn:hr:department

c. The <department_name> element?

Answer: urn:hr:department

d. The <location_id> element?

Answer: No namespace.

e. The <employees> element?

Answer: urn:hr:employees
n l y
O
f. The <employee> element containing the data for Alexander Hunold?

e
Answer: urn:hr:employee-default

U s
Answer: urn:hr:employee-default A I
g. The <last_name> element containing the text Hunold?

O
&
h. The <employee> element containing the data for Bruce Ernst?

l
Answer: No namespace.

n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-27
Practice 3: Solutions (continued)

Run the ShowXmlNS tool to verify your answers.

The answers are highlighted in bold, in the ShowXmlNS output, with added XML comments
containing the corresponding question letter:
<hr xmlns(expanded)="urn:hr:default"> <!-- (a) -->
<dept:department xmlns(expanded)="urn:hr:department"> <!-- (b) -->
<dept:department_id xmlns(expanded)="urn:hr:department">
60
</dept:department_id xmlns(expanded)="urn:hr:department">
<!--(c)--><dept:department_name xmlns(expanded)="urn:hr:department">
IT
</dept:department_name xmlns(expanded)="urn:hr:department">
<dept:manager_id xmlns(expanded)="urn:hr:department">
103
</dept:manager_id xmlns(expanded)="urn:hr:department">
<location_id xmlns(expanded)=""> <!--(d) -->
1400
</location_id xmlns(expanded)="">
<emp:employees xmlns(expanded)="urn:hr:employees"> <!-- (e) -->
<!--(f)--> <employee xmlns(expanded)="urn:hr:employee-default">
<employee_id xmlns(expanded)="urn:hr:employee-default">
103
</employee_id xmlns(expanded)="urn:hr:employee-default">
<first_name xmlns(expanded)="urn:hr:employee-default">
Alexander
n l y
<!--(g)-->
e O
</first_name xmlns(expanded)="urn:hr:employee-default">
<last_name xmlns(expanded)="urn:hr:employee-default">
Hunold
s
</last_name xmlns(expanded)="urn:hr:employee-default">
U
I
<salary xmlns(expanded)="urn:hr:employee-default">
9000

OA
</salary xmlns(expanded)="urn:hr:employee-default">
</employee xmlns(expanded)="urn:hr:employee-default">
<!--(h)--> <employee xmlns(expanded)="">

104
l &
<employee_id xmlns(expanded)="">

n a
</employee_id xmlns(expanded)="">

Bruce
te r
<first_name xmlns(expanded)="">

</first_name xmlns(expanded)="">

I n
<last_name xmlns(expanded)="">

e
Ernst

c l </last_name xmlns(expanded)="">
<salary xmlns(expanded)="">

r a 6000
</salary xmlns(expanded)="">

O </employee xmlns(expanded)="">
</emp:employees xmlns(expanded)="urn:hr:employees">
</dept:department xmlns(expanded)="urn:hr:department">
</hr xmlns(expanded)="urn:hr:default">
Oracle 10g: XML Fundamentals A-28
Practice 4: Solutions
The goal of the practice is to create an XML Schema for the regions.xml and
employees.xml documents. You manually type XML Schema components for the
regions.xml file, and use the visual XML Schema Editor in JDeveloper to build the XML
Schema document for employees.xml. The XML Schema documents created are used to
validate their respective XML documents, by first using the oraxml command-line utility via
the Tools > OraXML-schema menu option, and then by registering the XML Schema in
JDeveloper and using the built-in XML Schema-aware features of JDeveloper.

1. Create an XML Schema for the regions.xml document.

a. Add the E:\labs\lab\regions.xsd file to your project. You will notice that it
contains a skeleton XML Schema with the <schema> element.
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema>
</xsd:schema>

b. Edit regions.xsd to add the W3C XML Schema namespace to the <schema> start
tag of the root element, using namespace prefix xsd. Ensure that the <schema>
element start and end tags are qualified with the namespace prefix.
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
</xsd:schema>
n l y
add the type attribute.
e O
c. Inside the <xsd:schema> element, create a child element named regions, but do not

<?xml version="1.0" encoding="windows-1252"?>


U s
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="regions">
</xsd:element>
A I
</xsd:schema>
O
l &
d. Inside the regions element declaration, create an anonymous <complexType>

a
containing a <sequence> that encloses one element named region with its
n
te r
maxOccurs attribute set to unbounded.
<?xml version="1.0" encoding="windows-1252"?>

I n
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="regions">

l e
<xsd:complexType>

c
<xsd:sequence>

r a
<xsd:element name="region" maxOccurs="unbounded">
</xsd:element>

O </xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

Oracle 10g: XML Fundamentals A-29


Practice 4: Solutions (continued)

e. Inside the region element declaration, add an anonymous <complexType> also


containing a <sequence> with two child element declarations: the first one named
region_id with the type attribute value of xsd:int, and the second named
region_name with the type attribute value of xsd:string.
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="regions">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="region" maxOccurs="unbounded">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="region_id" type="xsd:int"/>
<xsd:element name="region_name" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>

f. Check the syntax and validate the XML Schema document. Save the XML Schema
document when there are no syntax or validation errors.
n l y
Checking XML Syntax...
e O
s
Checking E:\labs\lab\regions.xsd
Check XML Syntax: 0 errors, 0 warnings.

Validating XML...
I U
Validating E:\labs\lab\regions.xsd
Validate XML: 0 errors, 0 warnings.
O A
&
Note: The Validate XML command that is active for an XML Schema document validates
l
a
the XML Schema document against an XML Schema based on W3C XML Schema

n
Recommendation that is already registered with JDeveloper.

r
I n te
c l e
r a
O
Oracle 10g: XML Fundamentals A-30
Practice 4: Solutions (continued)

g. Click the Design tab to view the XML Schema in the visual display mode, and expand
the nodes to show the complete XML Schema structure.

The XML Schema for regions.xsd should appear as shown in the following graphic:

2. Associate the regions.xml document with the regions.xsd XML Schema and
validate the XML instance document.

a. Edit regions.xml, and add the XML Namespace prefix xsi set to the URL value
http://www.w3.org/2001/XMLSchema-instance. Set the
n l y
e O
xsi:noNamespaceSchemaLocation attribute to the region.xsd XML Schema.
Check the XML syntax, correct errors (if any), and save the changes to the XML
document.
<?xml version='1.0' encoding='windows-1252'?>
U s
xsi:noNamespaceSchemaLocation="regions.xsd">
A I
<regions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

<region id="1">
<name>Europe</name> O
</region>
<region id="2">
l &
<name>Americas</name>

n a
</region>
<region id="3">
<name>Asia</name>
te r
</region>
<region id="4"> I n
l e
<name>Middle East and Africa</name>
</region>
c
a
</regions>
r
O
Oracle 10g: XML Fundamentals A-31
Practice 4: Solutions (continued)

b. Validate the region.xml XML document using the oraxml utility configured as the
Tools > OraXML-schema menu option. Explain the validation errors in the XML
document.
file:/E:/labs/lab/regions.xml<Line 4, Column 18>: XSD-2035: (Error)
Attribute 'id' not expected.

file:/E:/labs/lab/regions.xml<Line 5, Column 11>: XSD-2034: (Error)


Element 'name' not expected.

file:/E:/labs/lab/regions.xml<Line 6, Column 12>: XSD-2021: (Error)


Element not completed: 'region'

file:/E:/labs/lab/regions.xml<Line 7, Column 18>: XSD-2035: (Error)


Attribute 'id' not expected.

file:/E:/labs/lab/regions.xml<Line 8, Column 11>: XSD-2034: (Error)


Element 'name' not expected.

file:/E:/labs/lab/regions.xml<Line 9, Column 12>: XSD-2021: (Error)


Element not completed: 'region'

file:/E:/labs/lab/regions.xml<Line 10, Column 18>: XSD-2035: (Error)


Attribute 'id' not expected.

n l y
O
file:/E:/labs/lab/regions.xml<Line 11, Column 11>: XSD-2034: (Error)
Element 'name' not expected.

s
file:/E:/labs/lab/regions.xml<Line 12, Column 12>: XSD-2021: (Error) e
Element not completed: 'region'

I U
Attribute 'id' not expected.
O A
file:/E:/labs/lab/regions.xml<Line 13, Column 18>: XSD-2035: (Error)

Element 'name' not expected.


l &
file:/E:/labs/lab/regions.xml<Line 14, Column 11>: XSD-2034: (Error)

n a
file:/E:/labs/lab/regions.xml<Line 15, Column 12>: XSD-2021: (Error)
r
Element not completed: 'region'

te
I n
Error while parsing input file: regions.xml(Attribute 'id' not expected.)

l e
The validation errors indicate that the id attribute is not expected. The region.xsd

c
XML Schema document does not declare an attribute for the <region> element, but

r a
specifies a <region_id> element. The <name> child element inside the<region>

O element is also invalid, and must be specified as <region_name>. Alternatively, the


XML Schema can be altered to match the XML instance document. In this case, you
change the XML instance document to conform with the XML Schema definition.

Oracle 10g: XML Fundamentals A-32


Practice 4: Solutions (continued)

c. Correct all the validation errors, ensure that the XML syntax is correct, save the changes,
and validate the XML document again by using the Tools > OraXML-schema.
<?xml version='1.0' encoding='windows-1252'?>
<regions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="regions.xsd">
<region>
<region_id>1</region_id>
<region_name>Europe</region_name>
</region>
<region>
<region_id>2</region_id>
<region_name>Americas</region_name>
</region>
<region>
<region_id>3</region_id>
<region_name>Asia</region_name>
</region>
<region>
<region_id>4</region_id>
<region_name>Middle East and Africa</region_name>
</region>
</regions>

The changes, in bold, in the region.xml document above are valid with respect to the
n
XML Schema document. When valid, the oraxml utility displays the following message:
l y
The input XML file is parsed without errors using Schema validation mode
e O
U s
3. Using the JDeveloper XML Schema Editor, create an XML Schema for the
employees.xml document.

A I
a. Create a new XML Schema by right-clicking the XMLLabsProject node and
O
clicking New…. Expand the General category and select the XML node, and double-

Name: employees.xsd, and click Ok.


l &
click the XML Schema item. In the Create XML Schema file dialog box, enter the File

a
Note: JDeveloper creates the XML Schema and displays it in the XML Editor Design tab
n
r
window, with a default <schema> and <exampleElement> element.

te
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-33
Practice 4: Solutions (continued)
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.example.org"
targetNamespace="http://www.example.org"
elementFormDefault="qualified" >

<xsd:element name="exampleElement">
<xsd:annotation>
<xsd:documentation>A sample element</xsd:documentation>
</xsd:annotation>
</xsd:element>

</xsd:schema>

b. In the <schema> element, replace the URL in the targetNamespace attribute with
the value http://www.hr.com/employees. Be sure to press the [ENTER] key.
Click exampleElement and change its name to employees. Save the changes.
Note: Remember to press the [ENTER] key after entering text values.

n l y
e O
<?xml version="1.0" encoding="windows-1252"?>
U s
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.hr.com/employees"
A
targetNamespace="http://www.hr.com/employees"
I
elementFormDefault="qualified" >
O
<xsd:element name="employees">
l &
<xsd:annotation>

n a
<xsd:documentation>A sample element</xsd:documentation>
</xsd:annotation>
</xsd:element>
te r
</xsd:schema>
I n
c l e
Note: If you view the XML Schema elements created in Source tab, you will see that the

r a
default namespace and the targetNamespace attributes are both modified to reflect
the value entered for the targetNamespace.

O
Oracle 10g: XML Fundamentals A-34
Practice 4: Solutions (continued)

c. In the Structure pane, expand the employees node, its annotation child node, and the
documentation node. Click the A – A Sample Element node, and in the Property
Inspector pane (on the right) click and replace the Data property field value containing
“A Sample Element”, with “The employees element.”
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.hr.com/employees"
targetNamespace="http://www.hr.com/employees"
elementFormDefault="qualified" >

<xsd:element name="employees">
<xsd:annotation>
<xsd:documentation>The employees element</xsd:documentation>
</xsd:annotation>
</xsd:element>

</xsd:schema>

Note: The change to the annotation is not visible in the Design tab window.

d. Create the employee child element in a sequence by right-clicking the employees


element and selecting Insert inside element – employees > sequence. Then, right-click
the sequence object and select Insert inside sequence – element. Change the new
element name to employee.
n l y
e O
U s
A I
O
l &
n a
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"

te r
xmlns="http://www.hr.com/employees"
targetNamespace="http://www.hr.com/employees"

I n
elementFormDefault="qualified" >

c l e
r a
O
Oracle 10g: XML Fundamentals A-35
Practice 4: Solutions (continued)
<xsd:element name="employees">
<xsd:annotation>
<xsd:documentation>The employees element</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
<xsd:element name="employee"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

</xsd:schema>

e. Select the employee element and change the maxOccurs property in the Property
Inspector pane to the unbounded value.

n l y
<?xml version="1.0" encoding="windows-1252"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
e O
s
xmlns="http://www.hr.com/employees"
targetNamespace="http://www.hr.com/employees"
elementFormDefault="qualified" >

I U
<xsd:element name="employees">
<xsd:annotation>
O A
<xsd:documentation>The employees element</xsd:documentation>
</xsd:annotation>
<xsd:complexType>
l &
<xsd:sequence>

n a
</xsd:sequence>
</xsd:complexType>
te r
<xsd:element name="employee" maxOccurs="unbounded"/>

</xsd:element>
I n
</xsd:schema>

c l e
r a
Note: The XML Schema Editor automatically adds a required <complexType> parent

O element enclosing the <sequence> and its children.

Oracle 10g: XML Fundamentals A-36


Practice 4: Solutions (continued)

f. Create four child elements of the employee by using the Component Palette, locate, and
drag the sequence object and drop it on the employee element in the Design tab. Drag
four element objects and drop them on the employee sequence object. Change the
name of the four elements starting from the top to employee_id, first_name,
last_name, and salary, respectively. Save the changes to the XML Schema
document.

<?xml version="1.0" encoding="windows-1252"?>


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.hr.com/employees"
n l y
targetNamespace="http://www.hr.com/employees"
elementFormDefault="qualified" >
e O
<xsd:element name="employees">
<xsd:annotation>
U s
</xsd:annotation>
A I
<xsd:documentation>The employees element</xsd:documentation>

<xsd:complexType>
<xsd:sequence> O
<xsd:complexType>
l &
<xsd:element name="employee" maxOccurs="unbounded">

<xsd:sequence>
n a
te r
<xsd:element name="employee_id"/>
<xsd:element name="first_name"/>
<xsd:element name="last_name"/>

I n
<xsd:element name="salary"/>
</xsd:sequence>

l e
</xsd:complexType>

c
</xsd:element>

r a
</xsd:sequence>
</xsd:complexType>

O
</xsd:element>

</xsd:schema>

Oracle 10g: XML Fundamentals A-37


Practice 4: Solutions (continued)

g. Right-click the employee_id element and click Set Type and enter the xsd:int
value in the blank field (this can also be done using the type property in the Property
Inspector). Set the type of the first_name and last_name elements to
xsd:string, and the salary element to xsd:decimal. Save the changes to the
XML Schema document.

<?xml version="1.0" encoding="windows-1252"?>


n l y
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns="http://www.hr.com/employees"
e O
targetNamespace="http://www.hr.com/employees"
elementFormDefault="qualified" >
U s
<xsd:element name="employees">
<xsd:annotation>
A I
O
<xsd:documentation>The employees element</xsd:documentation>

&
</xsd:annotation>
<xsd:complexType>
<xsd:sequence>
a l
<xsd:complexType>
r n
<xsd:element name="employee" maxOccurs="unbounded">

<xsd:sequence>

n te
<xsd:element name="employee_id" type="xsd:int"/>

I
<xsd:element name="first_name" type="xsd:string"/>

c l e
<xsd:element name="last_name" type="xsd:string"/>
<xsd:element name="salary" type="xsd:decimal"/>

a
</xsd:sequence>

O r </xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
Oracle 10g: XML Fundamentals A-38
Practice 4: Solutions (continued)

4. Register the employees.xsd XML Schema in JDeveloper, and then associate and validate
the employees.xml document with its XML Schema by using JDeveloper’s built-in
Validate XML menu option.

a. To register the employees.xsd with JDeveloper, select Tools > Preferences and
click the XML Schemas node.

b. Click Add… to display the Add Schema window. In the Add a Schema from the file
system or a URL: field, enter the URL file:/E:/labs/lab/employees.xsd,
or browse for the file E:\labs\lab\employees.xsd. In the Extension field, enter
the value xml. Click Ok to close the Add Schema window, and confirm that the XSD
file has been added to the User Schemas for XML Editing list. Click Ok to close the
Preferences window.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
c. Right-click employees.xml. Can the Validate XML menu be invoked? Why not?

r a
The Validate XML menu option cannot be invoked; it is disabled. This is because the
O
XML instance document (employees.xml) has not been associated to the registered
XML Schema document.

Oracle 10g: XML Fundamentals A-39


Practice 4: Solutions (continued)

d. Edit employees.xml in the XML Editor and associate it with the employees.xsd
schema document by using the xsi:schemaLocation attribute in the root element.
Modify the default namespace to have the same value as the targetNamespace
defined in the XML Schema document. Ensure that the employees.xml document has
a well-formed syntax, and save the changes.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hr.com/employees employees.xsd"
xmlns="http://www.hr.com/employees">
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
<employee>
<employee_id>104</employee_id>
<first_name>Bruce</first_name>
<last_name>Ernst</last_name>
<salary>6000</salary>
</employee>
<employee>
<employee_id>105</employee_id> n l y
<first_name>David</first_name>
<last_name>Austin</last_name>
e O
<salary>4800</salary>
</employee>
U s
<employee>
<employee_id>106</employee_id>
<first_name>Valli</first_name> A I
<last_name>Pataballa</last_name>
O
<salary>4800</salary>
</employee>
l &
<employee>

n
<employee_id>107</employee_id> a
te r
<first_name>Diana</first_name>
<last_name>Lorentz</last_name>

</employee>
I n
<salary>4200</salary>

<employee>

c l e
<employee_id>007</employee_id>

r a
<first_name>James</first_name>
<last_name>Bond</last_name>

O<salary>5000</salary>
</employee>
</employees>

Oracle 10g: XML Fundamentals A-40


Practice 4: Solutions (continued)

e. Right-click employees.xml, and click Validate XML. What is the result?


Validating XML...
Validating E:\labs\lab\employees.xml
Validate XML: 0 errors, 0 warnings.

f. Confirm the validation results by clicking the Tools > OraXML-schema command-line
tool for XML Schema validation.
The input XML file is parsed without errors using Schema validation mode

If you have time…

5. Create a new employee using the XML-Schema-aware Editor.

a. Edit employees.xml and add a blank line after the last <employee> element, just
before the </employees> end tag. Type the less than (<) character and wait for code-
insight to display the available choices.

n l y
e O
U s
A I
O
l &
n a
r
The choices are: employee and xsi:

te
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-41
Practice 4: Solutions (continued)

b. Double-click the employee option, and enter the greater-than (>) character to close the
start tag of the element. If you have enabled the Tools > Preferences > Code Editor >
XML > End Tag Completion option, the </employee> end tag is automatically
added, otherwise enter the </employee> end tag.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hr.com/employees employees.xsd"
xmlns="http://www.hr.com/employees">
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
...
<employee>
<employee_id>007</employee_id>
<first_name>James</first_name>
<last_name>Bond</last_name>
<salary>5000</salary>
</employee>
<employee></employee>
</employees>
n l y
e O
c. Create a blank line between the new <employee> element start and end tags. In the

U s
blank line enter the less than (<) character and wait for code-insight to display your
choices. What choices are available? employee_id, first_name, last_name, salary and
xsi:
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-42
Practice 4: Solutions (continued)

d. From the code-insight choices offered, select employee_id and enclose the value 108
between the start and end tags. Repeat the process and insert the <first_name>
element containing the text Nancy, the <last_name> element with text Greenberg,
and <salary> element with 12000.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.hr.com/employees employees.xsd"
xmlns="http://www.hr.com/employees">
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
...
<employee>
<employee_id>007</employee_id>
<first_name>James</first_name>
<last_name>Bond</last_name>
<salary>5000</salary>
</employee>
<employee>
<employee_id>108</employee_id> n l y
<first_name>Nancy</first_name>
<last_name>Greenberg</last_name>
e O
<salary>12000</salary>
</employee>
U s
</employees>

A I
e. Right-click in the XML Editor and select Auto Indent XML to tidy the document
O
contents. Right-click and select Validate XML, and save the changes to the document.
Validating XML...
l &
n
Validate XML: 0 errors, 0 warnings. a
Validating E:\labs\lab\employees.xml

te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-43
Practice 5: Solutions
The goal of this practice is to gain experience in writing XPath expressions by testing them using
the TestXPath tool, and writing the XPath expressions in the XSLT stylesheet
<xsl:template> elements for outputting employee data in HTML format.

1. In the XMLLabsProject, edit the root element in employees.xml to remove all the
xmlns, and xsi:schemaLocation attributes. Save the changes to your document.
<?xml version='1.0' encoding='windows-1252'?>
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees>
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
...
<employee>
<employee_id>108</employee_id>
<first_name>Nancy</first_name>
<last_name>Greenberg</last_name>
<salary>12000</salary>
</employee>
</employees>
n l y
for the following scenarios:
e O
2. Right-click employees.xml and select TestXPath path tool to write XPath expressions

s
Note: There may be more than one way to write the XPath expression.

U
a. Locate all employee elements.

A I
/employees/employee
employees/employee O
//employee
l &
<employee>

n a
r
<employee_id>103</employee_id>

te
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>

I n
<salary>9000</salary>
</employee>
<employee>

c l e
<employee_id>104</employee_id>

r a
<first_name>Bruce</first_name>
<last_name>Ernst</last_name>

O
<salary>6000</salary>
</employee>

Oracle 10g: XML Fundamentals A-44


Practice 5: Solutions (continued)
<employee>
<employee_id>105</employee_id>
<first_name>David</first_name>
<last_name>Austin</last_name>
<salary>4800</salary>
</employee>
<employee>
<employee_id>106</employee_id>
<first_name>Valli</first_name>
<last_name>Pataballa</last_name>
<salary>4800</salary>
</employee>
<employee>
<employee_id>107</employee_id>
<first_name>Diana</first_name>
<last_name>Lorentz</last_name>
<salary>4200</salary>
</employee>
<employee>
<employee_id>007</employee_id>
<first_name>James</first_name>
<last_name>Bond</last_name>
<salary>5000</salary>
</employee>
<employee>
<employee_id>108</employee_id>
n l y
<first_name>Nancy</first_name>
<last_name>Greenberg</last_name>
e O
s
<salary>12000</salary>
</employee>

b. Locate the first employee element.


I U
/employees/employee[1]
employees/employee[1] O A
//employee[1]
l &
<employee>
n a
te r
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>

I n
<salary>9000</salary>
</employee>

c l e
c. Locate the last employee element.

r a
/employees/employee[last()]
O
employees/employee[last()]
//employee[last()]

Oracle 10g: XML Fundamentals A-45


Practice 5: Solutions (continued)
<employee>
<employee_id>108</employee_id>
<first_name>Nancy</first_name>
<last_name>Greenberg</last_name>
<salary>12000</salary>
</employee>

d. Locate the last two employee elements.

/employees/employee[position()>=last()-1]
employees/employee[position()>=last()-1]
//employee[position()>=last()-1]
<employee>
<employee_id>007</employee_id>
<first_name>James</first_name>
<last_name>Bond</last_name>
<salary>5000</salary>
</employee>
<employee>
<employee_id>108</employee_id>
<first_name>Nancy</first_name>
<last_name>Greenberg</last_name>
<salary>12000</salary>
</employee>

n l y
e. Find all the last name elements.

//last_name
e O
/employees/employee/last_name
employees/employee/last_name
U s
<last_name>Hunold</last_name>
A I
O
<last_name>Ernst</last_name>
<last_name>Austin</last_name>
<last_name>Pataballa</last_name>
<last_name>Lorentz</last_name>
l &
<last_name>Bond</last_name>
<last_name>Greenberg</last_name>
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-46
Practice 5: Solutions (continued)

f. Obtain the text for the first name of employees

//first_name/text()
employees/employee/first_name/text()
/employees/employee/first_name/text()
Alexander
Bruce
David
Valli
Diana
James
Nancy

g. Locate employee elements containing a salary less than 5000.

//employee[number(salary)<5000]
employees/employee[number(salary)<5000]
/employees/employee[number(salary)<5000]
//employee[salary<5000]
<employee>
<employee_id>105</employee_id>
<first_name>David</first_name>
<last_name>Austin</last_name>
n l y
<salary>4800</salary>
</employee>
<employee>
e O
<employee_id>106</employee_id>
<first_name>Valli</first_name>
U s
<last_name>Pataballa</last_name>
<salary>4800</salary>
A I
O
</employee>
<employee>
<employee_id>107</employee_id>
<first_name>Diana</first_name>
l &
<last_name>Lorentz</last_name>
<salary>4200</salary>
n a
</employee>

te r
I n
h. List the salary elements greater than 5000.

l e
//employee/salary[number(.)>5000]

c
//employee/salary[.>5000]

r a
//employee/salary[number(text())>5000]
//employee/salary[text()>5000]
O
<salary>9000</salary>
<salary>6000</salary>
<salary>12000</salary>

Oracle 10g: XML Fundamentals A-47


Practice 5: Solutions (continued)

i. Find employees with a first name beginning with the letter A, or their last name beginning
with the letter P.

//employee[starts-with(first_name,'A') or starts-
with(last_name,'P')]
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
<employee>
<employee_id>106</employee_id>
<first_name>Valli</first_name>
<last_name>Pataballa</last_name>
<salary>4800</salary>
</employee>

j. Obtain the comment for the root element.

employees/comment()
/employees/comment()
<!-- Company Employee Data -->
n l y
e O
<xsl:template> element in an XSL stylesheet document.
U s
3. Test the effects of changing an XPath expression in the match attribute of an

A I
a. In the XMLLabsProject, add the E:\labs\lab\regions.xsl file. The first
<xsl:template> element matching the root element /regions outputs HTML tags
O
for a document with a HTML table. The <xsl:apply-templates/> element

l &
invokes the second <xsl:template> matching region elements and outputs their
<region_id> and <region_name> element values in a table row.

n a
<xsl:stylesheet version="1.0"

te r
<?xml version='1.0' encoding='windows-1252'?>

xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

I n
<!-- Root template -->
<xsl:template match="/regions">
<html>
<body>
c l e
r a
<h1>Regions</h1>
<table border="1"><xsl:apply-templates/></table>

O </body>
</html>
</xsl:template>

Oracle 10g: XML Fundamentals A-48


Practice 5: Solutions (continued)
<!-- Second template: Change this template's match attribute value -->
<xsl:template match="region">
<tr>
<td><xsl:value-of select="region_id"/></td>
<td><xsl:value-of select="region_name"/></td>
</tr>
</xsl:template>

<xsl:template match="*/text()"/>
</xsl:stylesheet>

b. Right-click the regions.xml document, and select Internet Explorer to display the
XML document in the browser, showing the document tree.
Note: Do not close the browser window because it is refreshed in the subsequent steps.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-49
Practice 5: Solutions (continued)

c. Edit regions.xml to include a declaration to use the regions.xsl stylesheet by


inserting the following line just above the start tag of the <regions> root element:
<?xml-stylesheet type="text/xsl" href="regions.xsl"?>
Save the changes to regions.xml.
<?xml version='1.0' encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="regions.xsl"?>
<regions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="regions.xsd">
<region>
<region_id>1</region_id>
<region_name>Europe</region_name>
</region>
<region>
<region_id>2</region_id>
<region_name>Americas</region_name>
</region>
<region>
<region_id>3</region_id>
<region_name>Asia</region_name>
</region>
<region>
<region_id>4</region_id>
<region_name>Middle East and Africa</region_name>
</region>
</regions>
n l y
e O
d. Refresh the browser window to see the results of the XSL transformation.

U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-50
Practice 5: Solutions (continued)

e. Edit the regions.xsl stylesheet and modify the XPath expression in the match
attribute, of the second <xsl:template> element, to include a predicate to address
the last two region elements. Save the changes to the XSL stylesheet, and refresh the
browser to confirm the results.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:template match="/regions">
<html>
<body>
<h1>Regions</h1>
<table border="1"><xsl:apply-templates/></table>
</body>
</html>
</xsl:template>

<!-- Second template: Change this template's match attribute value -->
<xsl:template match="region[position()>last()-2]">
<tr>
<td><xsl:value-of select="region_id"/></td>
<td><xsl:value-of select="region_name"/></td>
</tr>
</xsl:template>
n l y
<xsl:template match="*/text()"/>
</xsl:stylesheet>
e O
Refreshing the browser should produce the following result:
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-51
Practice 5: Solutions (continued)

f. Modify the XPath expression in the match attribute, of the second <xsl:template>
element, to use a predicate that returns the first two region elements. Save the changes
to the XSL stylesheet, and refresh the browser to confirm the results. Did you encounter
any problems? If so, what happened? Close the browser when you have finished.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:template match="/regions">
<html>
<body>
<h1>Regions</h1>
<table border="1"><xsl:apply-templates/></table>
</body>
</html>
</xsl:template>

<!-- Second template: Change this template's match attribute value -->
<xsl:template match="region[position()&lt;3]">
<tr>
<td><xsl:value-of select="region_id"/></td>
<td><xsl:value-of select="region_name"/></td>
</tr>
</xsl:template>
n l y
<xsl:template match="*/text()"/>
</xsl:stylesheet>
e O
The browser window when refeshed should display:
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-52
Practice 5: Solutions (continued)

A possible problem you can encounter with the XSL stylesheet is that if you used the XPath
expression region[position()<3], then this XPath expression causes an XML syntax
error in the XSL stylesheet document because the less than (<)character signals the start of
an element. In the browser, you see the following error displayed:

n l y
e O
When using TestXPath, this is not an issue; however, because an XSL Stylesheet

U s
document is an XML document, it must be well-formed. In this case, the less than (<)
character is not correct and must be represented using its built-in entity reference, that is, &lt;
I
this results in the following XPath expression: region[position()&lt;3].
A
If you have time:
O
l &
4. Test XPath expressions with the departments.xml document that declares and uses a
namespace prefix called dept for some of its elements.

n a
te r
a. First, edit departments.xml and place the entity reference &deptData; in a
comment and replace it with all the <department> element data in the external file it

I n
references, that is, all the data in E:\labs\lab\newdepts.txt. Save the changes
to department.xml and start TestXPath.

c l e
<?xml version='1.0' encoding='windows-1252'?>

r a
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>

O
<!ENTITY deptData SYSTEM "newdepts.txt">
]>

Oracle 10g: XML Fundamentals A-53


Practice 5: Solutions (continued)
<departments xmlns:dept="http://www.hr.com/departments">
<dept:department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
</dept:department>
<dept:department num="2">
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</dept:department>
<!-- &deptData; -->
<department num="3">
<department_id>30</department_id>
<department_name>Purchasing</department_name>
<manager_id>114</manager_id>
<location_id>1700</location_id>
</department>
<department>
<department_id>40</department_id>
<department_name>Human Resources</department_name>

y
<location_id>2400</location_id>
</department>
</departments>
n l
e
b. Locate department elements in the http://www.hr.com/departments O
U
Hint: Use the namespace prefix to qualify element names. s
namespace. Which department elements do not match the expression and why not?

/departments/dept:department
A I
O
<dept:department num="1" xmlns:dept="http://www.hr.com/departments">
<department_id>10</department_id>

l &
<department_name>Administration</department_name>

a
<manager_id/>

n
<location_id>1700</location_id>
</dept:department>

te r
<dept:department num="2" xmlns:dept="http://www.hr.com/departments">

n
<department_id>20</department_id>

e I
<department_name>Marketing</department_name>
<manager_id>201</manager_id>

c l
<location_id>Toronto</location_id>
</dept:department>

r a
O
Note: The <department> elements containing the <department_name> element with
text Purchasing and Human Resources are not matched by the XPath expression,
because they are not qualified with the dept namespace prefix.

Oracle 10g: XML Fundamentals A-54


Practice 5: Solutions (continued)

c. Locate department elements without a namespace.


/departments/department
<department num="3">
<department_id>30</department_id>
<department_name>Purchasing</department_name>
<manager_id>114</manager_id>
<location_id>1700</location_id>
</department>
<department>
<department_id>40</department_id>
<department_name>Human Resources</department_name>
<location_id>2400</location_id>
</department>

d. Find all department nodes whether qualified by a namespace or not.


Hint: Use the node() function to locate any node, and local-node() function to
compare the node name with the string 'department'.

/departments/node()[local-name()='department']
/departments/node()[local-name(.)='department']
<dept:department num="1" xmlns:dept="http://www.hr.com/departments">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id/>
n l y
<location_id>1700</location_id>
</dept:department>
e O
<department_id>20</department_id>
U s
<dept:department num="2" xmlns:dept="http://www.hr.com/departments">

<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
A I
</dept:department>
<department num="3"> O
<department_id>30</department_id>
l &
<manager_id>114</manager_id>
n a
<department_name>Purchasing</department_name>

</department>
te r
<location_id>1700</location_id>

<department>

I n
<department_id>40</department_id>

e
<department_name>Human Resources</department_name>

l
<location_id>2400</location_id>
c
</department>

r a
O
Note: From the previous three examples, you must now realize that it is not good practice to
apply a namespace prefix to a subset of elements in a document; otherwise, it complicates the
process of locating related elements. Remember that it is good practice to use namespace
prefix to qualify all the elements in your document to avoid ambiguity of element names.
Oracle 10g: XML Fundamentals A-55
Practice 5: Solutions (continued)

e. Locate all department elements without a <manager_id> element.

departments/node()[local-name()='department' and
boolean(not(manager_id))]
//node()[local-name()='department' and not(manager_id)]
//node()[local-name()='department'][not(manager_id)]
<department>
<department_id>40</department_id>
<department_name>Human Resources</department_name>
<location_id>2400</location_id>
</department>

Note: The <department> element with attribute is not included as the <manager_id>
element exists, but as an empty element. To include the department with the empty
<manager_id/> element, add the following condition to the predicate:
manager_id='', where the manager_id element is compared to the empty string. For
example:

//node()[local-name()='department' and (not(manager_id) or


manager_id='']

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-56
Practice 6: Solutions
The goal of this practice is to gain some experience in writing XSLT template rules to transform
an XML document into an HTML page, for display in a browser or on the Web, and another
XML document.

1. Transform the departments.xml document into a Web page.

a. In the XMLLabsProject, create a new XSL stylesheet called departments.xsl.

Select the project node, click File > New…, then expand the General category and click
XML, and then double-click XSL Stylesheet in the items list. Enter the file name
departments.xsl and click Ok.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:template match="/">

</xsl:template>
</xsl:stylesheet>

b. In the skeleton document root template rule of departments.xsl, create a level one
HTML header, by using a <h1> tag, containing the text Departments, followed by
the <xsl:apply-templates/> element.
<?xml version='1.0' encoding='windows-1252'?>
n l y
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
e O
<!-- Root template -->
<xsl:template match="/">
U s
<h1>Departments</h1>
<xsl:apply-templates/>
</xsl:template>
A I
</xsl:stylesheet>
O
l &
c. Create a template rule for the department element to create the following HTML
structure:

n a
<h2>department_name</h2>
<ul>
te r
I n
<li>Id: department_id</li>
<li>Manager: manager_id</li>

l e
<li>Location: location_name</li>
</ul>
c
r a
Note: The manager_id information must only appear if it exists as an element and is

O not empty. The location_name is derived by mapping a location_id value of


1700 to Seattle, and 2400 to London; otherwise, output the value unchanged.
Hint: Use <xsl:if> to test the manager_id, and <xsl:choose> to test the
location_id.
Oracle 10g: XML Fundamentals A-57
Practice 6: Solutions (continued)
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:template match="/">
<h1>Departments</h1>
<xsl:apply-templates/>
</xsl:template>

<xsl:template match="department">
<h2><xsl:value-of select="department_name"/></h2>
<ul>
<li>Id: <xsl:value-of select="department_id"/></li>
<xsl:if test="manager_id and manager_id=''">
<li>Manager: <xsl:value-of select="manager_id"/></li>
</xsl:if>
<li>Location:
<xsl:choose>
<xsl:when test="location_id=1700">Seattle</xsl:when>
<xsl:when test="location_id=2400">London</xsl:when>
<xsl:otherwise>
<xsl:value-of select="location_id"/>
</xsl:otherwise>
</xsl:choose>
</li>
</ul>
</xsl:template> n l y
</xsl:stylesheet>

e O
U s
d. Check the XML syntax and perform a Validate XML operation on the
departments.xsl document. Correct any errors and save the stylesheet.
Checking XML Syntax...
Checking E:\labs\lab\departments.xsl A I
Check XML Syntax: 0 errors, 0 warnings.
O
Validating XML...
Validating E:\labs\lab\departments.xsl
l &
Validate XML: 0 errors, 0 warnings.

n a
te r
e. Open departments.xml in the XML Editor, and remove the dept namespace
prefix and declaration from the root element, and remove the dept prefix from the start

I n
and end tags of the first two <department> elements.

l e
<?xml version='1.0' encoding='windows-1252'?>

c
<!DOCTYPE departments SYSTEM "departments.dtd" [

r a
<!ATTLIST department num CDATA #IMPLIED>
<!ENTITY deptData SYSTEM "newdepts.txt">
]>
O
Oracle 10g: XML Fundamentals A-58
Practice 6: Solutions (continued)
<departments>
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
</department>
<department num="2">
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</department>
<!-- &deptData; -->
<department num="3">
<department_id>30</department_id>
<department_name>Purchasing</department_name>
<manager_id>114</manager_id>
<location_id>1700</location_id>
</department>
<department>
<department_id>40</department_id>
<department_name>Human Resources</department_name>
<location_id>2400</location_id>
</department>
</departments>
n l y
e O
f. In departments.xml, add the xml-stylesheet processing instruction to

<?xml version='1.0' encoding='windows-1252'?>


U s
associate with the departments.xsl stylesheet created. Save the changes.

<!DOCTYPE departments SYSTEM "departments.dtd" [


A I
<?xml-stylesheet type="text/xsl" href="departments.xsl"?>

<!ATTLIST department num CDATA #IMPLIED>


<!ENTITY deptData SYSTEM "newdepts.txt"> O
]>
<departments>
l &
<department num="1">
n a
te r
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>

I n
<location_id>1700</location_id>

e
</department>

c l
<department num="2">
<department_id>20</department_id>

r a
<department_name>Marketing</department_name>
<manager_id>201</manager_id>

O <location_id>Toronto</location_id>
</department>

Oracle 10g: XML Fundamentals A-59


Practice 6: Solutions (continued)
<!-- &deptData; -->
<department num="3">
<department_id>30</department_id>
<department_name>Purchasing</department_name>
<manager_id>114</manager_id>
<location_id>1700</location_id>
</department>
<department>
<department_id>40</department_id>
<department_name>Human Resources</department_name>
<location_id>2400</location_id>
</department>
</departments>

g. Observe the transformation to departments.xml in the browser by using Tools >


Internet Explorer, or by right-clicking the document and selecting Internet Explorer.
Note: Correct any errors until you obtain the desired transformation.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-60
Practice 6: Solutions (continued)

2. Transform the employees.xml document into a Web page by making the requested
modifications to the skeleton employees.xsl stylesheet provided.

a. In the XMLLabsProject, add or open the file called


E:\labs\lab\employees.xsl.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Document Root template -->
<xsl:template match="/">
<html>
<head><title>Employee Information</title></head>
<body>
<h1>Employee Information</h1>
<!-- Add instructions here -->
</body>
</html>
</xsl:template>

<xsl:template match="employees">
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<!-- Add instructions here -->
</table>
</xsl:template> n l y
<xsl:template match="employee">
e O
<tr>
<td><xsl:value-of select="employee_id"/></td>
U s
<td><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></td>
<td><xsl:value-of select="salary"/></td>
A I
</tr>
O
&
</xsl:template>

<xsl:template match="salary">
a l
<xsl:choose>

r n
<xsl:when test="number(.)&lt;5000">

</xsl:when>

I
<xsl:otherwise>
te
<b><xsl:value-of select="."/></b>

n
c l e
<i><xsl:value-of select="."/></i>
</xsl:otherwise>

a
</xsl:choose>

O r
</xsl:template>
</xsl:stylesheet>

Oracle 10g: XML Fundamentals A-61


Practice 6: Solutions (continued)

b. Attach the employees.xsl stylesheet to the employees.xml document, and


display employees.xml in the browser. What do you observe?
<?xml version='1.0' encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="employees.xsl"?>
<!DOCTYPE employees SYSTEM "employees.dtd">
<employees>
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
<employee>
<employee_id>104</employee_id>
<first_name>Bruce</first_name>
<last_name>Ernst</last_name>
<salary>6000</salary>
</employee>
<employee>
<employee_id>105</employee_id>
<first_name>David</first_name>
<last_name>Austin</last_name>
<salary>4800</salary>
</employee> n l y
<employee>
<employee_id>106</employee_id>
e O
<first_name>Valli</first_name>
<last_name>Pataballa</last_name>
U s
<salary>4800</salary>
</employee>
<employee> A I
<employee_id>107</employee_id>
O
<first_name>Diana</first_name>
<last_name>Lorentz</last_name>
l &
<salary>4200</salary>
</employee>
n a
<employee>

te r
<employee_id>007</employee_id>

I n
<first_name>James</first_name>
<last_name>Bond</last_name>

l e
<salary>5000</salary>

c
</employee>

r a
<employee>
<employee_id>108</employee_id>

O<first_name>Nancy</first_name>
<last_name>Greenberg</last_name>
<salary>12000</salary>
</employee>
</employees>
Oracle 10g: XML Fundamentals A-62
Practice 6: Solutions (continued)

To display the transformation in the browser, right-click employees.xml and select


Internet Explorer. The browser window should display the following result:

Note: The browser only displays the heading Employee Information without the
employee data. This is because the document root template copies the HTML header to
the output tree. However, because the <xsl:apply-templates> elements are not
used, the document root decendant elements are not processed.
n l y
e O
c. Modify employees.xsl to enable the employee elements to be transformed and its
child data to be displayed in an HTML table format under the Employee
s
Information heading. Save the changes and display the results in the browser. What
U
is the order of employee elements?
<?xml version='1.0' encoding='windows-1252'?>
A I
<xsl:stylesheet version="1.0"
O
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Document Root template -->
<xsl:template match="/">
l &
<html>

n a
<body>

te r
<head><title>Employee Information</title></head>

<h1>Employee Information</h1>

I n
<!-- Add instructions here -->
<xsl:apply-templates/>
</body>

c
</html> l e
r a
</xsl:template>

O
Oracle 10g: XML Fundamentals A-63
Practice 6: Solutions (continued)
<xsl:template match="employees">
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<!-- Add instructions here -->
<xsl:apply-templates/>
</table>
</xsl:template>

<xsl:template match="employee">
<tr>
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></td>
<td><xsl:value-of select="salary"/></td>
</tr>
</xsl:template>

<xsl:template match="salary">
<xsl:choose>
<xsl:when test="number(.)&lt;5000">
<b><xsl:value-of select="."/></b>
</xsl:when>
<xsl:otherwise>
<i><xsl:value-of select="."/></i>
</xsl:otherwise>
</xsl:choose>
n l y
</xsl:template>
</xsl:stylesheet>
e O
The browser display should contain the following results:
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-64
Practice 6: Solutions (continued)

Note: The data from the employee elements is processed and displayed in the order of
their appearance in the input XML document by default.

d. Modify employees.xsl to sort the employee elements by their last name. Save the
changes and display the result in the browser.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Document Root template -->
<xsl:template match="/">
<html>
<head><title>Employee Information</title></head>
<body>
<h1>Employee Information</h1>
<!-- Add instructions here -->
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="employees">
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<!-- Add instructions here -->
<xsl:apply-templates>
n l y
<xsl:sort select="last_name"/>
</xsl:apply-templates>
e O
</table>
</xsl:template>
U s
<xsl:template match="employee">
<tr>
A I
O
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/>,

&
<xsl:value-of select="first_name"/></td>
l
a
<td><xsl:value-of select="salary"/></td>

n
</tr>
</xsl:template>

te r
n
<xsl:template match="salary">
<xsl:choose>

e I
<xsl:when test="number(.)&lt;5000">

c l
<b><xsl:value-of select="."/></b>
</xsl:when>

r a
<xsl:otherwise>
<i><xsl:value-of select="."/></i>
O </xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Oracle 10g: XML Fundamentals A-65
Practice 6: Solutions (continued)

The browser display should show employee table entries sorted by their last name:

n l y
e O
e. Examine the last template rule for the salary elements in the employees.xsl file.
Notice that this template rule formats the style of the salary value in bold or italics

U s
based on a condition. Alter the employee element template rule to apply the salary
template rule, instead of outputting the value of the salary, in the last cell of each row

<?xml version='1.0' encoding='windows-1252'?>


A I
of the HTML table. Save the changes and view the results in the browser.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> O
<!-- Document Root template -->
l &
<xsl:template match="/">
<html>
n a
<body>
te r
<head><title>Employee Information</title></head>

I n
<h1>Employee Information</h1>
<!-- Add instructions here -->

</body>
</html>
c e
<xsl:apply-templates/>

l
r a
</xsl:template>

O
Oracle 10g: XML Fundamentals A-66
Practice 6: Solutions (continued)
<xsl:template match="employees">
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<!-- Add instructions here -->
<xsl:apply-templates>
<xsl:sort select="last_name"/>
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="employee">
<tr>
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></td>
<td><!--xsl:value-of select="salary"/-->
<xsl:apply-templates select="salary"/>
</td>
</tr>
</xsl:template>

<xsl:template match="salary">
<xsl:choose>
<xsl:when test="number(.)&lt;5000">
<b><xsl:value-of select="."/></b>
</xsl:when>
n l y
<xsl:otherwise>
<i><xsl:value-of select="."/></i>
e O
s
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I U
O A
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-67
Practice 6: Solutions (continued)

The browser page displays employee salaries less than 5000 in bold, and salaries greater
or equal to 5000 in italic. For example:

n l y
e O
U s
f. Modify the salary template rule to accept a parameter called minsal, and using the
select attribute, assign it with a default value of 4500. Modify the condition to

<?xml version='1.0' encoding='windows-1252'?>


A I
reference the minsal parameter. Save the change and display the results in the browser.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> O
<!-- Document Root template -->
l &
a
<xsl:template match="/">

n
<html>

<body>
te r
<head><title>Employee Information</title></head>

n
<h1>Employee Information</h1>

e I
<!-- Add instructions here -->
<xsl:apply-templates/>
</body>

c
</html> l
r a
</xsl:template>

O
Oracle 10g: XML Fundamentals A-68
Practice 6: Solutions (continued)
<xsl:template match="employees">
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<!-- Add instructions here -->
<xsl:apply-templates>
<xsl:sort select="last_name"/>
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="employee">
<tr>
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></td>
<td><!--xsl:value-of select="salary"/-->
<xsl:apply-templates select="salary"/>
</td>
</tr>
</xsl:template>

<xsl:template match="salary">
<xsl:param name="minsal" select="4500"/>
<xsl:choose>
<xsl:when test="number(.)&lt;$minsal">
<b><xsl:value-of select="."/></b>
n l y
</xsl:when>
<xsl:otherwise>
e O
s
<i><xsl:value-of select="."/></i>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
I U
</xsl:stylesheet>

OA
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-69
Practice 6: Solutions (continued)

The browser page shows employee salaries less than 4500 in bold, and other salaries in
italic:

n l y
e O
s
g. Modify the employee template rule to pass a parameter value of 8000 to the salary
template rule when it is applied. Use the parameter template body to supply the parameter
U
<?xml version='1.0' encoding='windows-1252'?>
A I
value. Save changes and display the results in the browser.

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> O
<!-- Document Root template -->
<xsl:template match="/">
l &
<html>

n a
<body>

te r
<head><title>Employee Information</title></head>

<h1>Employee Information</h1>

I n
<!-- Add instructions here -->
<xsl:apply-templates/>
</body>
</html>
c l e
a
</xsl:template>
r
O
Oracle 10g: XML Fundamentals A-70
Practice 6: Solutions (continued)
<xsl:template match="employees">
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<!-- Add instructions here -->
<xsl:apply-templates>
<xsl:sort select="last_name"/>
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="employee">
<tr>
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/>,
<xsl:value-of select="first_name"/></td>
<td><!--xsl:value-of select="salary"/-->
<xsl:apply-templates select="salary">
<xsl:with-param name="minsal">8000</xsl:with-param>
</xsl:apply-templates>
</td>
</tr>
</xsl:template>

<xsl:template match="salary">
<xsl:param name="minsal" select="4500"/>
<xsl:choose>
n l y
<xsl:when test="number(.)&lt;$minsal">
<b><xsl:value-of select="."/></b>
e O
s
</xsl:when>
<xsl:otherwise>
<i><xsl:value-of select="."/></i>
</xsl:otherwise>
I U
</xsl:choose>
</xsl:template>
</xsl:stylesheet> OA
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-71
Practice 6: Solutions (continued)

The browser must display salaries less than 8000 in bold, and others in italic:

n l y
If you have time…

e O
s
3. Transform the departments.xml file into an HTML file by using the oraxsl utility.

U
A I
a. Right-click departments.xml and select Oraxsl from the menu. At the prompt, enter
the XSL stylesheet name departments.xsl, a space, and the output file name

O
departments.html.

l &
n a
te r
I n
E:\labs\lab>

c l e
E:\JDeveloper\jdk\bin\java.exe -cp E:\oracle\ora10\lib\xmlparserv2.jar

r a
oracle.xml.parser.v2.oraxsl departments.xml departments.xsl
departments.html

O
Oracle 10g: XML Fundamentals A-72
Practice 6: Solutions (continued)

b. In JDeveloper, open the departments.html file, generated by oraxsl, in your


XMLLabsProject.

JDeveloper opens the file and displays the file in the Design tab with only the
Departments header visible.

c. In the HTML Editor window, click the Preview tab. What do you observe?

The Preview tab displays the HTML file as it is seen in a browser window. For example:

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-73
Practice 6: Solutions (continued)

d. Click the Source tab for the HTML file to view the HTML tags, and notice the first line
of the HTML document. Why is the first line present and is it relevant for this kind of
file?
<?xml version = '1.0' encoding = 'UTF-8'?>
<h1>Departments</h1>
<h2>Administration</h2><ul><li>Id: 10</li><li>Location:
Seattle</li></ul>
<h2>Marketing</h2><ul><li>Id: 20</li><li>Manager: 201</li><li>Location:
Toronto</li></ul>

<h2>Purchasing</h2><ul><li>Id: 30</li><li>Manager:
114</li><li>Location:
Seattle</li></ul>
<h2>Human Resources</h2><ul><li>Id: 40</li><li>Location:
London</li></ul>

The first line of output contains an XML declaration. This is generated by the XSLT
Processor because it assumed that the ouput format was for an XML file. That is, the
output document does not contain <html> as the root element. No, the XML declaration
is not appropriate for a HTML document. However, most browsers ignore the XML
declaration because they know that the file contains HTML tags based on the file
extension.

n l
e. Delete the departments.html file from the disk by selecting the file name in the
y
request.
e O
Navigator and clicking File > Erase from Disk. Click Yes to confirm the deletion

U s
f. Modify the departments.xsl to include the <xsl:output> element by using

A I
html as the value for the method attribute, and add the attribute to suppress the output
of the XML declaration from the results. Save the changes.
<?xml version='1.0' encoding='windows-1252'?>
O
&
<xsl:stylesheet version="1.0"

<!-- Root template -->


a l
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
r n
<xsl:output method="html" omit-xml-declaration="yes"/>

<h1>Departments</h1>

n
<xsl:apply-templates/>
</xsl:template>
I te
l e
<xsl:template match="department">
...
c
r a
</xsl:template>

O
</xsl:stylesheet>

Oracle 10g: XML Fundamentals A-74


Practice 6: Solutions (continued)

g. Run the oraxsl utility on departments.xml, and at the prompt, enter the file name
departments.xsl, a space, and file name departments.html. Open
departments.html in the XMLLabsProject and examine the Source tab to check
that the XML declaration is no longer present.

The XML declaration should not be present anymore. The result is:
<h1>Departments</h1>
<h2>Administration</h2>
<ul>
<li>Id: 10</li>
<li>Location:
Seattle</li>
</ul>
<h2>Marketing</h2>
<ul>
<li>Id: 20</li>
<li>Manager: 201</li>
<li>Location:
Toronto</li>
</ul>

y
<h2>Purchasing</h2>
<ul>
<li>Id: 30</li>
n l
<li>Manager: 114</li>
<li>Location:
e O
s
Seattle</li>
</ul>
<h2>Human Resources</h2>
<ul>
I U
<li>Id: 40</li>
<li>Location:
London</li> O A
</ul>

l &
a
h. Run oraxsl on the employees.xml file, and at the prompt, enter
n
te r
employees.xsl, a space, and employees.html. Open the employees.html
file in JDeveloper. Click the Source tab, right-click in the editor, and select Auto Indent

observation.
I n
to tidy the contents. Does the first line contain the XML declaration? Explain your

<html>
c l e
a
<head>

O r
<META http-equiv="Content-Type" content="text/html"/>
<title>Employee Information</title>
</head>
<body>
<h1>Employee Information</h1>

Oracle 10g: XML Fundamentals A-75


Practice 6: Solutions (continued)
<table border="1">
<tr>
<th>Id</th>
<th>Name</th>
<th>Salary</th>
</tr>
<tr>
<td>105</td>
<td>Austin, David</td>
<td>
<b>4800</b>
</td>
</tr>
<tr>
<td>007</td>
<td>Bond, James</td>
<td>
<b>5000</b>
</td>
</tr>
<tr>
<td>104</td>
<td>Ernst, Bruce</td>
<td>
<b>6000</b>
</td>
n l y
</tr>
<tr>
e O
s
<td>108</td>
<td>Greenberg, Nancy</td>
<td>
<i>12000</i>
I U
</td>
</tr>
<tr> O A
<td>103</td>
<td>Hunold, Alexander</td>
l &
<td>

n a
<i>9000</i>
</td>
</tr>
te r
<tr>
I
<td>107</td>n
l e
<td>Lorentz, Diana</td>

c
<td>

r a <b>4200</b>
</td>

O </tr>

Oracle 10g: XML Fundamentals A-76


Practice 6: Solutions (continued)
<tr>
<td>106</td>
<td>Pataballa, Valli</td>
<td>
<b>4800</b>
</td>
</tr>
</table>
</body>
</html>

The employees.html file should not contain the XML declaration. The Oracle XSLT
processor detected the <html> root element in the output document tree, and set the
default output method to html.

4. Using oraxsl, transform the departments.xml into an XML file called


departments_new.xml, where the children of the <department> element are
converted to attribute names by using their text part as the attribute values.

a. In the XMLLabsProject, create a new XSL stylesheet with the name


departments_new.xsl.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
n l y
<!-- Root template -->
<xsl:template match="/">
e O
</xsl:template>
</xsl:stylesheet>
U s
A I
b. In departments_new.xsl, add to the document root template <xsl:element>
O
element to create a new output element called DEPARTMENTS. Inside the template body

&
of the <xsl:element>, include the <xsl:apply-templates/> element.

l
<xsl:stylesheet version="1.0"
n a
<?xml version='1.0' encoding='windows-1252'?>

<!-- Root template -->


te r
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

I n
<xsl:template match="/">
<xsl:element name="DEPARTMENTS">

c l e
<xsl:apply-templates/>
</xsl:element>

a
</xsl:template>

r
</xsl:stylesheet>

O
Oracle 10g: XML Fundamentals A-77
Practice 6: Solutions (continued)

c. In departments_new.xsl, add another template rule for the department input


element. In the template body, use the <xsl:element> element to create a new output
element with the same name as the context node, that is, department. Save the
changes.
Hint: Use the local-name() XPath function inside braces (using the attribute
template value technique) to determine the current element name.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:template match="/">
<xsl:element name="DEPARTMENTS">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>

<xsl:template match="department">
<xsl:element name="{local-name()}">
</xsl:element>
</xsl:template>
</xsl:stylesheet>

d. Run oraxsl on the departments.xml file, and at the prompt, enter


n
departments_new.xsl, and click Ok. You can view the generated XML document
l y
O
element in the Oraxsl tab Log window in JDeveloper. What do you observe?
e
<?xml version = '1.0' encoding = 'UTF-8'?>

U s
<DEPARTMENTS>
<department/>
<department/>
A I
O
&
<department/>
<department/>
</DEPARTMENTS>
a l
r n
As shown above, you should observe in the Log window for Oraxsl that the resulting

I n te
XML document tree contains a <DEPARTMENTS> root element, and four empty
<department/> child elements.

c l e
r a
O
Oracle 10g: XML Fundamentals A-78
Practice 6: Solutions (continued)

e. Edit departments_new.xsl, modify the <xsl:template> for the department


rule, and use the <xsl:for-each> element inside the <xsl:element> template
body to process all the children of the department node. For each child element,
create an attribute by using the <xsl:attribute> element with the attribute name
set to the name of child node, and attribute value to the child text value. Save the
changes.
Hint: Use the local-name() Xpath function enclosed in braces, to derive the node
name of child elements, and use <xsl:value-of> to obtain the node text as the value.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:template match="/">
<xsl:element name="DEPARTMENTS">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
<xsl:template match="department">
<xsl:element name="{local-name()}">
<xsl:for-each select="*">
<xsl:attribute name="{local-name()}">

y
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
n l
</xsl:element>
</xsl:template>
e O
s
</xsl:stylesheet>

I U
f. Run oraxsl on the departments.xml file, and at the prompt, enter
departments_new.xsl, and click Ok. You can view the generated XML document

<?xml version = '1.0' encoding = 'UTF-8'?>


O A
element in the Oraxsl tab Log window in JDeveloper. What do you observe?

<DEPARTMENTS>

l &
<department department_id="10" department_name="Administration"
manager_id="" location_id="1700"/>

n a
<department department_id="20" department_name="Marketing"

te r
manager_id="201" location_id="Toronto"/>
<department department_id="30" department_name="Purchasing"

I n
manager_id="114" location_id="1700"/>
<department department_id="40" department_name="Human Resources"

l e
location_id="2400"/>

c
</DEPARTMENTS>

r a
As shown above, the transformation yields the desired output with each child element of the
O
<department> node from the input XML document being output as an attribute of the
<department> element in the result document.

Oracle 10g: XML Fundamentals A-79


Practice 6: Solutions (continued)

g. Run oraxsl on the departments.xml file again, and at the prompt, enter
departments_new.xsl, a space, and departments_new.xml, and click Ok.
Open the departments_new.xml output document in your XMLLabsProject, and
check the XML syntax. Is the document well-formed? Optionally, right-click the file in
the XML Editor, and select Auto Indent XML to tidy the contents.
<?xml version = '1.0' encoding = 'UTF-8'?>
<DEPARTMENTS>
<department department_id="10" department_name="Administration"
manager_id="" location_id="1700"/>
<department department_id="20" department_name="Marketing"
manager_id="201" location_id="Toronto"/>
<department department_id="30" department_name="Purchasing"
manager_id="114" location_id="1700"/>
<department department_id="40" department_name="Human Resources"
location_id="2400"/>
</DEPARTMENTS>

On checking the XML Sytnax the result should be:


Checking XML Syntax...
Checking E:\labs\lab\departments_new.xml
Check XML Syntax: 0 errors, 0 warnings.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-80
Practice 7: Solutions
The goal of this practice is to use the Oracle XSQL Pages Publishing Framework to generate
XML data by using the information stored in the Oracle database tables, and transform the
information into HTML for display on the Web.

1. Write an XSQL Page to display the rows in the DEPARMENTS table, and create the named
database connection for the database.

a. Right-click XMLLabsProject, and create an XSQL Page by selecting New…, expand


the General category, and click XML (if required). Double-click XSQL Page in the
Items list.
Note: JDeveloper creates a skeleton XSQL Page in a file called untitled1.xsql.
<?xml version="1.0" encoding='windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
<page xmlns:xsql="urn:oracle-xsql">

</page>

b. Rename the XSQL file to departments.xsql by clicking untitled1.xsql

l
in the Navigator and selecting File > Rename… from the menu. In the Rename dialog
box, enter departments.xsql in the File Name: field, and click Save.
n y
O
c. In the XML Editor, replace the root element start and end tag name page with the text
e
departments, in lowercase.
<?xml version="1.0" encoding='windows-1252'?>
U s
<!--

A I
| Uncomment the following processing instruction and replace

O
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
<departments xmlns:xsql="urn:oracle-xsql">
l &
</departments>
n a
te r
d. In the departments.xsql code, place the cursor inside the <departments>

I n
element and click the Query component in the Component Palette to add an XSQL

l e
query element. In the Query wizard, accept the default settings for property values and
click Next. In the next wizard page, click the New… button to the right of the
c
r a
Connection field to create a database connection.

O
e. Click Next to step past the Welcome page in the Create Database Connection window.
In Step 1 of 4: Type window, enter the hr in Connection Name field, and click Next.

Oracle 10g: XML Fundamentals A-81


Practice 7: Solutions (continued)

f. In Step 2 of 4: Authentication window, enter the database username assigned to you,


such as orax, where x is a number assigned to you, and your password oracle, and
click Next.

g. In Step 3 of 4: Connection window, enter the Host Name:, JDBC Port:, and SID: field
values assigned by your instructor for this course. Then, click Next.

h. In Step 4 of 4: Test window, click Test Connection and make sure that you get the text
Success! displayed in the Status: field, and then click Finish.
Note: In the E:\JDeveloper\jdev\system9.0.5.2.1618 directory, double-
click the file called XSQLConfig.xml to open it in your Web browser. Locate the
<connectiondefs> element and you will find that it contains the <connection
name="hr"> child element with your connection details.

i. You are returned to the Query window. Click Next. In the Enter SQL Statement: field,
type the following SQL statement:
SELECT * FROM departments
Click Finish. Tidy the code created and save the changes to the XSQL Page.
<?xml version = '1.0' encoding = 'windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
n l y
O
-->
<departments xmlns:xsql="urn:oracle-xsql" connection="hr">
<xsql:query max-rows="-1" null-indicator="no"
tag-case="lower">
s e
SELECT *
FROM departments
I U
</xsql:query>
</departments>
O A
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-82
Practice 7: Solutions (continued)

j. Execute the XSQL Page to view the generated XML elements. Right-click
departments.xsql in the XML Editor or Navigator, and select Run. Close the
browser window after you have finished.

n l y
e O
U s
A I
O
l &
n a
te r
I n
l e
2. Create an XSQL Page to retrieve all the employee data, and parameterize the page to display

c
employees in a specified department.

r a
O
Oracle 10g: XML Fundamentals A-83
Practice 7: Solutions (continued)

a. In the XMLLabsProject, create a new XSQL Page and rename it as


employees.xsql.
<?xml version="1.0" encoding='windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
<page xmlns:xsql="urn:oracle-xsql">

</page>

b. Edit employees.xsql, changing the root element start and end tags to be
xsql:query, in lowercase. Inside the <xsql:query> element, enter the following
SQL statement:
SELECT employee_id, first_name, last_name, salary
FROM employees
Save the changes to the XSQL Page.
<?xml version="1.0" encoding='windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
n l y
O
-->
<xsql:query xmlns:xsql="urn:oracle-xsql">
SELECT employee_id, first_name, last_name, salary
FROM employees
s e
</xsql:query>

I U
A
c. Run employees.xsql. Explain the result. Close the browser window.

O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-84
Practice 7: Solutions (continued)

The error indicates that no connection attribute exists in the root element. The query
cannot be executed without connection information.

d. Edit employees.xsql, and add the connection attribute with a value of hr to the
root element. Save the changes and run employees.xsql page. Did it work this time?
Close the browser window.
<?xml version="1.0" encoding='windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
<xsql:query xmlns:xsql="urn:oracle-xsql" connection="hr">
SELECT employee_id, first_name, last_name, salary
FROM employees
</xsql:query>

Yes, the XSQL page runs successfully after the connection attribute is added. All the
rows in the table should appear in <ROW num…> elements.

e. Edit employees.xsql. In the <xsql:query> element, add the appropriate


attributes to change the ROWSET element name to EMPLOYEES, the ROW element name

n l
to EMPLOYEE, and to set the output tags in lowercase. Save the changes and run the
XSQL page to view the results.
y
<?xml version="1.0" encoding='windows-1252'?>
e O
<!--
| Uncomment the following processing instruction and replace
U s
| the stylesheet name to transform output of your XSQL Page using XSLT

-->
A I
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>

O
<xsql:query xmlns:xsql="urn:oracle-xsql" connection="hr"
rowset-element="EMPLOYEES" row-element="EMPLOYEE"
tag-case="lower">
l &
SELECT employee_id, first_name, last_name, salary
FROM employees
n a
</xsql:query>

te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-85
Practice 7: Solutions (continued)

n l y
e O
U s
A I
O
l &
n a
te r
Note: All the element tag names are in lowercase, and the root element is
<employees>, and each row element name is <employee>.

I n
f. Edit employees.xsql. In the <xsql:query> start tag, define an attribute called

c l e
dept_id with a string value of NULL. Modify the SELECT statement to have a WHERE
clause with a condition comparing the department_id column with the value of

r a
dept_id referenced as a lexical parameter. Include an NVL() test in the comparison to
convert a NULL value in dept_id to the department_id, which will cause all the
O employee rows to be displayed when dept_id is the value NULL. Save the changes and
run the employees XSQL page. Which employees are displayed? Do not close the
browser.
Hint: Use the NVL({@dept_id},department_id)in the condition.
Oracle 10g: XML Fundamentals A-86
Practice 7: Solutions (continued)
<?xml version="1.0" encoding='windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
<xsql:query xmlns:xsql="urn:oracle-xsql" connection="hr"
rowset-element="EMPLOYEES" row-element="EMPLOYEE"
tag-case="lower" dept_id="NULL">
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = NVL({@dept_id}, department_id)
</xsql:query>

All employees, except one, are displayed. There is one employee whose
department_id is NULL. Because of the way in which the condition is written, the
query cannot retrieve an employee with the NULL value in the department_id. For
this example, you are not concerned about the missing employee row.

g. In the browser window, append ?dept_id=90 to the URL address, and press [Enter].
Do not close the browser window.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-87
Practice 7: Solutions (continued)

h. Modify the dept_id value in the URL to 140, and press [Enter]. What is the result
returned? Do not close the browser window.

There are no employees in department 140, which does exist. The browser displays an
empty root element:

l y
i. Edit the employees.xsql page, and insert a fallback query to display the following
message enclosed in an <error> element: The department {@dept_id} does n
not exist or does not have any employees. Save the changes, and
refresh the browser to observe the results.
e O
<?xml version="1.0" encoding='windows-1252'?>
U s
<!--

A I
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT

O
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>

&
-->

a l
<xsql:query xmlns:xsql="urn:oracle-xsql" connection="hr"
rowset-element="EMPLOYEES" row-element="EMPLOYEE"

r n
tag-case="lower" dept_id="NULL">
SELECT employee_id, first_name, last_name, salary
FROM employees

n te
WHERE department_id = NVL({@dept_id}, department_id)

I
<xsql:no-rows-query>

c l e
<error>The department {@dept_id} does not exist or does not have any
employees.</error>

a
</xsql:no-rows-query>

r
</xsql:query>

O
Oracle 10g: XML Fundamentals A-88
Practice 7: Solutions (continued)

The fallback query must contain a SELECT statement.

j. Modify the fallback query by using a SELECT statement to print the message. Save the
changes and refresh the browser. Add rowset-element and row-element

n l y
attributes to suppress these elements, and use the lower tag case. After this is
completed, close the browser window.
Hint: In the query, use the DUAL table, and the ERROR column alias.
e O
<?xml version="1.0" encoding='windows-1252'?>
<!--
U s
A I
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
O
l &
<xsql:query xmlns:xsql="urn:oracle-xsql" connection="hr"
rowset-element="EMPLOYEES" row-element="EMPLOYEE"
tag-case="lower" dept_id="NULL">

n a
SELECT employee_id, first_name, last_name, salary
FROM employees

te r
WHERE department_id = NVL({@dept_id}, department_id)

I n
<xsql:no-rows-query rowset-element="" row-element=""
tag-case="lower">

l e
SELECT 'The department {@dept_id} does not exist '||

c'or does not have any employees.' ERROR

r a
FROM dual
</xsql:no-rows-query>

O
</xsql:query>

Oracle 10g: XML Fundamentals A-89


Practice 7: Solutions (continued)

3. Transform the departments XSQL Page into an HTML page.

a. Edit departments.xsql, add the rowset-element attribute to suppress the


<rowset> element, and use row-element to change the tag name of the <row>
elements to department. Delete the comment text surrounding the <?xml-
stylesheet?> processing instruction after the XML declaration page. Keep the
processing instruction and change the file name YourStylesheet.xsl in the href

Save the changes.


n l y
attribute to departments.xsl (the stylesheet you created in a previous practice).

<?xml version = '1.0' encoding = 'windows-1252'?>


<?xml-stylesheet type="text/xsl" href="departments.xsl"?>
<departments xmlns:xsql="urn:oracle-xsql" connection="hr">
e O
<xsql:query max-rows="-1" null-indicator="no"
tag-case="lower" rowset-element=""
U s
SELECT *
row-element="department">

A I
FROM departments
</xsql:query>
O
</departments>

l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-90
Practice 7: Solutions (continued)

b. Run the departments.xsql XSQL page to view the results. Then, close the browser
window.

n l y
e O
U s
If you have time:
A I
O
4. Transform the employees XSQL Page into an HTML page.

l &
n a
a. Edit employees.xsql, and delete the comment text surrounding the
<?xml-stylesheet?> processing instruction, but keep the processing instruction.

te r
Replace the href value of YourStylesheet.xsl with employees.xsl. Save the
changes and run employees.xsql in the browser to view the results. Do not close the
browser window.
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-91
Practice 7: Solutions (continued)
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="employees.xsl"?>
<xsql:query xmlns:xsql="urn:oracle-xsql" connection="hr"
rowset-element="EMPLOYEES" row-element="EMPLOYEE"
tag-case="lower" dept_id="NULL">
SELECT employee_id, first_name, last_name, salary
FROM employees
WHERE department_id = NVL({@dept_id}, department_id)
<xsql:no-rows-query rowset-element="" row-element=""
tag-case="lower">
SELECT 'The department {@dept_id} does not exist '||
'or does not have any employees.' ERROR
FROM dual
</xsql:no-rows-query>
</xsql:query>

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-92
Practice 7: Solutions (continued)

b. In the browser, append ?dept_id=60 to the URL, and refresh the browser. Are the
results formatted as expected? Do not close the browser window.

Yes, the employees in department 60 are displayed in the alphabetical order of their last
names in an HTML table.

n l y
e O
U s
c. In the browser, change the dept_id value of 60 to 140, and press [Enter] to display the

A I
results in the browser. Does the page format as expected? Close the browser window.

Yes, the error text is displayed indicating that the department does not exist or does not
have employees.
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-93
Practice 7: Solutions (continued)

d. Edit employees.xsl, add a new template rule for the <error> element by using the
following template body:
<h3>
<font color="red">
<xsl:value-of select="."/>
</font>
</h3>.
Save the changes to the XSL stylesheet.
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Document Root template -->
<xsl:template match="/">
<html>
<head><title>Employee Information</title></head>
<body>
<h1>Employee Information</h1>
<!-- Add instructions here -->
<xsl:apply-templates/>
</body>
</html>
</xsl:template>

<xsl:template match="employees">
n l y
O
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<!-- Add instructions here -->
<xsl:apply-templates>
s e
<xsl:sort select="last_name"/>
</xsl:apply-templates>
I U
</table>
</xsl:template>
O A
<xsl:template match="employee">
<tr>
l &
n a
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/>,

te r
<xsl:value-of select="first_name"/></td>
<td><!--xsl:value-of select="salary"/-->

I n
<xsl:apply-templates select="salary">
<xsl:with-param name="minsal">8000</xsl:with-param>

l e
</xsl:apply-templates>

c
</td>

r a
</tr>
</xsl:template>

O
Oracle 10g: XML Fundamentals A-94
Practice 7: Solutions (continued)
<xsl:template match="salary">
<xsl:param name="minsal" select="4500"/>
<xsl:choose>
<xsl:when test="number(.)&lt;$minsal">
<b><xsl:value-of select="."/></b>
</xsl:when>
<xsl:otherwise>
<i><xsl:value-of select="."/></i>
</xsl:otherwise>
</xsl:choose>
</xsl:template>

<xsl:template match="error">
<h3>
<font color="red">
<xsl:value-of select="."/>
</font>
</h3>
</xsl:template>
</xsl:stylesheet>

e. Run employees.xsql, and then after appending ?dept_id=140 to the URL, press
[Enter] to display the results in the browser. After this is completed, close the browser.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-95
Practice 8: Solutions
The goal of this practice is to explore the additional features of XSQL Pages. You create an
XSQL Page by using a CURSOR expression to combine department and employee information to
conform to an XSLT stylesheet. You create page or session parameters to navigate between
department rows. You use an existing HTML form to post XML data for inserting, updating, and
deleting rows in the COUNTRIES table. If you have time, add cookies to track the number of
page hits in a browser session.

1. Create an XSQL Page to display a department and its employees, and use a supplied XSLT
stylesheet to transform the results.

a. In the XMLLabsProject, create a new XSQL Page called deptandemp.xsql.


<?xml version="1.0" encoding='windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
<page xmlns:xsql="urn:oracle-xsql">

</page>

b. In deptandemp.xsql, structure the XML output results to display a department


root element containing the department_id, department_name, and using a

n l y
CURSOR expression, include the employees in the department. Inside the employees

e O
element, include each employee with employee_id, last_name, and salary.
Ensure that the tag names are output in lowercase. In the <xsql:query> start tag,

U s
define a lexical parameter dept_id with a default value of 10 that you use in the
condition of the query to display the department with the specified ID.

<department> A I
Hint: The XML output tree must contain the following structure:

<department_id>...</department_id>
O
<employees>
l &
<department_name>...<department_name>

<employees_row num="n">
n a
te r
<employee_id>...</employee_id>
<last_name>...</last_name>

n
<salary>...</salary>

:
e I
</employees_row>

c l
</employees>

r a
</department>

O
Oracle 10g: XML Fundamentals A-96
Practice 8: Solutions (continued)
<?xml version="1.0" encoding='windows-1252'?>
<!--
| Uncomment the following processing instruction and replace
| the stylesheet name to transform output of your XSQL Page using XSLT
<?xml-stylesheet type="text/xsl" href="YourStylesheet.xsl" ?>
-->
<department xmlns:xsql="urn:oracle-xsql"
connection="hr">
<xsql:query rowset-element="" row-element=""
tag-case="lower" dept_id="10">
SELECT d.department_id, d.department_name,
CURSOR(SELECT e.employee_id, e.last_name, e.salary
FROM employees e
WHERE e.department_id = d.department_id) employees
FROM departments d
WHERE d.department_id = {@dept_id}
</xsql:query>
</department>

c. Check the XML syntax, fix any syntax errors, and save the changes to
deptandemp.xsql. Run deptandemp.xsql to confirm whether the XML output
has the correct structure. Modify the XSQL Page until it produces the correct output, if
required.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-97
Practice 8: Solutions (continued)

d. Add the E:\labs\lab\deptandemp.xsl XSLT stylesheet file to the


XMLLabsProject. Edit deptandemp.xsql, and add an <?xml-stylesheet?>
processing instruction to transform the results of the deptandemp.xsql page output.
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"
connection="hr">
<xsql:query rowset-element="" row-element=""
tag-case="lower" dept_id="10">
SELECT d.department_id, d.department_name,
CURSOR(SELECT e.employee_id, e.last_name, e.salary
FROM employees e
WHERE e.department_id = d.department_id) employees
FROM departments d
WHERE d.department_id = {@dept_id}
</xsql:query>
</department>

e. Run deptandemp.xsql and view the transformed results in the browser. Test the
application with the HTTP parameter dept_id set to a value of 60 and then 300.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-98
Practice 8: Solutions (continued)

URL:
http://localhost:8988/XMLLabs/deptandemp.xsql?dept_id=60
produces:

n l y
e O
produces:
U s
http://localhost:8988/XMLLabs/deptandemp.xsql?dept_id=300

A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-99
Practice 8: Solutions (continued)

2. The deptandemp.xsl stylesheet provides support for navigation between department


records in the HTML page. However, the XSQL Page must calculate values for and include
XML elements that are used to create the Next and Previous buttons in the HTML page.
You write queries to set page or session parameters to the next and previous
department_id, with respect to the current dept_id lexical parameter value. The page
or session parameter values are created as child elements of a <page_navigation>
element that must be added.

a. Edit the XSQL Page deptandemp.xsql, and move the dept_id="10" default
lexical parameter and value from the <xsql:query> element into the start tag of the
<department> root element.
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"
connection="hr" dept_id="10">
<xsql:query rowset-element="" row-element=""
tag-case="lower">
SELECT d.department_id, d.department_name,
CURSOR(SELECT e.employee_id, e.last_name, e.salary
FROM employees e
WHERE e.department_id = d.department_id) employees
FROM departments d
WHERE d.department_id = {@dept_id}
</xsql:query>
n l y
</department>

e O
U s
b. In the deptandempl.xsql page after the start tag of the <department> root
element, add the XSQL element to set a page or session parameter called

A I
previous_id, whose value is calculated by using the following SQL statement (which
can be copied from the E:\labs\lab\lab_08_02_b.txt file):

SELECT NVL(MAX(department_id),0) previous_idO


FROM departments
l &
a
WHERE department_id &lt; (SELECT department_id

r n
FROM departments
WHERE department_id = {@dept_id})

I te
<?xml version="1.0" encoding='windows-1252'?>
n
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"

l e
connection="hr" dept_id="10">

c
<xsql:set-session-param name="previous_id">

r a
SELECT NVL(MAX(department_id),0) previous_id
FROM departments

O
WHERE department_id &lt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
</xsql:set-session-param>

Oracle 10g: XML Fundamentals A-100


Practice 8: Solutions (continued)
<xsql:query rowset-element="" row-element=""
tag-case="lower">
SELECT d.department_id, d.department_name,
CURSOR(SELECT e.employee_id, e.last_name, e.salary
FROM employees e
WHERE e.department_id = d.department_id) employees
FROM departments d
WHERE d.department_id = {@dept_id}
</xsql:query>
</department>

c. In deptandemp.xsql, after setting the previous_id session parameter, add


another XSQL element to a page or session parameter called next_id, whose value is
calculated from the following SQL statement (which can be copied from the
E:\labs\lab\lab_08_02_c.txt file):

SELECT NVL(MIN(department_id),0) next_id


FROM departments
WHERE department_id &gt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"
n l y
connection="hr" dept_id="10">
<xsql:set-session-param name="previous_id">
SELECT NVL(MAX(department_id),0) previous_id
e O
FROM departments
WHERE department_id &lt; (SELECT department_id
U s
FROM departments

A I
WHERE department_id = {@dept_id})
</xsql:set-session-param>
<xsql:set-session-param name="next_id">
O
FROM departments
l &
SELECT NVL(MIN(department_id),0) next_id

a
WHERE department_id &gt; (SELECT department_id

n
r
FROM departments

te
WHERE department_id = {@dept_id})
</xsql:set-session-param>

I n
<xsql:query rowset-element="" row-element=""
tag-case="lower">

l e
SELECT d.department_id, d.department_name,

c
CURSOR(SELECT e.employee_id, e.last_name, e.salary

r a FROM employees e
WHERE e.department_id = d.department_id) employees

OFROM departments d
WHERE d.department_id = {@dept_id}
</xsql:query>
</department>

Oracle 10g: XML Fundamentals A-101


Practice 8: Solutions (continued)

d. In deptandemp.xsql before the end tag of the root element (the </departments>
tag), add the start and end tags for an XML element called <page_navigation> (in
lowercase). Inside the body of the <page_navigation> element, add XML child
elements for the previous_id and next_id page or session parameters and values
by using the <xsl:include-param> XSQL action element.
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"
connection="hr" dept_id="10" counter="1">
<xsql:set-session-param name="previous_id">
SELECT NVL(MAX(department_id),0) previous_id
FROM departments
WHERE department_id &lt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
</xsql:set-session-param>
<xsql:set-session-param name="next_id">
SELECT NVL(MIN(department_id),0) next_id
FROM departments
WHERE department_id &gt; (SELECT department_id
FROM departments

</xsql:set-session-param>
WHERE department_id = {@dept_id})

<xsql:query rowset-element="" row-element=""


n l y
tag-case="lower">
SELECT d.department_id, d.department_name,
e O
CURSOR(SELECT e.employee_id, e.last_name, e.salary
FROM employees e
U s
WHERE e.department_id = d.department_id) employees
FROM departments d
WHERE d.department_id = {@dept_id}
A I
</xsql:query>
<page_navigation> O
<xsql:include-param name="next_id"/>
l &
<xsql:include-param name="previous_id"/>

</page_navigation>
n a
</department>

te r
I n
e. Ensure that the XML syntax of deptandemp.xsql is well-formed, and save the
changes to the file. Run deptandemp.xsql to view how the XSL transformation

l e
changes the appearance of the HTML Page, which should now include a Next button.

c
Test the HTML page functionality by clicking the Next and Previous buttons.

r a
Note: The Previous button appears after you navigate to the next department, and
disappears when you are at the first department. The Next button disappears when you
O reach the last department record in the set.

Oracle 10g: XML Fundamentals A-102


Practice 8: Solutions (continued)
If you have time…

3. Use a cookie in deptandemp.xsql to track the number of times the XSQL Page is
requested in a browser session. The XSL template produces a hit count when you include a
cookie parameter in the <page_navigation> element.

a. Edit deptandemp.xsql, by adding an attribute called counter set to the value 1 in


the document root element.
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"
connection="hr" dept_id="10" counter="1">
<xsql:set-session-param name="previous_id">
SELECT NVL(MAX(department_id),0) previous_id
FROM departments
WHERE department_id &lt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
</xsql:set-session-param>
<xsql:set-session-param name="next_id">
SELECT NVL(MIN(department_id),0) next_id
FROM departments
WHERE department_id &gt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
n l y
</xsql:set-session-param>
<xsql:query rowset-element="" row-element=""
tag-case="lower">
e O
SELECT d.department_id, d.department_name,
CURSOR(SELECT e.employee_id, e.last_name, e.salary
U s
FROM employees e

A I
WHERE e.department_id = d.department_id) employees
FROM departments d
WHERE d.department_id = {@dept_id}
O
</xsql:query>
<page_navigation>
l &
a
<xsql:include-param name="previous_id"/>

n
r
<xsql:include-param name="next_id"/>

te
</page_navigation>
</department>

I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-103
Practice 8: Solutions (continued)

b. In deptandemp.xsql, after the start tag of the document root, use the XSQL set
cookie action to create a cookie named counter whose value is set using the following
SQL statement:
SELECT TO_NUMBER(NVL('{@counter}','1'))+1
FROM dual
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"
connection="hr" dept_id="10" counter="1">
<xsql:set-cookie name="counter">
SELECT TO_NUMBER(NVL('{@counter}','1'))+1
FROM dual
</xsql:set-cookie>
<xsql:set-session-param name="previous_id">
SELECT NVL(MAX(department_id),0) previous_id
FROM departments
WHERE department_id &lt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
</xsql:set-session-param>
<xsql:set-session-param name="next_id">
SELECT NVL(MIN(department_id),0) next_id
FROM departments
WHERE department_id &gt; (SELECT department_id
n l y
</xsql:set-session-param>
FROM departments
WHERE department_id = {@dept_id})

e O
<xsql:query rowset-element="" row-element=""
tag-case="lower">
U s
SELECT d.department_id, d.department_name,

A I
CURSOR(SELECT e.employee_id, e.last_name, e.salary

O
FROM employees e
WHERE e.department_id = d.department_id) employees
FROM departments d
WHERE d.department_id = {@dept_id}
l &
</xsql:query>

n a
r
<page_navigation>

te
<xsql:include-param name="previous_id"/>
<xsql:include-param name="next_id"/>
</page_navigation>
</department>
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-104
Practice 8: Solutions (continued)

c. In deptandemp.xsql, in the <page_navigation> element, include the


counter cookie parameter as the last child element. Check the XML syntax for the
page and save the changes when errors have been eliminated.
<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="deptandemp.xsl"?>
<department xmlns:xsql="urn:oracle-xsql"
connection="hr" dept_id="10" counter="1">
<xsql:set-cookie name="counter">
SELECT TO_NUMBER(NVL('{@counter}','1'))+1
FROM dual
</xsql:set-cookie>
<xsql:set-session-param name="previous_id">
SELECT NVL(MAX(department_id),0) previous_id
FROM departments
WHERE department_id &lt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
</xsql:set-session-param>
<xsql:set-session-param name="next_id">
SELECT NVL(MIN(department_id),0) next_id
FROM departments
WHERE department_id &gt; (SELECT department_id
FROM departments
WHERE department_id = {@dept_id})
n l y
O
</xsql:set-session-param>
<xsql:query rowset-element="" row-element=""
tag-case="lower">
SELECT d.department_id, d.department_name,
s e
CURSOR(SELECT e.employee_id, e.last_name, e.salary
FROM employees e
I U
FROM departments d
WHERE d.department_id = {@dept_id}
O A
WHERE e.department_id = d.department_id) employees

</xsql:query>
<page_navigation>
l &
n a
<xsql:include-param name="previous_id"/>
<xsql:include-param name="next_id"/>

</page_navigation>
te r
<xsql:include-param name="counter"/>

</department>
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-105
Practice 8: Solutions (continued)

d. Run deptandemp.xsql. Notice that the Page Hit count line, which appears under the
page heading, increments each time you click the Next or Previous buttons.
Note: You can look at the template rule for the document root in the
deptandemp.xsl stylesheet and see the XSL elements that conditionally include the
page hit counter.

n l y
e O
U s
A I
O
l &
n a
r
Note: The image shows the browser after clicking the Next button once.

te
I n
4. Use the country_form.html file provided to submit country data to be inserted,
updated, and deleted from the COUNTRIES table. You use and modify the XML data in the

l e
form to send to XSQL Pages to insert, update, delete or query country rows. In this task, you

c
create the XSQL Pages to perform the DML operations behind the form. The query

a
functionality has been provided.
r
O
Oracle 10g: XML Fundamentals A-106
Practice 8: Solutions (continued)

a. In the XMLLabsProject, from the E:\labs\lab directory, add the


country_dml.html, country-list.xsql, and country-list.xsl files.
Briefly, examine the source of each file, which form the basis of a country maintenance
Web application. Run the application by right-clicking country_dml.html and
selecting Run.
Note: The Insert XML, Update XML, and Delete XML buttons will not work because
the XSQL Pages for their functionality need to be created.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-107
Practice 8: Solutions (continued)

Source for the country_dml.html file:


<HTML>
<HEAD><TITLE>Country Web Application</TITLE></HEAD>
<BODY>
<SCRIPT>
function PostOrder (xmldoc, xsqlfile)
{
var xmlhttp = new ActiveXObject ("Microsoft.XMLHTTP");
xmlhttp.open("POST", xsqlfile, false);
xmlhttp.send(xmldoc);
return xmlhttp.responseXML;
}
function submitInfo(xsqlfile)
{
var xmldoc = new ActiveXObject ("Microsoft.XMLDOM");
xmldoc.async = false;
xmldoc.loadXML(rowset.xmldocText.value);
var response = PostOrder(xmldoc, xsqlfile);
alert(response.documentElement.xml);
}
</SCRIPT>
<H2>Country Web Application</H2>

y
<P><B>Enter Canonical XML for Country Rows to Post:</B></P>
<FORM name="rowset">
<TEXTAREA rows="12" style="width:100%" NAME="xmldocText">
n l
<ROWSET>
<ROW>
<COUNTRY_ID>ZA</COUNTRY_ID>
e O
<COUNTRY_NAME>South Africa</COUNTRY_NAME>
<REGION_ID>4</REGION_ID>
U s
</ROW>
</ROWSET>
A I
</TEXTAREA>
<P> O
<INPUT type="button" value="Insert XML"

l &
onclick="submitInfo('country-insert.xsql')">

a
<INPUT type="button" value="Update XML"

n
te r
onclick="submitInfo('country-update.xsql')">
<INPUT type="button" value="Delete XML"
onclick="submitInfo('country-delete.xsql')">

I n
<INPUT type="button" value="List Countries"
onclick="location='country-list.xsql'">
</P>
</FORM>
c l e
r a
<FORM action="country-list.xsql">
Country Id: <INPUT type="text" name="id">

O
<INPUT type="submit" value="Find...">
</FORM>
</BODY>
</HTML>

Oracle 10g: XML Fundamentals A-108


Practice 8: Solutions (continued)

Source for the country-list.xsql file:


<?xml version="1.0" encoding='windows-1252'?>
<?xml-stylesheet type="text/xsl" href="country-list.xsl"?>
<COUNTRIES xmlns:xsql="urn:oracle-xsql" connection="hr">
<xsql:query rowset-element="" row-element="COUNTRY">
SELECT * FROM countries
WHERE country_id like '{@id}'||'%'
</xsql:query>
</COUNTRIES>

Source for the country-list.xsl file:


<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates/>
<p><a href="javascript:history.back()">Back</a></p>
</body>
</html>
</xsl:template>
<xsl:template match="COUNTRIES">
n l y
<h2>Countries</h2>
<xsl:choose>
e O
<xsl:when test="(count(COUNTRY)&gt;0)">
<table border="1">
U s
<xsl:apply-templates/>
</table>
A I
<tr><th>Id</th><th>Name</th><th>Region</th></tr>

</xsl:when>
O
&
<xsl:otherwise><p>Country not found!</p></xsl:otherwise>
</xsl:choose>
</xsl:template>
a l
<tr>
r n
<xsl:template match="COUNTRY">

te
<td><xsl:value-of select="COUNTRY_ID"/></td>

n
<td><xsl:value-of select="COUNTRY_NAME"/></td>

I
<td><xsl:value-of select="REGION_ID"/></td>
</tr>

l e
</xsl:template>
c
</xsl:stylesheet>

r a
O
Oracle 10g: XML Fundamentals A-109
Practice 8: Solutions (continued)

b. In the Country Web Application browser window, click List Countries to display a list
of countries. Observe that the country South Africa is not present in the existing
database data. Close the browser window.

n l y
e O
U s
A I
O
l &
n a
te r
c. In the XMLLabsProject, create an XSQL Page called country-insert.xsql,
and add the instruction to insert the XML data into the COUNTRIES database table.

I n
Check the XML syntax, and save the file when there are no errors.
Note: The source data is in canonical form; therefore, an XSLT is not required.

l e
Remember to include the connection attribute in the document element.
c
r a
<?xml version="1.0" encoding='windows-1252'?>
<page xmlns:xsql="urn:oracle-xsql" connection="hr">

O
<xsql:insert-request table="countries"/>
</page>

Oracle 10g: XML Fundamentals A-110


Practice 8: Solutions (continued)

d. In the XMLLabsProject, create an XSQL Page called country-update.xsql,


and add the instruction to update the XML data in the COUNTRIES table. Specify
country_id in the key-columns attribute, and specify "country_name,
region_id" as the value of the columns attribute. Add the connection attribute.
Check the XML syntax, and save the file when there are no errors.
<?xml version="1.0" encoding='windows-1252'?>
<page xmlns:xsql="urn:oracle-xsql" connection="hr">
<xsql:update-request table="countries"
key-columns="country_id" columns="country_name,region_id"/>
</page>

e. In the XMLLabsProject, create an XSQL Page called country-delete.xsql,


and add the instruction to delete the XML data from the COUNTRIES table. Specify the
key-columns attribute with the country_id value. Add the connection
attribute. Check the XML syntax, and save the file when there are no errors.
<?xml version="1.0" encoding='windows-1252'?>
<page xmlns:xsql="urn:oracle-xsql" connection="hr">
<xsql:delete-request key-columns="country_id"
table="countries"/>
</page>

f. Right-click country_dml.html in the Navigator or Editor, and click Run.


n l y
O
g. Click Insert XML. What is the response? Clear the alert window by clicking Ok. To
e
s
confirm whether the new country record has been inserted, enter ZA in the Country Id:
field at the bottom of the page, and then click Find…. Click Back to return to the country
U
HTML form page.

The Javascript alert response is:


A I
O
l &
n a
te r
I n
l e
The <xsql-status> element is returned indicating that one row was inserted. The

c
text in the alert window is the XML response from posting the XML to the country-

r a
insert.xsql in an HTTP request.

O
Oracle 10g: XML Fundamentals A-111
Practice 8: Solutions (continued)

The insert result is confirmed with the following results of the Web find operation:

h. In the form XML text area, modify the text value of the <COUNTRY_NAME> element to
be Southern Africa. Click Update XML. What is the response? Clear the alert
window by clicking Ok. To confirm whether the country record has been updated, enter
ZA in the Country Id: field at the bottom of the page, and then click Find…. Click Back
to return to the country form page.

The Javascript alert response is:


n l y
e O
U s
A I
O
&
The update result is confirmed with the following results of the Web find operation:

l
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-112
Practice 8: Solutions (continued)

i. In the country HTML form, click Delete XML. What is the response? Clear the alert
window by clicking Ok. To confirm whether the country record has been deleted, enter
ZA in the Country Id: field at the bottom of the page, and then click Find…. Click Back
to return to the country form page. Close the browser window.

The Javascript alert response is:

The delete result is confirmed with the following results of the Web find operation:

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-113
Practice 9: Solutions
The goal of this practice is to work with the Oracle XML DB (XDB) repository for storing and
accessing XML resources, such as XML and XSL documents.

1. Map a Web Folder to the Oracle XDB repository.

a. Start Windows Explorer on your computer.


Hint: You can do this by selecting your XMLlabsProject node and click Tools >
Explorer, which has been configured for this course.

n l y
e O
U s
A I
O
b. In the Explorer window, click Tools > Map Network Drive ….

l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-114
Practice 9: Solutions (continued)

c. Click the text Web Folder or FTP site in the last line that reads Create a shortcut to a
Web Folder or FTP site in the Map Network Drive window.

d. In the Add Network Place Wizard, enter the URL http://<host>:160<nn>/ in


the Type the location of the Network Place: field. Then, click Next >.

l y
Note: Check with your instructor for the name of the <host> and port number suffix
<nn> for your course.
n
e O
e. You will be prompted for a User Name and Password in the Enter Network Password
window. Enter your Oracle database User Name ORAx and Password oracle, select
s
the Save this password in your password list check box, and then click OK.
U
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-115
Practice 9: Solutions (continued)

f. In the Add Network Place Wizard, you are prompted to Enter a name for this
Network Place. Enter the name OracleXDB in the field and click Finish.

g. Windows opens another Explorer window with the top-level folders of the Oracle XDB
repository displayed.
Note: Do not close the Explorer windows.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-116
Practice 9: Solutions (continued)

2. In the home parent folder, create your own student folder called ORAx, where x is your
assigned student number.

a. Double-click the home folder, and create a new folder called ORAx by clicking
File > New > Folder.

l y
b. Replace the text New Folder, next to your new folder icon, with the text ORAx (using
uppercase characters), where x is your student number, and press [Enter]. n
O
Note: The folder must have the same name as your Oracle database username.

e
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-117
Practice 9: Solutions (continued)

3. Copy the E:\labs\lab\hr.xml file to your /home/ORAx folder in the Oracle XDB
repository.
Hint: Using the two Windows Explorer windows, drag hr.xml from Explorer with
E:\labs\lab displayed and drop it to the ORAx Web folder in the other Explorer window
that you created in the previous step.

n l y
4. Navigate to your ORAx folder in the OracleXDB Explorer window, and double-click on the

O
hr.xml file. Internet Explorer is started and you are prompted to enter your User Name
ORAx and Password oracle. Click OK and the hr.xml file contents are displayed.
e
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-118
Practice 9: Solutions (continued)

5. Using the Explorer windows, copy the employees.xml and the related files to the Web
folder.

a. Copy employees.xml, employees.xsl, and employees.dtd files from the


E:\labs\lab directory to your /home/ORAx folder.

b. After copying the files to the Web folder, double-click employees.xsl in the Web
folder.

Note: The XSL stylesheet will not display correctly.


n l y
O
c. Double-click the employees.xml. Does a complete transformation occur?

e
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Yes, the XSL transformation should be completed on the employees.xml file, as shown.

Oracle 10g: XML Fundamentals A-119


Practice 10: Solutions
The goal of this practice is to generate XML by using SQL/XML generation functions. You use
SQL*Plus for tasks by using the login information provided by your instructor. SQL*Plus can be
started in several ways, two of which are:

a. Double-click the SQLPlus icon on the desktop.

b. Click the Connections tab in JDeveloper Navigator, expand the Database node and
right-click the hr connection created in the XSQL practices, and select SQL*Plus. If you
use this technique, then the first time you invoke SQL*Plus from a database connection,
JDeveloper prompts you for the location of the SQL*Plus executable. In this case, enter
the file path name E:\oracle\ora10\bin\sqlplusw.exe, or click Browse…
and locate the same file.
The practices generate XML by using the EMPLOYEES table.

1. Generate XML by using SQL by retrieving the concatenated value of the first and last names,
and the salaries of employees in department 60. Use fullname and pay as the respective
XML elements, and employee for their parent element.
SELECT XMLElement("employee",
XMLElement("fullname", first_name||' '||last_name),
XMLElement("pay", salary)) as "result"
FROM employees
WHERE department_id = 60;

The results are: n l y


result
e O
s
-------------------------------------------------------------------------
<employee><fullname>Alexander Hunold</fullname><pay>9000</pay></employee>
U
<employee><fullname>Bruce Ernst</fullname><pay>6000</pay></employee>

A I
<employee><fullname>David Austin</fullname><pay>4800</pay></employee>
<employee><fullname>Valli Pataballa</fullname><pay>4800</pay></employee>

O
<employee><fullname>Diana Lorentz</fullname><pay>4200</pay></employee>

l &
2. Use XSQL/XML to generate employees’ salaries and their department IDs as content for

n a
employees in department 10 and 20. Employees’ last names and department IDs should
appear as XML attributes of an element named employee.
SELECT XMLElement("employee",
te r
I n
XMLAttributes(employee_id, last_name),
XMLForest(salary, department_id)) as "result"
FROM employees

l e
WHERE department_id in (10,20);
c
r a
O
Oracle 10g: XML Fundamentals A-120
Practice 10: Solutions (continued)

The result using SQL*Plus set linesize 70 long 200 is:


result
----------------------------------------------------------------------
<employee EMPLOYEE_ID="200" LAST_NAME="Whalen"><SALARY>4400</SALARY><D
EPARTMENT_ID>10</DEPARTMENT_ID></employee>
<employee EMPLOYEE_ID="201" LAST_NAME="Hartstein"><SALARY>13000</SALAR
Y><DEPARTMENT_ID>20</DEPARTMENT_ID></employee>
<employee EMPLOYEE_ID="202" LAST_NAME="Fay"><SALARY>6000</SALARY><DEPA
RTMENT_ID>20</DEPARTMENT_ID></employee>

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-121
Practice 10: Solutions (continued)

3. Use SYS_XMLGEN to generate the values of employees’ last names and salaries for all
employees in department 20.
SELECT SYS_XMLGEN(last_name).getStringVal() last_name,
SYS_XMLGEN(salary) salary
FROM employees
WHERE department_id = 20;

Results using SQL*Plus linesize 80, col last_name format a40, and col
salary format a30 are:
LAST_NAME SALARY
---------------------------------------- ------------------------------
<?xml version="1.0"?> <?xml version="1.0"?>
<LAST_NAME>Hartstein</LAST_NAME> <SALARY>13000</SALARY>

<?xml version="1.0"?> <?xml version="1.0"?>


<LAST_NAME>Fay</LAST_NAME> <SALARY>6000</SALARY>

4. Use SYS_XMLAGG to create a group of employees in department 30 and 40, in an element


named EMPLOYEE_GROUP, listing the employees’ last names and grouping them by
hire_date.
SELECT SYS_XMLAGG(
SYS_XMLGEN(last_name),
n l y
SYS.XMLGENFORMATTYPE.createFormat('EMPLOYEE_GROUP')
).getClobVal() aggregate
e O
s
FROM employees
WHERE department_id in (30, 40)
GROUP BY hire_date;

I U
The results of the query are:
AGGREGATE
O A
----------------------------------
<?xml version="1.0"?>
l &
<EMPLOYEE_GROUP>

n a
r
<LAST_NAME>Mavris</LAST_NAME>

te
</EMPLOYEE_GROUP>

I
<?xml version="1.0"?>
<EMPLOYEE_GROUP> n
l e
<LAST_NAME>Raphaely</LAST_NAME>

c
</EMPLOYEE_GROUP>

r a
<?xml version="1.0"?>

O
<EMPLOYEE_GROUP>
<LAST_NAME>Khoo</LAST_NAME>
</EMPLOYEE_GROUP>

Oracle 10g: XML Fundamentals A-122


Practice 10: Solutions (continued)
<?xml version="1.0"?>
<EMPLOYEE_GROUP>
<LAST_NAME>Tobias</LAST_NAME>
</EMPLOYEE_GROUP>
<?xml version="1.0"?>
<EMPLOYEE_GROUP>
<LAST_NAME>Baida</LAST_NAME>
</EMPLOYEE_GROUP>

<?xml version="1.0"?>
<EMPLOYEE_GROUP>
<LAST_NAME>Himuro</LAST_NAME>

</EMPLOYEE_GROUP>
<?xml version="1.0"?>
<EMPLOYEE_GROUP>
<LAST_NAME>Colmenares</LAST_NAME>
</EMPLOYEE_GROUP>

7 rows selected.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-123
Practice 11: Solutions
The goal of this practice is to work with XMLType. Use SQL*Plus for these tasks.

1. Create a table to store employee internal orders. The table must contain two columns,
employee_id and order_document. The employee_id column must be numeric
and the order_document column must be of XMLType. The table can be called
internal_orders.
CREATE TABLE internal_orders (
employee_id NUMBER,
order_document XMLTYPE);

Table created.

2. Using script lab_11_02.sql, insert some data into the table created in the previous
question.
SQL> @lab_11_02.sql

1 row created.

1 row created.

1 row created.

n l
3. Query the internal_orders table to select the number of purchase orders for employeey
100.
SELECT count(*)
e O
FROM internal_orders
WHERE employee_id = 100;
U s
COUNT(*)
----------
A I
2
O
l &
4. In SQL*Plus, enter SET HEADING OFF, and then COL order_document format

a
a60. Then, write a query on the internal_orders table to retrieve the purchase order
n
for the employee called King.

te r
Hint: Use the LIKE operator and use SQL*Plus SET LONG nn to increase the amount of

SET HEADING OFF I n


XML data you want to see.

l e
COLUMN order_document format a60
SELECT *
c
r a
FROM internal_orders
WHERE order_document LIKE '%Higgins%';

O
205 <PurchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi

Oracle 10g: XML Fundamentals A-124


Practice 11: Solutions (continued)

5. Use the extract function to get the text value of the Description element from the
order_document column.
SELECT o.order_document.extract('//Description/text()').getStringVal()
FROM internal_orders o;

The Ruling ClassDiabolique8 1/2


The Life of BrianHamletSalesmanNights of CabiriaUnbearable Lightness Of
Being
The Seven SamuraiAlphavilleRebeccaVariety LightsBeauty and the
BeastAmarcord

6. Use the existsNode() function to determine the number of purchase orders for the user
King.
SELECT COUNT(*)
FROM internal_orders
WHERE existsNode(order_document,'/PurchaseOrder[User="King"]') = 1;

COUNT(*)
----------
2

7. Use the extractValue() function to obtain the value of the User node from
internal_orders table where the description contains “Amarcord”.
n l y
SELECT extractValue(order_document,'/PurchaseOrder/User') value
FROM internal_orders o
e O
WHERE o.order_document

U s
.extract('//Description/text()').getStringVal() Like '%Amarcord%';

VALUE
A I
----------------
Higgins
O
l &
8. Retrieve the content of the order_document column from the internal_orders
table.
n a
te r
SELECT o.order_document.getClobVal() order_document
FROM internal_orders o;

I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-125
Practice 11: Solutions (continued)

ORDER_DOCUMENT
------------------------------------------------------------
<PurchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi

<PurchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi

<PurchaseOrder
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-126
Practice 12: Solutions
The goal of this practice is to create a non-schema-based XMLType view of employees by using
SQL/XML functions, and an updateable XML Schema-based XMLType view based on an object
type by using the department data.

1. Create a non-schema-based XMLType view of employee data.

a. Start SQL*Plus (if not already started) and log in with your username and password to
the course database.
Note: You can start SQL*Plus by using the hr connection in JDeveloper.

b. In JDeveloper, right-click XMLLabsProject, select New…, expand the Database


Tier category, and click Database Files. Double-click SQL File in the Items section. In
the File name field, enter employee_xmlv.sql and click Ok.

The file content is empty, and the file must be created in the E:\labs\lab
directory.

c. Edit the employee_xmlv.sql file in JDeveloper and create an XMLType view called
employee_xmlv that uses SQL/XML generation functions to form the XML structure.
For each employee row, use employee as the root element containing the
employee_id and department_id as attributes, and child elements for the
first_name, last_name, job_id, and salary. Use the employee_id attribute
in the WITH OBJECT ID expression. Save changes to the file.
n
Note: The element and attribute names are the same as the column names but in l y
lowercase.
CREATE OR REPLACE VIEW employee_xmlv OF XMLType
e O
WITH OBJECT ID
(extract(OBJECT_VALUE,'/employee/@employee_id').getNumberVal())
U s
AS SELECT XMLElement("employee",
XMLAttributes(employee_id AS "employee_id",
A I
O
department_id AS "department_id"),
XMLForest(first_name AS "first_name",
last_name AS "last_name",
job_id AS "job_id",
l &
a
salary AS "salary")) AS "result"

n
FROM employees;

te r
d. Execute the E:\labs\lab\employee_xmlv.sql file in SQL*Plus to create the
view object.
I n
c l e
Note: You can cut and paste the text between windows or use the @file.sql
SQL*Plus command.

r a
SQL> @E:\labs\lab\employee_xmlv.sql

O
View created.

Oracle 10g: XML Fundamentals A-127


Practice 12: Solutions (continued)

e. In SQL*Plus, write a query on the employee_xml view to display the first three rows.
Hint: Use the ROWNUM pseudocolumn in the WHERE clause.
Note: To control the output display format, execute the script called
E:\labs\lab\lab_12_01_e.sql or enter the following SQL*Plus commands
before executing the query: set pagesize 60, set long 500, column
SYS_NC_ROWINFO$ format a70 word.
SQL> @E:\labs\lab\lab_12_01_e.sql
SQL> SELECT *
2 FROM employee_xmlv
3 WHERE ROWNUM < 4;

SYS_NC_ROWINFO$
----------------------------------------------------------------------
<employee employee_id="100"
department_id="90"><first_name>Steven</first_name><last_name>King</las
t_name><job_id>AD_PRES</job_id><salary>24000</salary></employee>

<employee employee_id="101"
department_id="90"><first_name>Neena</first_name><last_name>Kochhar</l
ast_name><job_id>AD_VP</job_id><salary>17000</salary></employee>

<employee employee_id="102"
department_id="90"><first_name>Lex</first_name><last_name>De
Haan</last_name><job_id>AD_VP</job_id><salary>17000</salary></employee
n l y
>

e O
U s
f. In SQL*Plus, write a query to display employee_xml rows with a department_id
value of 60 by using the existsNode() function in the WHERE clause.
SQL> SELECT *
2 FROM employee_xmlv
A I
O
3 WHERE existsNode(object_value,'/employee[@department_id="60"]') = 1;

SYS_NC_ROWINFO$
l &
<employee employee_id="103"
n a
----------------------------------------------------------------------

te r
department_id="60"><first_name>Alexander</first_name><last_name>Hunold
</last_name><job_id>IT_PROG</job_id><salary>9000</salary></employee>

I n
<employee employee_id="104"

c l e
department_id="60"><first_name>Bruce</first_name><last_name>Ernst</las
t_name><job_id>IT_PROG</job_id><salary>6000</salary></employee>

r a
<employee employee_id="105"

O
department_id="60"><first_name>David</first_name><last_name>Austin</la
st_name><job_id>IT_PROG</job_id><salary>4800</salary></employee>

Oracle 10g: XML Fundamentals A-128


Practice 12: Solutions (continued)
<employee employee_id="106"
department_id="60"><first_name>Valli</first_name><last_name>Pataballa<
/last_name><job_id>IT_PROG</job_id><salary>4800</salary></employee>

<employee employee_id="107"
department_id="60"><first_name>Diana</first_name><last_name>Lorentz</l
ast_name><job_id>IT_PROG</job_id><salary>4200</salary></employee>

g. In SQL*Plus, write a query to display employee_xml rows with last_name starting


with the uppercase letter P by using the extract() function with a LIKE operation in
the WHERE clause.
SQL> SELECT *
2 FROM employee_xmlv e
3 WHERE e.extract('/employee/last_name/text()') LIKE 'P%';

SYS_NC_ROWINFO$
----------------------------------------------------------------------
<employee employee_id="106"
department_id="60"><first_name>Valli</first_name><last_name>Pataballa<
/last_name><job_id>IT_PROG</job_id><salary>4800</salary></employee>

<employee employee_id="113"
department_id="100"><first_name>Luis</first_name><last_name>Popp</last
_name><job_id>FI_ACCOUNT</job_id><salary>6900</salary></employee>
n l y
<employee employee_id="136"

e
department_id="50"><first_name>Hazel</first_name><last_name>Philtanker O
s
</last_name><job_id>ST_CLERK</job_id><salary>2200</salary></employee>

U
<employee employee_id="140"

A I
department_id="50"><first_name>Joshua</first_name><last_name>Patel</la
st_name><job_id>ST_CLERK</job_id><salary>2500</salary></employee>

<employee employee_id="146" O
l &
department_id="80"><first_name>Karen</first_name><last_name>Partners</

a
last_name><job_id>SA_MAN</job_id><salary>13500</salary></employee>

n
<employee employee_id="191"

te r
department_id="50"><first_name>Randall</first_name><last_name>Perkins<

n
/last_name><job_id>SH_CLERK</job_id><salary>2500</salary></employee>

I
c l e
r a
O
Oracle 10g: XML Fundamentals A-129
Practice 12: Solutions (continued)

2. Create an updateable XML-Schema-based XMLType view for the DEPARTMENTS table.

a. In SQL*Plus, create an object type called DEPARTMENT_T with the attribute names: ID,
NAME, MANAGER_ID, LOCATION_ID with the same database data type as their
associated columns in the DEPARTMENTS table.
Hint: In SQL*Plus, use the DESCRIBE departments command to view the table
and column data type details.
Note: Do not double-quote the object type or its attribute names, so that they are stored in
uppercase in the data dictionary.
CREATE TYPE department_t AS OBJECT (
id NUMBER(4),
name VARCHAR2(30),
manager_id NUMBER(6),
location_id NUMBER(4));
/

Type created.

b. In SQL*Plus, register an XML Schema with the name


http://www.hr.com/department_t generated from the DEPARTMENT_T object
type, assigning the name DEPARTMENT (in uppercase) for the root element of the XML
Schema.
Note: Use the E:\labs\lab\lab_12_02_b.sql file as a template for this step.
n l y
BEGIN
e O
s
DBMS_XMLSCHEMA.registerSchema(
'http://www.hr.com/department_t.xsd',
DBMS_XMLSCHEMA.generateSchema(
USER,'DEPARTMENT_T','DEPARTMENT'),
I U
END;
/
TRUE, FALSE, FALSE);

O A
PL/SQL procedure successfully completed.
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-130
Practice 12: Solutions (continued)

c. In SQL*Plus, create the XMLType view called department_xmlv to query all rows
in the DEPARTMENTS table by using the relational column names as arguments in the
DEPARTMENT_T object constructor used in the query. Use the numeric value of the
/DEPARTMENT/ID path in the OBJECT ID expression. Save the CREATE VIEW
statement in a file called department_xmlv.sql after it is executed.
CREATE OR REPLACE VIEW department_xmlv OF XMLType
XMLSCHEMA "http://www.hr.com/department_t.xsd" ELEMENT "DEPARTMENT"
WITH OBJECT ID (object_value.extract('/DEPARTMENT/ID').getNumberVal())
AS SELECT department_t(department_id, department_name,
manager_id, location_id)
FROM departments;

View created.

d. In SQL*Plus, write a query on the department_xmlv to display XML fragments for


departments with a LOCATION_ID of 1800.
Note: First, execute a SET LONG 200 command to display all the XML elements for
each fragment.
SELECT *
FROM department_xmlv r
WHERE TO_NUMBER(r.extract('/DEPARTMENT/LOCATION_ID/text()')) = 1800;

-- Or n l y
SELECT *
e O
FROM department_xmlv r
WHERE r.existsNode('/DEPARTMENT[LOCATION_ID="1800"]') = 1;
U s
SYS_NC_ROWINFO$
------------------------------------- A I
<DEPARTMENT>
O
<ID>20</ID>
<NAME>Marketing</NAME>
l &
<MANAGER_ID>201</MANAGER_ID>

n
<LOCATION_ID>1800</LOCATION_ID> a
</DEPARTMENT>

te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-131
Practice 12: Solutions (continued)

e. Insert a new department with the name Education Services, with a


LOCATION_ID of 1800, and MANAGER_ID value of 201. Use the
DEPARTMENTS_SEQ sequence to generate the value for the ID element.
Hint: Concatenate DEPARTMENTS_SEQ.NEXTVAL. to obtain the ID value.
Note: Use the E:\labs\lab\lab_12_02_e.sql file as a template for this step.
INSERT INTO department_xmlv
VALUES (XMLType.createXML(
'<DEPARTMENT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://www.hr.com/department_t.xsd">
<ID>' || DEPARTMENTS_SEQ.NEXTVAL ||'</ID>
<NAME>Education Services</NAME>
<MANAGER_ID>201</MANAGER_ID>
<LOCATION_ID>1800</LOCATION_ID>
</DEPARTMENT>'));

1 row created.

f. In SQL*Plus, write a query to display departments in location_id 1800 to confirm


that the new row is inserted into the DEPARTMENTS relational table. Commit the
operation.
Note: Format the DEPARTMENT_NAME column by using the following SQL*Plus
command:
COL DEPARTMENT_NAME FORMAT A30
n l y
COL DEPARTMENT_NAME FORMAT A30
SELECT *
e O
FROM departments
WHERE location_id = 1800;
U s
DEPARTMENT_ID DEPARTMENT_NAME
A I
MANAGER_ID LOCATION_ID

O
------------- ------------------------------ ---------- -----------
280 Education Services 201 1800
20 Marketing

l & 201 1800

COMMIT;

n a
Commit complete.

te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-132
Practice 13: Solutions

1. XML Transformation in Oracle Database 10g is done: (Choose the best answer.)

a. Only by using XDK


b. In the server and by using XDK
c. Only in the server

Answer: b. In Oracle Database 10g, the XSL Transformation can be done both in the
database and by using XDK.

2. The XMLTransform() function: (Choose all that apply.)

a. Accepts XSL stylesheet and an XMLType instance as arguments


b. Applies an XSL stylesheet to the XMLType instance
c. Returns an XSL stylesheet
d. Returns an XMLType instance
e. Returns both XML and HTML as output

Answer: a, b, and e. The XMLTransform() function accepts an XSL stylesheet and an


XMLType instance as arguments, and applies the stylesheet to the XMLType instance and
returns either XML or HTML.

3. Which of the following are XPath functions? (Choose all that apply.)

a. existsNode()
n l y
b.
c.
under_path()
extract()
e O
d.
e.
get_path()
extractValue()
U s
A I
Answer: a, c, and e. The existsNode(), extract(), and extractValue() are all
Xpath functions.
O
&
4. Which of the following is true? (Choose all that apply.)
l
a.
n a
You can update only the entire XML document.
b.
c.
te r
You can perform piecewise updates.
The updateXML() function can be used for updating XML.
d.
n
Using the updateXML() function, you can update an attribute value, text nodes, and
I
node or node tree.

c l e
Answer: b, c, and d. Using the updateXML() function, you can perform piecewise updates

r a
on an attribute value, text nodes, and node or node tree.

O
Oracle 10g: XML Fundamentals A-133
Practice 13: Solutions (continued)
Run the script E:\labs\lab\lab_13_05.sql to create the objects for the following
questions:

SQL> @E:\labs\lab\lab_13_05.sql

Table created.

1 row created.

1 row created.

1 row created.

1 row created.

Table created.

1 row created.

5. Apply the stylesheet stored in stylesheet_tab to the XML PurchaseOrder with


reference containing “King-20011127121040988PST”.
Hint: Use existsNode() in the WHERE clause, and either use a subquery or a join
operation with the stylesheet_tab to obtain the stylesheet column.
SET HEADING OFF
SET LONG 8000 n l y
SELECT XMLTransform(x.xmlcol,
e O
FROM xml_tab x
U s
(select stylesheet from stylesheet_tab where id=1))

WHERE existsNode(xmlcol,

A I
'/PurchaseOrder [Reference = "King-20011127121560988PST"]') = 1;

Or
O
l &
SELECT XMLTransform(x.xmlcol, s.stylesheet)
FROM xml_tab x, stylesheet_tab s
WHERE s.id = 1
n a
AND existsNode(xmlcol,

te r
'/PurchaseOrder [Reference = "King-20011127121560988PST"]') = 1;

Or
I n
l e
SELECT XMLTransform(x.xmlcol,

c
dburiType('/'||user||

r a
'/STYLESHEET_TAB/ROW[ID = 1]/STYLESHEET/text()').getXML()) AS X1
FROM xml_tab x
O
WHERE existsNode(xmlcol,
'/PurchaseOrder [Reference = "King-20011127121560988PST"]') = 1;

Oracle 10g: XML Fundamentals A-134


Practice 13: Solutions (continued)

6. Update the XPath expression for ‘/PurchaseOrder/Reference’ for


‘King-20011127121560988PST’ to
‘De Haan-200203311200000000PST’
UPDATE XML_TAB t
SET xmlcol = updateXML(xmlcol,
'/PurchaseOrder/Reference/text()',
'De Haan-200203311200000000PST')
WHERE existsNode(xmlcol,
'/PurchaseOrder[Reference = "King-20011127121560988PST"]') = 1;

1 row updated.

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-135
Practice 14: Solutions
The goal of the practice is to use the DBUri servlet.
Note: Your solutions in this practice may have a different URL path prefix for the
http://localhost:port/oradb/HR part of the URI request. The HR part will contain
your assigned username of ORAx. The remaining path expression must be identical to the text
shown in bold in the solutions below.

1. Using the machine name and port provided by the instructor, retrieve the DEPARTMENTS
table by using the DBUri servlet with Internet Explorer.
Note: When prompted with a login dialog box, enter your assigned username oraX and
password oracle.
http://localhost:8080/oradb/HR/DEPARTMENTS

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-136
Practice 14: Solutions (continued)

2. Modify the above question to retrieve the table in the text format. When prompted, specify
that you want to open the result by using WordPad.
http://localhost:8080/oradb/HR/DEPARTMENTS?contenttype=text/text

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-137
Practice 14: Solutions (continued)

3. Using the DBUri servlet, retrieve the department name department with
DEPARTMENT_ID value 50 in the XML format.
http://localhost:8080/oradb/HR
/DEPARTMENTS/ROW[DEPARTMENT_ID="50"]/DEPARTMENT_NAME

4. Modify the previous question to retrieve the department name in the text format.
http://localhost:8080/oradb/HR
/DEPARTMENTS/ROW[DEPARTMENT_ID="50"]/DEPARTMENT_NAME/text()

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-138
Practice 14: Solutions (continued)

5. Using DBUri servlet, retrieve the row for employee last names with the DEPARTMENT_ID
value 60 in the XML format. Use the rowsettag parameter with the value ROWSET.
http://localhost:8080/oradb/HR
/EMPLOYEES/ROW[DEPARTMENT_ID="60"]/LAST_NAME?rowsettag=ROWSET

6. Using the DBUri servlet, retrieve the EMPLOYEES table and apply the stylesheet in
E:\labs\lab\lab_14_06.xsl to the output.

l y
a. Using Windows Explorer, open the OracleXDB Web Folder you created in Practice 9.
n
Navigate to the /home/ORAx folder that you created, where x is your student number.

using your database username and password.


e O
Note: If you previously closed the Explorer window, you will be prompted to log in by

s
Hint: Expand My Network Places in Explorer to locate the OracleXDB folder.

U
I
b. Drag lab_14_06.xsl and drop the file into your /home/ORAx folder.

A
O
c. Make a DBUri servlet request to display the EMPLOYEES table data as HTML by using
the XSL stylesheet you copied to the /home/ORAx folder.

l &
Hint: You will need to use the transform, contenttype, and rowsettag
parameters.

n a
te r
http://localhost:8080/oradb/HR/EMPLOYEES?transform=/home/HR/lab_14_06.xsl
&contenttype=text/html&rowsettag=ROWSET

I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-139
Practice 14: Solutions (continued)

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals A-140
B
Table Descriptions
and Data

n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
Entity Relationship Diagram

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals B-2
Tables in the Schema
SELECT * FROM tab;

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals B-3
REGIONS Table
DESCRIBE regions

SELECT * FROM regions;

n l y
e O
U s
A I
O
l &
n a
te r
I n
c le
r a
O
Oracle 10g: XML Fundamentals B-4
COUNTRIES Table
DESCRIBE countries

SELECT * FROM countries;

n l y
e O
U s
A I
O
l &
na
te r
I n
c le
r a
O
Oracle 10g: XML Fundamentals B-5
LOCATIONS Table
DESCRIBE locations;

SELECT * FROM locations;

n l y
e O
U s
A I
O
l &
n a
te r
I n
c le
r a
O
Oracle 10g: XML Fundamentals B-6
DEPARTMENTS Table
DESCRIBE departments

SELECT * FROM departments;

n l y
e O
U s
A I
O
l &
n a
te r
I n
c le
r a
O
Oracle 10g: XML Fundamentals B-7
JOBS Table
DESCRIBE jobs

SELECT * FROM jobs;

n l y
e O
U s
A I
O
l &
n a
te r
I n
c le
r a
O
Oracle 10g: XML Fundamentals B-8
EMPLOYEES Table
DESCRIBE employees

n l y
e O
U s
A I
O
l &
na
te r
I n
c le
r a
O
Oracle 10g: XML Fundamentals B-9
EMPLOYEES Table (continued)
The headings for columns COMMISSION_PCT, MANAGER_ID, and DEPARTMENT_ID
are set to COMM, MGRID, and DEPTID in the following screenshot, to fit the table values
across the page.

SELECT * FROM employees;

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals B-10
EMPLOYEES Table (continued)

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals B-11
EMPLOYEES Table (continued)

n l y
e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals B-12
JOB_HISTORY Table
DESCRIBE job_history

SELECT * FROM job_history;

n l y
e O
U s
A I
O
l &
n a
te r
I n
c le
r a
O
Oracle 10g: XML Fundamentals B-13
n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
Additional Information About Document
Type Definitions

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
U s
A I
O
l &
n a
te r
I n
c le
ra
O
Objectives

After completing this lesson, you should be able to do


the following:
• Include element, attribute, entity, and notation
declarations in a DTD
• Declare attribute with the following attribute types:
– CDATA
– Enumerated
– NOTATION
– ENTITY and ENTITIES
– NMTOKEN and NMTOKENS
– ID, IDREF, and IDREFS

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Objectives
U s
A I
This lesson focuses on the use of declarations in a document type definition (DTD) to
control the validity of XML documents. Apart from revisiting basic DTD declarations for

O
element, attributes, entities, and notations, this lesson discusses additional information about

&
each of the attribute types listed in the slide.

l
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals C-2
Declaration Types in a DTD (Revisited)

A DTD contains declarations (using the syntax shown)


for:
• Elements:
<!ELEMENT element-name content-model>

• Attributes:
<!ATTLIST element-name attrib-name type default>
• Entities:
<!ENTITY entity-name "replacement text">

• Notations:
<!NOTATION notation_name SYSTEM "text">

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Declaration Types in a DTD (Revisited)
U s
shows the generic syntax for declaring markup components:
A I
A DTD consists of declarations for markup and data used in an XML document. The slide

O
• Elements, which declare tag names and their content model. The content model for an

l &
element includes other nested markup (elements) and/or character data.
• Attributes, which declare the attribute names for the containing element, the type of
a
attribute (not a data type), and optional default values
n
te r
• Entities, which provide a notation for replacement text
• Notations, used in DTD attribute types to provide a way for identifying how parsed or

I n
unparsed data is encoded, typically used by applications that can process the data

c l e
Note: Notations are not frequently used in DTDs.
The next few pages of this lesson show examples of how to use each of these declarations,

r a
and finally how to use the DTD to validate the structure and content of an XML document.

O
Oracle 10g: XML Fundamentals C-3
Referencing the DTD (Revisited)

Reference the DTD in an XML document:


• After the XML declaration, before the root using:
<!DOCTYPE employees [ ... ]>
• Externally with the SYSTEM or PUBLIC keywords:
<!DOCTYPE employees SYSTEM "employees.dtd">

<!DOCTYPE employees PUBLIC "-//formal-public-ID">


• Internally in the <!DOCTYPE root [...]> entry:
<?xml version="1.0"?>
<!DOCTYPE employees [
<!ELEMENT employees (#PCDATA)>
]>
<employees>Employee Data</employees>
Note: Use the root element name after <!DOCTYPE.
n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Referencing the DTD (Revisited)
U s
<!DOCTYPE root-element-name [declarations]>
A I
The XML document references the DTD to be validated by using a specification syntax of:

O
The root element name must be used after the <!DOCTYPE keyword in the declaration. To

l &
use an external DTD, replace the [declarations] part with:
• The SYSTEM keyword and a quoted string containing a URL or a relative path to a file

a
with a .dtd extension, such as employees.dtd in the slide, or a URL such as
n
te r
http://www.myserver.com/dtd/employees.dtd
• The PUBLIC keyword and a quoted string containing a Formal Public Identifier (FPI).

I n
For example: To reference the public XHTML identifier, use:
<!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">

l e
The last example in the slide uses an internal DTD by placing declarations between the
following:
c
r a
• The <!DOCTYPE root-element [ characters

O
• The closing ]> characters
Note: The <!DOCTYPE name [...]> specification appears after the XML declaration
and before the root element of the XML document. Public identifiers are seldom used.
An XML document can reference both an external DTD and an internal DTD. The internal
DTD takes precedence if an element is defined both externally and internally.
Oracle 10g: XML Fundamentals C-4
Element Declarations (Revisited)

• Element declaration syntax:


<!ELEMENT element-name content-model>
• Four kinds of content models:
<!ELEMENT job EMPTY> <!-- Empty --> 1

<!–- Elements: single, ordered list, or choice -->


<!ELEMENT employees (employee)> 2
<!ELEMENT employee (employee_id,last_name,job_id)>
<!ELEMENT job_id (manager | worker)>

<!-- Mixed -->


3
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT hire_date (date| (day,month,year))>

<!ELEMENT employee_id ANY> <!-- Any -->

n l4
y
Copyright © 2004, Oracle. All rights reserved.

e O
Element Declarations (Revisited)
U s
A I
Elements are the basic building blocks of DTDs. An element declaration contains the
ELEMENT keyword, followed by the element name, and a content model. The content

O
model defines what can be contained in the element. The four kinds of content models are:

l &
1. Empty, which cannot contain subelements or text. However, an empty element may
hold attributes. Examples of empty element are:
a
<job/> <job></job> <job id="ST_CLERK"/>
n
te
- A single element name r
2. Child elements, (subelements) enclosed in brackets, can be:

I n
- A sequence of element names, containing an ordered comma-separated list of
names. For example: The <employee> element must contain an

l e
<employee_id> element appearing before the <last_name>, which must
c
appear before the <job_id>.

r a
- A choice of element names, which is a list of names separated by a vertical bar

O (|), each representing an alternative choice. For example:


The <job_id> element may contain either a <manager> element or a
<worker> element, but not both.
- A combination of single element and a list, for example:
<!ELEMENT person (full_name | (first_name, last_name))>
Oracle 10g: XML Fundamentals C-5
Element Declarations (Revisited) (continued)
3. Mixed: The simplest form of mixed content is a text-only element known as parsed
character data that uses the #PCDATA keyword enclosed in parentheses. When
content is specified as #PCDATA, the element can contain a text string, but not child
elements. For example, by using the last_name element declaration, the following
XML is valid:

<last_name>Vennapusa</last_name>

Another example of mixed content is shown in the hire_date element declaration,


which can contain either parsed character data, or a combination of date, month,
and year elements. The following XML elements will be valid for hire_date:

<hire_date><date>May 20, 1996<date></hire_date>


or
<hire_date>
<day>20</day
<month>May</month>
<year>1996</year>
</hire_date>

4. Any: This content model means that anything can be contained within the element and

y
is very loose. The ANY content model allows any element declared in the DTD to be
l
used, in any order, by using any number of occurrences. That is, the DTD does not
validate the element content. n
O
Note: You must avoid using ANY because it entirely defeats the purpose of using a
e
s
DTD for validating the structure and content of an XML document.

U
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals C-6
Cardinality of Elements (Revisited)

Cardinality symbols:
• Indicate the number of children permitted
• Appear as suffixes:
No symbol (default) Mandatory (one and only one)
? (question mark) Optional (zero or one)
* (asterisk) Zero or more (optional)
+ (plus sign) One or more (mandatory)
• Are placed after:
– An element name, or group in the content model
– The content model
Note: A group is formed by using parentheses.

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Cardinality of Elements (Revisited)
U s
A I
The content model indicates how to group subelements by using parentheses and how many
occurrences of each element is permitted, which is known as cardinality. Cardinality is
expressed by using the following symbols:
O
• Question mark (?): Indicates an optional or single-valued subelement (zero to one)
&
• Asterisk (*): Indicates an optional or multivalued subelement (zero to many)
l
a
• Plus sign (+): Indicates a mandatory or many-valued subelement (one to many)

n
te r
The symbols can appear, in the content model specification, as a suffix with the individual
elements, a group, and the entire content model. By default, the absence of a symbol means
that the element, group, or content model must appear at least once, that is, it is mandatory.
I n
Examples of using symbols are:

c l e
1. An <employees> element can contain zero or many <employee> elements:
<!ELEMENT employees (employee*)>

r a
2. A <department> must contain at least one or many <employee> elements:
<!ELEMENT department (employee+)>

O
3. An <employee> must have one <last_name> and <salary> element, and an
optional <commission_pct> element:
<!ELEMENT employee (last_name,salary,commission_pct?)>
4. The <jobs> must have one president, and optionally many managers or clerks:
<!ELEMENT jobs (president , (manager | clerk)*)>
Oracle 10g: XML Fundamentals C-7
Attribute Declarations (Revisited)

• Attribute declaration syntax:


<!ATTLIST element-name attrib-name type default>
• Requires:
– An element name
– An attribute name
– An attribute type, specified as:
CDATA, enumerated, ENTITY, ENTITIES, ID, IDREF,
IDREFS, NMTOKEN, NMTOKENS, and NOTATION
– An attribute default, specified as:
#IMPLIED, #REQUIRED, #FIXED, or a literal value
• Example:
<!ELEMENT employee (employee_id, last_name)>
<!ATTLIST employee manager_id CDATA #IMPLIED>

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Attributes Declarations (Revisited)
U s
A I
Attributes specify additional information about an element, and form a name-value pair
describing a particular property of the element. Declaring an attribute requires:

O
• An element name identifying the element to which the attribute belongs

l &
• An attribute name specifying the name of the attribute
• An attribute type set to one of the ten possible types of attributes listed in the slide.
a
An attribute default specifies a mandatory, or optional, value that uses:
n
te r
- #REQUIRED indicating that the attribute must be specified (mandatory)
- #IMPLIED indicating that the attribute is optional

I n
- #FIXED indicating that the attribute is a constant, that is, a supplied single value
- A literal value enclosed in quotes that is used as a default value, when no value is
l e
specified in the XML document
c
r a
An example of XML that uses the manager_id attribute declaration is:
<employee manager_id=“100”>
O <employee_id>101</employee_id>
<last_name>Kochhar</last_name>
</employee>

Oracle 10g: XML Fundamentals C-8


CDATA and Enumerated Attribute Types

• CDATA: For character data values


<!ELEMENT employee (employee_id, last_name)>
<!ATTLIST employee manager_id CDATA #IMPLIED>

<employee manager_id="102"> <!-- XML -->


<employee_id>104</employee_id>
<last_name>Ernst</last_name>
</employee>
• Enumerated: For a choice from a list of values
<!ELEMENT employee (employee_id, last_name)>
<!ATTLIST employee gender (male|female) #IMPLIED>
<employee gender="male"> <!-- XML -->
<employee_id>104</employee_id>
<last_name>Ernst</last_name>
</employee>

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
CDATA and Enumerated Attribute Types
U s
The CDATA Attribute Type

A I
The CDATA type indicates that the attribute value can be set to any character data. Most
O
attributes use the CDATA type. The example in the slide declares manager_id as an

l &
attribute of the employee element. The manager_id attribute may have any character
data as its value. The sample XML code, in the slide, under the manager_id attribute

n a
declaration shows an example of using the attribute.

te
The Enumerated Attribute Type r
I n
The Enumerated type provides a choice from a list of allowable values for the attribute. The
choices of values in the list:

l e
• Are enclosed in brackets
c
• Are separated by the vertical bar (|) character (spaces are optional)

r a
• Can be case-sensitive strings (without quotes), or numeric values

O
For example, the employee element is given a gender attribute with male or female
as its possible values. In the slide, the XML code under the gender attribute declaration
shows an example of using the attribute.

Oracle 10g: XML Fundamentals C-9


The ENTITY and ENTITIES Attribute Types

• ENTITY: For a string value representing the name


of an unparsed entity
<!ELEMENT employee (#PCDATA)>
<!ATTLIST employee resume ENTITY #IMPLIED>
<!–- Requires an unparsed entity declaration, made
by using these ENTITY & NOTATION declarations -->
<!ENTITY resume-data SYSTEM "emp.txt" NDATA text>
<!NOTATION text SYSTEM "textual data">
– In ENTITY declaration, NDATA marks it as unparsed.
– The NDATA requires a NOTATION declaration.
• In XML, the attribute value uses the entity name
without the ampersand (&) and semicolon (;).
<employee resume="resume-data">
Bruce Ernst
</employee>

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
The ENTITY and ENTITIES Attribute Types
U s
The ENTITY Attribute Type

A I
The ENTITY type indicates that the attribute value will be a string name that is declared as
O
an unparsed entity in the DTD. Defining a general entity is done by using the following
declaration:
l &
<!ENTITY name "replacement text">

n a
However, for the entity name to be used as an attribute value, it must be declared as an

te r
external unparsed entity by using:
<!ENTITY name SYSTEM "sometext" NDATA notation-name>

I n
The two items that mark the entity as an external unparsed entity are:

l e
• The SYSTEM keyword identifying the entity as an external one

c
• The NDATA notation-name indicating that the data is unparsed. However, the

r a
notation-name specified after the NDATA keyword requires a corresponding DTD

Onotation declaration, such as:


<!NOTATION notation-name SYSTEM "sometext">
The XML code example, in the slide, shows the entity name resume-data used as the
value for the resume attribute, without the ampersand (&) escape character and a
semicolon (;) terminator.
Oracle 10g: XML Fundamentals C-10
The ENTITY and ENTITIES Attribute Types (continued)
The ENTITIES Attribute Type
The ENTITIES type is an extension of the ENTITY type that:
• Allows the attribute value to be a space-separated list of external unparsed entity
names
• Is declared, as in the following example:
<!ATTLIST employee resume ENTITIES #IMPLIED>
• Requires an entity declaration for each entity name used in the attribute value list of
the XML document
For example: A DTD, in a file called employee.dtd, shows the declaring of the resume
attribute by using the ENTITIES type:
<!ELEMENT employee (#PCDATA)>
<!ATTLIST employee resume ENTITIES #IMPLIED>
<!–- Required ENTITY declarations -->
<!ENTITY resume-data SYSTEM "emp.txt" NDATA text>
<!ENTITY resume-image SYSTEM "emp.jpg" NDATA jpeg>
<!-- Required NOTATION declarations -->
<!NOTATION text SYSTEM "text/plain">
<!NOTATION jpeg SYSTEM "image/jpeg">

An example of some XML code that uses the resume attribute can be written as:
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM "employee.dtd">
n l y
<employee resume="resume-data resume-image">
Bruce Ernst
e O
</employee>

U s
Note: The resume attribute, in the XML document, contains a space-separated list of

I
unescaped entity names, each of which must also be declared in the DTD.
A
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals C-11
The ID Attribute Type

• ID: For uniquely identifying an element instance


<!ELEMENT employees (employee*)>
<!ELEMENT employee (last_name)>
<!ATTLIST employee employee_id ID #REQUIRED>
• The ID type:
– Can be given to only one attribute per element
– Must have a different value for all its elements
– Must be a valid XML name (not start with a numeric)
<employees>
<employee employee_id="_103">
<last_name>Hunold</last_name>
</employee>
<employee employee_id="_104">
<last_name>Ernst</last_name>
</employee>
</employees>

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
The ID Attribute Type
U s
A I
The ID type is used to uniquely identify specific elements in an XML document. This is
useful if you want to locate a specific element by its ID value. Using the ID type requires
that:
O
• An element has only one ID attribute

l &
• All elements have a different (unique) value for the ID attribute in the entire document
a
• The value must be a valid XML name. That is, it must start with an alphabetic or
n
te r
underscore character, and not a numeric character.
The XML document, in the example in the slide, uniquely identifies each employee

I n
element by using the employee_id attribute whose values are prefixed with an
underscore (_).

c l e
Note: Using the #REQUIRED attribute default is not mandatory, but it is recommended for

r a
attributes that are declared with the ID attribute type.

O
Oracle 10g: XML Fundamentals C-12
The IDREF and IDREFS Attribute Types

IDREF and IDREFS: Reference one or more ID type


attribute values (respectively) in some element
<!ELEMENT employees (employee*)>
<!ELEMENT employee (last_name)>
<!ATTLIST employee employee_id ID #REQUIRED>
<!ATTLIST employee manager_id IDREF #IMPLIED>
<!ATTLIST employee mgrs IDREFS #IMPLIED>
<employees>
<employee employee_id="_102">
<last_name>De Haan</last_name>
</employee>
<employee employee_id="_103" manager_id="_102">
<last_name>Hunold</last_name>
</employee>
<employee employee_id="_104" mgrs="_102 _103">
<last_name>Ernst</last_name>
</employee>
</employees>

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
The IDREF and IDREFS Attribute Types
U s
with a unique ID attribute. Declare the attribute type as:
A I
The IDREF and IDREFS types allow other elements to reference one or more elements

O
• An IDREF type to reference a single attribute value of an element with an ID attribute

The DTD, in the slide, declares:


l &
• An IDREFS type to reference multiple ID attribute values

n a
• The manager_id attribute as an IDREF type

The XML example shows:


te r
• The mgrs attribute as an IDREFS type

I n
• The employee element with its ID type value _103 (in employee_id) references

l e
the employee element with a _102 value

c
• The employee with ID value _104 references, as a space-separated list, the two

r a
employee elements:
- With employee_id value _102
O - With employee_id value of _103

Oracle 10g: XML Fundamentals C-13


The NMTOKEN and NMTOKENS Attribute
Types

NMTOKEN and NMTOKENS: Represent one or more XML


name tokens without spaces
<!ELEMENT employees (employee*)>
<!ELEMENT employee (last_name)>
<!ELEMENT last_name (#PCDATA)>
<!ATTLIST employee hire_date NMTOKEN #IMPLIED>
<!ATTLIST employee email NMTOKENS #IMPLIED>
<employees>
<employee hire_date="13-JAN-1993">
<last_name>De Haan</last_name>
</employee>
<employee email="AHUNOLD Alexander.Hunold">
<last_name>Hunold</last_name>
</employee>
</employees>

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
The NMTOKEN and NMTOKENS Attribute Types
U s
The NMTOKEN Attribute Type

A I
The NMTOKEN types represents a name token, whose value is like an XML name, that is, it
can contain character values including:
O
• Letters (A-Z, a-z)
• Numbers (0-9)
l &
• Underscores (_)
n a
• Colons (:)
• Hyphens (-)
te r
• Periods (.)
I n
l e
Note: However, an NMTOKEN value cannot contain any white space characters.

c
The NMTOKENS Attribute Type

r a
Using the NMTOKENS type allows a space-separated list of NMTOKEN attribute values.

O
The example in the slide shows the hire_date attribute declared as an NMTOKEN type,
and the email attribute as an NMTOKENS type.

Oracle 10g: XML Fundamentals C-14


Entities in XML (Revisited)

• Entity types predefined by XML standards:


– Built-in entities
– Character entities
• Entities in the DTD can be declared as:
– General entities <!ENTITY name "text">
– Parameter entities <!ENTITY % name "text">
• Reference:
– Built-in, character, and general entities in an XML
document or the DTD by using:
&entity-name;
– Parameter entities in the DTD by using:
%entity-name;

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Entities in XML (Revisited)
U s
entities include:
A I
In XML, according to the World Wide Web Consortium (W3C) standard, predefined

O
• The built-in entities, which represent these special characters:
- The less than sign (<), represented by &lt;

l &
- The greater than sign (>), represented by &gt;
- The ampersand (&), represented by &amp;

n a
- The single quote or apostrophe ('), represented by &apos;

te r
- The double quotation mark ("), represented by &quot;
• The character entities, which represent any Unicode character specified in the XML

I n
recommendation, which is based on Unicode 3.0. Character entities are formed by
using an ampersand (&), followed by a hash (#), then a Unicode character number, and

l e
terminated by a semicolon (;). For example, &#169 denotes the copyright symbol
c
(©). The character number can be a decimal or a hexadecimal Unicode value.

r a
Hexadecimal values are prefixed with an x. For example, &#xA9 represents the

O copyright symbol (©).


An entity reference to a parameter entity is done in a DTD. However, references to built-in,
character, and general entities can be done in the XML document and a DTD, for example:
<Company>&quot;Oracle Corporation&quot;</Company> instead of
<Company>"Oracle Corporation"</Company>
Oracle 10g: XML Fundamentals C-15
Summary

In this lesson, you should have learned how to:


• Declare elements, attributes, entities, and
notations in a DTD
• Use additional attribute types provided by DTD
specifications
• Reference the DTD in an XML document
• Reference entity declarations

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Summary
U s
A I
A document type definition (DTD) establishes the grammar for a valid structure of an XML
document. When an XML document follows the rules declared in the DTD, the XML

O
document is said to be “valid.” The DTD can be thought of as an agreement that will be

l &
conformed to by applications generating or receiving the XML documents.
This lesson provided additional information about the various attribute types available in

n
<!ATTRLIST> declarations, such as: a


CDATA and Enumerated type
ID, IDREF and IDREFS
te r

I n
ENTITY and ENTITY

c
NOTATION
l e
NMTOKEN and NMTOKENS

r a
This material revised and supported the basic information about DTD covered in the lessons.

O
Oracle 10g: XML Fundamentals C-16
Using Oracle XML Developer’s Kit

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
U s
A I
O
l &
n a
te r
I n
c le
ra
O
Objectives

After completing this lesson, you should be able to do


the following:
• Describe XDK
• List the XDK components
• Describe class generator
• Explain TransX Utility

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Objectives
U s
Note: For more information about these topics, refer to:
A I
In this lesson, you learn about XDK and the benefits of XDK in Oracle Database10g.

• XML API Reference–XDK and Oracle XML DB


O
• XML Developer’s Kits Guide–XDK
l &
• XML Database Developer’s Guide–Oracle XML DB

n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-2
Overview of XML Developer’s Kit

Oracle XML Developer’s Kit (Oracle XDK):


• Is designed for integrating XML data into
applications
• Can read and write data formatted as XML to and
from the database
• Is a powerful API for developing e-commerce
applications

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Overview of XML Developer’s Kit
U s
A I
The ability to read and write XML data to and from databases and to integrate this data into
existing applications is a key requirement for any e-commerce application. XDK is designed

O
for building database applications that can read and write data formatted as XML just as

l &
easily as reading or writing data in any other form.
XDK is a set of commonly used building blocks or utilities for development and run-time

n a
support. Oracle XDK contains the basic building blocks for reading, manipulating,

te r
transforming, and viewing XML documents. To provide a broad variety of deployment
options, Oracle XDKs are available for Java, Java Beans, C, C++, and PL/SQL. Oracle

I n
XDKs consist of XML Parsers, an XSLT Processor, XML Schema Processor, XML Class

c l e
Generator, XML transviewer JavaBeans, XML SQL Utility, SOAP, TransX Utility, XML
compressor and XSQL servlet.

r a
O
Oracle 10g: XML Fundamentals D-3
XML Trees

XML data is highly structured in a tree form. Parsing


an XML document makes it possible to operate on the
tree structure of the data to:
• Traverse this tree programmatically
• Access the components of the document in
random order
• Rearrange or remove the components of the
document
• Directly access data

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XML Trees
U s
A I
XML documents are hierarchical in structure, making it easy to store them as trees. After
you parse XML documents into tree structures, you can operate on the tree. This yields a

O
great deal of flexibility in dealing with the documents. You can access the components of

l &
the document in random order, rearrange them, and add or remove them. This is especially
appropriate for applications in which the flow of processing is based on external logic, as
a
opposed to the order and occurrence of elements within the XML document. Storing the
n
te r
document as a tree enables random access to its data and structure, instead of having the
processing governed by when and where the tags and elements occur.

I n
However, tree-based methodologies do have some drawbacks. They require parsing the

c l e
entire XML document and creating the tree data structure before the processing and business
logic take place. Because the tree data structure is generally stored in memory, these

a
methods have much larger memory footprints than stream-based methods.
r
O
Oracle 10g: XML Fundamentals D-4
XML Trees: Example

<?xml version="1.0"?> employees


<employees>
<employee> employee
<id>100</id>
<last_name>King</last_name> id
<job>AD_PRES</job> last_name
<salary>24000</salary>
</employee> job
<employee>
<id>101</id> salary
<last_name>Kochhar</last_name>
<job>AD_VP</job> employee
<salary>17000</salary>
id
</employee>
</employees> :

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XML Trees: Example
U s
A I
To understand the tree concept of XML, look at the simple XML example, shown in the
slide, which has the file name employee.xml. The root object of this data set is named

O
employeelist, and it represents a group of employees. Each employee in this portfolio is

&
essentially a record. Each record contains fields for id, last_name, and so on.

l
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-5
XML Developer’s Kit Components

Oracle XDK consists of:


• XML Parsers
• XSLT Processor
• XML Schema Processor
• XML Class Generator
• XML Transviewer JavaBeans
• XML SQL Utility
• XSQL Servlet
• SOAP
• The TransX Utility
• XML Compressor

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XML Developer’s Kit Components
U s
A I
The XDK components are included with a variety of Oracle products, including Oracle8i,
Oracle9i, and Oracle Database 10g, Oracle9i Application Server, Oracle Application Server

O
10g, Oracle Developer’s Suite, and Oracle JDeveloper. The Java XDKs can be used with
JDK 1.1.6 or later. The C and C++ XDKs include the appropriate dependent CORE and,

l &
national language support (NLS) libraries. In earlier releases, the PL/SQL XDK required the

a
Java parser, but starting with Oracle9i, it is built on the C parser.
n
te r
Oracle XDK provides support for the following W3C standard components:
• XML 1.0: The original XML Recommendation

I n
• DOM 1.0: Document Object Model
• SAX 1.0: Simple API for XML
l e
• XML Namespaces 1.0
c
r a
• XSLT 1.0 and XPath 1.0, including some features from XSLT 2.0 and XPath 2.0.
• XML Schema 1.0
O
• DOM 2.0 and SAX 2.0
• SOAP support SOAP 1.1
• XML Parser also supports JAXP 1.1
• XML compressor is integrated with XML Parser

Oracle 10g: XML Fundamentals D-6


XML Developer's Kit Components (continued)
Oracle XDK components are designed for flexible deployment to optimize performance and
scalability. XDK for Java can be loaded into the Oracle Database, run on the middle tier in
iAS or on the client. XDKs for C and C++, are linked into the kernel and are available as
stand-alone libraries. XDK for PL/SQL can be used through either the database or iAS
through mod_plsql. This architectural flexibility, along with XML’s independence from
specific protocols, gives application developers powerful functionality.
Support for Multiple Languages and Platforms
XML components have been supplied in language specific packages. The Java components
are platform-independent, having a minimum JDK 1.1.6 requirement whereas JavaBeans
XDK additionally requires Swing. The PL/SQL XDK was previously built on the Java XDK
but uses the C XDK in Oracle Database 10g. The platform-dependent C and C++ XDKs are
being ported to all Oracle Database 10g platforms. Oracle XDK components are available in
Java, C, C++, and PL/SQL.
Scalability and Performance
Oracle XDKs focus on providing a conformant set of components, and also on delivering
enterprise-level performance and scalability. This requirement is important because of the
need to deliver on-demand rendering of content in XML, as well as providing client-server
performance, which in many cases was the previous application architecture.
To this end, numerous strategies are incorporated, including caching content on the middle

l y
tier, building into XSL processor the ability to cache stylesheets, and output (DOM) objects

n
ready to be transformed directly from SQL queries. The XSQL servlet includes integrated
load balancing and connection pooling to enhance scalability.

e O
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-7
XML Parsers

XML Parsers are the components that provide


programmatic access to XML documents.

Parsed XML

Original XML DOM/SAX parser XSLT Processor


document
Parsed XSL Transformed
commands document

XSL XML Parser for Java


stylesheet

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XML Parsers
U s
A I
The foundation component of any XML development is the XML Parser. The parser
provides programmatic access to XML documents, which you can use in many ways within

O
an application. Oracle’s parsers support the two industry-standard specifications for XML

l &
document access: DOM and SAX. The parsers support W3C standard DOM 2.0 CORE
specifications. They also support SAX 2.0, which adds namespace support. Oracle XML
a
Parser is implemented in C, C++, PL/SQL, and Java.
n
te r
This slide shows the original XML document and XSL stylesheet going through the XML
Parser for Java. It passes through the DOM/SAX Parser, and sends the parsed XSL

I n
commands and the parsed XML to the XSLT Processor, which uses the XSL stylesheet to

c l e
transform the original XML document into the transformed XML document.

r a
O
Oracle 10g: XML Fundamentals D-8
XSL Transformation (XSLT) Processors

Transform one XML document to another through


stylesheets. XSL processor object methods:
• removeParam()
• resetParam()
java.io.Reader • setParam()
java.io.inputStream • setBaseURL()
XMLDocument • setEntityResolver()
java.net.URL • setLocale()

XSL
XSL processor
XSL stylesheet
XML object

Create an XML Write to an Report as


document object. output stream. SAX events.

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XSL Transformation (XSLT) Processors
U s
A I
Extensible Stylesheet Language (XSL) is the XML-based technology responsible for
transforming or formatting an input XML document into a different output one or rendered

O
into virtually any text-based output. Integrated into all Oracle XML Parsers are XSLT

l &
Processors that perform these transformations. Oracle XSL processors are available in Java,
C, C++, and PL/SQL. The Java processor supports the W3C XSL Transformations 1.1
a
specification, adding support for standard extension functions in Java and ECMAScript
n
(JavaScript).

te r
The Java processor supports the W3C XSL transformations 1.1 specification with support

I n
for standard extension functions in Java and JavaScript.

c l e
r a
O
Oracle 10g: XML Fundamentals D-9
XSL Transformation (XSLT) Processors (continued)
Steps for XSLT transformation
1. Create an XSLT Processor object and then use methods from the following list in your
Java code. Some of the available methods are:
removeParam(): To remove a parameter
resetParam(): To remove all parameters
setParam(): To set parameters for the transformation
setBaseURL(): To set a base URL for any relative references in the stylesheet
setEntityResolver(): To set an entity resolver for any relative references in
the stylesheet
setLocale(): To set a locale for error reporting
2. Use one of the following input parameters to the XSLT
Processor.newXSLstylesheet() function to create a stylesheet object:
java.io.Reader
java.io.InputStream
XMLDocument
java.net.URL
3. This creates a stylesheet object, which is thread-safe and can be used in multiple XSL
Processors.
4. Use one of the input parameters on the XML input.
5. Your XML inputs and the stylesheet object are input (each using one of the input
parameters listed above) to the XSL Processor:
XSLProcessor.processXSL(xslstylesheet, xml instance)
6. The results can be one of the following:
n l y
create an XML document object
write to an output stream
e O
report as SAX events
U s
A I
O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-10
Oracle XML Schema Processors

Oracle XML Schema Processor:


• Validates the use of data types within XML
documents
• Provides supports for:
– The full W3C XML Schema specifications
– Simple data types and structures within XML
documents
– XML Schema definition files (.xsd)
– Streaming (SAX) processing, with constant memory
usage, and linear processing time
• Is built on Oracle XML Parser for Java v2

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Oracle XML Schema Processors
U s
A I
Oracle XML Schema Processor validates XML documents against their schema definitions,
adding default values where necessary, and, when used with the XML Parser, gives

O
programmatic access for use in applications. Oracle XML Schema Processors are available

l &
for Java, C, and C++. The processors fully support W3C XML Schema Recommendation
1.0. Oracle XML Schema processors support both Lax and Strict Mode Validation Process.

n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-11
Using XML Schema Processors

DOM Schema DOM


Schema
parser DOM tree parser

XML Schema
document

SAX Schema DOM builder


parser SAX validator SAX + PSV or application
+ Default
Instance value
document

Error messages

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Using XML Schema Processors
U s
Oracle’s XML Schema Processor performs two major tasks:
• A builder assembles schema from schema XML documents
A I
O
• A validator uses the schema to validate the instance document

l &
When building the schema, the builder first calls the DOM parser to parse the schema XML
documents into corresponding DOM trees. It then compiles them into an internal schema

n a
object. The validator works as a filter between the SAX parser and your applications for the

te r
instance document. The validator takes SAX events of the instance document as input and
validates them against the schema. If the validator detects any invalid XML component,

I n
then it sends an error message. The output of the validator is:

c l e
• Input SAX events
• Default values it supplies

a
• Post-Schema Validation (PSV) information
r
O
Oracle 10g: XML Fundamentals D-12
XML Class Generators

Oracle XML Class Generator:


• Creates XML class files that are used to construct
XML documents with data type support
• For Java, creates Java source files from an XML
DTD or XML Schema definition
• Supports XML Schema definition files (.xsd) and
DTDs
• Are based on the DOM APIs in Oracle XML Parser
V2

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XML Class Generators
U s
A I
XML Class Generators in C++ and Java are utilities that create C++ and Java classes from a
DTD. In Oracle Database 10g, XML Schema definitions (.xsd) can also be used. The

O
generated classes enable programmatic construction of XML documents. The class

l &
generator works in conjunction with the Oracle XML Parser, and provides complete
character set support and optional validation mode for ease of debugging. Documents
a
created by these classes are fully compliant with the XML 1.0 W3C standard. Classes can be
n
used:

te r
• When an application wants to send an XML message to another application based on

I n
agreed-upon DTDs or XML Schemas.
• As the code that handles data from a Web form used to construct an XML document.

c l e
Oracle XML Class Generator for Java:

r a
• Generates a Java class for each top-level (global) element, such as simpleType and
complexType elements.
O
• Maintains the type hierarchy among the elements in the generated Java classes. For
example, if a complexType element extends another complexType element, then
the class corresponding to them extends the base type complexType element.
Otherwise, they extend the CGSXDElement class.

Oracle 10g: XML Fundamentals D-13


Using XML Class Generators

XML Parser for Java

DTD or XML Parsed


Schema DTD or
XML
Schema

XML Class Generator Java


for Java application

Valid XML
document
based on
Java classes based on DTD or XML
DTD or XML Schema Schema
(one class per element)

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Using XML Class Generators
U s
A I
The generated classes can be used to programmatically construct XML documents. XML
Class Generator for Java also optionally generates javadoc comments on the generated

O
source files. XML Class Generator for Java requires the XML Parser for Java and the XML

l &
Schema Processor for Java. It works in conjunction with XML Parser for Java, which parses
the DTD (or XML Schema) and sends the parsed XML document to the class generator.
a
XML Class Generator for Java consists of the following two class generators:
n
• DTD class generator

te
• XML Schema class generator r
I n
These can both be invoked from the oracg command-line utility. The oracg command

c l
input arguments. e
line utility is used to invoke the DTD or Schema class generator for Java, depending on the


• a
dtd [-root]: The input file is a DTD file or DTD-based XML file.
r
Schema: The input file is a Schema file or Schema-based XML file.
O
Oracle 10g: XML Fundamentals D-14
Oracle XDK for JavaBeans

• A set of XML visual and nonvisual JavaBeans for


building XML-enabled applications
• Transviewer Beans:
– DOMBuilder Bean
– XSLTransformer Bean
– Treeviewer Bean
– XMLSourceView Bean
– XMLTransformPanel Bean
– DBViewer Bean
– DBAccess Bean
– XMLDiff Bean

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Oracle XDK for JavaBeans
U s
A I
Oracle XDK for JavaBeans is a set of visual and nonvisual beans that are useful in creating a
variety of XML-enabled Java applications or applets. The bean encapsulation includes

O
documentation and descriptors that may be accessed directly from Java integrated

l &
development environments such as JDeveloper. The visual beans extend JPanel and thus are
dependent on Swing. The following instruction describes the functionality of the Beans in
a
Oracle Database 10g. XDK for JavaBeans is composed of the following component:
n
XML Transviewer JavaBeans

te r
Oracle XML Transviewer JavaBeans are a set of XML components that constitute XML for
I n
JavaBeans. These are used for Java applications or applets to view and transform XML

l e
documents. They are visual and nonvisual Java components that are integrated into Oracle

c
JDeveloper to enable the fast creation and deployment of XML-based database applications.

r a
The XML Transviewer beans include:

O
DOM Builder Bean: This wraps the Java XML (DOM) parser with a bean interface,
allowing multiple files to be parsed at once (asynchronous parsing). By registering a listener,
a Java application can parse large or successive documents as a background task while
returning control of the application to the caller.

Oracle 10g: XML Fundamentals D-15


Oracle XDK for JavaBeans (continued)
XML Transviewer JavaBeans (continued)
XML Source Viewer Bean: This bean extends JPanel by enabling the viewing of XML
documents. It improves the viewing of XML and XSL files by color-highlighting XML and
XSL syntax. This is useful when modifying an XML document with an editing application.
It is easily integrated with the DOM Builder Bean, and enables preparsing and postparsing,
and validation against a specified DTD.
XML Tree Viewer Bean: This bean extends JPanel by enabling the viewing of XML
documents in the tree form along with the ability to expand and collapse XML Parsers. It
displays a visual DOM view of an XML document, enabling users to easily manipulate the
tree with a mouse to hide or view selected branches.
XSL Transformer Bean: This wraps the XSLT Processor with a bean interface and
performs XSL Transformations on an XML document based on an XSL stylesheet. It
enables users to transform an XML document to almost any text-based format including
XML, HTML and DDL, by applying an XSL stylesheet. When integrated with other beans,
this bean enables an application or user to view the results of transformations immediately.
This bean can also be used as the basis of a serverside application or servlet to render an
XML document, such as an XML representation of a query result, into HTML for display in
a browser.
XML TransPanel Bean: This bean uses the other beans to create a sample application that
can process XML files. This bean includes a file interface to load XML documents and XSL
stylesheets. It uses the beans as follows:
• Visual beans to view and edit files n l y
O
• Transformer bean to apply the stylesheet to the XML document and view the output
e
text documents.
U s
DBAccess Bean: DBAccess bean maintains CLOB tables that contain multiple XML and

A I
DBViewer Bean: DBViewer bean is a JavaBean that displays database queries or any XML
by applying XSL stylesheets and visualizing the resulting HTML in a scrollable Swing
O
panel. DBViewer bean has XML and XSL tree buffers as well as a result buffer. DBViewer
bean allows the calling program to:
l &
• Load or save buffers from various sources such as from CLOB tables in an Oracle
a
database or from the file system. Control can be also used to move file between the file
n
te r
system and the user schema in the database.
• Apply stylesheet transformations to the XML buffer by using the stylesheet in the XSL
buffer
I n
c l e
The result can be stored in the result buffer. The XML and XSL buffer content can be shown
as a source or tree structure. The result buffer content can be rendered as HTML and also

r a
shown as source or tree structure. The XML buffer can be loaded from a database query.

O
XMLDiff Bean: The XMLDiff Bean performs a tree comparison on two XML DOM trees.
It displays the two XML trees and shows the differences between the XML trees.

Oracle 10g: XML Fundamentals D-16


XML SQL Utility

XML SQL Utility (XSU):


• Generates XML results from SQL queries
• Generates XML Schema definition files (.xsd)
• Transforms data retrieved from object-relational
database tables or views into XML
• Inserts data from an XML document in canonical
form into related columns of a table or view
• Can update and delete rows based on the XML
data in canonical form by using the API

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XML SQL Utility
U s
A I
XML SQL Utility (XSU) has been created to support reading and writing XML documents
to the database by using SQL statements. In Oracle Database 10g, XSU supports generating

O
XML Schema definition files (.xsd) and DTDs from database schemas. These data models

l &
can be input into the XML Class Generator for the generated class to use in an application
for creating conforming XML documents.This ensures that documents can be decomposed
a
and inserted into a suitable database schema.
n
The XSU can be run from:

te r
• The Java-enabled Oracle Database 10g allows the XSU class library to be loaded

I n
enabling Java applications in the database to directly access the XSU Java API. An
Oracle database contains PL/SQL wrappers publishing the XSU Java API enabling:
l e
- PL/SQL applications to access the XSU through its PL/SQL API wrappers
c
r a
- SQL statements to access XSU functionality through the PL/SQL API
• The Application Server or Java-enabled middle-tier services can run Java applications

O that obtain the data from the database by using Java Database Connectivity (JDBC).
The data can be converted by XSU into XML and then sent to other Web servers or
systems. Using the JDBC sources, the applications can store the data in database tables
by using the XSU. Oracle XSQL servlet is built on top of the Oracle XSU and provides
a declarative interface to the XSU functionality.
Oracle 10g: XML Fundamentals D-17
Applications of XSU

• Generate XML documents from any SQL query.


• Dynamically generate DTDs.
• Dynamically generate XML Schemas.
• Generate XML documents in their string or DOM
representations.
• Insert XML into database tables or views.
• Update or delete records.
• Easily generate complex nested XML documents.
• Create object views over flat tables and query over
these views.

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Applications of XSU
U s
of the data types in Oracle Database 10g.
A I
• Generate XML Schema and XML documents from a SQL Query. XSU supports most

O
• Dynamically generate document type definition (DTD) files. XSU performs simple
transformations, such as modifying default tag names for the ROW element. XSL

l &
transformations can be applied to the generated XML documents, as needed.

a
• Generate XML documents in their string or DOM representations
n
te r
• Insert XML into database tables or views. XSU can also update or delete records from
a database object, given an XML document.

I n
• Easily generate complex nested XML documents. XSU can also store them in
relational tables by creating object views over the flat tables and querying over these
views.
c l e
• Generate XML as a stream of SAX2 callbacks

r a
• Support XML attributes during generation, to specify that a particular column or

Ocolumns must be mapped to an XML attribute instead of an XML element


• Support SQL identifier to XML identifier escaping for column names that are not valid
XML tag names

Oracle 10g: XML Fundamentals D-18


XSU Command-Line Utility

XSU provides a command-line front end, for access to


XML generation and insertion operations. XSU
command-line utility:
• Is invoked by using the syntax:
java OracleXML command options
• Requires the CLASSPATH, which includes:
– Oracle XML Parser Java library (xmlparserv2.jar)
– JDBC library (classes12.jar if using xsu12.jar
or classes111.jar if using xsu111.jar)
– A JAR file for XMLType (xdb.jar)

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
XSU Command-Line Utility
U s
A I
XSU comes with a simple command-line front end which gives you quick access to XML
generation and insertion. The XSU command-line options are provided through the Java
class, OracleXML. Invoke it by typing:
O
java OracleXML

l &
This prints the front-end usage information. To run the XSU command line front end, first
a
specify where the executable is located. Add the XSU Java library (xsu12.jar or
n
xsu111.jar)to your CLASSPATH.

te r
Also, because XSU depends on Oracle XML Parser and JDBC drivers, make the location of

I n
these components known. To do this, the CLASSPATH must include the locations of:

c l e
• Oracle XML Parser Java library (xmlparserv2.jar)
• JDBC library (classes12.jar if using xsu12.jar or classes111.jar if

r a
using xsu111.jar)
• A JAR file for XMLType (the xdb.jar file located in the
O ORACLE_HOME/rdbms/jlib directory)

Oracle 10g: XML Fundamentals D-19


Using the OracleXML Utility

The OracleXML command can be used for:


• Generating XML from a query with getXML:
java OracleXML getXML -user "HR/HR"
"SELECT * FROM EMPLOYEES WHERE department_id=10"
– Add the –withSchema option to generate an XML
Schema for the query.
– Suppress XML data with a condition that returns no
rows.
• Inserting an XML document by using putXML with
the –fileName option to identify the XML
document:
java OracleXML putXML -user "HR/HR"
-fileName "code_apd_21_newdept.xml" departments

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Using the OracleXML Utility
U s
A I
The XSU getXML command-line option generates XML content by using the SELECT
statement. The first example in the slide generates an XML document by querying the

O
employees table in the HR schema. The result is a canonical XML document:
<?xml version = '1.0'?>
<ROWSET>
l &
<ROW num="1">

n a
te r
<EMPLOYEE_ID>200</EMPLOYEE_ID>
<FIRST_NAME>Jennifer</FIRST_NAME>
<LAST_NAME>Whalen</LAST_NAME>

I n
<EMAIL>JWHALEN</EMAIL>

c le <PHONE_NUMBER>515.123.4444</PHONE_NUMBER>
<HIRE_DATE>9/17/1987 0:0:0</HIRE_DATE>

r a <JOB_ID>AD_ASST</JOB_ID>
<SALARY>4400</SALARY>

O <MANAGER_ID>101</MANAGER_ID>
<DEPARTMENT_ID>10</DEPARTMENT_ID>
</ROW>
</ROWSET>

Oracle 10g: XML Fundamentals D-20


Using the OracleXML Utility (continued)
The –withSchema command-line option generates an XML Schema document in addition
to the XML. Suppress the XML output with a condition that does not return any rows.
Note: The command-line text must appear in one line.
Insert XML data into a database table by using the putXML option of the XSU command-
line utility. The second example in the slide insert the contents of the XML document
code_apd_21_newdept.xml into the DEPARTMENTS table in the HR schema. XSU
accomplishes this task by using the following steps:
1. Connecting to the current database
2. Reading the XML document from the given file
3. Parsing the document to ensure that it matches the XML tag name with column names
4. Inserting new rows with values in their appropriate columns in the target table
If the XML file code_apd_21_newdept.xml contains the following data:
<?xml version = '1.0'?>
<ROWSET>
<ROW num="1">
<DEPARTMENT_ID>290</DEPARTMENT_ID>
<DEPARTMENT_NAME>IT Security</DEPARTMENT_NAME>
<MANAGER_ID>201</MANAGER_ID>
<LOCATION_ID>1800</LOCATION_ID>
</ROW>
</ROWSET>
XSU generates the following INSERT statement.
n l y
INSERT INTO DEPARTMENTS (DEPARTMENT_ID,
DEPARTTMENT_NAME, MANAGER_ID, LOCATION_ID)
e O
VALUES (?,?,?,?)

U s
XSU parses the XML document, and for each record, it binds the XML element text values

following INSERT statement:


A I
to the appropriate columns. For the example XML document, it effectively executes the

O
INSERT INTO DEPARTMENTS (DEPARTMENT_ID,

l &
DEPARTTMENT_NAME, MANAGER_ID, LOCATION_ID)
VALUES (290,'IT Security',201, 1800)
Note
n a
te r
• Use the –conn option to select the JDBC driver and specify the JDBC connect string,
for example, by using the thin JDBC driver:

• I n
-conn "jdbc:oracle:thin:@localhost:1521:ORCL"
Insert processing can be optimized to insert in batches by using the –batchSize
l e
size option, and commit in batches by using the –commitBatch size option.
c
r a
O
Oracle 10g: XML Fundamentals D-21
Updating and Deleting by Using XSU API

Rows can be updated and deleted by using the XSU


programmatic interface API, and not by using the
command-line utility.
• For updating, supply element and values for:
– The key columns in the target table
– The columns to be updated
• For deleting, supply element value for the key
columns
Note: A single XML element can affect zero or more
rows in an UPDATE or DELETE operation if the key
columns are not supplied.

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Updating and Deleting by Using XSU API
U s
A I
Updates and deletes differ from inserts in that they can affect more than one row in the
database table. An XML element can match more than one row if the matching columns are
not key columns in the table.
O
l &
For updates, provide a list of key columns which XSU needs to identify the row to update.
For example, to update the DEPARTMENT_NAME column to Transport instead of

n a
Transportation, the following XML document can be used:
<ROWSET>
<ROW num="1">
te r
I n
<DEPARTMENT_ID>280</DEPARTMENT_ID>
<DEPARTMENT_NAME>Transportation</DEPARTMENT_NAME>
</ROW>
c
</ROWSET>l e
r a
The DEPARTMENT_ID is used as the key column.

O
For deletes, choose a set of key columns to identify the rows to be deleted. If elements for
the key columns are not provided, then the DELETE operation will match all the elements in
the document to their respective columns.

Oracle 10g: XML Fundamentals D-22


Generating an XML Schema with XSU

To generate an XML Schema based on a database


table, use the getXML command:
• With the –withschema option:
java OracleXML getxml -user "HR/HR" –withschema
"SELECT * from REGIONS"

• With the XML Schema only, use the –withSchema


option and a query condition that returns no rows:
java OracleXML getxml -user "HR/HR" –withschema
"SELECT * from REGIONS WHERE 1=2"

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Generating an XML Schema with XSU
U s
The output results of the first example is:
<?xml version = '1.0'?>
A I
<DOCUMENT xmlns:xsd="http://www.w3.org/2001/XMLSchema">
O
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="ROWSET">
l &
a
<xsd:complexType>
<xsd:sequence>

r n
<xsd:element name="ROW" minOccurs="0"

n te
<xsd:complexType>
I<xsd:sequence>
maxOccurs="unbounded">

c l e <xsd:element name="REGION_ID"
type="xsd:double" minOccurs="0"/>

r a <xsd:element name="REGION_NAME"
nillable="true" minOccurs="0">
O <xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:maxLength value="25"/>
</xsd:restriction>
...
Oracle 10g: XML Fundamentals D-23
Generating an XML Schema with XSU (continued)
</xsd:simpleType>
</xsd:element>
</xsd:sequence>
<xsd:attribute name="num" type="xsd:integer"/>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<ROWSET xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation=
"#/DOCUMENT/xsd:schema[not(@targetNamespace)]">
<ROW num="1">
<REGION_ID>1</REGION_ID>
<REGION_NAME>Europe</REGION_NAME>
</ROW>
<ROW num="2">
<REGION_ID>2</REGION_ID>
<REGION_NAME>Americas</REGION_NAME>
</ROW>
<ROW num="3">
<REGION_ID>3</REGION_ID>
<REGION_NAME>Asia</REGION_NAME>
n l y
</ROW>
<ROW num="4">
e O
<REGION_ID>4</REGION_ID>
<REGION_NAME>Middle East and Africa</REGION_NAME>
U s
</ROW>
</ROWSET>
</DOCUMENT> A I
Note: In the XML generated, the line containing O
l
xsi:noNamespaceSchemaLocation with value &
a
"#/DOCUMENT/xsd:schema[not(@targetNamespace)]" associates the XML
n
te r
document to the schema document at the specified schema location. It must not associate a
namespace to elements definitions.

I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-24
TransX Utiliity

• Simplifies the loading of translated seed data and


messages into a database
• Reduces internationalization costs by:
– Preparing strings to be translated
– Translating the strings
– Loading the strings to the database
• Minimizes translation data format errors
• Accurately loads translation contents into the
database

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
TransX Utility
U s
It also reduces internationalization costs by:
A I
TransX Utility simplifies the loading of translated seed data and messages into a database.

• Preparing strings to be translated


O
• Translating the strings
• Loading the strings to the database
l &
a
• Minimizing translation data format errors and accurately loading the translation
n
te r
contents into predetermined locations in the database
Other advantages of the TransX Utility are the following:

I n
• Translation vendors no longer have to work with unfamiliar SQL and PL/SQL scripts.

c l e
• Syntax errors because of varying Globalization Support settings are eliminated.
• The UNISTR construct is no longer required for every piece of NCHAR data.

r a
Development groups that need to load translated messages and seed data can use the
TransX Utility to simplify what it takes for meeting internationalization requirements.
O
After the data is in a predefined format, the TransX Utility validates its format.
Choosing the correct encoding when loading translated data is automated as TransX takes
advantage of the XML description of encoding. This means that loading errors because of
incorrect encoding is impossible as long as the data file conforms to the XML standard.
Oracle 10g: XML Fundamentals D-25
Using TransX Utility

The TransX command-line utility syntax is:

java oracle.xml.transx.loader [options]


connect_string username password datasource
[datasource]

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Using TransX Utility
U s
A I
The syntax for invoking TransX Utility from the command line is as follows:
java oracle.xml.transx.loader [options] connect_string
username password datasource [datasource]
O
l &
In the above syntax, the following are the options:
• The connect_string: Specifies a JDBC connect string. If you omit the connect
a
string information through the '@' symbol, then the string 'jdbc:oracle:thin:@'
n
will be assumed.

te r
• The username: Specifies the database username

I n
• The password: Specifies the password for the database user
• The datasource: Specifies an XML data source

c l e
r a
O
Oracle 10g: XML Fundamentals D-26
Using TransX Utility (continued)
The options are:
• -u: Updates existing rows. When this option is specified, existing rows are not
skipped but updated. To exclude a column from the update operation, specify the
useforupdate attribute to be no.
• -e: Raises exception if a row is already existing in the database. When this option is
specified, an exception will be thrown if a duplicate row is found. By default, duplicate
rows are simply skipped. Rows are considered duplicate if the values for lookup-key
columns in the database and the dataset are the same.
• -x: Prints data in the database in the predefined format. Similar to the –s option, it
causes TransX to perform the opposite operation of loading. Unlike the –s option, it
prints the output to standard output.
Note: Redirecting this output to a file is discouraged because intervention of the
operating system may result in data loss because of unexpected transcoding.
• -s: Saves data in the database into a file in the predefined format. This is an option to
perform unloading. It queries the database, formats the result into the predefined XML
format, and stores it under the specified file name.
• -p: Prints out the dataset to insert in the canonical format of XSU.
• -t: Prints out the dataset to update in the canonical format of XSU.
• -o: Omits validation (because the dataset is parsed, it is validated by default). It causes
TransX to skip the format validation, which is performed by default.

validation and exit.
n y
-v: Validates the data format and exits without loading. It causes TransX to perform
l
-w : Preserves white space. It causes TransX to treat white space characters (such as
O

\t, \r, \n, and ' ') as significant. Consecutive white space characters in string data
elements are condensed into one space character by default.
s e
I U
O A
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-27
Summary

In this lesson, you should have learned how to:


• Describe XDK
• List the XDK components
• Describe class generator
• Describe TransX Utility

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Summary
U s
different components, their functionality, and usage.
A I
In the lesson, you were introduced to the XML Developer’s Kit. You learned about the

O
l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals D-28
Managing External Tools in JDeveloper

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
U s
A I
O
l &
n a
te r
I n
c le
ra
O
Objectives

After completing this lesson, you should be able to do


the following:
• Create External Tool commands in JDeveloper
• Use External Tools added to the JDeveloper
environment

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Objectives
U s
A I
In this appendix, you are guided through the process of managing External Tool commands
in JDeveloper. Adding External Tools enables you to invoke a variety of utilities and

O
operating system commands within JDeveloper to process files in projects.

l &
n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals E-2
External Tools with JDeveloper

An external tool:
• Is any script or application external to JDeveloper
– Command-line or graphical applications
– Operating system commands or scripts
• Is added to JDeveloper Tools or context menus
• Is configured by using the External Tools dialog box
that is opened by clicking Tools > External Tools …

Tools > External Tools …

External Tool
• Configuration is
saved in tools.xml.
tools.xml

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
External Tools with JDeveloper
U s
A I
Oracle JDeveloper is an integrated development environment (IDE). Therefore, most of the
development tasks you perform must be possible through the JDeveloper interface.

O
If JDeveloper does not provide built-in tools or extensions to facilitate a particular

program or tool to perform the task.


l &
development task, then you can configure JDeveloper to invoke an appropriate external

n a
Any application, command-line or graphical in nature, that is external to JDeveloper can be

te r
invoked by using the JDeveloper Tools menu, context menus, or toolbar.

I n
To add an external tool in JDeveloper:
1. Click Tools > External Tools …

l e
2. In the External Tools dialog box, click Add…
c
r a
When you click Add…, a wizard guides you through the steps to configure JDeveloper to
invoke the external tool. These steps to add an external tool is shown in this appendix.
O
Note: JDeveloper saves the configuration of External Tools in a file called tools.xml,
which is created in the JDEV_HOME\jdev\system9.0.5.2.1618 directory. Here,
JDEV_HOME is the directory into which JDeveloper is installed, and the numbers appended
to the system subdirectory name reflect the version of JDeveloper being used.

Oracle 10g: XML Fundamentals E-3


Managing External Tools

In the External Tools dialog box, you can:


• Add new tools
• Edit existing tools
• Remove existing tools

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Managing External Tools
U s
A I
The External Tools dialog box is displayed after you select Tools > External Tools…. The
slide shows the External Tools dialog box with a preconfigured list of tools used in this
course.
O
In the External Tools dialog box, you can:
l
1. Click Add… to create a new external tool&
n a
2. Click Edit… after selecting the tool name to alter the settings for the external tool

JDeveloper environment
te r
3. Click Remove… after selecting the tool name to delete the external tool from the

I n
These operations are explained in the next few pages.

c l e
r a
O
Oracle 10g: XML Fundamentals E-4
Adding an External Tool

Step 1 of 4: External Program Options

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Adding an External Tool
U s
A I
After clicking Add… in the External Tools dialog box, you are guided through fours steps
by using the Create External Tool wizard. In the Step 1 of 4: External Program Options,
you enter:
O
or script file in the file system
l &
1. The location of the Program Executable, or click Browse… to locate the executable

a
2. Optional Arguments that can be passed on the command line to the executable
n
te r
3. The Run Directory to set the working directory in which the executable is invoked
The Command Sample field displays the full command structure with expanded macros.

I n
Click Next > when the fields are completed.

l e
Note: You can type a value directly in the Arguments and Run Directory fields, or click

c
Insert… to add a macro string representing an argument value generated by JDeveloper.

r a
When you click Insert…, the Insert Macro window is displayed from which you can select

O
a macro to use. The slide shows an example of selecting the File URL macro, which
JDeveloper expands into the URL for the file selected in the JDeveloper Navigator, XML
Editor, or Code Editor window when the tool is invoked. The Insert Macro window
displays a sample of the expanded macro argument results. For the File URL macro,
JDeveloper inserts the string ${file.url} into the Arguments field.
Oracle 10g: XML Fundamentals E-5
Adding an External Tool

Step 2 of 4: Display

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Adding an External Tool (continued)
U s
In the Step 2 of 4: Display window, you specify:

A I
1. The Caption of Menu Items field to the text string representing the external

O
application or tool. The text string entered appears in the menu.

l &
2. The ToolTip Text, which is a string that appears when you place the cursor on a
toolbar button if the external tool is added to the toolbar. This is optional.
a
3. The Icon Location, which is set to the name of an icon file with the .ico extension.
n
te r
By default, the icon is derived from the default icon supplied for the executable. If the
external tools is a script file, then JDeveloper provides a default built-in icon that

I n
varies according to the development platform.

c l e
Note: The Preview field provides a visual representation of the menu item. Click Next >
after the desired fields are completed.

r a
O
Oracle 10g: XML Fundamentals E-6
Adding an External Tool

Step 3 of 4: Integration

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Adding an External Tool (continued)
U s
into the JDeveloper menu, toolbar or both.
A I
In the Step 3 of 4: Integration window, you specify how the tool is invoked or integrated

O
To integrate the tool into the menus, select from the following options:

l &
1. Select Tools Menu to make the command available in the Tools menu.
2. Select Navigator Context Menu to make the command appear in the menu displayed

n a
when you right-click a node in the JDeveloper Navigator window.

te r
3. Select Code Editor Context Menu to make the command appear in the menu
displayed when you right-click in the editor window of an open file.

I n
To add the command as a button icon in the toolbar, select Main Toolbar.

l e
Select the Reload Open Files check box if the tool invoked on a file saves modifications to
c
the file. You need JDeveloper to refresh its in-memory copy of the file, if it is already open.

r a
Modifications made by using external tools are saved in the file system, and not reflected in

O
JDeveloper unless its new contents are reloaded. The Reload Open Files check box need
not be selected if the external tool reads only the file contents.
Click Next > after you have made the desired selections.

Oracle 10g: XML Fundamentals E-7


Adding an External Tool

Step 4 of 4: Availability

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Adding an External Tool (continued)
U s
A I
In the Step 4 of 4: Availability window, you select an option when the external tool
command becomes visible or active for use in the JDeveloper menu and toolbar. If you
select:
O
l &
• Always, then the command is always active and available
• When a File is Selected or Open in the Editor, then the command is only active and
a
available if you have selected a file in the Navigator, or have the file open in an editor
n
te r
• When Specific File Types are Selected, then you can choose one or more file types
from the list in the Available Types area. Select one or more types in the Available

I n
Type area and click the arrow (>) to move them to the Selected Types area. In this
case, JDeveloper makes the command active and available only when a file of the
l e
selected type has been chosen in the Navigator, or opened in the editor.
c
r a
Click Finish to save the newly added external tool configuration details to the tools.xml
file in the JDeveloper environment. JDeveloper returns you to the External Tools dialog
O
box and the new tool may be used immediately after closing the External Tools dialog box.

Oracle 10g: XML Fundamentals E-8


Editing an External Tool

Select the tool name and click Edit:

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Editing an External Tool
U s
A I
If a configured external tool is not operating as expected or its definition needs to be
changed, then you can edit it. To edit the configuration of an external tool, in the External
Tools dialog box:
O
1. Select the name of the external tool
2. Click Edit…
l &
n a
The Edit External Tool window is displayed with four tabs allowing you to alter :

• The Display settings


te r
• The External Program Options settings

I n
• The Integration settings

c l e
• The Availability settings
Click OK when all the changes have been made.

r a
O
Oracle 10g: XML Fundamentals E-9
Removing an External Tool

In the External Tools dialog box,


1. Select the tool name
2. Click Remove

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Removing an External Tool
U s
To remove an external tool, in the External Tools dialog box:
1. Select the name of the tool to be removed
A I
2. Click Remove
O
l &
Note: JDeveloper does not prompt for confirmation before the tool entry is deleted. You
may want to make a copy of the tools.xml file before removing or editing a command.

n a
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals E-10
Summary

In this lesson, you should have learned how to:


• Manage External Tool commands in the
JDeveloper menus by:
– Adding external tools
– Editing external tools
– Removing external tools
• Invoke External Tools added to the JDeveloper
menus

n l y
Copyright © 2004, Oracle. All rights reserved.

e O
Summary
U s
A I
In this appendix, you learned how to use the External Tool dialog box to add, edit, and
remove external tools, and menu or toolbar options in the JDeveloper environment. The

O
configuration of each external tool is stored in a file called tools.xml, which is located in

l &
the JDEV_HOME\jdev\system<version> directory, where JDEV_HOME is the
directory in which JDeveloper is installed, and <version> is a number sequence
a
representing the version of JDeveloper installed.
n
te r
I n
c l e
r a
O
Oracle 10g: XML Fundamentals E-11
n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
Index

n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O
A
<attribute> 4-8, 4-25, 4-25, 4-27
<attributeGroup> 4-8, 4-25, 4-26, 4-27
absolute location path 5-6, 5-7
access control list 9-9, 9-22, 9-28, 9-30
ANY 2-9, 2-10, C-5, C-6
ATTLIST 2-6, 2-12, 2-13, 2-14, 2-15, 2-20, C-3, C-8, C-9, C-10, C-12, C-13, C-14
Attribute defaults 4-10
Attribute Value Templates 6-20
Axes 5-8, 5-9, 5-25, 14-11
axis 5-8, 5-9, 5-25
B
Bind parameters 7-12
bind-params 7-12, 8-5, 8-10
boolean() 5-18
Built-in entities 2-16, C-15
C
<choice> 4-8, 4-20, 4-22
<complexType> 4-8, 4-12, 4-20, 4-21, 4-22, 4-23, 4-26, 4-27
Cardinality 2-7, 2-11, 2-20, 2-22, C-7

n l y
CDATA 1-7, 2-7, 2-8, 2-9, 2-12, 2-13, 2-14, 2-15, 2-20, 2-22, 4-38, C-4, C-5, C-8, C-9, C-10, C-14, C-16
ceiling() 5-19
Character entities 2-16, C-15
e O
choices 2-13, 2-22, 12-3, C-9

U s
code-insight 4-31, 4-33, 6-9
comment() 5-10, 5-13
A I
concat() 5-21, 10-3, 10-9, 10-10, 12-4
O
contains() 5-21

l &
a
contenttype 14-5, 14-6, 14-12
context indexes 13-18

r n
te
cookie parameters 8-21
count() 5-20
CREATE VIEW 9-8, 12-5, 12-8 I n
l
CTXXPATH 13-14, 13-16, 13-18

c e
r a
CTXXPATH index 13-14, 13-16
CURSOR expressions 8-3, 8-21
D O
<!DOCTYPE 2-8, 2-14, 2-20, 2-21, C-4
Database Connection 7-6, 7-7, 7-8, 7-9, 8-5, 8-13
DBMS_XMLGEN 10-15, 10-19

Oracle 10g: XML Fundamentals Index-2


D
DBMS_XMLSCHEMA 9-12, 9-15, 9-17, 9-18, 9-21, 9-22, 9-23, 12-8, 12-9, 12-9, 12-12, 12-15
DBUri servlet 14-4, 14-5, 14-6, 14-12, I-6, I-14, I-15
DBUriType 14-4
Declaring an XMLType 11-5
Default attribute value 2-15,2-20, 4-14, 7-13
default namespace 3-5, 3-7, 3-8, 3-10, 4-7, 4-26, 5-24
Default Template Rules 6-12, 6-16, 6-17
deleteSchema 9-22, 9-23
DML with XMLType Views 12-14
DOCTYPE 2-8, 2-14, 2-20, 2-21, C-4
Document Object Model 9-3, 11-3, D-6, I-9, I-10
document tree 1-9, 1-16, 2-23, 5-9, 5-25, 6-7, 6-10, 6-16, 13-10, I-10, I-19
DOM 2-8, 3-4, 3-9, 5-8, 5-24, 9-3, 9-4, 9-8, 9-11, 11-3, 11-6, 11-8, C-4, D-4, D-6, D-8, D-12, D-13,
D-15, D-18, I-9, I-10, I-11
DTD 1-5, 1-6, 1-12, 2-3, 2-4, 2-5, 2-6, 2-7, 2-8, 2-9, 2-14, 2-15, 2-16, 2-17, 2-19, 2-20, 2-21, 2-22, 2-23, 4-3,
4-4, 4-36, 4-37, 4-37, 4-38, 4-39, 5-20, C-3, C-4, C-5, C-10, C-13, C-15, C-16, D-13, D-14, D-17, D-18,
I-4, I-8, I-9, I-20
E
<element> 4-5, 4-8, 4-12, 4-14, 4-17, 4-25
ELEMENT clause 12-8
n l y
EMPTY 2-9, 2-14, C-5, I-16
empty element 1-8, 1-9, 1-10, 1-14, 2-9, 3-8, 4-23, 4-26, 6-19, 7-9, 8-7, 13-9, C-5
e O
s
ENTITIES 2-3, 2-5, 2-6, 2-12, 2-16, 2-17, 2-19, 2-22, 2-23, C-3, C-8, C-10, C-15, C-16

U
Entity references 2-16, 8-13
A I
ENTITY 2-5, 2-6, 2-12, 2-14, 2-16, 2-17, 2-19, 2-20, 4-37, 5-16, 6-24, 8-13, C-3, C-8, C-10, C-15, C-16, D-9

Enumerated 2-12, 2-13, C-8, C-9, C-16


O
&
existsNode() 9-11, 11-4, 11-10, 11-12, 11-13, 11-16, 11-20, 13-10, 13-13, 13-14, 13-15, 13-16, 13-18

l
a
expanded name 3-9, 3-10, 5-4

n
Extensible Markup Language 1-3, 1-19, I-7, I-20

r
te
Extensible Stylesheet Language 13-3, D-9

I n
extract() 9-11, 11-4, 11-10, 11-12, 11-14, 11-15, 11-16, 11-17, 11-20, 12-5, 13-14, 13-15
extractValue() 11-10, 11-12, 11-17, 11-18, 11-20
F

c l e
r a
#FIXED 2-12, 2-12, 2-15, C-8
Fallback query 7-15, 7-16, 7-20

O
false() 5-18
floor() 5-19
Folders 9-6, 9-9, 9-31, 9-32, 9-33
Formal Public Identifier 2-8, C-4

Oracle 10g: XML Fundamentals Index-3


F
FPI 2-8, C-4
FTP 9-3, 9-6, 9-12, 9-18, 9-28, 9-30, 9-31, 9-33, 11-9, 14-5, 14-12
Function-Based Indexes 13-14, 13-15
G
General entities 2-16, 2-17, C-15
generateSchema 12-12, 12-15
getClobVal() 11-11
getNumberVal() 11-11, 12-5, 12-6, 12-7, 12-10, 12-11, 12-16
getStringVal() 11-14, 13-15
H
HTML 1-3, 1-16. 1-19, 2-8, 4-24, 4-28, 5-22, 5-26, 6-3, 6-4, 6-6, 6-8, 6-10, 6-17, 6-19, 6-23, 6-26, 6-27, 6-29,
6-31, 6-34, 7-3, 7-4, 7-5, 7-13, 7-18, 7-19, 8-4, 8-5, 8-17, 8-22, 13-3, 13-4, 13-7, 13-8, 14-3, 14-4,
14-12, 14-13, C-4, I-10
HTTP 2-8, 3-3, 3-5, 3-6, 4-5, 4-6, 4-7, 4-9, 4-10, 4-12, 4-31, 4-32, 4-35, 5-22, 6-5, 6-6, 6-9, 6-14, 6-15, 6-16,
6-17, 6-18, 6-22, 6-23, 7-3, 7-12, 7-13, 7-14, 7-16, 7-17, 7-18, 8-4, 8-7, 8-9, 8-10, 8-12, 8-15, 8-16,
8-17, 8-18, 8-20, 8-21, 9-3, 9-6, 9-7, 9-12, 9-18, 9-23, 9-28, 9-30, 9-31, 12-9, 12-10, 12-11, 12-13,
12-15, 12-16, 14-4, 14-5, 14-6, 14-7, 14-8, 14-9, 14-10, 14-11, 14-12, 14-13, C-4, D-23, I-9
HttpUriType 14-4
I
#IMPLIED 2-12, 2-13, 2-15, 2-20, C-8, C-9, C-10, C-13, C-14
n l y
ID 2-12, C-8, C-12
id() 5-20, 9-24, 9-25, 9-27
e O
IDREF 2-12, C-8, C-13, C-16

U s
IDREFS 2-12, C-8, C-13, C-16
IRI 3-3, 3-5, 3-10, 8-13
A I
isFragment() 11-11, 11-15
O
isSchemaValid() 9-24, 9-25, 9-27

l &
a
isSchemaValidated() 9-24, 9-27
J

r n
te
JDeveloper 1-17, 1-18, 1-19, 1-20, 2-21, 2-22, 2-23, 3-9, 4-28, 4-29, 4-30, 4-31, 4-32, 4-33, 4-34, 4-35,

I n
4-39, 4-40, 5-24, 6-8, 6-9, 6-19, 6-31, 7-2, 7-7, 7-8, 7-9, 7-10, 7-20, 13-7, D-6, D-15, E-3, E-4,
E-5, E-6, E-7, E-8, E-10, E-11, I-12, I-13, I-14, I-15, I-16, I-17, I-18, I-19, I-20, I-21
L

c l e
last() 5-20 r a
<list> 4-8, 4-17, 4-19

O
Lexical parameters 7-12, 7-17, 8-16
Loading Data into XMLType 11-9, 11-13
local-name() 5-20, 6-21

Oracle 10g: XML Fundamentals Index-4


L
location path 5-5, 5-6, 5-7, 5-8, 5-11, 5-17, 5-25
Location Path Expression 5-6, 5-6, 5-7, 5-8, 5-17
M
Master-Detail 10-12, 10-13
max-rows 7-9, 7-11, 8-3, 8-12
Modes 6-25, 6-26, 6-27, 9-23
Multiple Predicates 14-11
N
namespace prefix 3-5, 3-6, 3-10, 4-5, 4-6, 4-7, 4-9, 4-10, 4-12, 4-25, 4-33, 5-10, 5-20, 6-5, 6-6, 6-9, 6-15, 12-11
Native XML 9-3, 9-6, 9-33, 10-15, 11-3, 11-4, 11-5
NDATA 2-14, 2-17. C-10
NMTOKEN 2-12, C-8, C-14, C-16
NMTOKENS 2-12, C-8, C-14, C-16
node() 5-10, 5-11, 9-11, 11-4, 11-10, 11-12, 11-13, 11-16, 11-20, 13-10, 13-13, 13-14, 13-15, 13-16, 13-18
non-schema-based XMLType views 12-4, 12-5, 12-17
noNamespaceSchemaLocation 4-6, 4-6, 4-7, 4-35, 12-9, 12-10, 12-10, 12-13, 12-16
not() 5-18
NOTATION 2-3, 2-6, 2-12, 2-14, 2-17, 2-22, C-3, C-8, C-10, C-16
number() 5-19
O
n l y
/oradb 14-5, 14-6, 14-7, 14-8, 14-9, 14-10, 14-11, 14-12
Object-Relational Storage 11-3, 11-7, 11-8, 13-9
e O
OBJECT_ID 12-5

U s
OBJECT_VALUE 12-5, 12-6, 12-7, 12-10, 12-11, 12-16

A I
Oracle XML DB 9-3, 9-4, 9-6, 9-7, 9-8, 9-9, 9-11, 9-21, 9-24, 9-28, 9-30, 9-31, 9-32, 9-33, 9-34, 11-7, 11-12,
12-3, 12-4, 13-3, 13-8, 14-4, 14-5, 14-12, I-5
O
oraxml 2-21,2-22, 2-23, 4-34, 4-35, 4-39
oraxsl 6-8, 6-31, 6-33, 6-34
l &
P
n a
te r
#PCDATA 2-7, 2-8, 2-9, 2-20, 2-22, 4-38, C-4, C-5, C-10, C-14
page-private parameters 8-12, 8-21

I n
Parameter entities 2-16, 2-19, C-15
parsed entity C-10

c l e
r a
Piecewise Update 9-33, 11-4, 11-8, 13-9, 13-11
position() 5-14, 5-17, 5-20

O
processing-instruction() 5-10, 6-15
PUBLIC 2-8, 4-36, 14-12, 14-13, C-4

Oracle 10g: XML Fundamentals Index-5


Q
qualified name 3-5, 3-10, 5-10, 5-20, 6-19
Querying XML 9-3, 9-34, 11-10, 11-20
R
#REQUIRED 2-12, 2-14, 2-15, C-8, C-12, C-13
registerSchema 9-18, 9-20, 12-8, 12-9, 12-15
relative location path 5-6, 5-7
Repository 9-3, 9-6, 9-9, 9-12, 9-21, 9-22, 9-28, 9-29, 9-30, 9-31, 9-33, 9-34, 9-35, 11-6, 13-3, 14-4,
14-12, 14-13
round() 5-19
row-element 7-11, 7-20, 8-12
rowset-element 7-11, 7-11, 7-17, 7-20, 8-12
rowsettag 14-6, 14-7, 14-8, 14-9, 14-12
S
<schema> 4-5, 4-8, 4-9, 4-10, 4-12, 4-14, 4-25, 4-28, 4-29, 14-6, 14-11
<sequence> 4-8, 4-12, 4-20, 4-21,
<simpleType> 4-8, 4-17, 4-25, 4-26, 4-26
SAX D-6, D-8, D-9, D-11, D-12, D-18, I-9, I-10, I-11
schemaLocation 4-6, 4-7, 4-31, 4-32, 4-33, 4-35, 12-9, 12-10, 12-11, 12-13, 12-16
schemaValidate 9-24, 9-26, 9-27
session parameters 8-4, 8-7, 8-21, 8-22
n l y
setSchemaValidated 9-24, 9-26
ShowXmlNS 3-9, 3-11, I-15
e O
Simple API for XML D-6, I-9, I-11

U s
skip-rows 7-11
SQL Operations on XML 9-4, 9-33, 11-3, 13-18, I-6
A I
O
SQL/XML 9-7, 9-8, 9-9, 10-3, 10-4, 10-6, 10-8, 10-9, 10-10, 10-11, 10-14, 10-20, 10-21, 11-14, 12-4, 12-5,
12-6, 12-9, 12-10, 12-11, 12-17, 12-18

l &
SQL/XML Functions 9-9, 10-20, 10-21, 12-5, 12-6, 12-9, 12-10, 12-11, 12-17, 12-18
SQL/XML operators 9-8, 12-4
n a
starts-with() 5-21

te r
string() 5-21
string-length() 5-21
I n
l e
Structured Storage 9-4, 9-7, 9-11, 9-20, 11-7

c
substring() 5-21
sum() 5-19
r a
O
SYS_XMLAGG 9-9, 10-15, 10-18, 10-20, 11-4
SYS_XMLGEN 9-9, 10-15, 10-16, 10-17, 10-18, 10-20, 11-4, 11-15, 12-4, 12-5, 12-7, 12-17

Oracle 10g: XML Fundamentals Index-6


T
tag-case 7-9, 7-11, 7-20
TestXPath 5-23, 5-24, 5-25, 5-26, I-15
text() 5-10, 5-11, 5-13, 5-22, 6-16, 11-12, 11-13, 11-14, 11-16, 11-17, 12-16, 13-10, 13-15, 14-5, 14-10, 14-11
transform 5-22, 5-23, 6-4, 6-5, 6-6, 7-4, 7-18, 7-19, 8-7, 8-20, 9-8, 13-3, 13-4, 13-5, 13-6, 13-7, 13-8, 14-12,
14-13, D-8, D-9, D-15, D-17
transform attribute 8-17, 8-20
true() 5-18
U
<union> 4-8, 4-17, 4-19
Uniform Resource Identifier 5-3, 14-3
Uniform Resource Locator 2-3, 3-3, 14-3
unparsed entity C-10
Updateable XMLType View 12-14, 12-15, 12-16
updateXML 11-4, 12-16, 13-9, 13-10, 13-11, 13-18
URI 1-3, 3-3, 3-5, 3-10, 4-10, 4-24, 4-36, 5-3, 5-20, 6-5, 6-6, 6-7, 6-9, 6-12, 7-5, 9-12, 9-20, 9-28, 12-11, 14-3,
14-4, 14-5, 14-6, 14-9, 14-12, D-18
UriFactory 14-4, 14-6
UriType 14-4

l y
URL 2-3, 2-8, 2-17, 2-19, 2-22, 3-3, 3-5, 3-10, 4-6, 4-7, 4-31, 4-32, 5-4, 6-7, 7-10, 7-13, 7-16, 8-7, 8-10, 8-12,

n
8-13, 8-15, 9-15, 9-18, 9-21, 9-22, 9-28, 9-31, 12-8, 12-9, 13-8, 14-3, 14-4, 14-5, 14-6, C-4, D-9, E-5
URN 3-3, 3-5, 3-5, 3-6, 3-7, 3-8, 3-10, 7-6
V
e O
Validating XML 2-4, 2-21, 4-34, 4-35, 9-24, I-4, I-14, I-15

U s
W

A I
W3C 1-3, 1-16, 2-8, 2-16, 4-3, 4-4, 4-5, 4-9, 5-3, 7-5, 9-3, 9-12, 9-24, 9-33, 9-34, 11-12, 13-3, 13-8, C-4,
C-15, D-6, D-8, D-9, D-11, D-13, I-7, I-10, I-11, I-20
O
l &
WebDAV 9-3, 9-6, 9-28, 9-30, 9-31, 9-33, 11-9, 14-5, 14-12
World Wide Web Consortium 1-3, 1-16, 2-16, 9-12, 11-12, 13-3, C-15, I-7
X
n a
xdbconfig.xml 14-5

te r
XDBUriType 14-4

I n
XML attribute 1-10, 3-5, 5-3, 10-4, 10-5, D-18

l e
XML element 1-8, 1-10, 1-13, 3-3, 3-5, 4-8, 4-14, 4-21, 4-30, 4-32, 6-25, 7-6, 7-9, 7-10, 7-20, 8-7, 8-14,

c
r a
9-8, 9-28, 10-3, 10-4, 10-5, 10-6, 10-7, 10-11, 10-13, 11-7, 11-12, 11-14, 12-5, 12-15, 13-3, 13-9,
14-9, 14-10, D-18, D-22, I-20

O
XML instance document 4-3, 4-5, 4-6, 4-7, 4-9, 4-12, 4-14, 4-22, 4-23, 4-31, 4-34, 4-35, 4-39, 4-40, 9-15
XML Namespace 3-3, 3-4, 3-5, 3-6, 3-7, 3-8, 3-9, 3-10, 3-11, 4-3, 4-5, 4-6, 4-7, 4-12, 4-37, 4-39, 5-3,
5-4, 5-10, 6-6, 6-9, D-6, I-4, I-9, I-10, I-15

Oracle 10g: XML Fundamentals Index-7


X
XML Parser 1-6, 1-7, 1-9, 1-12, 1-13, 1-14, 2-3, 2-17, 2-21, 2-22, 3-9, 4-6, 4-35, 5-24, 7-4, 7-5, 9-11,
9-24, D-3, D-6, D-8, D-9, D-11, D-13, D-14, D-19, I-10, I-11, I-12, I-15
XML Path Language 5-3, I-9
XML Processor 1-3, 4-6, I-7, I-9
XML Schema 1-17, 2-4, 3-4, 4-3, 4-4, 4-5, 4-6, 4-7, 4-8, 4-9, 4-10, 4-12, 4-14, 4-15, 4-17, 4-20, 4-23, 4-24,
4-26, 4-28, 4-29, 4-30, 4-31, 4-32, 4-33, 4-34, 4-35, 4-36, 4-37, 4-38, 4-39, 4-40, 6-9, 9-6, 9-9, 9-12,
9-13, 9-15, 9-17, 9-17, 9-18, 9-20, 9-21, 9-22, 9-23, 9-24, 9-25, 9-26, 9-27, 9-28, 9-32, 9-34, 11-4,
11-6, 11-7, 11-8, 12-3, 12-4, 12-5, 12-8, 12-9, 12-10, 12-11, 12-12, 12-13, 12-14, 12-15, 12-16, 12-17,
12-18, D-3, D-6, D-11, D-12, D-13, D-14, D-17, D-18, D-20, D-23
XML Schema document 4-3, 4-5, 4-6, 4-7, 4-8, 4-9, 4-10, 4-12, 4-20, 4-26, 4-28, 4-29, 4-29, 4-31, 4-32,
4-34, 4-35, 4-38, 4-39, 4-40, 9-6, 9-9, 9-18, 9-20, 12-9, 12-11
XML Schema Editor 4-29, 4-40
XML Schema I-4, I-9, I-12, I-20
XML Schema Validation 9-24
XML Schema-aware 4-31, 4-32, 4-33, 6-9
XML Schema-based XMLType views 12-4, 12-8, 12-17
XML Through URIs 14-1, I-6
xml-stylesheet 5-23, 6-7, 6-8, 6-27, 7-18, 7-19, 7-20, 8-12
XMLAgg 9-9, 10-3, 10-9, 10-11, 10-12, 10-13, 10-15, 10-18, 10-20, 11-4, 12-4
XMLAttributes 10-3, 10-5, 10-12, 10-13, 10-14, 12-6, 12-10, 12-11
n l y
XMLColAttVal 10-3, 10-14, 12-4
XMLConcat 10-3, 10-9, 10-10, 12-4
e O
s
XMLElement 9-8, 9-9, 10-3, 10-4, 10-5, 10-6, 10-7, 10-9, 10-11, 10-12, 10-13, 10-14, 12-4, 12-6, 12-10, 12-11

U
XMLForest 10-3, 10-6, 10-7, 10-11, 10-16, 12-4, 12-6, 12-10, 12-11
XMLisValid 9-24, 9-25
A I
O
xmlns:xsql 7-6, 7-9, 7-11, 7-13, 7-14, 7-15, 7-17, 7-18, 7-19, 8-3, 8-5, 8-7, 8-9, 8-10, 8-12, 8-13, 8-15
XMLSCHEMA clause 12-8
XMLSequence 10-3, 10-8, 10-9
l &
n a
XMLTransform 12-13, 13-3, 13-4, 13-5, 13-6, 13-7, 13-8, 13-18, D-15
XMLType dta type 11-2, 11-5, 11-19

te r
I n
XMLType member functions 11-2, 11-10, 11-11, 11-19
XMLType Storage 11-2, 11-6, 11-19

l e
XMLType Tables 9-12, 9-13, 9-15, 9-20, 9-28, 11-7, 12-3, 12-5, 12-13, 12-17, 13-3, 14-11

c
r a
XMLType views 9-4, 9-8, 9-15, 9-28, 12-3, 12-4, 12-5, 12-6, 12-7, 12-8, 12-9, 12-10, 12-12, 12-13,
12-14, 12-17, I-6

O
XPath Expression 5-5, 5-6, 5-7, 5-8, 5-10, 5-11, 5-13, 5-14, 5-16, 5-22, 5-23, 5-24, 5-25, 5-26, 6-4, 6-5,
6-6, 6-10, 6-11, 6-20, 6-23, 8-5, 9-7, 11-12, 11-13, 11-14, 11-15, 11-17, 13-9, 13-10, 13-11, 13-13,
13-14, 14-6, 14-8, 14-11, I-3, I-4, I-9, I-15, I-20

Oracle 10g: XML Fundamentals Index-8


X
XPath functions 5-17, 5-18, 5-25, 11-2, 11-19, 13-19
XPath Predicates 5-14
xsl:apply-templates 5-22, 6-6, 6-10, 6-12, 6-14, 6-15, 6-16, 6-17, 6-18, 6-20, 6-22, 6-25, 6-26, 6-27,
6-27, 6-29, 6-30, 6-33
xsl:attribute 6-21
xsl:attribute-set 6-21
xsl:call-template 6-29, 6-30, 6-33
xsl:choose 6-23, 6-24, 6-33
xsl:element 6-20, 6-21
xsl:if 6-23, 6-33
xsl:otherwise 6-24, 6-33
xsl:output 6-19, 6-34
xsl:param 6-21, 6-30, 6-33
xsl:sort 6-22, 6-33
xsl:stylesheet 5-22, 6-4, 6-5, 6-6, 6-9, 6-14, 6-15, 6-16, 6-17, 6-18, 6-19, 6-22, 6-23, 12-13, 13-5, 14-13
xsl:template 5-22, 5-26, 6-4, 6-5, 6-6, 6-9, 6-10, 6-11, 6-12, 6-14, 6-15, 6-16, 6-17, 6-18, 6-20, 6-21,
6-22, 6-23, 6-24, 6-25, 6-26, 6-29, 6-30, 6-33, 14-13
xsl:value-of 5-22, 6-5, 6-6, 6-11, 6-14, 6-16, 6-17, 6-18, 6-22, 6-23, 6-24, 6-26, 6-29, 6-30, 6-33,
8-20, 14-13
xsl:when 6-24, 6-33
n l y
xsl:with-param 6-30, 6-33
XSQL Pages Publishing Framework 7-3, 7-4, 7-5, 7-20, 8-17, 8-18, 8-21, I-5
e O
xsql:delete-request 8-17, 8-18, 8-20

U s
xsql:dml 8-15, 8-16
xsql:include-param 8-78-9
A I
xsql:include-request-params 8-7, 8-10
O
xsql:include-xml 8-13, 8-14
xsql:include-xsql 8-12
l &
xsql:insert-param 8-17, 8-18
n a
xsql:insert-request 8-17, 8-18, 8-20

te r
I n
xsql:no-rows-query 7-15, 7-16, 7-20
xsql:query 7-6, 7-7, 7-9, 7-10, 7-11, 7-12, 7-13, 7-14, 7-15, 7-16, 7-17, 7-19, 7-20, 8-3, 8-5, 8-12,
8-15, 8-16

c l e
r a
xsql:set-cookie 7-14, 8-9
xsql:set-page-param 7-14, 8-5, 8-7

O
xsql:set-session-param 7-14, 8-10
xsql:update-request 8-17, 8-18
XSQLServlet 7-4, 7-5, 7-10, 7-14, 7-20, 8-4, 8-9, 8-10, 8-12, 8-17, 8-21

Oracle 10g: XML Fundamentals Index-9


n l y
e O
U s
A I
O
l &
na
te r
I n
c le
ra
O

Вам также может понравиться