0% found this document useful (0 votes)
64 views228 pages

XML Fundamentals Ag

This document is an Oracle training guide focused on XML fundamentals, including practices for creating, validating, and transforming XML documents. It contains various lessons with detailed tasks and solutions aimed at enhancing the user's understanding of XML and related technologies. The document is proprietary and protected by copyright, with restrictions on reproduction and distribution.

Uploaded by

Boban Vasiljevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views228 pages

XML Fundamentals Ag

This document is an Oracle training guide focused on XML fundamentals, including practices for creating, validating, and transforming XML documents. It contains various lessons with detailed tasks and solutions aimed at enhancing the user's understanding of XML and related technologies. The document is proprietary and protected by copyright, with restrictions on reproduction and distribution.

Uploaded by

Boban Vasiljevic
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 228

Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
XML Fundamentals @ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m hi Guide
a Activity
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

D64794GC11
Edition 1.1
June 2014
D86759
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Disclaimer

This document contains proprietary information and is protected by copyright and other intellectual property laws. You may copy and
print this document solely for your own use in an Oracle training course. The document may not be modified or altered in any way.
Except where your use constitutes "fair use" under copyright law, you may not use, share, download, upload, copy, print, display,
perform, reproduce, publish, license, post, transmit, or distribute this document in whole or in part without the express authorization
of Oracle.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

The information contained in this document is subject to change without notice. If you find any problems in the document, please
report them in writing to: Oracle University, 500 Oracle Parkway, Redwood Shores, California 94065 USA. This document is not
warranted to be error-free.

Restricted Rights Notice

If this documentation is delivered to the United States Government or anyone using the documentation on behalf of the United
States Government, the following notice is applicable:

U.S. GOVERNMENT RIGHTS


The U.S. Government’s rights to use, modify, reproduce, release, perform, display, or disclose these training materials are restricted
a
by the terms of the applicable Oracle license agreement and/or the applicable U.S. Government contract.
s
Trademark Notice
) h a
r s
sฺ eฺ
Oracle and Java are registered trademarks of Oracle and/or its affiliates. Other names may be trademarks of their respective
owners. m
e uid
c @
r o vi nt G
jsto Stude
Author
Lauran K. Serhal a
ฺm this
Technical Contributors and Reviewers nt i n a
l e u s e
Amitabh James Hans, Brian Fry, Diganta
c ( va Choudhury,
e to Jeffrey
Doug Mcmahon, Geeta Arora,

o v i Nandimalla,
Gerry Jurrens, Hui X Zhang, Janarthanan
n sMohan, Stephenson, Lakshmi Narapareddi,
t o r
Mark Drake, Mike Yawn, Nageswara
l i c e Nancy Greenberg, Pranab Chakraborty,
Qin Yu, Radek Felcman,
a j s Sailaja
b l ePasupuleti, Shay Shmeltzer, Sriram X Krishnamurthy, Ying Lu,
Zhen Liu
a M fera
n t i n n s
e
al book r a
-t published using: Oracle Tutor
VThis n o nwas
Table of Contents
Practices for Lesson 1: Introduction ........................................................................................................ 1-1
Practice 1-1: Oracle Database Documentation and XML Resources ......................................................... 1-2
Practices for Lesson 2: Introduction to XML............................................................................................ 2-1
Practices for Lesson 2: Overview............................................................................................................. 2-2
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Practice 2-1: Introduction to XML ............................................................................................................. 2-3


Solution 2-1: Introduction to XML ............................................................................................................. 2-6
Practice 2-2: Introducing the XML Document ........................................................................................... 2-23
Solution 2-2: Introducing the XML Document ........................................................................................... 2-25
Practices for Lesson 3: Validating XML Documents with Document Type Definitions ........................... 3-1
Practices for Lesson 3: Overview............................................................................................................. 3-2
Practice 3-1: Validating XML with Document Type Definitions (DTDs)....................................................... 3-3
Solution 3-1: Validating XML with Document Type Definitions (DTDs)....................................................... 3-5
Practices for Lesson 4: Modularizing XML with Namespaces ................................................................. 4-1
s a
h a
Practices for Lesson 4: Overview............................................................................................................. 4-2
ฺrs) ฺ
Practice 4-1: Modularizing XML with Namespaces ................................................................................... 4-3
s
em uide
Solution 4-1: Modularizing XML with Namespaces ................................................................................... 4-4
@
o v ic t G
Practices for Lesson 5: Validating XML Documents with XML Schemas ................................................ 5-1

t o r de n
Practices for Lesson 5: Overview............................................................................................................. 5-2
s
aj s Stu
Practice 5-1: Validating XML by Using an XML Schema ........................................................................... 5-3
ฺ m i
Solution 5-1: Validating XML by Using an XML Schema ...........................................................................
a h 5-5

n t i n e t
Practice 5-2: Using JDeveloper XML Schema Editor ................................................................................ 5-31
Solution 5-2: Using JDeveloper XML Schema Editor
a e
l to u s
................................................................................ 5-32
v
c (Documents
Practices for Lesson 6: Navigating XML
o v i
e n se with XPath ............................................................ 6-1
t o r
Practices for Lesson 6: Overview
l i cby Using XPath........................................................................ 6-3
............................................................................................................. 6-2
Practice 6-1: Navigating s
aj XMLraDocuments
XML
ble by Using XPath........................................................................ 6-4
Documents
M
Solution 6-1: Navigating
a 7:sTransforming
fe
n inLesson
Practicestfor
a n XML Documents with XSL Transformations ............................... 7-1
l e - t r
VaPractice
Practices
n o
for
n
7-1:
Lesson 7: Overview
Transforming XML
.............................................................................................................
by Using XSL Transformations ...............................................................
7-2
7-3
Solution 7-1: Transforming XML by Using XSL Transformations ............................................................... 7-6
Practices for Lesson 8: Working with XQuery ......................................................................................... 8-1
Practices for Lesson 8: Overview............................................................................................................. 8-2
Practice 8-1: XQuery ............................................................................................................................... 8-3
Solution 8-1: XQuery ............................................................................................................................... 8-5
Practices for Lesson 9: Introduction to Oracle XML DB .......................................................................... 9-1
Practices for Lesson 9: Overview............................................................................................................. 9-2
Practice 9-1: Introduction to Oracle XML DB ............................................................................................ 9-3
Practice 9-2: Creating a WebDAV Connection Using JDeveloper .............................................................. 9-20

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

XML Fundamentals Table of Contents


iii
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G1:
Lesson
t o r den
s
aj s Stu
Introduction
ฺ m
a 1 thi
t i n
Chapter
n use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 1
Practice 1-1: Oracle Database Documentation and XML Resources

Overview
In this practice, you use your web browser to access and review some of the useful XML
resources and documentation, and then bookmark such resources for easier access.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks
1. Access, review, and bookmark (on the Bookmarks toolbar) the XML DB home page at
http://www.oracle.com/technetwork/database-features/xmldb/overview/index.html.
Note: If your browser’s bookmark is hidden, select View > Toolbars > Bookmarks
Toolbar.
2. Access, review, and bookmark the Oracle Database Documentation website at
http://www.oracle.com/technology/documentation.
s a
a
a. Scroll down the page, and click the Database 12c Release 1 link in the Database
h
section on the page. The Documentation tab is displayed.
s ฺrs) ฺ
b. Click the Oracle Database, 12c Release 1 (12.1) link. The Oracle Database
Documentation Library 12c (12.1) is displayed.
@ em uide
v ic t G
c. Click the Master Book List link on the left, and then bookmark this page.
o
t o r den
s
d. Click the XML link to view the available XML documentation references.
aj s Stu
ฺ m
e. Access and review the HTML version of the XML DB Developer’s Guide.
a hi
t i n e t
3. Access and review the various products tutorials using Oracle Learning Library (OLL) at
http://www.oracle.com/oll. a l en o us
c (v se t
4. Access and review the XML Technology page at http://www.w3.org/standards/xml/.
i
t o rov licen
5. Access and review the W3C XML Path Language (XPath) Version 1.0 web page at
js ble
http://www.w3.org/TR/xpath/.
a
M fera
6. Access and review the W3C XML Schema Part 0: Primer Second Edition web page at
a
t i n n s
http://www.w3.org/TR/xmlschema-0/.
n
l e r a
Va non-t
7. Access any of the other additional XML websites listed in the lesson.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 1: Introduction


Chapter 1 - Page 2
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G2:
Lesson
t o r den
s
aj s Sto
Introduction tu XML
ฺ m
a 2 thi
t i n
Chapter
n use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 1
Practices for Lesson 2: Overview

Practices Overview
In this Activity Guide, a “practice” is a series of high-level steps, and a “solution” has detailed
steps. The solutions (if you require them) can be found after the practice. The practices are
intended to cover most of the topics that are presented in the corresponding lesson.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 2
Practice 2-1: Introduction to XML

Overview
In this practice, you become familiar with the basic JDeveloper features that are used to create
and open XML documents. After creating a simple XML document, you view the default
appearance of the XML document in an XML-enabled browser.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks

1. Start Oracle JDeveloper by using the JDeveloper Java Edition 12c (12.1.2) icon on the
desktop. The Oracle JDeveloper 12c: Start Page window is displayed.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

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


/home/oracle/labs directory. The application contains a project called
XMLLabsProject.

Note: If you get the Open Warning window with a message about the application
being migrated to JDeveloper 12.1.2.0.0 file format, click Yes.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 3
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

The Migration Status window is displayed. Click OK.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m hi
a/home/oracle/labs/soln
The solutions to the practices are found in t i
n and n
the
e t folder. The
solutions are named after the lessonle number
a to practiceu spractice question. For example:
( v t o
sol_03_01_a.sql holds the
v i c nse
answer question 1a in practice 3.
sol_05_01_01_b.xsd
t o ro holdslithe
ceanswer to practice question 1b in practice 5-1.
In addition, the j s le
aXMLSolutionsProject
b project in the
M
a sfe r a application contains the solutions. If you missed a
stept n
XMLFundamentalsWorkspace.jws
i
n tratonthe next step or next practice. Use thefor
in a practice, run the appropriate solution script that practice step before
l e -
Va XMLSolutionsProject
proceeding
n o n project.
solutions found in the

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 4
3. Create an XML document called employees.xml in the XMLLabsProject project.
a. Add the following employees XML element after the XML declaration. You can copy
and paste to add this information.

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


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<employees>
Company Employee Data
</employees>

Notes
− The first line, containing the XML declaration, is automatically inserted for you.
− To indent or format the code appropriately, right-click any area in the displayed a
document, and then select Reformat from the context menu.
h a s
r s )
Note that the red box that indicated an error in the file is replaced bym sฺ box
a green eฺnoton
e i d
saved the file yet, the name of the XML document, employees.xml,v i c@ t G
the Scroll bar to indicate that there are no errors. In addition, because you
is
u have
displayed in
italics. t o ro den
a js Stu
n a ฺmSavethinisthe toolbar.
b. Save the file by selecting File > Save i
nt use
or click
a l e
c ( vnamed e to
4. Add an existing XML document
o
The XML document isrlocated vi in the e n s regions.xml to your XMLLabsProject project.
directory.
s t o l i c /home/oracle/labs/lab

M aj rable
5. View the
t i naemployees.xml
s f e and regions.xml documents using the Mozilla Firefox web

a lea.n Use
browser.
- t r an
V non File Browser tool to navigate to the employees.xml file in the
the
/home/oracle/labs/lab directory. Double-click the employees.xml file to display
the file in the web browser.
b. In JDeveloper, display regions.xml using the Mozilla Firefox external tool.

You can launch Mozilla Firefox from within JDeveloper using the Tools > Firefox
menu, or you can right-click regions.xml, and then select Firefox from the context
menu.

6. Select the XMLLabsProject project in the Applications Navigator, and then click Save
in the toolbar to save your changes.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 5
Solution 2-1: Introduction to XML

Overview
In this practice, you become familiar with the basic JDeveloper features that are used to create
and open XML documents. After creating a simple XML document, you view the default
appearance of the XML document in an XML-enabled browser.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks

1. Start Oracle JDeveloper by using the JDeveloper Java Edition 12c (12.1.2) icon on the
desktop. The Oracle JDeveloper 12c: Start Page window is displayed.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

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


/home/oracle/labs directory. The application contains a project called
XMLLabsProject.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 6
Select File > Open from the Menu bar. The Open window is displayed. Navigate to the
/home/oracle/labs/XMLFundamentalsWorkspace.jws application, and then click
Open.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

If you get the Open Warning window with a message about the application being
migrated to JDeveloper 12.1.2.0.0 file format, click Yes.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 7
The Migration Status window is displayed. Click OK.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

The XMLFundamentalsWorkspace.jws application is displayed along with the projects it


contains, including the XMLLabsProject project.
s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

The solutions to the practices are found in the /home/oracle/labs/soln folder. The
solutions are named after the lesson number and practice question. For example:
sol_01_03.sql holds the answer to practice question 3 in lesson 1.
sol_01_05_a.sql holds the answer to practice question 5a in lesson 1.
In addition, the XMLSolutionsProject project in the
XMLFundamentalsWorkspace.jws application contains the solutions. If you missed a
step in a practice, run the appropriate solution script for that practice step before

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 8
proceeding to the next step or next practice. Use the solutions found in the
XMLSolutionsProject project.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
3. Create an XML document called employees.xml in the XMLLabsProject
o v ic t Gproject.
t o r den
s
aj s Stuproject, and then select
In the Applications Navigator, right-click the XMLLabsProject

New > From Gallery from the context menu.aThe Newhm i
Gallery window is displayed.
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
va eCategories,
Select the XML node from the (General
c to and then double-click (or click OK) the
XML Document item in the
r o vi Itemseregion.
ns The Create XML File window is displayed.
c
a jsto ble li
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

In the Create XML File window, enter employees.xml as the File Name, select the
/home/oracle/labs/lab directory (if not already selected), and then click OK.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 11
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

The new XML document is displayed.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Note that the employees.xml - Structure pane shows a couple of expected errors
(issues). In addition, the red boxes on the Scroll bar can be used to find the nature
of the error message. To view error messages, move the cursor over the red box on
the Scroll bar, and then press and hold the Ctrl key.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 12
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m hi XML declaration. You can copy
a aftertthe
t
a. Add the following employees XML elementi n e
l
and paste to add this information.
a en o us
i c (v se t
<?xml version="1.0"
t o rov licencoding="UTF-8"?>
en
<employees>a js ble
na
Company M Employee
f e ra Data
i
nt</employees>
an s
a l e - t r
V non
Notes:
− The first line, containing the XML declaration, is automatically inserted for you.
− To indent or format the code appropriately, right-click any area in the displayed
document, and then select Reformat from the context menu.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 13
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
Note that the red box that indicated an error in ฺ m
aIntheaddition,
file is i
hreplaced by a green box on the
t i n
nemployees.xml,
Scroll bar to indicate that there are no errors. e t because you have not saved the
file yet, the name of the XML document,a l e u s is displayed in italics.
( v t o
o v ic nse
b. Save the file by
s t o r
selecting l i ce> Save (or click the Save toolbar icon).
File

M aj rable
4. Add ian
n t naexisting
n s fe document named regions.xml to your XMLLabsProject project.
XML

a le n-tra
The XML document is located in the /home/oracle/labs/lab directory.
V no
Right-click the XMLLabsProject project in the Applications Navigator, and then select
Project Properties. Alternatively, you can double-click the XMLLabsProject project. The
Project Properties window is displayed.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 14
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

In the Project Properties window, add the /home/oracle/labs/lab folder (if not
already selected) that contains the regions.xml XML document. Click the Add button
next to the Java Source Paths region.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 15
To add the XML document, click the Add button next to the Included tab. An Add Include
Filters window is displayed. Select the regions.xml document to be added, and then
click OK. The regions.xml document is displayed in the XMLLabsProject project under
Application Sources.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 16
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 17
5. View the employees.xml and regions.xml documents using the Mozilla Firefox web
browser.
a. Use the File Browser tool to navigate to the employees.xml file in the
/home/oracle/labs/lab directory. Double-click the employees.xml file to display
the file in the web browser.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Select Applications > System Tools > File Browser from the NX menu.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
The a M ferawindow is displayed. Navigate to the employees.xml file.
File Browser
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 18
Next, double-click the employees.xml file to display the file in the web browser.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Note: The message that is displayed in the preceding Mozilla Firefox web browser is
informational and is specific to Firefox. It indicates that there is no style (XSL file,
s
covered later in this course) associated with this XML file. Therefore, it displays only a
the raw XML. Basically the author of an XML document can associate the XML h a
ฺrs) ฺ
document with a Cascading Style Sheet (CSS) or XSL Style Sheet so that the browser
s
@ em uide
knows how to render the XML. If that is not done, the browser displays this note and
shows a nicely formatted version of the XML source (created with the default XSL Style
Sheet). o v ic t G
t o r den
s
aj theswhite
S tu
ฺ m
To display the entire contents of the file, right-click
acontext menu.
h i area of the browser, and
then select View Page Source fromtthe i
n usen t
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 19
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Internet Explorer (IE) does not display any informational message. Instead, IE defaults
to a tree-like view.

b. In JDeveloper, display regions.xml using the Firefox external tool.

s a
a
You can launch Mozilla Firefox from within JDeveloper using the Tools > Firefox
h
menu. The content of the file is displayed in Mozilla Firefox.s ฺrs) ฺ
menu, or you can right-click regions.xml, and then select Firefox from the context

@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 20
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
r s) and
To display the entire contents of the file, right-click the white area of the browser,
s ฺ
then select View Page Source from the context menu.
e m ideฺ
v i c@ t Gu
t o ro den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 21
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
emthenuiclick
6. Select the XMLLabsProject project in the Applications Navigator, and
@ deSave
in the toolbar to save your changes. c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 22
Practice 2-2: Introducing the XML Document

Overview
In this practice, you create a well-formed XML document that contains 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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

violate the rules of a well-formed document.

Steps

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


a. Start SQL*Plus from a terminal window.
b. Log in to the database as username hr with matching password hr.
s a
c. Execute the SQL script called /home/oracle/labs/lab/lab_02_01.sql to
h a
file called /home/oracle/labs/lab/lab_02_01.txt. s ฺrs) ฺ
generate a list of data for employees in department 60. The script saves the data in a

@ em uide
v c telements
icontain G for the
2. Modify the employees.xml document in XMLLabsProject r o to n
employees in department 60.
a jsto Stude
a. Convert the Company Employee Data text
n a ฺmin the
t h is
<employees> element into an
XML comment. i
nt use
l e
b. For each employee row in the
the following structure:ic (
valab_02_01.txt
e to file, add an employee element with

r o v
c e ns
a jsto ble li
a M fera
n i n
<employee>
s
t <employee_id><!--
n
l e t r a id --></employee_id>
Va non-<first_name><!-- first name --></first_name>
<last_name><!-- last name --></last_name>
<salary><!-- salary --></salary>
</employee>

Hint: You can use the /home/oracle/labs/lab/lab_02_02.xml file as a


template. There are five employees in department 60.
c. Save the changes to your document, and check the employees.xml document to see
whether it is well-formed. Correct the errors and save the changes.
d. Display the employees.xml file in your browser. You can use the Firefox external tool
which was added to JDeveloper in this course. Right-click the document, and then
select Firefox from the context menu.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 23
3. Add an XML document containing department data to XMLLabsProject and correct the
errors that violate the rules of a well-formed document.
Optional: After checking—but before correcting— the syntax errors in JDeveloper, view the
XML document in the browser to observe how it reports the syntax errors.
a. Add the /home/oracle/labs/lab/departments.xml file to your project.
b. Check whether there are syntax errors in the XML document. Explain and correct all
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

errors. Check the document again to confirm your correction. Save the changes to the
document and the project.
Hint: There are three syntax errors in the document, which are indicated as warnings
in the Structure pane.
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 quotation marks around the attribute value.
d. Check whether there are errors in the XML document syntax, and explain and fix the
syntax error. Check the document for syntax to confirm your correction, and save the
changes to the document and the project. s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 24
Solution 2-2: Introducing the XML Document

Overview
In this practice, you create a well-formed XML document that contains 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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

violate the rules of a well-formed document.

Tasks
1. Generate XML data from the database for an XML document.
a. Start SQL*Plus from a terminal window.

Click the Terminal icon on your desktop. A terminal window is displayed.


s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina Inan fe
snext
a le /home/oracle/labs/lab
Note:
- t r the step, you will execute a SQL script that is found in the
V non directory. To be able to run the script without providing the
entire path, use cd (change directory command) at the $ prompt.
$ cd labs/lab
Enter the following pwd (print working directory) at the $ prompt to ensure that the
working directory is /home/oracle/labs/lab.
$ pwd

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 25
b. Log in to the database as username hr with matching password hr.

In the terminal window, enter the following at the $ operating system prompt:
sqlplus hr/hr
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
c. Execute the SQL script called /home/oracle/labs/lab/lab_02_01.sql
@ em uideto
generate a list of data for employees in department 60. The
o v icscript saves
t Gthe data in a
r n
jsto Stude
file called /home/oracle/labs/lab/lab_02_01.txt.
a
ฺm this
i n a
REM
REM Script: lab_02_01.sql l e nt use
REM
c ( va e to
set pagesize 24vtrimspool
o i ns on feedback off echo off verify off
r c e
li a20 temp
jsto format
col first_name format a20 temp
a l e
ab
col last_name
a
define M outfile="/home/oracle/labs/lab/lab_02_01.txt"
f e r
n in a&outfile
tspool n s
e
l select r
Va nofrom n-t employees
employee_id, first_name, last_name, salary

where department_id = 60
/
spool off
prompt
prompt NOTE: The results have been saved in the file: "&outfile"
prompt
undefine outfile
set feedback on

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 26
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

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


employees in department 60. h a
s ฺrs) ฺ
a. Convert the Company Employee Data text in the <employees> element into an
XML comment.
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
b. For each
na M employee
f e ra row in the lab_02_01.txt file, add an employee element with
i
nt tran
the s
following structure:
l e
Va non-
<employee>
<employee_id><!-- id --></employee_id>
<first_name><!-- first name --></first_name>
<last_name><!-- last name --></last_name>
<salary><!-- salary --></salary>
</employee>

Hint: You can use the /home/oracle/labs/lab/lab_02_02.xml file as a


template. There are five employees in department 60. The employees.xml file should
now contain the following additional elements (in bold):

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


<employees>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 27
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
<last_name>Hunold</last_name>
<salary>9000</salary>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

</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>
s a
<last_name>Austin</last_name>
h a
ฺrs) ฺ
<salary>4800</salary>
</employee>
s
<employee>
@ em uide
<employee_id>106</employee_id>
o v ic t G
<first_name>Valli</first_name>
t o r den
<last_name>Pataballa</last_name> s
aj s Stu
<salary>4800</salary>
a ฺ m hi
</employee>
t i n e t
<employee>
a l en o us
i c (v se t
<employee_id>107</employee_id>
<first_name>Diana</first_name>

t o rov licen
<last_name>Lorentz</last_name>
js ble
<salary>4200</salary>
a
M fera
</employee>
a
n t i n s
</employees>
n
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 28
c. Save the changes to your document, and check the employees.xml document to see
whether it is well-formed. Correct the errors and save the changes. If there are no XML
syntax errors, your results should look like the following:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va noClick n-tSave in the toolbar to save your changes.
d. Display the employees.xml file in your browser. You can use the Firefox external tool
which was added to JDeveloper in this course. Right-click the document, and then
select Firefox from the context menu.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 29
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s
Mozilla Firefox lic File > Open File from the menu, and then navigate to the
toopens.leSelect
M a ralocation.
employees.xml b
a
tin ans f e
l e n r
Va non-t

...

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 30
The employees.xml document is displayed.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Right-click the browser white area, and then select View Page Source from the
context menu to display the entire source code.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 31
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
3. Addn i n n s
t an XMLadocument containing department data to XMLLabsProject and correct the
a e
l errorsnthat
- t rviolate the rules of a well-formed document.
V no
Optional: After checking—but before correcting—the syntax errors in JDeveloper,
view the XML document in the browser to observe how it reports the syntax errors.
a. Add the /home/oracle/labs/lab/departments.xml file to your project.

Right-click the XMLLabsProject project name in the Applications Navigator, and


then select Project Properties. The Project Properties window is displayed.
In the Project Properties window, add the folder that has the XML document,
/home/oracle/labs/lab, if not already added. Click the Add button next to Java
Source Paths to add the folder information, if needed.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 32
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t
n usi n e t
a l eXML
To add the departments.xml
c ( v e document,to click the Add button next to the
Included tab. An Add
o iInclude nFilters
vand s window is displayed. Select
departments.xml, r
to le li c e
then click OK.
j s
a rab
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 33
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Click OK in the Project Properties window. The existing XML document is added as a
node to the project. Double-click the document name to open it in the JDeveloper XML
Editor.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 34
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 35
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
b. Check whether there are syntax errors in the XML document. Explain
m
e anducorrect
id all
i c @ G
errors. Check the document again to confirm your correction.
r o v Save
nthe
t changes to the
document and the project.
jstowhich u e
dindicated
a
Hint: There are three syntax errors in the document,
m S t are as warnings
ฺ i s
in the Structure pane.
n t ina se th
v a le CodetoEditor
u pane, select Tools > Preferences from
To display the line numbers ( in the
ic nswindow e is displayed. Expand the Code Editor node in
o v
the Menu bar. The Preferences
rthen select e
tothen
the left pane, and
j s e ic the Line Gutter option. Select the Show Line Numbers
lOK.
check box,
M a rab
and l
click

n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 36
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Syntax Error 1: “>” missing from the end tag for the first department element.
Click the red box in the XML editing window and the XML editor directs you to the start
tag of the second <department> element. Observe that the XML syntax error is one
line above it.
Correction for XML Syntax Error 1: Add the missing “>” character in the Code Editor.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 37
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

XML Syntax Error 2: The end tag does not match the start tag for “Manager_id.”
s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

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.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 38
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Syntax Error 3: The end tag for the second <department> element is
misspelled, with an “e” missing.
Correction for XML Syntax Error 3: Change the end tag to read </department>.
The departments.xml document is now well-formed, without XML syntax errors.
Note the green box instead of the red box in the upper-right corner of the Code Editor
pane. This indicates that the document is error free.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 39
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
s torof 1,tutodtheenstart tag of the first
c. Add an attribute called num, which is assigned a jvalue

<department> element. Do not use quotation m amarkssaround
S the attribute value.
a h i
e n tin se t
( v al to u
<?xml version='1.0' encoding="UTF-8"?>
<departments>
<departmentro vic ense
to le lic
num=1>
j s
<department_id>10</department_id>
a rab
M
<department_name>Administration</department_name>

n t ina <location_id>1700</location_id>
n s fe
le -tra
<manager_id></manager_id>
Va non</department>
<department>
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</department>
</departments>

d. Check whether there are errors in the XML document syntax, and explain and fix the
syntax error. Check the document for syntax to confirm your correction, and save the
changes to the document and the project.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 40
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
ฺrs) ฺ
Correction: Enclose the num attribute value 1 within quotation marks.
s
<?xml version='1.0' encoding="UTF-8"?>
@ em uide
<departments>
o v ic t G
<department num="1">
<department_id>10</department_id> sto
r den
ฺ m aj s Stu
<department_name>Administration</department_name>
<location_id>1700</location_id>
t i n a
e t hi
<manager_id></manager_id>
a l en o us
</department>
i c (v se t
rov licen
<department>
t o
<department_id>20</department_id>
js ble
a
<department_name>Marketing</department_name>
M fera
<manager_id>201</manager_id>
i na s
t </department>
<location_id>Toronto</location_id>
l e n t r a n
on-
Va n</departments>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 41
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h
)thena
r
Click Save in the toolbar to save your changes. Click the project's name, ฺand
s s click
Save in the toolbar to save your changes to the project.
em uide ฺ
@
ic t G
o v
r den
s t o
ฺ m aj s Stu
t i n a
e t hi
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 2: Introduction to XML


Chapter 2 - Page 42
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G3:
Lesson
t o r den
s
aj sXML
Validating S tu Documents
ฺ m hi
a Document
with Type
n t i n e t
v a t o us
le Definitions
v i c ( nse Chapter 3
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 1
Practices for Lesson 3: Overview

Practices Overview
In these practices, you create a valid XML document based on document type definition (DTD)
declarations. You also create internal DTD declarations.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 2
Practice 3-1: Validating XML with Document Type Definitions (DTDs)

Overview
In this practice, you add a new employee to the employees.xml file and you validate the
employees.xml file by using a DTD.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks
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>
s a
<last_name>Bond</last_name>
) h a
<first_name>James</first_name> r s
sฺ eฺ
</employee>
m
e uid
c @
Hint: Copy the text from /home/oracle/labs/lab/lab_03_01_a.xml
r o vi nt G to save
time. Check the syntax of the employees.xml document
j s to tand u e the changes.
dsave
b. Add the DTD file /home/oracle/labs/lab/employees.dtd
ฺ a
m his S to your project and
examine the DTD declarations. a t
tin employees.xml
c. Associate the employees.dtdle nwith
file u s e by inserting an appropriate
v
document type declaration(using a e
a t o …> entry. Check the XML syntax,
v i c saventhes <!DOCTYPE
correct syntax errors,
t o r o lice
and changes to the file.
d. Observe thejserrors in the etheStructure pane. JDeveloper automatically validates the XML
a b l
aM era in the Structure pane, make corrections to the
document and displays errors.
i n
e. tUsing s f
the information
a - t an
len employees.xml
r document and save your changes. Validate the employees.xml
n
V nodocument until the errors are fixed.
f. Save your changes to the employees.xml and XMLLabsProject.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 3
2. Associate an external DTD with departments.xml and validate and correct the document
using an internal DTD declaration.
Note: Use the oraxml command-line utility to validate the XML document with the DTD.
a. Add the /home/oracle/labs/lab/departments.dtd file into XMLLabsProject,
add a DOCTYPE reference in departments.xml to use the DTD, and then save the
file.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

b. Validate the departments.xml document using the oraxml command-line utility.


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

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


declaration that redefines the num attribute to use the #IMPLIED default setting. Save
the changes and validate departments.xml by using the oraxml command-line a
utility.
h a s
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 4
Solution 3-1: Validating XML with Document Type Definitions (DTDs)

Overview
In this solution, you add a new employee to the employees.xml file and you validate the
employees.xml file by using a DTD.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Steps
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>
s a
<first_name>James</first_name>
h a
ฺrsto)save
</employee>
Hint: Copy the text from /home/oracle/labs/lab/lab_03_01_a.xml
m s eฺ
time. Check the syntax of the employees.xml document and save e i d
the changes.
v i c@ t Gu
t o ro den
<?xml version='1.0' encoding="UTF-8"?> s
aj s Stu
<employees>
a ฺ m hi
t i n e t
<!-- Company Employee Data
a l en o us-->
<employee>
i c (v se t
rov licen
<employee_id>103</employee_id>
t o
<first_name>Alexander</first_name>
js ble
a
M fera
<last_name>Hunold</last_name>
i na
t </employee> s
<salary>9000</salary>
l e n r a n
-t
Va non<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>
<employee>
<employee_id>106</employee_id>
<first_name>Valli</first_name>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 5
<last_name>Pataballa</last_name>
<salary>4800</salary>
</employee>
<employee>
<employee_id>107</employee_id>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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>
s a
</employee>
h a
</employees>
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

At this point, the document syntax is well-formed, but the document is not valid. The
DTD enables you to validate the document.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 6
b. Add the DTD file /home/oracle/labs/lab/employees.dtd to your project and
examine the DTD declarations.
Double-click XMLLabsProject. The Project Properties window is displayed. Click
the Add button on the Included tab. The Add Include Filters window is displayed.
Select employees.dtd, and then click OK.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 7
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto Stude
Note: You can use the gedit text editor to
n a ฺm thethcontents
view is of the DTD file. Navigate to
Applications > Accessories > gedit i
nt TextuEditor.
s e
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 8
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

<!ELEMENT employees (employee*)>


<!ELEMENT employee (employee_id, first_name, last_name, salary)>
<!ELEMENT employee_id (#PCDATA)>
<!ELEMENT first_name (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>
<!ELEMENT salary (#PCDATA)>

c. Associate the employees.dtd file with employees.xml by inserting an 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="UTF-8"?>
<!DOCTYPE employees SYSTEM "employees.dtd">

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 9
<employees>
<!-- Company Employee Data -->
<employee>
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<last_name>Hunold</last_name>
<salary>9000</salary>
</employee>
...
<employee>
<emp_id>007</emp_id>
<last_name>Bond</last_name>
<first_name>James</first_name>
s a
</employee>
) h a
</employees>
r
sฺ eฺs
m
e validates
id the XML
d. Observe the errors in the Structure pane. JDeveloper automatically
i c @ G u
document and displays the errors.
t o rov dent
a js Stu
a m Editor,
Note: Optionally, you can right-click in the ฺXML
h i–sLog
and then select Validate XML
t i
from the context menu. The XML Validation
n n e t
Errors window displays the errors.
e
al from
These errors may be slightly different s
uthe errors in the Structure pane.
( v t o
o v ic nse
s t o r l i ce
M aj rable
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
e. Using the information in the
i c (vStructure
s e tpane, make corrections to the
r o v
employees.xml document
e n your changes. Validate the employees.xml
and save
document untilto c
i fixed.
lare
a js the errors
l e
a M ferab
n in corrections
tThe a n s needed are in the employee data for James Bond:
l e r
Va non-••t The <emp_id> element is invalid and should be <employee_id>.
Bond’s <last_name> should appear after the <first_name> element.
• The Bond <employee> element requires a <salary> child element.
The changes for the solution are shown in bold.
(Note: You can use any salary value.)

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


<!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>
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 11
...
<employee>
<employee_id>107</employee_id>
<first_name>Diana</first_name>
<last_name>Lorentz</last_name>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<salary>4200</salary>
</employee>
<employee>
<employee_id>007</employee_id>
<first_name>James</first_name>
<last_name>Bond</last_name>
<salary>5000</salary>
</employee>
s a
</employees>
h a
s ฺ rs)
After you save the changes, observe that the Structure pane no longer
e m shows d e ฺ
any
errors. You can also revalidate the documents and display the@
c Messages i
uby– Log
window. In addition, the red square box on the Scroll bar v
is i
now t G
replaced a green
t
square box indicating that the document is now valid.o ro den
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 12
f. Save your changes to the employees.xml and XMLLabsProject.
Click Save in the toolbar to save your changes to the employees.xml document.
Click the XMLLabsProject project in the Applications pane, and then click Save in
the toolbar.

2. Associate an external DTD with departments.xml and validate and correct the document
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

using an internal DTD declaration.


Note: Use the oraxml command-line utility to validate the XML document with the DTD.
a. Add the /home/oracle/labs/lab/departments.dtd file into XMLLabsProject,
add a DOCTYPE reference in departments.xml to use the DTD, and save the file.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 13
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

You can use the gedit text editor to view the contents of the departments.dtd file as
follows:

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 14
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!ELEMENT departments (department)*>


<!ELEMENT department (department_id, department_name,
manager_id?, location_id?)>
<!ATTLIST department num CDATA #REQUIRED> s a
h a
ฺrs) ฺ
<!ELEMENT department_id (#PCDATA)>
s
em uide
<!ELEMENT department_name (#PCDATA)>
<!ELEMENT manager_id (#PCDATA)> @
ic t G
<!ELEMENT location_id (#PCDATA)> o v
r den
s t o
m ajaddeds line
S tu
In the departments.xml document, the
n a ฺ
newly
t h i is shown in bold:
i
nt use
l e
va e to
<?xml version='1.0' (encoding="UTF-8"?>
c
o
<!DOCTYPE departments
r vi eSYSTEM
ns "departments.dtd">
sto ble li c
j
<departments>
a
M feranum="1">
<department
a
e n tin <department_id>10</department_id>
a n s
l r
Va non-t <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>

IMPORTANT: Save the changes made in the departments.xml XML document


before you proceed to the next step. Click Save on the toolbar.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 15
Note: If you make changes to a document and do not save it, the name of the
document is displayed in italics. Once you save the document, the name of the
document is displayed in regular plain text.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m
a usingth i
b. Validate the departments.xml document t i
n them,n e the oraxml command-line utility.
Explain any errors reported andle
v a correct
t o us and validate again to confirm that the
changes you have made are
v i cis (usednassaesequence number.
valid.

t o ro lice
Hint: The num attribute
s
aj able go to the /home/oracle/labs/lab directory and set the
Open Ma terminalrwindow,
n t ina CLASSPATH.
Java n s fe Enter the following code:
le tra
Va nocdn-labs/lab
export CLASSPATH=
/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2/xmlparserv2.ja
r

Note: Due to space restriction on this page, the export command is listed on two lines.
When you enter this command in a terminal window, make sure there are no spaces
between CLASSPATH=/home/…

Next, run the oraxml command-line utility by using the following code:

java oracle.xml.parser.v2.oraxml -dtd departments.xml

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 16
The messages are:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

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

The second error indicates that the num attribute is missing from department 20.
The DTD indicates that the num attribute is mandatory (#REQUIRED).
s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m
Instead of validating using the commandaline, you h cani also identify the errors and
t i
n us
review the suggestions of the corrections
n e
needed
tusing the following components:
l e
c ( va e to
• You can display
r o vi the error
e nsmessages in the Structure pane.
c
a jsto ble li
a M fera
n t i n n s
l e r a
Va non-t

• You can click the small red box to view the errors/issues.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 17
You can identify the validation errors by using the line gutters by moving the mouse
cursor over the red boxes.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

To correct the errors:


• Move the <manager_id> before <locations_id>.
• Add the attribute num="2" to the start tag of the <department> element for
department 20.

Save the changes made in the document. Click Save in the toolbar.
The corrections to the departments.xml file are shown in bold:
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 18
<?xml version='1.0' encoding="UTF-8"?>
<!DOCTYPE departments SYSTEM "departments.dtd">
<departments>
<department num="1">
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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>
s a
<manager_id>201</manager_id>
) h a
<location_id>Toronto</location_id>
r
sฺ eฺs
</department> m
e uid
</departments>
c @
r o vi nt G
a jstoboxes
Note that the errors are corrected and the red square t u de
in the line gutter are

replaced with a green square box to indicate mthat thei s S
document is now valid. Save your
changes.
a
tin se t h
e n
( v al to u
o v ic nse
s t o r l i ce
M aj rable
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 19
You can also validate the document by right-clicking the document and then selecting
Validate XML from the context menu as follows. The validation results are displayed in
the Messages – Log window.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
c. Modify the DOCTYPE declaration in departments.xml to include an internal DTD
declaration that redefines the num attribute to use the #IMPLIED default setting. Save
the changes and validate departments.xml by using the oraxml command-line
utility.

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


<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
]>
<departments>
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id></manager_id>
<location_id>1700</location_id>
</department>
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 20
<department num="2">
<department_id>20</department_id>
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</department>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

</departments>

java oracle.xml.parser.v2.oraxml -dtd departments.xml

The validation is successful as indicated by the following message:

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 21
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 3: Validating XML Documents with Document Type Definitions
Chapter 3 - Page 22
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G4:
Lesson
t o r den
s
aj s StXML
Modularizing u with
a ฺ m
Namespaces hi
t i n e t
a l en Chapter
o us 4
v
c ( nse t
v i
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 1
Practices for Lesson 4: Overview

Practices Overview
In this practice, you add XML namespaces to an existing XML document, and then use the
ShowXMLns tool to examine the effects of using XML namespaces.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 2
Practice 4-1: Modularizing XML with Namespaces

Overview
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 by doing the following:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

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


2. Select ShowXMLns from the menu.
ShowXMLns displays the expanded XML namespace in the start tag of each element in the
Message Log window.

Tasks

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


s a
a. In the employees root element, add a default XML namespace with the URN string
) h a
value urn:hr:employees and save the change to the XML document. r
sฺ eฺs
m
@e Gubyidusing a
b. Run the ShowXMLns tool and explain the results.
2. In XMLLabsProject, edit departments.xml and add an XMLic namespace
namespace prefix.
t o rov dent
a. In the <departments> root element, add an XML a js namespace
S tu by using the dept
ฺ m
and predict the namespace appliednto t a
e thiinsthe document.Save
prefix and the URL value of http://www.hr.com/departments.
ineach element Run
the changes

l e u s ShowXMLns
to confirm your results.
c ( va e to
b. Apply the dept namespace
r o vi prefix e nsto document,
the start and end tags of the <department>
elements in the
s to le li c
departments.xml and save the changes.
Note: Doa j b file identified by the &deptData; entity reference.
not edit the
M r a
n t ina your
c. Save
n s fe to the employees.xml document.
changes
led. Run - raShowXMLns tool and explain the results.
tthe
V3.a Add n
nothe file /home/oracle/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 question: What is the expanded namespace for each of the
following?
a. The <hr> element
b. The <department> element
c. The <department_name> element
d. The <location_id> element
e. The <employees> element
f. The <employee> element containing the data for Alexander Hunold
g. The <last_name> element containing the text Hunold
h. The <employee> element containing the data for Bruce Ernst
4. Run the ShowXMLns tool to verify your answers.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 3
Solution 4-1: Modularizing XML with Namespaces

Overview
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 by doing the following:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

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


2. Select ShowXMLns from the menu.
ShowXMLns displays the expanded XML namespace in the start tag of each element in the
Message Log window.

1. In 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.
s a
h a
<?xml version='1.0' encoding="UTF-8"?> s ฺrs) ฺ
<!DOCTYPE employees SYSTEM "employees.dtd">
@ em uide
o v ic t G
<employees xmlns="urn:hr:employees">
t o r den
<!-- Company Employee Data --> s
aj s Stu
<employee>
a ฺ m hi
t i
<employee_id>103</employee_id> n e t
a l en o us
<first_name>Alexander</first_name>
i c (v se t
<last_name>Hunold</last_name>

t o rov licen
<salary>9000</salary>
a js ble
</employee>
a M fera
n i n<employee>
n s
t <employee_id>104</employee_id>
l e r a
Va non-t<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>
...
<employee>
<employee_id>007</employee_id>
<first_name>James</first_name>
<last_name>Bond</last_name>
<salary>5000</salary>
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 4
</employee>
</employees>

b. Save your changes to the employees.xml document.


Click Save in the toolbar.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

c. Run the ShowXMLns tool and explain the results.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
... @ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

<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">

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 6
<last_name xmlns(expanded)="urn:hr:employees">
Hunold
</last_name xmlns(expanded)="urn:hr:employees">
<salary xmlns(expanded)="urn:hr:employees">
9000
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

</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">
Bruce
s a
</first_name xmlns(expanded)="urn:hr:employees">
h a
<last_name xmlns(expanded)="urn:hr:employees">
s ฺrs) ฺ
Ernst
@ em uide
o v ic t G
</last_name xmlns(expanded)="urn:hr:employees">

t o r den
<salary xmlns(expanded)="urn:hr:employees">
6000 s
aj s Stu
a ฺ m hi
</salary xmlns(expanded)="urn:hr:employees">
t i n e t
a l en o us
</employee xmlns(expanded)="urn:hr:employees">

i c (v se t
<employee xmlns(expanded)="urn:hr:employees">

rov licen
<employee_id xmlns(expanded)="urn:hr:employees">
t o
js ble
105
a
M fera</employee_id xmlns(expanded)="urn:hr:employees">
t i na s
<first_name xmlns(expanded)="urn:hr:employees">
l e n r a n
Va non-t
David
</first_name xmlns(expanded)="urn:hr:employees">
<last_name xmlns(expanded)="urn:hr:employees">
Austin
</last_name xmlns(expanded)="urn:hr:employees">
<salary xmlns(expanded)="urn:hr:employees">
4800
</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">
106
</employee_id xmlns(expanded)="urn:hr:employees">
<first_name xmlns(expanded)="urn:hr:employees">
Valli
</first_name xmlns(expanded)="urn:hr:employees">

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 7
<last_name xmlns(expanded)="urn:hr:employees">
Pataballa
</last_name xmlns(expanded)="urn:hr:employees">
<salary xmlns(expanded)="urn:hr:employees">
4800
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

</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">
107
</employee_id xmlns(expanded)="urn:hr:employees">
<first_name xmlns(expanded)="urn:hr:employees">
Diana
s a
</first_name xmlns(expanded)="urn:hr:employees">
h a
<last_name xmlns(expanded)="urn:hr:employees">
s ฺrs) ฺ
Lorentz
@ em uide
</last_name xmlns(expanded)="urn:hr:employees">
o v ic t G
<salary xmlns(expanded)="urn:hr:employees">
t o r den
4200 s
aj s Stu
a ฺ m hi
</salary xmlns(expanded)="urn:hr:employees">
t i n e t
a l en o us
</employee xmlns(expanded)="urn:hr:employees">

i c (v se t
<employee xmlns(expanded)="urn:hr:employees">

rov licen
<employee_id xmlns(expanded)="urn:hr:employees">
t o
js ble
007
a
M fera
</employee_id xmlns(expanded)="urn:hr:employees">
t i na s
<first_name xmlns(expanded)="urn:hr:employees">
l e n r a n
Va non-t
James
</first_name xmlns(expanded)="urn:hr:employees">
<last_name xmlns(expanded)="urn:hr:employees">
Bond
</last_name xmlns(expanded)="urn:hr:employees">
<salary xmlns(expanded)="urn:hr:employees">
5000
</salary xmlns(expanded)="urn:hr:employees">
</employee xmlns(expanded)="urn:hr:employees">
</employees xmlns(expanded)="urn:hr:employees">

As shown by the output results from the ShowXMLns tool, all the elements in the
document are qualified by the default namespace string urn:hr:employees.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 8
2. In 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 the 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

The changes to the departments.xml file are shown in bold:


<?xml version='1.0' encoding="UTF-8"?>
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
]>
<departments xmlns:dept="http://www.hr.com/departments">
<department num="1">
<department_id>10</department_id>
s a
h a
ฺrs) ฺ
<department_name>Administration</department_name>
<manager_id></manager_id> s
<location_id>1700</location_id>
@ em uide
</department>
o v ic t G
t o r den
<department num="2">
s
aj s Stu
<department_id>20</department_id>
a ฺ m hi
t i n t
<department_name>Marketing</department_name>
e
a l en o us
<manager_id>201</manager_id>
i c (v se t
<location_id>Toronto</location_id>
o
</department>
t rov licen
a js ble
</departments>
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en with
None of the elements are associated
o
any
u s namespace. This can be seen by the

c ( v et
empty string for the expanded namespace value in the output generated by
ShowXMLns:
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
rov licprefix
b. Apply the deptonamespace
t en to the start and end tags of the <department>
elementsa injs le
the departments.xml
b document, and save the changes.
M
a Do not
Note: fedit
a
er the file identified by the &deptData; entity reference.
t i n n s
a len <!DOCTYPE
<?xml
- t r aversion='1.0' encoding="UTF-8"?>
V non departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
]>
l<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>
</departments>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 11
c. Run the ShowXMLns tool and explain the results.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s o output
As seen by the tabove
l e licfrom ShowXMLns, only the <department> elements
a startraand
(including the
M b end tags) are qualified by the namespace prefix dept that
n t ina anthesfexpanded
represents e XML namespace string
le n-tThe r
http://www.hr.com/departments.
Va nNote:
o child elements of the <department> elements are not qualified with the
dept namespace prefix. All other elements are not qualified with a namespace because
a default namespace has not been defined.

3. Add the file /home/oracle/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 question: What is the expanded namespace for each of the
following?
a. The <hr> element
b. The <department> element
c. The <department_name> element
d. The <location_id> element
e. The <employees> element
f. The <employee> element containing the data for Alexander Hunold
g. The <last_name> element containing the text Hunold
h. The <employee> element containing the data for Bruce Ernst
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 12
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 13
a. The <hr> element
Answer: urn:hr:default
b. The <department> element
Answer: urn:hr:department
c. The <department_name> element
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Answer: urn:hr:department
d. The <location_id> element
Answer: No namespace
e. The <employees> element
Answer: urn:hr:employees
f. The <employee> element containing the data for Alexander Hunold
Answer: urn:hr:employee-default
g. The <last_name> element containing the text Hunold s a
h a
ฺrs) ฺ
Answer: urn:hr:employee-default
h. The <employee> element containing the data for Bruce Ernst s
Answer: No namespace @ em uide
o v ic t G
t o r den
4. Run the ShowXMLns tool to verify your answers.
a j s S tu
ฺ m his
Right-click hr.xml in XMLLabsProject andaselect ShowXMLns from the context menu.
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

The results are displayed in the ShowXMLns-Log window.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 14
<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">
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!--(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)="null">
<!--(d) -->
1400
s a
</location_id xmlns(expanded)="null">
h a
ฺrs) ฺ
<emp:employees xmlns(expanded)="urn:hr:employees">
<!-- (e) -->
s
<!--(f)-->
@ em uide
<employee xmlns(expanded)="urn:hr:employee-default">
o v ic t G
<employee_id xmlns(expanded)="urn:hr:employee-default">
t o r den
103 s
aj s Stu
a ฺ m
</employee_id xmlns(expanded)="urn:hr:employee-default">
hi
i n t
<first_name xmlns(expanded)="urn:hr:employee-default">
t e
Alexander
a l en o us
<!--(g)--> i c (v se t
</first_name xmlns(expanded)="urn:hr:employee-default">

t o rov licen
<last_name xmlns(expanded)="urn:hr:employee-default">
Hunold
a js ble
M fera
</last_name xmlns(expanded)="urn:hr:employee-default">
a
n t i n s
<salary xmlns(expanded)="urn:hr:employee-default">
n
l e r a
Va non-t
9000
</salary xmlns(expanded)="urn:hr:employee-default">
</employee xmlns(expanded)="urn:hr:employee-default">
<!--(h)-->
<employee xmlns(expanded)="null">
<employee_id xmlns(expanded)="null">
104
</employee_id xmlns(expanded)="null">
<first_name xmlns(expanded)="null">
Bruce
</first_name xmlns(expanded)="null">
<last_name xmlns(expanded)="null">
Ernst
</last_name xmlns(expanded)="null">
<salary xmlns(expanded)="null">
6000
</salary xmlns(expanded)="null">
</employee xmlns(expanded)="null">
</emp:employees xmlns(expanded)="urn:hr:employees">
</dept:department xmlns(expanded)="urn:hr:department">

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 15
</hr xmlns(expanded)="urn:hr:default">
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 4: Modularizing XML with Namespaces


Chapter 4 - Page 16
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G5:
Lesson
t o r den
s
aj sXML
Validating S tu Documents
ฺ m
a XML
with hiSchemas
t i n e t
a l en Chapter
o us 5
v
c ( nse t
v i
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 1
Practices for Lesson 5: Overview

Practices Overview
In this practice, you create XML schema documents for the regions.xml and
employees.xml documents. You manually type the XML schema components for the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

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. You register the XML schema in JDeveloper and use
the built-in XML schema–aware features of JDeveloper.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 2
Practice 5-1: Validating XML by Using an XML Schema

Overview
In this practice, you create XML schema documents for the regions.xml and
employees.xml documents. You manually type the XML schema components for the
regions.xml file and use the visual XML Schema Editor in JDeveloper to build the XML
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

schema document for employees.xml. Then you use the XML schema documents to validate
their respective XML documents.

Tasks
1. Create an XML schema for the regions.xml document.
a. Add the /home/oracle/labs/lab/regions.xsd file to your project. You notice
that it contains a skeleton XML schema with the <schema> element. Click the Source
tab to view the source code. s a
) h a
b. Edit regions.xsd to add the W3C XML Schema namespace to the <schema> start
r
sฺ eฺs
tag of the root element, using the namespace prefix xsd. Ensure that the start and end
tags of the <schema> element are qualified with the namespace prefix.
m
e uid
c @
c. Within the <xsd:schema> element, create a child element
r o vinamednregions,
t G but do
not add the type attribute.
a jsto Stude
d. Within the regions element declaration, create
a ฺ m anhanonymous
i s <complexType>
containing a <sequence> that encloses
n e t named region with its
tin oneselement
e
al to u
maxOccurs attribute set to unbounded.
e. Within the region element ( v
c nse add an anonymous <complexType> that also
declaration,
v i
o with
torthe
contains a <sequence>
l i etwo child element declarations: the first one named
cattribute
region_idjswith l e value of xsd:int, and the second named
a
M fewith
region_name b type
ra the type attribute value of xsd:string.
i na
f. ntCheck the s
l e t r a nwhen
syntax and validate the XML schema document. Save the XML schema
n- the Designthere
Va g.noClick
document are no syntax or validation errors.
tab to view the XML schema in visual display mode, and expand the
nodes to show the complete XML schema structure.
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, and set it to the URL
value http://www.w3.org/2001/XMLSchema-instance. Set the
xsi:noNamespaceSchemaLocation attribute to the region.xsd XML schema.
Observe the errors, if any, in the Structure pane. In the Applications Navigator, right-
click regions.xml and select Validate XML from the context menu.
b. Correct all the validation errors, ensure that the XML syntax is correct, save the
changes, and validate the regions.xml XML document by using the oraxml
command-line utility.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 3
3. Using JDeveloper XML Schema Editor, create an XML schema for the employees.xml
document.
a. Create a new XML schema by right-clicking the XMLLabsProject node and
selecting New > From Gallery. Expand the General category and select the XML
node. Double-click the XML Schema item. In the Create XML Schema file dialog box,
enter employees.xsd for the file name and then click OK.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Note: JDeveloper creates the XML schema and displays it in the XML Editor Design
tab window, with a default <schema> and <exampleElement> element.
b. Click the Design tab. 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 the text values.
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
s a
a
Inspector pane (on the right), click and replace the text property field value containing
h
“A sample element” with “The employees element.”
s ฺrs) ฺ
@ em uide
d. Create the employee child element in a sequence by right-clicking the employees
element and selecting Insert inside element > sequence. Then right-click the
v ic t G
sequence object and select Insert inside sequence > element. Change the element1
o
name to employee. t o r den
s
aj s Stu
a ฺ m
e. Select the employee element and change the maxOccurs property in the Property
hi
i n
Inspector pane to the unbounded value.
t e t
l en o us
f. Create four child elements of the employee by using the Components Palette. Locate
a
i c (v se t
and drag the sequence object to the employee element on the Design tab. Drag four

t o rov licen
element objects to the employee sequence object. Change the name of the four
js ble
elements, starting from the top, to employee_id, first_name, last_name, and
a
M fera
salary, respectively. Save the changes to the XML schema document.
a
n t i n s
g. Right-click the employee_id element, select Set Type, and then enter the xsd:int
n
l e r a
Va non-t
value in the blank field (this can also be done by 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.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 4
Solution 5-1: Validating XML by Using an XML Schema

Overview
In this practice, you create XML schema documents for the regions.xml and
employees.xml documents. You manually type the XML schema components for the
regions.xml file and use the visual XML Schema Editor in JDeveloper to build the XML
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

schema document for employees.xml. Then you use the XML schema documents to validate
their respective XML documents.

Steps
1. Create an XML schema for the regions.xml document.
a. Add the /home/oracle/labs/lab/regions.xsd file to your project. You notice
that it contains a skeleton XML schema with the <schema> element. Click the Source
tab to view the source code.
s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 6
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Double-click the regions.xsd document.

Click View > Editor > Source from the menu bar.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 7
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

b. Edit regions.xsd to add the W3C XML Schema namespace to the <schema> start
tag of the root element, using the namespace prefix xsd. Ensure that the start and end
tags of the <schema> element are qualified with the namespace prefix.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 8
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

c. Within the <xsd:schema> element, create a child element named regions, but do
not add the type attribute.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

d. Within the regions element declaration, create an anonymous <complexType>


containing a <sequence> that encloses one element named region with its
maxOccurs attribute set to unbounded.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

e. Within the region element declaration, add an anonymous <complexType> that also
contains 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. s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

f. Check the syntax and validate the XML schema document. Save the XML schema
document when there are no syntax or validation errors.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 11
Note: The Validate XML command that is active for an XML schema document
validates the XML schema document against an XML schema based on the W3C XML
Schema Recommendation that is already registered with JDeveloper.

g. Click the Design tab to view the XML schema in visual display mode, and expand the
nodes using the plus sign to show the complete XML schema structure.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

The XML schema for regions.xsd should look like the following screenshot.

s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s ic
to le ldocument
2. Associate the regions.xml with the regions.xsd XML schema and validate
a
M feraband add the XML namespace prefix xsi, and set it to the URL
the XML instance document.
a. Edit a
in http://www.w3.org/2001/XMLSchema-instance.
tvalue s
regions.xml
l e n r a n Set the
n-t
Va noxsi:noNamespaceSchemaLocation attribute to the region.xsd XML schema.
Observe the errors, if any, in the Structure pane. In the Applications pane, right-click
regions.xml and select Validate XML from the context menu.

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


<regions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="regions.xsd">
<region id="1">
<name>Europe</name>
</region>
<region id="2">
<name>Americas</name>
</region>
<region id="3">
<name>Asia</name>
</region>
<region id="4">
<name>Middle East and Africa</name>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 12
</region>
</regions>

The XML Validation Errors - Log looks like the following screenshot:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 13
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le n-tvalidation
r
Va noThe errors indicate that the id attribute is not expected. The region.xsd
XML schema document does not declare an attribute for the <region> element, but it
specifies a <region_id> element. The <name> child element inside the <region>
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 to the XML schema definition.
Note: If the Validate XML context menu option is disabled, use an alternative
validation method such as the oraxml command-line utility that is described in the
following note.
Note: Optionally, if you want to validate the XML document by using the oraxml utility,
perform the following steps:

1) Open a command prompt window, go to the /home/oracle/labs/lab


directory, and then set the Java CLASSPATH. Enter the following code:
cd labs/lab

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 14
export
CLASSPATH=/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2
/xmlparserv2.jar

2) Run the oraxml command-line utility by using the following code:


Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

java oracle.xml.parser.v2.oraxml -schema regions.xml

If you validate region.xml by using the oraxml utility, you see the following
validation errors in the command window:

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i
n us n e t
a l eensure
c
changes, and validate ithe
v e tothatXML
b. Correct all the validation errors,
(regions.xml the XML syntax is correct, save the

o v e n s document by using the oraxml

j s tor le lic
command-line utility.

M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 15
After you make the corrections, the preceding region.xml document is valid with
respect to the XML schema document as reflected by the green square box at the top
right corner of the Code Editor window.
To run the oraxml command-line utility, first set the Java CLASSPATH by using the
following code in a terminal window:
cd /labs/lab
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

export
CLASSPATH=/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2/xmlp
arserv2.jar

Next, run the oraxml command-line utility by using the following code:

java oracle.xml.parser.v2.oraxml –schema regions.xml

s a
When valid, the oraxml utility displays the following message:
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
a l e
c ( vEditor, e to an XML schema for the employees.xml
3. Using JDeveloper XML Schema
document. r o vi ens create

a. Create a a new
o schema
jstXML l e licby right-clicking the XMLLabsProject node and
selecting
a ab Gallery. Expand the General category and select the XML
M Newfe>rFrom
n in Double-click
tnode. a n s the XML Schema item. In the Create XML Schema file dialog box,
l e t r
Va noNote: n- JDeveloper createsforthe
enter employees.xsd the file name, and then click OK.
XML schema and displays it in the XML Editor Design
tab window, with a default <schema> and <exampleElement> element.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 16
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

The XML schema appears in the XML Editor Design tab window, with a default
<schema> and <exampleElement> element:

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 17
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
To view the code for the employees.xsd document, t o n tab:
rclick thedSource
e
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

b. Click the Design tab. 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 the text values.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 18
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 19
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
Note: If you view the XML schema elements on the Source tab, you
m
e seeuthati dthe
default namespace and the targetNamespace attributesvare c @
i both tmodified
G to reflect
r o n
jsto Stude
the value entered for targetNamespace.
a
ฺm node,
c. In the Structure pane, expand the employees
t i n a
e t hisits annotation child node, and
the documentation node. Click e
l n A – Ausample
the s element node and, in the Property
( a
v et
Inspector pane (on the right), click and oreplace the text property field value containing
c
vi ens
“A sample element” with “The employees element.”
r o
to le lic
j s
a rab
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 20
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
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
element1 name to employee.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 21
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 22
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
js bleelement and change the maxOccurs property in the Property
e. Select theaemployee
na M pane
Inspector f e rtoathe unbounded value.
i
nt tran s
l e
Va non-

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 23
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 24
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r s
sฺ eฺ
m
e uid
c @
Note: The XML Schema Editor automatically adds a required
r o vi <complexType>
n t G parent

jsto Stude
element that encloses the <sequence> and its children.
a
n a ฺmelement
t hisby using the Components
f. Create four child elements of the employee
t i e
l en oobject
palette. Locate and drag the sequence
a us to the employee element on the
Design tab. Drag four element
c v objectst
( se the employee sequence object. Change the
to
i
ov licrespectively.
name of the four elements,
last_name,sand t o rsalary, en from Save
starting the top, to employee_id, first_name,
the changes to the XML schema
a j l e
M ferab
document.
a
e n tin ans
l Note:n-trYou can
Va noemployee
also add a sequence object to the employee element by clicking the
element first, and then click the sequence component in the Components
palette.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 25
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 26
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 27
g. Right-click the employee_id element, select Set Type, and then enter the xsd:int
value in the blank field (this can also be done by 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 28
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 29
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 30
Practice 5-2: Using JDeveloper XML Schema Editor

Overview
In this practice, you register the employees.xsd XML schema in JDeveloper, and then you
associate and validate the employees.xml document with its XML schema. You also create a
new employee using the XML schema–aware editor.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks
1. 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 employees.xsd with JDeveloper, select Tools > Preferences and click
the XML Schemas node.
s a
b. h a
Click Add (the green plus icon) to open the Register Schema for File Type window.
In the Add a Schema from the file system or a URL: field, enter the URL
s ฺrs) ฺ
@ em uide
file:/home/oracle/labs/lab/employees.xsd or browse for and select the
/home/oracle/labs/lab/employees.xsd file. In the Extension field, enter the
o v ic t G
value .xml. Click OK to close the Register Schema for File Type window, and
t o r den
OK to close the Preferences window.
s
confirm whether the XSD file is added to the User XML Editing Schemas list. Click
aj s Stu
a ฺ m hi
c. i n t
Edit employees.xml in the XML Editor by modifying the default namespace to have
t e
l en o us
the same value as targetNamespace defined in the employees.xsd XML schema
a
i c (v se t
document. Ensure that the employees.xml document has a well-formed syntax, and
save the changes.
t o r ov licen
d. js ble
Right-click employees.xml and select Validate XML. What is the result?
a
a M fera
2. Create
n i n s
t a newanemployee using the XML schema–aware editor.
l e t r
Va a.noEdit n-employees.xml and add a blank line after the last <employee> element, just
before the </employees> end tag. Enter the less-than (<) character and wait for
code-insight to display the available choices.
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 and JSP/HTML > End Tag Completion option, the </employee> end tag is
automatically added; otherwise, enter the </employee> end tag.
c. Create a blank line between the start and end tags of the new <employee> element.
In the blank line, enter the less than (<) character and wait for code-insight to display
your choices. The available choices are: employee_id, first_name, last_name,
and salary.
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
the text Greenberg, and the <salary> element with 12000.
e. Save the changes to the document, and right-click employees.xml and select
Validate XML.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 31
Solution 5-2: Using JDeveloper XML Schema Editor

Overview
In this solution, you register the employees.xsd XML schema in JDeveloper, and then you
associate and validate the employees.xml document with its XML schema. You also create a
new employee using the XML schema–aware editor.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Steps
1. 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 employees.xsd with JDeveloper, select Tools > Preferences and click
the XML Schemas node.
s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 32
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
b. Click Add (the green plus icon)lto
a enopenotheuRegister
s Schema for File Type window.
In the Add a Schema from
c v
( sethe file t
system or a URL: field, enter the URL
o v i n
file:/home/oracle/labs/lab/employees.xsd or browse for and select the
r li
to OKleto close c
/home/oracle/labs/lab/employees.xsd e file. In the Extension field, enter the
value .xml.a jsClick b the Register Schema for File Type window, and
confirm M
a sfe whether r a
the XSD file is added to the User XML Editing Schemas list. Click
t i n
a len n-tran
OK to close the Preferences window.
V no

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 33
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 34
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
c. Edit employees.xml in
i c (vXMLsEditor
the e t by modifying the default namespace to have
the same value as r v en
otargetNamespace defined in the employees.xsd XML schema
t o l i c
a js that
document. Ensure
b l ethe employees.xml document has a well-formed syntax, and
a M fera
save the changes.

n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 35
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n
n Validate e t
d. Right-click employees.xml a
v l
andeselect
t o us XML. What is the result?
v i c ( nse
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t
2. 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. Enter the less-than (<) character and wait for
code-insight to display the available choices.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 36
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 37
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
b. Double-click the employeevoption l e
a andtoenter the greater-than (>) character to close the
start tag of the element. i cIf
(
you seenabled the Tools > Preferences > Code Editor
have
o v e n
> XML > End Tag
j s tor enter
l e lic</employee>
Completion option, the </employee> end tag is automatically

M a rab
added; otherwise, the end tag.

n t ina ansfe
le r
Va non-t

c. Create a blank line between the start and end tags of the new <employee> element.
In the blank line, enter the less than (<) character and wait for code-insight to display
your choices. The available choices are: employee_id, first_name, last_name,
and salary.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 38
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
d. From the code-insight choices offered, select employee_id and enclose the
108 between the start and end tags. Repeat the process and insert thesฺ
r s value

<first_name> element containing the text Nancy, the <last_name>e m element


i d eฺwith
the text Greenberg, and the <salary> element with 12000. v i c@ t Gu
t o ro den
<?xml version='1.0' encoding="UTF-8"?> s
aj s Stu
<!DOCTYPE employees SYSTEM "employees.dtd">
a ฺ m hi
t i n e t
<employees xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
a l en o us
xsi:schemaLocation="http://www.hr.com/employees
employees.xsd"
i c (v se t
t o rov licen
xmlns="http://www.hr.com/employees">
a js bEmployee
<!-- Company le Data -->
M
a sfer
<employee> a
t i n
a l e - r an
n t<employee_id>103</employee_id>
V non <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>
<first_name>Nancy</first_name>
<last_name>Greenberg</last_name>
<salary>12000</salary>
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 39
</employee>
</employees>

e. Save the changes to the document, and then right-click employees.xml and select
Validate XML.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 5: Validating XML Documents with XML Schemas


Chapter 5 - Page 40
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G6:
Lesson
t o r den
s
aj s XML
Navigating S tu Documents
ฺ m
a XPath
with hi
t i n e t
a l en Chapter
o us 6
v
c ( nse t
v i
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 1
Practices for Lesson 6: Overview

Practices Overview
In this practice, you experiment with writing XPath expressions and testing them using the
XPath Search option available in JDeveloper.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 2
Practice 6-1: Navigating XML Documents by Using XPath

Overview
In this practice, you experiment with writing XPath expressions and testing them using the
XPath Search option available in JDeveloper.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks

1. In XMLLabsProject, edit the root element in employees.xml to remove all the xmlns
and xsi:schemaLocation attributes. Save the changes to your document.

2. Select Search > XPath Search to write XPath expressions for the following scenarios:
Note: There may be multiple ways to write the XPath expression.
a. Locate all employee elements. a
b. Locate the first employee element. h a s
c. Locate the last employee element.
s ฺrs) ฺ
d. Locate the last two employee elements.
@ em uide
e. Find all the last name elements.
o v ic t G
f. Obtain the text for the first names of all employees.tor d e n
g. Locate employee elements containing a salary
s
aj lesssthan
S tu5000.
a ฺ m hi
h. List the salary elements greater than t i n5000.
e t
i. Find employees with first names a l enbeginning
o uswith the letter A or last names beginning
with the letter P.
v
c ( nse t
v i
j. Obtain the comment
t o ro for the
l i e element.
croot
s
aj rable
3. Test XPath
M
a expressions
f e with the departments.xml document that declares and uses a
t i n
n traprefix s
n called dept for some of its elements.
a lenamespace-
n departments.xml and add all the <department> element data in the external
V a.noEdit
file in /home/oracle/labs/lab/newdepts.txt. Save the changes to
department.xml and start XPath Search.
Note: Ignore any errors that might be displayed in the Structures pane.
b. Locate department elements without a namespace.
c. Find all department nodes whether qualified by a namespace or not.
Hint: Use the node() function to locate any node and the local-name() function to
compare the node name with the string 'department'.
d. Locate all department elements without a <manager_id> element.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 3
Solution 6-1: Navigating XML Documents by Using XPath

Overview
In this practice, you experiment with writing XPath expressions and testing them using the
XPath Search option available in JDeveloper.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Steps
1. In 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="UTF-8"?>


<!DOCTYPE employees SYSTEM "employees.dtd">
<employees>
<!-- Company Employee Data -->
<employee> s a
h a
ฺrs) ฺ
<employee_id>103</employee_id>
<first_name>Alexander</first_name>
s
<last_name>Hunold</last_name>
<salary>9000</salary> @ em uide
</employee>
o v ic t G
... t o r den
<employee> s
aj s Stu
<employee_id>108</employee_id> a ฺ m hi
t i n e t
<first_name>Nancy</first_name>
a l en o us
<last_name>Greenberg</last_name>
i
<salary>12000</salary>c (v se t
</employee>
t o rov licen
</employees>
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 4
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
i n
nt tran>sXPath Search to write XPath expressions for the following scenarios:
2. le
Select Search
-
Va Note:
nonThere may be multiple ways to write the XPath expression.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 5
The XPath Search - Log pane is displayed as follows:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o r ov licen
js blelements.
a. Locate all employee
a e
M
You can use anyraof the following XPath expressions:
n t ina ansfe
le n-tr
Va no/employees/employee
employees/employee
//employee

Note: To view employees detail data, you must drill down on the nodes. To drill down
on an employee, click the + sign next to the employee element. In this practice, use
XPath Version 1.0. By default, XPath version 2.0 is selected from the drop-down list.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 6
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

b. Locate the first employee element.


You can use any of the following XPath expressions:
s a
/employees/employee[1]
h a
employees/employee[1]
//employee[1] s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
c. Locate the last employee element.
You can use any of the following XPath expressions:

/employees/employee[last()]
employees/employee[last()]
//employee[last()]

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 7
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

d. Locate the last two employee elements.


You can use any of the following XPath expressions: a
h a s
/employees/employee[position()>=last()-1]
s ฺ r s)
employees/employee[position()>=last()-1]
e m ideฺ
//employee[position()>=last()-1]
v i c@ t Gu
t o ro den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
e. Find all the last name elements.
You can use any of the following XPath expressions:
//last_name
/employees/employee/last_name
employees/employee/last_name

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 8
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

f. Obtain the text for the first names of all employees.


You can use any of the following XPath expressions:
s a
//first_name/text()
h a
employees/employee/first_name/text()
/employees/employee/first_name/text() s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
g. Locate employee elements containing a salary less than 5000.
You can use any of the following XPath expressions:
//employee[number(salary)<5000]
employees/employee[number(salary)<5000]
/employees/employee[number(salary)<5000]
//employee[salary<5000]

Note: To display the actual salaries that meet the condition, you can add
/salary/text() to the end of each of the preceding expressions, as in the following
example:
employees/employee[number(salary)<5000]/salary/text()

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
h. List the salary t o rov ligreater
elements c en than 5000.
a jsany ofbthelefollowing XPath expressions:
a M fera
You can use

n i n s
t//employee/salary[.>5000]
//employee/salary[number(.)>5000]
n
l e r a
n-t
Va no//employee/salary[number(text())>5000]
//employee/salary[text()>5000]

Note: To display the actual salaries that meet the condition, you can add /text() to
the end of each of the preceding expressions, as in the following example:
//employee/salary[number(.)>5000]/text()

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

i. Find employees with first names beginning with the letter A or last names beginning
with the letter P.
//employee[starts-with(first_name,'A') or starts-
with(last_name,'P')]
s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
j. ObtainM
na f e ra for the root element.
the comment
i
ntYou tcana n s
use any of the following XPath expressions:
l e r
n-
Va noemployees/comment()
/employees/comment()

3. Test XPath expressions with the departments.xml document that declares and uses a
namespace prefix called dept for some of its elements.
a. Edit departments.xml and add all the <department> element data in the external
file in /home/oracle/labs/lab/newdepts.txt. Save the changes to
department.xml and start XPath Search.
Note: Ignore any errors that might be displayed in the Structures pane.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 11
<?xml version='1.0' encoding="UTF-8"?>
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
]>
<departments xmlns:dept="http://www.hr.com/departments">
<dept:department num="1">
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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>
s a
</dept:department>
h a
ฺrs) ฺ
<department num="3">
<department_id>30</department_id>
s
<department_name>Purchasing</department_name>
@ em uide
<manager_id>114</manager_id>
o v ic t G
<location_id>1700</location_id>
t o r den
</department> s
aj s Stu
<department>
a ฺ m hi
i n
<department_id>40</department_id>
t e t
a l en o us
<department_name>Human Resources</department_name>

</department> i c (v se t
<location_id>2400</location_id>

</departments>
t o rov licen
a js ble
a
b. Locate
n
Mdepartment
f e ra elements without a namespace.
i s
nt/departments/department
an
a l e - t r
V non

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


Hint: Use the node() function to locate any node and the local-name() function to
compare the node name with the string 'department'.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 12
You can use any of the following XPath expressions:

/departments/node()[local-name()='department']
/departments/node()[local-name(.)='department']
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s
Note: From the previous three examples, you must now realize that it is not a good a
h a
ฺrs) ฺ
practice to apply a namespace prefix to a subset of elements in a document; applying a
s
namespace prefix complicates the process of locating related elements. Remember
em uide
that it is a good practice to use a namespace prefix to qualify all the elements in your
@
document to avoid ambiguity of element names.
o v ic t G
t o r den
s
aj s Stuelement.
d. Locate all department elements without a <manager_id>
a ฺ m hi
t i n e
n expressions: t
You can use any of the following
v a l eXPath
t o us
i c ( se
departments/node()[local-name()='department' and

rov licen
boolean(not(manager_id))]
t o
js ble
a
M fera
//node()[local-name()='department' and not(manager_id)]
a
tin ans
e n r
l //node()[local-name()='department'][not(manager_id)]
Va non-t

Note: The <department> element with an attribute is not included. This is because
the <manager_id> element exists, but as an empty element. To include the
<department> element with the empty <manager_id/> element, add the following

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 13
condition to the predicate: manager_id='', where the manager_id element is
compared to an empty string.

Example:
//node()[local-name()='department' and (not(manager_id)) or
manager_id='']
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 6: Navigating XML Documents with XPath


Chapter 6 - Page 14
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G7:
Lesson
t o r den
s
aj s StuXML
Transforming
a ฺ m
Documents hi with XSL
t i n e t
a l en Transformations
o us
v t
c ( nse Chapter 7
v i
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 1
Practices for Lesson 7: Overview

Practices Overview
In this practice, you write XSL Transformations (XSLT) template rules to transform an XML
document into an HTML page for display in a browser or on the web, or to transform it into
another XML document.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 2
Practice 7-1: Transforming XML by Using XSL Transformations

Overview
In this practice, you write XSL Transformations (XSLT) template rules to transform an XML
document into an HTML page for display in a browser or on the web, or to transform it into
another XML document.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks

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


a. In XMLLabsProject, create a new XSL style sheet called departments.xsl.
b. In the root template rule of the departments.xsl skeleton document:
1) Specify HTML as the output format as follows:
s a
<xsl:output method="html" omit-xml-declaration="yes"/>
h a
2) Create a level one HTML header by using an <h1> tag that containsฺthe
s rs)text
Departments, followed by the <xsl:apply-templates/> element.
e m ideฺ
c. Create a template rule for the department element to create
v i c@ u HTML
the following
t G
structure:
t o ro den
<h2>department_name</h2> a j s S tu
<ul> a ฺ m his
t i n e t
en o us
<li>Id: department_id</li>
<li>Manager: manager_id</li> a l
i c (v se t
<li>Location: location_name</li>
</ul>
t o rov licen
Note: The a js ble information must appear only if it exists as an element and is
manager_id
a
not empty.
n ra
M Thefelocation_name is derived by mapping a location_id value of
t i
nHint: Use
1700 to
a n s
Seattle, and 2400 to London; otherwise, output the value unchanged.
l e t r
on-
Va nlocation_id <xsl:if> to test manager_id and <xsl:choose> to test
.
d. Check the XML syntax and perform a Validate XML operation on the
departments.xsl document. Correct any errors and save the style sheet.
e. Open departments.xml in the XML Editor, remove the dept namespace prefix and
declaration from the root element, and then remove the dept prefix from the start and
end tags of the first two <department> elements.
f. In departments.xml, add the xml-stylesheet processing instruction to
associate with the departments.xsl style sheet created. Save the changes.
g. Observe the transformation to departments.xml in the browser.
Note: Correct any errors until you obtain the desired transformation.
2. Transform the employees.xml document into a web page by making the requested
modifications to the skeleton employees.xsl style sheet provided.
a. In XMLLabsProject, add or open the /home/oracle/labs/lab/employees.xsl
file.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 3
b. Attach the employees.xsl style sheet to the employees.xml document. Save the
changes and display employees.xml in the browser. What do you observe?
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
Information heading. Save the changes and display employees.xml in the
browser. What is the order of employee elements?
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

d. Modify employees.xsl to sort the employee elements by their last names. Save the
changes and display the result in the browser.
e. Examine the last template rule for the salary elements in the employees.xsl file.
Note that this template rule formats the style of the salary value in bold or italic based
on a condition. Alter the employee element template rule to apply the salary
template rule, instead of outputting the value of salary, in the last cell of each row of
the HTML table. Save the changes and view the results in the browser.
f. In the employees.xsl file, modify the salary template rule to accept a parameter
s
called minsal, and using the select attribute, assign it a default value of 4500. a
h a
ฺrs) ฺ
Modify the condition to reference the minsal parameter. Save the change and display
the results in the browser. s
em uide
g. Modify the employee template rule to pass a parameter value of 8000 to the salary
@
v ic t G
template rule when it is applied. Use the parameter template body to supply the
o
o r den
parameter value. Save the changes and display the results in the browser.
t
3.
s
aj s Stu
Transform the departments.xml file into an HTML file by using the JDeveloper XSL
utility. a ฺ m hi
t i n e t
a l en o us
a. Open departments.xsl and comment out the following line:

i c (v se t
<xsl:output method="html" omit-xml-declaration="yes"/>

t o rov licen
b. Right-click in the window and select Run from the context menu. Alternatively, click the
Run button on the toolbar. In the "How should the target be started?" window, select
a js ble
as an XSLT, and then click OK.
a M fera
i n s
c. In the XSLT Settings window, click Browse and select
n t n
l e r a
/home/oracle/labs/lab/departments.xml as the input XML file. Then, enter
Va non-t /home/oracle/labs/lab/departments.html in the Output File field, and then
click OK.
d. View departments.html in your web browser. You can also view the file in
JDeveloper by using the File > Open menu option. What do you observe?
e. Open departments.html in JDeveloper by using the File > Open menu option to
view the HTML tags, and notice the first line of the HTML document. Why is the first
line present? Is it relevant for this kind of file?
f. Modify departments.xsl to include (un-comment) the <xsl:output
method="html" omit-xml-declaration="yes"/> element by using 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.
g. Delete the departments.html file from the disk.
Issue the following commands:
$ cd labs/lab
$ rm departments.html

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 4
h. In JDeveloper, run the XSLT again to view the transformed document. Open
departments.html in JDeveloper and verify that the XML declaration is no longer
present.
4. Using the oraxsl command-line utility, transform 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.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

a. In XMLLabsProject, create a new XSL style sheet with the name


departments_new.xsl.
b. In departments_new.xsl, add to the document root template the <xsl:element>
element to create a new output element called DEPARTMENTS. Within the template
body of <xsl:element>, include the <xsl:apply-templates/> element.
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, department. Save the
changes. s a
h a
ฺrs) ฺ
Hint: Use the local-name() XPath function enclosed in braces (using the attribute
template value technique) to determine the current element name.
s
d. em uide
Run the oraxsl command-line utility on the departments.xml file, and specify the
@
o v ic t G
style sheet departments_new.xsl. You can view the generated XML document
o r den
element in the command prompt window. What do you observe?
t
e. s
aj s Stu
Edit departments_new.xsl, modify <xsl:template> for the department rule,
a ฺ m hi
and use the <xsl:for-each> element inside the <xsl:element> template body to
t i n e t
a l en o us
process all the children of the department node. For each child element, create an

i c (v se t
attribute by using the <xsl:attribute> element with the attribute name set to the
name of the child node and the attribute value set to the child text value. Save the
changes.
t o rov licen
js ble
Hint: Use the local-name() XPath function enclosed in braces to derive the node
a
M fera
name of the child elements, and use <xsl:value-of> to obtain the node text as the
a
n t i n
value. n s
l e r a
Va f. non-tRun the oraxsl command-line utility on the departments.xml file, and specify the
style sheet departments_new.xsl. You can view the generated XML document
element in the command prompt window. What do you observe?

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 5
Solution 7-1: Transforming XML by Using XSL Transformations

Overview
In this practice, you write XSL Transformations (XSLT) template rules to transform an XML
document into an HTML page for display in a browser or on the web, or transform it into another
XML document.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Steps
1. Transform the departments.xml document into a web page.
a. In XMLLabsProject, create a new XSL style sheet called departments.xsl.
Right-click the XMLLabsProject project node and select New > From Gallery from
the context menu. The New Gallery window is displayed. Expand the General
category, click XML, and then double-click XSL Style Sheet in the Items list. The
Create XSL File window appears. Enter departments.xsl as the file name. Select
XSL 1.0 from the Select Version drop-down list, and then click OK. s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 6
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
v
rocomplete n document:
eskeleton
The following tisothe l i c
a js ble encoding="UTF-8"?>
M fera version="1.0"
<?xml version='1.0'
a
n i n s
txmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:stylesheet
n
l e r a
t Root template -->
Va non-<!--
<xsl:template match="/">
</xsl:template>
</xsl:stylesheet>

b. In the root template rule of the departments.xsl skeleton document:


1) Specify HTML as the output format as follows:
<xsl:output method="html" omit-xml-declaration="yes"/>
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:template match="/">
</xsl:template>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 7
</xsl:stylesheet>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

2) Create a level-one HTML header by using an <h1> tag that contains the text
Departments, followed by the <xsl:apply-templates/> element.
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
s a
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
h a
ฺrs) ฺ
<!-- Root template -->
s
em uide
<xsl:output method="html" omit-xml-declaration="yes"/>
<xsl:template match="/"> @
ic t G
<h1>Departments</h1> o v
r den
s t o
<xsl:apply-templates/>
ฺ m aj s Stu
hi
</xsl:template>
t i n a t
</xsl:stylesheet>
l en o us e
a
(v se t
i c
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

c. Create a template rule for the department element to create the following HTML
structure:
<h2>department_name</h2>
<ul>
<li>Id: department_id</li>
<li>Manager: manager_id</li>
<li>Location: location_name</li>
</ul>
Note: The manager_id information must appear only if it exists as an element and is
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 manager_id and <xsl:choose> to test
location_id.
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 8
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->
<xsl:output method="html" omit-xml-declaration="yes"/>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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> s a
h a
ฺrs) ฺ
<xsl:if test="manager_id and manager_id!=''">
<li>Manager: <xsl:value-of select="manager_id"/></li>s
</xsl:if>
@ em uide
o v ic t G
<li>Location:
t o r den
<xsl:choose> s
aj s Stu
ฺ m hi
<xsl:when test="location_id=1700">Seattle</xsl:when>
a
t i n e t
en o us
<xsl:when test="location_id=2400">London</xsl:when>
a l
(v se t
<xsl:otherwise>
i c
rov licen
<xsl:value-of select="location_id"/>
t o
</xsl:otherwise>
js ble
a</xsl:choose>
M fera
t i na s
</li>
l e n r a n
Va non-t
</ul>
</xsl:template>
</xsl:stylesheet>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
d. Check the XML syntax
t o rov andlicperform
en a Validate XML operation on the
a js bdocument.
departments.xsl l e Correct any errors and save the style sheet.

na ra
M thefedepartments.xsl
Right-click document and select Validate XML from the
i
ntcontext a n s
menu.
a l e - t r
V non

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r already
d e n
a j s
Note: The green square box in the preceding screenshot
S t u indicates that the
syntax is valid.
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
lee. Open
n-tr from the root element, and then remove the dept prefix from the start and
departments.xml in the XML Editor, remove the dept namespace prefix and
Va nodeclaration
end tags of the first two <department> elements.
<?xml version='1.0' encoding="UTF-8"?>
<!DOCTYPE departments SYSTEM "departments.dtd" [
<!ATTLIST department num CDATA #IMPLIED>
]>
<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>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 11
<department_name>Marketing</department_name>
<manager_id>201</manager_id>
<location_id>Toronto</location_id>
</department>
<department num="3">
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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>
s a
<location_id>2400</location_id>
h a
</department>
s ฺrs) ฺ
</departments>
@ em uide
o v ic t G
f. In departments.xml, add the xml-stylesheet processing
t o e n
r dinstruction to
associate with the departments.xsl style sheet s
aj created.
S tuSave the changes.
a ฺ m his
<?xml version='1.0' encoding="UTF-8"?>
t i n e t
l
<?xml-stylesheet type="text/xsl"
a en o ushref="departments.xsl"?>
<!DOCTYPE departments
i c (v SYSTEM
s e t "departments.dtd" [
rov licnum
<!ATTLIST department
t o en CDATA #IMPLIED>
]>
a js ble
M fera
<departments>
a
n t i n n s
<department num="1">
l e r a
Va non-t<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>
<department num="3">
<department_id>30</department_id>
<department_name>Purchasing</department_name>
<manager_id>114</manager_id>
<location_id>1700</location_id>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 12
</department>
<department>
<department_id>40</department_id>
<department_name>Human Resources</department_name>
<location_id>2400</location_id>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

</department>
</departments>

Click Save in the toolbar to save your changes.


Issue the following commands in a terminal window to validate the departments.xml
document.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s tu
aj instheSbrowser.
g. Observe the transformation to departments.xml ฺ m hitransformation.
athe desired
t i n
Note: Correct any errors until you obtain
e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 13
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
-t the employees.xml document into a web page by making the requested
V2.a Transform
n o n
modifications to the skeleton employees.xsl style sheet provided.
a. In XMLLabsProject, add or open the /home/oracle/labs/lab/employees.xsl
file.
<?xml version='1.0' encoding="UTF-8"?>
<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>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 14
</xsl:template>

<xsl:template match="employees">
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!-- Add instructions here -->


</table>
</xsl:template>

<xsl:template match="employee">
<tr>
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/>,
s a
<xsl:value-of select="first_name"/></td>
h a
<td><xsl:value-of select="salary"/></td>
s ฺrs) ฺ
</tr>
@ em uide
</xsl:template>
o v ic t G
<xsl:template match="salary">
t o r den
<xsl:choose> s
aj s Stu
a ฺ m
<xsl:when test="number(.)&lt;5000">
hi
t i n e t
a l en o us
<b><xsl:value-of select="."/></b>
</xsl:when>
i c (v se t
rov licen
<xsl:otherwise>
t o
js ble
<i><xsl:value-of select="."/></i>
a
M fera
</xsl:otherwise>
t i na s
</xsl:choose>
l e n r a n
Va non-t
</xsl:template>
</xsl:stylesheet>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 15
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 16
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 17
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
b. Attach the employees.xsl style sheet to the employees.xml document. Save the
changes and display employees.xml in the browser. What do you observe?
<?xml version='1.0' encoding="UTF-8"?>
<?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>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 18
<employee_id>104</employee_id>
<first_name>Bruce</first_name>
<last_name>Ernst</last_name>
<salary>6000</salary>
</employee>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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>
s a
<first_name>Valli</first_name>
h a
<last_name>Pataballa</last_name>
s ฺrs) ฺ
<salary>4800</salary>
@ em uide
</employee>
o v ic t G
<employee>
t o r den
<employee_id>107</employee_id> s
aj s Stu
<first_name>Diana</first_name>
a ฺ m hi
t i n e t
a l en o us
<last_name>Lorentz</last_name>

i c (v se t
<salary>4200</salary>

rov licen
</employee>
t o
js ble
<employee>
a
M fera
<employee_id>007</employee_id>
t i na s
<first_name>James</first_name>
l e n r a n
Va non-t
<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>
</employees>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 19
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Note: The browser displays only 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 descendant elements are not processed.

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
Information heading. Save the changes and display employees.xml in the
browser. What is the order of employee elements?
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 20
<!-- Document Root template -->
<xsl:template match="/">
<html>
<head><title>Employee Information</title></head>
<body>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<h1>Employee Information</h1>
<!-- Add instructions here -->
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="employees">
<table border="1">
s a
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
h a
<!-- Add instructions here -->
s ฺrs) ฺ
<xsl:apply-templates/>
@ em uide
</table>
o v ic t G
</xsl:template>
t o r den
s
aj s Stu
<xsl:template match="employee">
a ฺ m hi
t i n e t
a l en o us
<tr>
i c (v seselect="employee_id"/></td>
t
rov licen select="last_name"/>,
<td><xsl:value-of
t o
js <xsl:value-of
<td><xsl:value-of
M a a b le select="first_name"/></td>
r
n t ina an<td><xsl:value-of
sfe select="salary"/></td>
le tr</tr>
Va non-</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>
</xsl:template>
</xsl:stylesheet>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 21
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a j sto Stude
Note: By default, the data from the employee
a ฺ m elements
h i s is processed and displayed in
the order of their appearance in the XML
e n tin se t
input document.

( v al the employee
t o u
d. Modify employees.xsl
v i c to sort
n s e elements by their last names. Save the
changes and display
t o ro the result
l i ce in the browser.
s
aj rable
<?xml M
a version='1.0'
f e encoding="UTF-8"?>
t i n s
a - t an
len <xsl:stylesheet
r version="1.0"
V non 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>
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 22
<!-- Add instructions here -->
<xsl:apply-templates>
<xsl:sort select="last_name"/>
</xsl:apply-templates>
</table>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

</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>
s a
</xsl:template>
h a
<xsl:template match="salary">
s ฺrs) ฺ
<xsl:choose>
@ em uide
<xsl:when test="number(.)&lt;5000">
o v ic t G
<b><xsl:value-of select="."/></b>
t o r den
</xsl:when> s
aj s Stu
<xsl:otherwise>
a ฺ m hi
t i n e t
a l en o us
<i><xsl:value-of select="."/></i>
</xsl:otherwise>
i c (v se t
rov licen
</xsl:choose>
t o
js ble
</xsl:template>
a
M fera
</xsl:stylesheet>
t i na s
l e n t r a n
Va noThe n-browser displays the employee table entries sorted by their last names:

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 23
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
jsto Sintthe e
udemployees.xsl
e. Examine the last template rule for the salarya elements file.
ฺ m i s
on a condition. Alter the employee n t ina stemplate
Note that this template rule formats the style
element
of
e th rule to apply the salary
the salary value in bold or italic based

v a le thetovalue
template rule, instead of outputting u of salary, in the last cell of each row of
the HTML table. Save
(
c changes
ithe seand view the results in the browser.
o v e n
j s tor le lic
M a rab
n t ina aversion='1.0'
<?xml
n sfe encoding="UTF-8"?>
le <xsl:stylesheet
-tr
Va nonxmlns:xsl="http://www.w3.org/1999/XSL/Transform">
version="1.0"

<!-- 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>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 24
<!-- Add instructions here -->
<xsl:apply-templates>
<xsl:sort select="last_name"/>
</xsl:apply-templates>
</table>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

</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"/-->
s a
<xsl:apply-templates select="salary"/>
h a
</td>
s ฺrs) ฺ
</tr>
@ em uide
</xsl:template>
o v ic t G
<xsl:template match="salary">
t o r den
<xsl:choose> s
aj s Stu
a ฺ m
<xsl:when test="number(.)&lt;5000">
hi
t i n e t
a l en o us
<b><xsl:value-of select="."/></b>
</xsl:when>
i c (v se t
rov licen
<xsl:otherwise>
t o
js ble
<i><xsl:value-of select="."/></i>
a
M fera
</xsl:otherwise>
t i na s
</xsl:choose>
l e n r a n
Va non-t
</xsl:template>
</xsl:stylesheet>

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

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 25
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
ajtemplate S tu
f. In the employees.xsl file, modify the salary
a ฺ m h is rule to accept a parameter
called minsal, and using the select i n
ntminsal e
attribute, t
assign it a default value of 4500.
Modify the condition to reference a l ethe u s parameter. Save the changes and
( v t o
display the results in the
o v ic nse
browser.

t o
<?xml version='1.0' r l i e
cencoding="UTF-8"?>
s
aj rablversion="1.0"
<xsl:stylesheet e
M
ina<!-- fe
xmlns:xsl="http:002F/www.w3.org/1999/XSL/Transform">
n t a n s
l e t r Document Root template -->
Va non-<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>
<xsl:sort select="last_name"/>
Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 26
</xsl:apply-templates>
</table>
</xsl:template>

<xsl:template match="employee">
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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>
s a
</xsl:template>
h a
s ฺ r s)
<xsl:template match="salary">
e m ideฺ
<xsl:param name="minsal" select="4500"/> @
v i c t Gu
<xsl:choose>
t o ro den
s
aj s Stu
<xsl:when test="number(.)&lt;$minsal">
a ฺ m
<b><xsl:value-of select="."/></b>
hi
t i n e t
</xsl:when>
a l en o us
<xsl:otherwise>
i c (v seselect="."/></i>
t
rov licen
<i><xsl:value-of
t o
js ble
</xsl:otherwise>
a
M fera
</xsl:choose>
i na s
t</xsl:stylesheet>
</xsl:template>
l e n r a n
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 27
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
g. Modify the employee template rule to pass a parameter value@ em touthe
of 8000 idesalary
template rule when it is applied. Use the parameter template c
vi bodyntot supply
G the
parameter value. Save the changes and display theto r o
results in d ebrowser.
the
a j s t u
<?xml version='1.0' encoding="UTF-8"?>
a ฺ m his S
<xsl:stylesheet version="1.0"
e n tin se t
v l to u
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
atemplate
<!-- Document Root
i c ( s e -->
v
ro match="/"> n
<xsl:template
s t o l i ce
M aj rable
<html>

i na s f e
<head><title>Employee Information</title></head>

l e nt tr<body>
an<h1>Employee Information</h1>
a
V non -
<!-- 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>
<xsl:sort select="last_name"/>
</xsl:apply-templates>
</table>
</xsl:template>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 28
<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>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<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>

s a
<xsl:template match="salary">
h a
<xsl:param name="minsal" select="4500"/>
s ฺrs) ฺ
<xsl:choose>
@ em uide
<xsl:when test="number(.)&lt;$minsal">
o v ic t G
<b><xsl:value-of select="."/></b>
t o r den
</xsl:when> s
aj s Stu
<xsl:otherwise>
a ฺ m hi
t i n e t
a l en o us
<i><xsl:value-of select="."/></i>
</xsl:otherwise>
i c (v se t
rov licen
</xsl:choose>
t o
js ble
</xsl:template>
a
M fera
</xsl:stylesheet>
t i na s
l e n t r a n
Va noThe n-browser must display salaries less than 8000 in bold and the others in italic:

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 29
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
3. Transform the departments.xml file into an HTML file byro
vic nt G XSL
using theeJDeveloper
utility. a jsto Stud
a. Open departments.xsl and commentaout
n ฺmthe following
t h is line:
t i e
<xsl:output method="html"nomit-xml-declaration="yes"/>
v a le to us
v i c ( nse
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

b. Right-click in the window and select Run from the context menu. Alternatively, click
Run on the toolbar. In the "How should the target be started?" window, select "as
an XSLT" and then click OK.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 30
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

c. In the XSLT Settings window, click Browse and select


/home/oracle/labs/lab/departments.xml as the input XML file. Enter
/home/oracle/labs/lab/departments.html in the Output File field, and then
click OK.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 31
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

a
d. View departments.html in your web browser. You can also view the file in) h
as
s ฺrs ฺ
JDeveloper by using the File > Open menu option. What do you observe?

@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 32
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

e. Open departments.html in JDeveloper by using the File > Open menu option to
view the HTML tags, and notice the first line of the HTML document. Why is the first
line present? Is it relevant for this kind of file?
s a
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vicontainsenansXML declaration. This is generated by the XSLT
The first line of o
j s
Processor because
output
e lic that the output format was for an XML file. That is, the
t it lassumed
outputM
a
documentra b not contain <html> as the root element. No, the XML
does
a f e
in ansis not appropriate for an HTML document. However, most browsers ignore
tdeclaration
l e n the tr declaration because they know that the file contains HTML tags based on the
XML
Va nofilen-extension. Recall that you did comment out the <xsl:output method="html"
omit-xml-declaration="yes"/> in an earlier step in the departments.
f. Modify departments.xsl to include (un-comment) the <xsl:output
method="html" omit-xml-declaration="yes"/> element by using 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.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 33
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺ s
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
ntfrom the s e Issue the following commands:
g. Delete the departments.html l efile u disk.
c ( va e to
vi ens
cd labs/lab
r o
to le lic
rm departments.html
j s
a rab
M
a sferun the XSLT again to view the transformed document. Open
h. InnJDeveloper,
t i an
a len departments.html
- t r in JDeveloper and verify that the XML declaration is no longer
V non present.
Note: To run the XSLT again, click the Run button on the toolbar. The XML declaration
should no longer be present.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 34
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 35
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va noNote: n-t
You can also open the departments.xsl file by using the File > Open menu
option in JDeveloper.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 36
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m hi
a departments.xml
t i
4. Using the oraxsl command-line utility, transform
n n e t into an XML file
called departments_new.xml, where
a e
l to u
the s
children of the <department> element are
converted to attribute names by v
c ( using their
e text part as the attribute values.
a. In XMLLabsProject, v i n s
o lice XSL style sheet with the name
create a new
t o r
js ble
departments_new.xsl.
a
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 37
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
b. In departments_new.xsl, add to the document root template the <xsl:element>
element to create a new output element called DEPARTMENTS. Within the template
body of <xsl:element>, include the <xsl:apply-templates/> element.
<?xml version='1.0' encoding="UTF-8"?>
<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:stylesheet>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 38
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

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, department. Save the
changes.
Hint: Use the local-name() XPath function enclosed in braces (using the attribute
template value technique) to determine the current element name. s a
h a
<?xml version='1.0' encoding="UTF-8"?>
s ฺ r s)
<xsl:stylesheet version="1.0"
e m ideฺ
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
v i c@ t Gu
<!-- Root template -->
t o ro den
<xsl:template match="/"> s
aj s Stu
<xsl:element name="DEPARTMENTS">
a ฺ m hi
t i n e t
en o us
<xsl:apply-templates/>
a l
</xsl:element>
</xsl:template>ic
(v se t
t o
<xsl:templaterov match="department">
l i c en
a js blename="{local-name()}">
<xsl:element
a M fera
n i n s
</xsl:element>
t </xsl:template>
n
l e r a
n-t
Va no</xsl:stylesheet>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 39
d. Run the oraxsl command-line utility on the departments.xml file, and specify the
style sheet departments_new.xsl. You can view the generated XML document
element in the command prompt window. What do you observe?
To use oraxsl command-line utility, perform the following steps:
First, open a command prompt window, go to the /home/oracle/labs/lab
directory, and set the Java CLASSPATH. Enter the following code all on one line:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

cd labs/lab
export
CLASSPATH=/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2/xmlp
arserv2.jar

Then run the oraxsl command-line utility by using the following code all on one line:

java oracle.xml.parser.v2.oraxsl departments.xml


s a
departments_new.xsl h a
s ฺrs) ฺ
The command window displays the following:
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e -t r a
Va noAsnshown in the preceding screenshot, the resulting XML document tree contains a
<DEPARTMENTS> root element and four empty <department/> child elements.

e. Edit departments_new.xsl, modify <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 the child node and the attribute value set to the child text value. Save the
changes.
Hint: Use the local-name() XPath function enclosed in braces to derive the node
name of the child elements, and use <xsl:value-of> to obtain the node text as the
value.
<?xml version='1.0' encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Root template -->

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 40
<xsl:template match="/">
<xsl:element name="DEPARTMENTS">
<xsl:apply-templates/>
</xsl:element>
</xsl:template>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<xsl:template match="department">
<xsl:element name="{local-name()}">
<xsl:for-each select="*">
<xsl:attribute name="{local-name()}">
<xsl:value-of select="."/>
</xsl:attribute>
</xsl:for-each>
</xsl:element>
s a
h a
ฺrs) ฺ
</xsl:template>
</xsl:stylesheet>
s
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

f. Run the oraxsl command-line utility on the departments.xml file, and specify the
style sheet departments_new.xsl. You can view the generated XML document
element in the command prompt window. What do you observe?

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 41
As shown in the preceding screenshot, the transformation yields the desired output
with each child element of the <department> node from the XML input document
being output as an attribute of the <department> element in the result document.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 7: Transforming XML Documents with XSL Transformations


Chapter 7 - Page 42
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G8:
Lesson
t o r den
Working s
aj with S u
tXQuery
ฺ m
a 8 this
t i n
Chapter
n use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 1
Practices for Lesson 8: Overview

Practices Overview
In these practices, you use XQuery expressions to retrieve data from XML documents. In
JDeveloper, you create XQuery files and execute them to view the results.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 2
Practice 8-1: XQuery

Overview
Perform the following steps to create an XQuery file and run the XQuery expression against an
XML document in JDeveloper:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks
1. Use lab_08_library.xml for this exercise. In JDeveloper, create XQuery files and
execute them to view the results.
a. Create the lab_08_01_a.xq XQuery file. List all the titles under each book element.
b. Create the lab_08_01_b.xq XQuery file. Display all the book elements under the
library element that have a price value greater than 45.
c. Create the lab_08_01_c.xq XQuery file. Display all the title elements under the
s a
books elements that have a price value greater than 40.
h a
ฺrs) ฺ
d. Use a FLWOR expression to display the same results as in step c. Create the
s
lab_08_01_d.xq XQuery file.
@ em uide
2.
ic t G
Use lab_08_emp.xml for this exercise. In JDeveloper, create XQuery files and execute
o v
them to view the results.
t o r den
s
aj s Stu
a. Create the lab_08_02_a.xq XQuery file. Display employees who report to Nancy.
a ฺ m hi
b. Create the lab_08_02_b.xq XQuery file. Display employees who do not report to
t i n e t
anybody.
a l en o us
i c (v se t
c. Write an XQuery statement that returns full_name and email_id of all employees
having employee_id greater than 3. Your query should display the output in the
o rov licen
following format. Create an XQuery file named lab_08_02_c.xq.
t
a js ble
M fera
<Result>
a
n i n s
t <email>email_id</email>
<full_name>full_name</full_name>
n
l e r a
n-t
Va no</Result>
<Result>
<full_name>full_name</full_name>
<email>email_id</email>
</Result>

d. Create the lab_08_02_d.xq XQuery file. Use a FLWOR expression and display
employees who report to Nancy in the following format:
<Database>full_name</Database>
e. Create the lab_08_02_e.xq XQuery file. Use the doc() and substring()
functions to list all employees whose names start with the letter “M.”
The substring()XPath function returns the substring from the start position to the
specified length. Index of the first character is 1. If length is omitted it returns the
substring from the start position to the end.
For example, the expression substring('XMLDB',1,3) returns "XML".

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 3
f. Using a conditional expression, write a query that displays full_name and
incremented_salary of all employees who have employee_id greater than 3. The
value of INCREMENTED_SALARY should be as follows:
1) Employees earning a salary greater than 3000:
INCREMENTED_SALARY =500 + salary
2) Employees earning a salary less than or equal to 3000:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

INCREMENTED_SALARY = 1000 + salary


3) You can use lab_08_02_f_txt for help. Replace all <TODO> with suitable text
and save the file as lab_08_02_f.xq.

3. Renumber the following as they appear in the syntax of the FLWOR expression.
a. expr_containing_result
b. where
c. return s a
h a
ฺrs) ฺ
d. $identifier := expr(expr)
e. conditional_expr s
f. order by
@ em uide
g. for
o v ic t G
t o r den
h. let s
aj s Stu
i. $identifier("ascending" | "descending")
a ฺ m hi
j. $identifier in expr(expr) t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 4
Solution 8-1: XQuery

Overview
In this solution, you perform the following steps to create an XQuery file and run the XQuery
expression against an XML document in JDeveloper:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Steps

Perform the following steps to create an XQuery file and run the XQuery expression against an
XML document in JDeveloper:
1. Right-click an existing project node and select New (or select File > New from the
menu).
2. In the New Gallery window, click XML under the General node in the Categories:
section, and then double-click XQuery File in the Items: section.
s
3. In the Create XQuery File window that appears, enter the file name in the File Name: a
h a
ฺrs) ฺ
field, and then click OK.
4. JDeveloper creates an XQuery file. Enter your XQuery expression and save the file.
s
em uide
5. Right-click in the XQuery file and select Run from the context menu. Alternatively, you
@
can use the Run button on the toolbar.
o v ic t G
6. View the XQuery results in the Log window.
t o r den
s
aj s Screate tu XQuery files in the
1. Use lab_08_library.xml for this exercise. ฺInm JDeveloper,
i n
XMLLabsProject project, and execute tthem
e hi results.
ato view tthe
a. Create the lab_08_01_a.xq a l en ofile.uList
XQuery s all the titles under each book element.
v
c ( nse t
v i
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 6
Enter the following code in the newly created file, and then click Save on the toolbar:

doc("lab_08_library.xml")/library/book/title

Next, right-click the XQuery file, and then select Run from the context menu.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

b. Create the lab_08_01_b.xq XQuery file under the XMLLabsProject. Display all the
book elements under the library element that have a price value greater than 45. Save
the changes.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 7
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 8
Enter the following code in the newly created file, and then click the Save icon on the
toolbar:

doc("lab_08_library.xml")/library/book[price>45]
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Click Save on the toolbar to save the changes. Right-click the file, and then select Run
from the context menu.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 9
c. Create the lab_08_01_c.xq XQuery file. Display all the title elements under the
books elements that have a price value greater than 40.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 10
Enter the following code in the newly created file, and then click the Save icon on the
toolbar:

doc("lab_08_library.xml")/library/book[price>40]/title
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Next, right-click the XQuery file, and then select Run from the context menu.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 11
d. Use a FLWOR expression to display the same results as in step c. Create the
lab_08_01_d.xq XQuery file.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Enter the following code in the newly created file, and then click the Save icon on the
toolbar:

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 12
for $i in doc("lab_08_library.xml")/library/book
where $i/price>40
return $i/title

Next, right-click the XQuery file, and then select Run from the context menu.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

2. Use lab_08_emp.xml for this exercise. In JDeveloper, create XQuery files and execute
them to view the results.
a. Create the lab_08_02_a.xq XQuery file. Display employees who report to Nancy.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 13
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

After you create the new file by using the same steps used earlier in this practice, enter
the following code in the newly created file, and then click the Save icon on the toolbar:

doc("lab_08_emp.xml")/employees/employee[manager='Nancy']

Next, right-click the XQuery file, and then select Run from the context menu. The code
and the results are shown as follows:

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 14
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

b. Create the lab_08_02_b.xq XQuery file. Display employees who do not report to
anybody.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 15
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj and S tu
c. Write an XQuery statement that returns full_name
a ฺ m h is display the ofoutput
email_id all employees
having employee_id greater than 3.
n t i n
Your query
e tshould in the
e
al filetonamed
following format. Create an XQuery s
u lab_08_02_c.xq.
( v
<Result>
o v ic nse
t o r l i ce
<full_name>full_name</full_name>
s
aj rable
<email>email_id</email>
M
n t ina ansfe
</Result>
le -tr
<Result>
Va non<full_name>full_name</full_name>
<email>email_id</email>
</Result>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 16
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a
d. Create
n f e ra
Mthe lab_08_02_d.xq XQuery file. Use a FLWOR expression and display
t i
n tran
employees s
who report to in the following format:
l e Nancy
Va non- <Database>full_name</Database>

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 17
e. Create the lab_08_02_e.xq XQuery file. Use the doc() and substring()
functions to list all employees whose names start with the letter “M.”
The substring()XPath function returns the substring from the start position to the
specified length. Index of the first character is 1. If length is omitted it returns the
substring from the start position to the end.
For example, the expression substring('XMLDB',1,3) returns "XML".
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
r
sฺ eฺs
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le Using n-tra conditional expression,
Va f. noincremented_salary write a query that displays full_name and
of all employees who have employee_id greater than 3. The
value of INCREMENTED_SALARY should be as follows:
1) Employees earning a salary greater than 3000:
INCREMENTED_SALARY =500 + salary
2) Employees earning a salary less than or equal to 3000:
INCREMENTED_SALARY = 1000 + salary
3) You can use lab_08_02_f_txt for help. Replace all <TODO> with suitable text
and save the file as lab_08_02_f.xq.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 18
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
3. Renumber the following as theyv a lenin tthe
appear o us of the FLWOR expression.
syntax
v i
a. expr_containing_result c ( nse
t o ro lice
b. where
s
aj rable
c. returnM
t ina ansfe := expr(expr)
d. $identifier
n
lee. conditional_expr
n-tr by
Va f.noorder
g. for
h. let
i. $identifier("ascending" | "descending")
j. $identifier in expr(expr)
for
$identifier in expr(expr)
let
$identifier := expr(expr)
where
conditional_expr
order by
identifier("ascending" | "descending")
return
expr_containing_result

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 19
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 8: Working with XQuery


Chapter 8 - Page 20
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
Practices for o v ic t G9:
Lesson
t o r den
s
aj s Sto
Introduction tu Oracle XML
DB a ฺ m hi
t i n e t
a l en Chapter
o us 9
v
c ( nse t
v i
t o ro lice
s
aj rable
M
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 1
Practices for Lesson 9: Overview

Practices Overview
In this guided practice, you work with Oracle XML DB (XDB) Repository to store and access
XML resources such as XML and XSL documents.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 2
Practice 9-1: Introduction to Oracle XML DB

Overview
In this guided practice, you map a web folder to the Oracle XML DB repository.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks

1. Map a web Folder to the Oracle XDB repository. In your NX session, select Connect to
Server from the Places menu. The Connect to Server window is displayed.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 3
2. Complete the Connect to Server window as follows, and then click Connect:
a. Service type: WebDAV (HTTP)
b. Server: localhost
c. Port: 8080
d. User Name: HR
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

e. Password: hr

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
3. In the “Enter password v i
for
(v Authentication
cXDB” s e t
r o c e n Connect. window, enter hr in lowercase letters
jsto ble li
in the Password field, and then click
a
M fera
t i na s
l e n r a n
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 4
4. A folder is opened with the top-level folders of the Oracle XDB repository displayed, and a
shortcut is created on your desktop.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i
n us n e t
a
5. In the home parent folder, createvyour
leown o folder and name it hr.
tstudent
c ( e
a. Double-click the home
r o vifolder,eand
nscreate a new folder called hr by selecting File >
Create Folderto c
li bar.
a js frombthe
l emenu
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 5
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t
b. A new “untitled folder” is displayed. Enter hr as the folder name.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 6
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
ฺrs) ฺ
Note: The folder must have the same name as your Oracle Database username.
s
m in ithe
6. Copy the /home/oracle/labs/lab/hr.xml file to your /home/hr @ efolder u deOracle
XDB repository. You can copy and paste as follows: c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 7
Right-click the hr.xml file, and then select Copy from the context menu.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic document
t G is
In your hr web folder, select Edit > Paste from the menu. The
r n
hr.xml
displayed in the hr web folder.
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o vi ens
j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

7. Use your web browser to navigate to your hr web folder in the home folder by using the
URL: http://localhost:8080.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 8
8. In the Authentication Required window, enter HR as the User Name and hr as the
Password. The contents of the hr web folder are displayed as follows:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 9
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n i n
t toathens/home/hr folder.
9. Navigate
l e r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 10
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 11
10. Click the hr.xml file to display its contents.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 12
Right-click the document, and then select View Page Source from the context menu.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 13
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi directory to your /home/hr
t i n
11. Copy the following files from the /home/oracle/labs/lab
e t
web folder: a l en o us
a. employees.xml ic (
v et
b. employees.dtd r o v
c e ns
a jsto ble li
c. employees.xsl
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 14
Copy the three highlighted files from the /home/oracle/labs/lab folder by using the Edit >
Copy from the menu, and then paste them in your /home/hr web folder:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

12. Using your web browser, navigate to the /home/hr folder, and then click employees.xsl
in the web folder. s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 15
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 16
Right-click and select View Page Source.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 17
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 18
13. Click employees.xml. Does a complete transformation occur?
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Yes, the XSL transformation should be completed on the employees.xml file as shown.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 19
Practice 9-2: Creating a WebDAV Connection Using JDeveloper

Overview
In this guided practice, you navigate to the /home/hr folder and then double-click
employees.xsl in the web folder.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Tasks
1. Start JDeveloper Studio Edition 12c (12.1.2). Select Resources from the Window menu.

s a
h a
ฺrs) and
Accept the default selection in the Role section, Studio Developer (All Features),
s
then click OK. e m ideฺ
v i c@ t Gu
t o ro den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

JDeveloper Studio Edition 12.1.2 is displayed.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 20
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
Select Resources from the Window menu.t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 21
2. The Resources pane is displayed. Click the New icon, and then select IDE Connections >
WebDAV from the drop-down menu as shown in the following screenshots:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 22
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m
awindow as i
hfollows,
3. Complete the Create WebDAV Connection
n t i n e t and then click Test
Connection: e
al to u s
( v
o v ic nse
a. URL Location: http://localhost:8080
b. Connection Name:
s t o r l i ce
OracleXDB-HR
aj HRrable
c. User Name:
M
n t ina anshrfe
d. Password:
le r
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 23
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

4. When the status of the test is successful in the Status region, click OK.

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 24
5. The new connection is displayed in the Resources pane.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

6. Drill down using the “+” sign next to WebDAV, OracleXDB-HR, home, and hr to display
the contents of the /home/hr web folder that you created earlier.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 25
7. To display the contents of any of the files in the hr folder, double-click the file name.
Double-click the hr.xml file. The contents of the file are displayed in the XML Code Editor.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov licen
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Practices for Lesson 9: Introduction to Oracle XML DB


Chapter 9 - Page 26

You might also like