0% found this document useful (0 votes)
7 views

XML Fundamentals Sg

This document is a guide on XML fundamentals provided by Oracle, detailing the objectives, course goals, and various topics related to XML, including its structure, validation, and application in Oracle technologies. It includes sections on creating and editing XML documents, using Document Type Definitions (DTDs), and XML schemas. The document also emphasizes the importance of copyright and proprietary information, restricting unauthorized reproduction and distribution.

Uploaded by

Boban Vasiljevic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

XML Fundamentals Sg

This document is a guide on XML fundamentals provided by Oracle, detailing the objectives, course goals, and various topics related to XML, including its structure, validation, and application in Oracle technologies. It includes sections on creating and editing XML documents, using Document Type Definitions (DTDs), and XML schemas. The document also emphasizes the importance of copyright and proprietary information, restricting unauthorized reproduction and distribution.

Uploaded by

Boban Vasiljevic
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 452

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
a Studenthi Guide
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
D86758
Author Copyright © 2014, Oracle and/or its affiliates. All rights reserved.

Lauran K. Serhal Disclaimer

This document contains proprietary information and is protected by copyright and


Technical Contributors 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
and Reviewers in any way. Except where your use constitutes "fair use" under copyright law, you
Amitabh James Hans may not use, share, download, upload, copy, print, display, perform, reproduce,
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

publish, license, post, transmit, or distribute this document in whole or in part without
Brian Fry the express authorization of Oracle.
Diganta Choudhury
The information contained in this document is subject to change without notice. If you
Doug Mcmahon 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
Geeta Arora warranted to be error-free.
Gerry Jurrens
Restricted Rights Notice
Hui X Zhang
Janarthanan Mohan 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
Jeffrey Stephenson applicable:
s a
Lakshmi Narapareddi
U.S. GOVERNMENT RIGHTS h
) or a
Mark Drake The U.S. Government’s rights to use, modify, reproduce, release, perform,
s ฺrsdisplay,

Mike Yawn
e
license agreement and/or the applicable U.S. Government contract. m ideOracle
disclose these training materials are restricted by the terms of the applicable

Nageswara Nandimalla
v i c@ t Gu
Nancy Greenberg
Trademark Notice
t o ro den
Pranab Chakraborty Oracle and Java are registered s
j owners.Oracle
trademarks
a of
S t uand/or its affiliates. Other names
Qin Yu
n a ฺm this
may be trademarks of their respective

Radek Felcman
i
nt use
l e
Sailaja
j Pasupuleti
p
c ( va e to
Shay Shmeltzer
r o vi ens
Sriram X Krishnamurthy
j s to le lic
Ying Lu M a rab
t
Zhen Liu
n ina ansfe
le n-tr
VaEditors
no
Daniel Milne
Malavika Jinka
Vijayalakshmi Narasimhan
Richard Wallis

Graphic Designer
Rajiv Chandrabhanu

Publishers
Giri Venugopal
Jayanthy Keshavamurthy
Nita Brozowski
Contents
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Preface

1 Introduction
Objectives 1-2
Course Goals 1-3
Suggested Course Agenda 1-4
Questions About You 1-6
Database Schemas Used in This Course 1-7
s a
Human Resources (hr) Schema 1-8 h a
Order Entry (oe) Schema 1-9
s ฺrs) ฺ
em uide
purchaseorder.xsd Purchase Order XML Schema Used in the XML DB Course 1-10
@
Appendixes 1-11
o v ic t G
Class Account Information 1-12 t o r den
s
aj s Stu
Course Environment 1-13
a ฺ m hi
t i n t
Entering SQL Statements by Using Oracle SQL*Plus in a Terminal Window 1-14
e
Using Oracle JDeveloper 1-15
a l en o us
i c (v se t
Entering SQL Statements and Coding PL/SQL by Using Oracle SQL
Developer 1-16
t o rov licen
a js ble
Related Oracle University Courses: education.oracle.com 1-17
a M fera
Additional Information 1-18
n t i n n s
l e r a
Oracle Database 12c Release 1 (12.1) Documentation 1-19
Va non-tAdditional Resources: Using “Oracle by Example” (OBE) in the Online
Learning Library 1-21
Additional Resources: Oracle Learning Library (OLL) 1-22
Oracle XML DB Home Page: http://www.oracle.com/technetwork/
database-features/xmldb/overview/index.html 1-23
Oracle JDeveloper Home Page: http://www.oracle.com/technetwork/
developer-tools/jdev/overview/index.html 1-24
Additional Web Resources for XML 1-25
Course Files in the /home/oracle/labs Folder 1-26
JDeveloper Application and Projects in the labs Folder 1-27
Course Environment and Data 1-28
Practice 1-1: Overview 1-29
Summary 1-30

iii
2 Introduction to XML
Objectives 2-2
Lesson Agenda 2-3
Extensible Markup Language 2-4
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Advantages of Using XML 2-5


XML Standards 2-6
Document Object Model 2-7
Simple API for XML 2-9
Oracle XML Support 2-10
XML in Oracle JDeveloper 2-11
Building XML Applications with Oracle Technology 2-12
XML in Service-Oriented Architecture 2-13
s a
Importance of XML in Web Services 2-14
h a
Creating an Application and Project in JDeveloper 2-16
s ฺrs) ฺ
Creating an XML Document in JDeveloper 2-19
@ em uide
Adding an XML Document to a Project 2-21
o v ic t G
Viewing the Contents of an XML Document 2-22
t o r den
Viewing an XML Document in JDeveloper 2-23 s
aj s Stu
a ฺ m hi
Quiz 2-24
t i n e t
Practice 2-1: Overview 2-25
a l en o us
Lesson Agenda 2-26
i c (v se t
t o rov licen
Extensible Markup Language 2-27
js ble
XML Documents Form a Tree Structure 2-28
a
a M fera
XML Document Structure 2-29
n t i n n s
Simple XML Document: Example 2-30
l e r a
Va non-tXML Declaration 2-31
Components of an XML Document 2-32
XML Elements 2-33
Empty Elements 2-34
Markup Rules for Elements 2-35
XML Attributes 2-36
Naming Convention Rules for Elements and Attributes 2-37
Using Elements Versus Attributes 2-38
XML Entity 2-39
XML Character Data (CDATA) 2-40
XML Comments 2-41
Well-Formed XML Documents 2-42
Comparing XML and HTML 2-43
Comparing XML and HTML: Example 2-44
XML Development 2-45
Creating an XML Document in JDeveloper 2-46

iv
Editing XML Documents in Oracle JDeveloper 2-47
Setting Preferences in Oracle JDeveloper 2-48
Quiz 2-49
Summary 2-50
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Practice 2-2: Overview 2-51

3 Validating XML Documents with Document Type Definitions


Objectives 3-2
Lesson Agenda 3-3
What Is a Document Type Definition? 3-4
Reasons to Validate an XML Document 3-5
General DTD Rules 3-6
s a
Syntax for DTD Declarations 3-7
h a
Example of a Simple DTD Declaration and an XML Document That
s ฺrs) ฺ
Conforms to the DTD 3-8
@ em uide
o v ic t G
Example of an XML Document That Does Not Conform to the DTD 3-9
Reference the DTD in the XML Document 3-10
t o r den
Lesson Agenda 3-11 s
aj s Stu
a ฺ m hi
Empty Element Declaration 3-12
t i n e t
l
Child Elements Declaration 3-13
a en o us
i c (v se t
Mixed Elements Declarations 3-14

t o rov licen
Any Element Declarations 3-15
js ble
Specifying the Cardinality of Elements 3-16
a
a M fera
Specifying the Cardinality of Elements: Examples 3-17
n t i n n s
Attribute Declarations: Syntax and Requirements 3-18
l e r a
Va non-tCDATA and Enumerated Attribute Types 3-20
NOTATION Declaration and Attribute Type 3-21
Specifying Default Attribute Values 3-22
Entities in XML 3-23
General Entity Declarations 3-24
Parameter Entities 3-26
Complete DTD: Example 3-27
Lesson Agenda 3-28
Validating XML Against a DTD 3-29
Using XML Catalogs 3-30
Benefits of Using XML Catalogs 3-31
Quiz 3-33
Summary 3-34
Practice 3: Overview 3-35

v
4 Modularizing XML with Namespaces
Objectives 4-2
What Is an XML Namespace? 4-3
Benefits of Using XML Namespaces 4-4
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Declaring XML Namespaces 4-5


XML Namespace Prefixes 4-6
XML Namespace Declarations: Example 4-7
Scope of XML Namespace Declarations 4-8
ShowXMLns Tool 4-9
Invoking the ShowXMLns Tool 4-10
Quiz 4-11
Summary 4-12
s a
Practice 4: Overview 4-13
h a
s ฺ rs)
5 Validating XML Documents with XML Schemas
e m ideฺ
Objectives 5-2
v i c@ t Gu
Lesson Agenda 5-3
t o ro den
What Is an XML Schema? 5-4 s
aj s Stu
a ฺ m hi
W3C XML Schema Recommendation: Overview
Benefits of XML Schemas 5-6 en t i n e t
5-5

XML Schema Versus DTD (5-7 v al to us


o v icExample
e n se5-8
torDocument
XML Schema Document:
Validating anjs
XML
l e licwith an XML Schema Document 5-9
M aan XML r abSchema with the schemaLocation Attribute 5-10
a
Referencing
tin 5-11 s f e
l e nQuiz
r a n
Va nLesson
o n-t Agenda 5-12
Components of an XML Schema 5-13
XML Schema Components: Example 5-14
<schema> Declaration 5-15
Global and Local Declarations 5-17
Referencing Global Type Declarations Within the Content Model 5-18
An XML Instance Document That Conforms to the Preceding XML Schema:
Example 5-19
Declaring an Element 5-20
Built-In XML Schema Data Types 5-21
Lesson Agenda 5-23
Declaring a <simpleType> Component 5-24
Using the <enumeration> Facet 5-26
Using <list> and <union> simpleType Declarations 5-27
Lesson Agenda 5-28
Declaring a <complexType> Component 5-29

vi
Declaring a <sequence> 5-30
Declaring a <choice> 5-31
Declaring an Empty Element 5-32
Using Element Wildcards 5-33
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Lesson Agenda 5-34


Declaring Attributes 5-35
Attribute Declarations: Example 5-36
Declaring and Referencing an <attributeGroup> 5-37
Documenting the XML Schema 5-38
Practice 5-1: Overview 5-39
Lesson Agenda 5-40
Creating an XML Schema Document in JDeveloper 5-41
s a
Using the JDeveloper XML Schema Editor 5-42
h a
Using the JDeveloper XML Schema‒Aware XML Editor 5-43
s ฺrs) ฺ
Registering an XML Schema with JDeveloper 5-44
@ em uide
Creating an XML Document from an XML Schema 5-45
o v ic t G
Using the XML Schema–Aware XML Editor 5-46
t o r den
Lesson Agenda 5-47 s
aj s Stu
a ฺ m hi
t i n e t
Validating an XML Document with Its XML Schema in JDeveloper 5-48
l en o us
Validating an XML Document with its XML Schema with the oraxml Java Utility 5-49
a
i c (v se t
Applications for XML Schema 5-50

t o rov licen
Converting a DTD to an XML Schema 5-51
Quiz 5-52
a js ble
a M fera
Summary 5-53
n t i n n s
Practice 5-2: Overview 5-54
l e r a
Va non-t
6 Navigating XML Documents with XPath
Objectives 6-2
Lesson Agenda 6-3
What Is XML Path Language? 6-4
XPath 1.0 and XPath 2.0 6-5
XPath Terminology: Node Types 6-6
XPath Terminology: Family Relationships 6-7
XPath Model 6-8
Lesson Agenda 6-9
XPath Expressions 6-10
Location Path Expression 6-11
Location Path Expression: Example 6-12
Results of Location Path Expressions 6-13
Results of Location Path Expressions: Examples 6-14
Location Steps in XPath Expressions 6-15

vii
XPath Axes 6-16
XPath Node Test Types 6-17
Selecting Nodes by Using Abbreviated XPath Expressions 6-18
Selecting Nodes by Using Abbreviated XPath Expressions: Examples 6-19
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Abbreviated and Unabbreviated XPath Expressions 6-20


Abbreviated XPath Expression Examples: employees.xml Document 6-22
Selecting Nodes by Using Abbreviated XPath Expressions: Examples 6-23
Abbreviated XPath Expression Examples: departments.xml Document 6-25
XPath Predicates 6-26
Operators in XPath Expressions in Precedence Order 6-28
Lesson Agenda 6-29
XPath Functions 6-30
s a
Boolean Functions 6-31
h a
Number Functions 6-32
s ฺrs) ฺ
Node-Set Functions 6-33
@ em uide
String Functions 6-34
o v ic t G
XSLT and XPath 6-35
t o r den
Lesson Agenda 6-37 s
aj s Stu
a ฺ m hi
Testing XPath Expressions 6-38
t i n e t
XPath 2.0 6-39
a l en o us
Quiz 6-41
i c (v se t
Summary 6-42
t o rov licen
js ble
Practice 6: Overview 6-43
a
a M fera
t i n n
7 nTransforming s XML Documents with XSL Transformations
l e r a
n-t 7-2
Va nObjectives
o
Lesson Agenda 7-3
What Is XSL? 7-4
XSL Transformations 7-5
XSLT Style Sheet 7-6
XSLT Style Sheet: Example 7-7
Using an XSLT Style Sheet with an XML Document 7-8
Viewing the Transformed XML Document 7-9
Creating an XSL File in JDeveloper 7-10
Lesson Agenda 7-11
Creating Template Rules 7-12
Obtaining Input Text with <xsl:value-of> 7-13
Applying Template Rules 7-14
Controlling Template Activation Order 7-16
Template Rules and Priorities 7-17
Default Template Rules 7-18

viii
Effects of Default Template Rules 7-19
Looping with <xsl:for-each> 7-20
Specifying Output Formats 7-21
Attribute Value Templates 7-22
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Creating Elements with Attributes 7-23


Lesson Agenda 7-25
Sorting an XML Document 7-26
Conditional Processing with <xsl:if> 7-27
Conditional Processing with <xsl:choose> 7-28
Modes 7-29
Example of Using Modes 7-30
Calling Templates by Name 7-33
s a
Lesson Agenda 7-34
h a
Creating and Using Parameters 7-35
s ฺrs) ฺ
Lesson Agenda 7-36
@ em uide
Performing the XSLT Process in JDeveloper 7-37
o v ic t G
Using the oraxsl Utility 7-38
t o r den
XSLT 2.0 7-40 s
aj s Stu
a ฺ m hi
Quiz 7-41
t i n e t
Summary 7-42
a l en o us
Practice 7: Overview 7-43
i c (v se t
t o r ov licen
8 Working with
a jsXQuery le
M 8-2 rab
ina Agenda
Objectives
t n s fe 8-3
nLesson
le n-traReview 8-4
Va nXQuery:
o
XQuery Terminology 8-5
What Is XQuery? 8-6
Applications of XQuery 8-7
Features of XQuery 8-8
XQuery Terminology 8-9
XQuery Data Model 8-10
Conversion of XML into Query Data Model 8-11
XQuery Syntax Rules 8-12
Lesson Agenda 8-13
XQuery Support in JDeveloper 8-14
Lesson Agenda 8-15
XQuery Expressions 8-16
Primary Expressions: Variables 8-17
Primary Expressions: Literals 8-18
Primary Expressions: Constructors 8-19

ix
Sequence Expressions 8-21
FLWOR Expressions 8-22
FLWOR Expression Clauses 8-23
departments.xml Document: Example 8-25
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Using FLWOR Expressions 8-26


Path Expressions 8-27
Using Path Expressions to Filter XML Data: Example 8-29
Using Conditional Expressions 8-30
Quantified Expressions 8-31
Using Quantified Expressions 8-32
Lesson Agenda 8-33
XQuery Functions 8-34
s a
XQuery Operators 8-36
h a
XQuery Comparison and Boolean Operators 8-37
s ฺrs) ฺ
Useful Websites About XML 8-39
@ em uide
Useful W3C Websites About XQuery 8-40
o v ic t G
Quiz 8-41
t o r den
Summary 8-42 s
aj s Stu
a ฺ m hi
Practice 8: Overview 8-43
t i n e t
a l en o us
9 Introduction to Oracle XML
i c (vDB se t
Objectives 9-2
t o r ov licen
Lesson Agenda
a js 9-3ble
What IsM
na Oracle XML
f e raDB? 9-4
i
t XML
nOracle a s
nDB 9-6
l e t r
Va nOracle
o n- XML DB: Benefits 9-7
Implementing Oracle XML DB 9-12
Lesson Agenda 9-13
Oracle XML DB: Features 9-14
Lesson Agenda 9-18
XMLType: Overview 9-19
Using XMLType 9-20
XMLType Storage Models 9-21
Lesson Agenda 9-23
XML Schema Support in Oracle Database 12c 9-24
XML Schema and Oracle XML DB 9-25
XMLType and XML Schema 9-27
XML Schema Management 9-29
Storage and Access Infrastructure 9-31
Validating XML 9-32
Lesson Agenda 9-33

x
Oracle XML DB Repository: Overview 9-34
What Is the XML DB Repository? 9-35
Mapping a Web Folder to Oracle XML DB Repository 9-36
Mapping a Web Folder to the Oracle XML DB Repository 9-37
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Oracle XML DB Repository: Features 9-38


Repository Support for Internet Protocols 9-39
Oracle XML DB Versioning 9-41
Lesson Agenda 9-42
Benefits of Oracle XML DB 9-43
XQuery Support in Oracle XML DB 9-44
XQuery Support in Oracle Database 9-45
Using XQuery with Oracle XML DB 9-47
s a
XMLQuery 9-48
h a
ฺrs) ฺ
URI Scheme oradb: Querying Table or View Data with the XQuery fn:collection
s
Function 9-50
@ em uide
fn:collection Function: Example 9-52
o v ic t G
XMLTable 9-53
t o r den
XMLTable: Example 9-54 s
aj s Stu
a ฺ m hi
Quiz 9-55
t i n e t
Summary 9-56
a l en o us
Practice 9: Overview 9-57
i c (v se t
t o r ov licen
js Descriptions
Appendix A: Table
a b le
M a
er Information About Document Type Definitions, XML
a B: Additional
f
t i n s
a en -trand
Appendix
lSchemas, anXSLT 2.0
V non
Objectives B-2
Declaration Types in a DTD (Review) B-3
Referencing the DTD (Review) B-4
Element Declarations (Review) B-5
Specifying Cardinality of Elements (Review) B-7
Attribute Declarations (Review) B-8
CDATA and Enumerated Attribute Types B-9
ENTITY and ENTITIES Attribute Types B-10
ID Attribute Type B-12
IDREF and IDREFS Attribute Types B-13
NMTOKEN and NMTOKENS Attribute Types B-14
Entities in XML (Review) B-15
Creating an XML Schema from Multiple XML Schema Documents B-16
Declaring an <import> B-17
Declaring an <include> B-18

xi
XSLT 2.0 B-20
Grouping Data B-21
Generating Multiple Outputs B-23
Creating Temporary Trees B-25
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Character Mapping B-27


Summary B-28

Appendix C: Managing External Tools in JDeveloper


Objectives C-2
External Tools with JDeveloper C-3
Managing External Tools C-4
Create External Tool: Step 1 of 11 – Type C-5
s a
Create External Tool: Step 2 of 11 – External Program Options C-6
h a
Create External Tool: Step 9 of 11 – Display C-7
s ฺrs) ฺ
Create External Tool: Step 10 of 11 – Integration C-8
@ em uide
Create External Tool: Step 11 of 11 – Availability C-9
o v ic t G
t o r den
Using the Newly Added “gedit Text Editor” External Tool C-10
Editing an External Tool C-11 s
aj s Stu
a ฺ m hi
Deleting an External Tool C-12
t i n e t
Summary C-13
a l en o us
i c (v se t
Appendix D: Glossaryov en
t o r l i c
a js ble
a M fera
n t i n n s
l e r a
Va non-t

xii
Preface
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
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
Profile
Before You Begin This Course
Before you begin this course, you should have some experience in SQL.
The following is a list of the suggested prerequisites:
• Basic experience with writing HTML documents
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Familiarity with Oracle JDeveloper and SQL*Plus

How This Course Is Organized


XML Fundamentals is an instructor-led course featuring lectures and hands-on
exercises. Online demonstrations and written practice sessions reinforce the
concepts and skills that are introduced.

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
Related Publications
Oracle Publications
Title Part Number
Oracle XML DB Developer’s Guide 12c Release 1 E17603-09
Oracle Database SQL Language Reference12c Release 1 E17209-14
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Oracle Database PL/SQL Language Reference12c Release 1 E17622-18


Oracle Database PL/SQL Packages and Types Reference E17602-14
12c Release 1
Oracle SQL Developer User’s Guide Release 3.2 E35117-05
Oracle Database Object-Relational Developer's Guide 12c E16801-07
Release 1
s a
Oracle Database SQLJ Developer's Guide 12c Release 1 E17660-11
h a
Oracle Text Reference 12c Release 1 (12.1) E17747-07 rs)
m sฺ eฺ
Oracle Text Application Developer's Guide 12c Release 1 e uid
E17748-07
@
c
vi nt G
r o
Additional Publications
a jsto Stude
• System release bulletins
n a ฺm this
• Installation and user’s guides ent
i s e
l
va e to u
• read.me files
c (
i Group
r o
• International Oracle vUser’s
c e ns (IOUG) articles
a jsto ble li
• Oracle Magazine
a M fera
n t i n n s
l e r a
Va non-t
Typographic Conventions
The following two lists explain Oracle University typographical conventions for
words that appear within regular text or within code samples.

1. Typographic Conventions for Words Within Regular Text


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

Convention Object or Term Example


Courier New User input; Use the SELECT command to view
commands; information stored in the LAST_NAME
column, table, and column of the EMPLOYEES table.
schema names;
functions; Enter 300.
PL/SQL objects;
paths Log in as scott
s a
h a
s ฺ rs)
Assign a When-Validate-Itemm trigger to ฺ
ide
Initial cap Triggers;
user interface object the ORD block. @ e u
c
vi nt G
names, such as r o
button names jsto button.
Click the Cancel
a t u de
a ฺ m his S
e n tin se t
Italic Titles of
coursescand( val e tForo umore information on the subject see
vi ens Manual
Oracle SQL Reference
r o
manuals;
toemphasizedlic
j s
a rwords l e
a M f e ab or phrases; Do not save changes to the database.
e n tin ans placeholders or
l r
Va non-t variables Enter hostname, where
hostname is the host on which the
password is to be changed.

Quotation marks Lesson or module This subject is covered in Lesson 3,


titles referenced “Working with Objects.”
within a course
Typographic Conventions (continued)

2. Typographic Conventions for Words Within Code Samples


Convention Object or Term Example
Uppercase Commands, SELECT employee_id
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

functions FROM employees;


Lowercase, Syntax variables CREATE ROLE role;
italic
Initial cap Forms triggers Form module: ORD
Trigger level: S_ITEM.QUANTITY
item
Trigger name: When-Validate-Item
. . . s a
) h a
Lowercase Column names, . . . r s
sฺ eฺ
table names, OG_ACTIVATE_LAYER m
e uid
filenames, c @
vi nt G
(OG_GET_LAYER ('prod_pie_layer'))
PL/SQL objects . . . r o
SELECT a jsto Stude
last_name
n a ฺm employees;
t his
t i
FROM
e
n us USER scott
Text that must ale CREATE
Bold
c ( to
va eIDENTIFIED
o v i
be entered by
n s BY tiger;
tor le lic
user e
j s
a rab
M
n t ina ansfe
le r
Va non-t
Graphical Conventions

Document Type or Expression Code Example Box Color


XML document
<?xml version="1.0" encoding='UTF-8'?>
<!-- XML document. -->
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<region xmlns:xsi="http://...">
<region_id>1</region_id>
<region_name>Europe</region_name>
</region>

XML schema document (XSD)


<?xml version="1.0" encoding='UTF-8'?>
<!– XML schema, XSD, document. -->
<xsd:schema xmlns:xsd="http ...
s a
<xsd:element name="region">
<xsd:complexType> ) h a
. . . r s
sฺ eฺ
m
e uid
c @
XSLT Style Sheet document r o vi nt G
jsStyle
<?xml version='1.0' e
to tudencoding='UTF-8'?>
ฺ m
<!– XSLTa s Sversion="1.0"
Sheet document. -->
a th i
inxmlns:xsl="http://www.w3.org/1999/XSL/...">
<xsl:stylesheet
n t e
v a us
le to <xsl:template match="/">
(
ic nse . . .
o v
s t o r l i ce
DocumentM aj Definitions
Type
r a b le (DTD)
t i na s f e <!ELEMENT employees (employee)>

l e n r a n <!ELEMENT employee (name)>

Va non-t
<!ELEMENT name (#PCDATA)>

XPath expressions /departments/department[2]

XQuery expressions for $i in


doc("code_08_dept.xml")//department
where $i/department_name = "Administration"
return $i
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ฺ

Introduction

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Discuss the course objectives and agenda


• List the database and XML schemas used in the course
• List the appendixes
• Identify the relevant documentation and other resources

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s discusses the course goals and the daily agenda of topics. The XML
e n
This introductory
r a n
lesson
al onand
terminology
V -t XML standards used in this course are briefly described.
n products that support development of XML data, XML applications, and storage
The Oracle
for XML data are identified. The technical environment of the course and data used are
described followed by instructions about using JDeveloper to open and edit XML documents
and about using a web browser to view the document.

XML Fundamentals 1 - 2
Course Goals

After completing this course, you should be able to:


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

• Create well-formed and valid XML documents


• Describe document type definition (DTD)
• Create an XML schema document
• Use tools and XML standards to validate XML documents
• Transform XML documents by using Extensible Stylesheet
Language (XSL) and XPath expressions s a
h a
• Use XML namespaces in an XML document s ฺrs) ฺ
• Identify Oracle XML DB components @ em uide
o v ic t G
• Use the XQuery language t o r den
s
aj Oracle S tuXDK and some
• Identify and use the capabilities ฺ m of
of its tools t i n a
e t his
n s
le to u
v a
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n t ina ansfe
le r
Va non-t

XML Fundamentals 1 - 3
Suggested Course Agenda

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

1. Introduction
2. Introduction to XML
3. Validating XML Documents with Document Type
Definitions (DTDs)
4. Modularizing XML with Namespaces
5. Validating XML Documents with XML Schemas 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
The agendai n n s
t is aasuggested list of lessons to be covered each day. It is possible that the
l e t r
a omay
schedule
V n- differ each time the course is delivered. Day 1 is devoted to the following
aspectsnof XML:
• Introduction to the course, the environment, and the tools used
• XML standards and basics of using some of these standards, such as validating XML
documents with DTDs and XML schemas
• Using XML namespaces and XML schemas

XML Fundamentals 1 - 4
Suggested Course Agenda

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

6. Navigating XML Documents by using XPath


7. Transforming XML Documents with Extensible Stylesheet
Language (XSL) Transformations (XSLT)
8. Working with XQuery
9. Introduction to Oracle XML DB (XML DB)
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t cover:
Day 2 topics n s
l e -t r a
V•a Using
n o nXPath
• Transformation of XML by using XSL and XSLT
• Using XQuery
• Introduction to XML DB

XML Fundamentals 1 - 5
Questions About You

To ensure that the class can be customized to meet your


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

specific needs and to encourage interaction, answer the


following questions:
• Which organization do you work for?
• What is your role in your organization?
• Do you have any XML experience?
• Do you meet the course prerequisites? s a
h a
• What Oracle Database versions do you use?
s ฺrs) ฺ
• How do you plan to use XML?
@ em uide
• What do you hope to learn from this o o v
course?
ic t G
r en jst Stud
ฺm thisa
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a
tbenefitamosts from this class if you know the background of your classmates and the
You can
e n r n
V al that
issues
o n -t face in the development and management of a data warehouse. Each
they
studentnhas unique perspectives, experience, and knowledge from which we all can learn.

XML Fundamentals 1 - 6
Database Schemas Used in This Course

The following database schemas are used in the course:


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

• Human Resources (HR) schema


• Order Entry (OE) schema

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t company s in the sample schemas operates worldwide to fulfill orders for several
e n
The fictitious
r a n
al oThe
products.
V n -tcompany has several divisions, with each division represented by a schema:
• Then Human Resources division tracks information about the company’s employees and
facilities. All the scripts that are necessary to create the HR schema are in the
$ORACLE_HOME/demo/schema/human_resources folder.
• The Order Entry division tracks product inventories and sales of the company’s products
through various channels. All the scripts that are necessary to create the OE schema are
in the $ORACLE_HOME/demo/schema/order_entry folder.
Notes
• Appendix A (“Table Descriptions”) contains more information about the sample
schemas.
• The code examples and the practices in this course specify the schema that must be
used.

XML Fundamentals 1 - 7
Human Resources (hr) Schema

DEPARTMENTS LOCATIONS
department_id location_id
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

department_name street_address
manager_id postal_code
location_id city
state_province
country_id
JOB_HISTORY
employee_id
start_date EMPLOYEES
end_date employee_id
job_id first_name
last_name COUNTRIES
department_id country_id
email
phone_number country_name s a
hire_date region_id
) h a
job_id
salary r s
sฺ eฺ
commission_pct m
e uid
JOBS manager_id
c @
job_id department_id
r o vi nt G
job_title
min_salary
j s t o ude REGIONS
max_salary
ฺ m a s St region_id
region_name
a h i
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
In the HR
n t na neach
irecords, s feemployee has an identification number, email address, job
ale on-code,
identification
V tra salary, and manager. Some employees earn commissions in addition to
n
their salary.
The company also tracks information about jobs within the organization. Each job has an
identification code, job title, and a minimum and maximum salary range for the job. Some
employees have been with the company for a long time and have held different positions
within the company. When an employee resigns, the duration for which the employee worked,
the job identification number, and the department are recorded.
The sample company is regionally diverse, so it tracks the locations of its warehouses and
departments. Each employee is assigned to a department and each department is identified
by a unique department number or a short name. Each department is associated with one
location, and each location has a full address that includes the street name, postal code, city,
state or province, and the country code.
In places where the departments and warehouses are located, the company records details
such as the country name, currency symbol, currency name, and the region where the country
is located geographically.

XML Fundamentals 1 - 8
Order Entry (oe) Schema
CUSTOMERS
customer_id
ORDER_ITEM cust_first_name
order_id ORDERS cust_ last_name
order_id
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

line_item_id
product_id order_date cust_ address_typ
unit_price order_mode
quantity customer_id street_address
order_status postal_code
order_total city
sales_rep_id state_province
promotion_id country_id
phone_numbers
PRODUCT_ nls_language
INFORMATION PRODUCT_ nls_territory
DESCRIPTION credit_limit
product_id
product_id cust_ email s a
product_name language_id account_mgr_id
date_of_birth ) h a
product_description
category_id
product_name
product_description marital_status r s
sฺ eฺ
weight_class
Gender
Income_level m
e uid
c @
warranty_period
supplier_id r o vi nt G
product_status
INVENTORIES
warehouse_id aj
product_id sto StuWAREHOUSES de
warehouse_id
list_price
min_price a
quantity_on_hand
n ฺm this warehouse_name
catalog_url n t i s e location_id
e
al to u
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t ina sells
The company n s fe categories of products, including computer hardware and
several
ale omusic,
software,
V n -traclothing, and tools. The company maintains product information that includes
n
product identification numbers, the category to which the product belongs, the weight group
(for shipping purposes), the warranty period (if applicable), the supplier, the status of the
product, a list price, a minimum price at which the product is sold, and a URL for manufacturer
information.

XML Fundamentals 1 - 9
purchaseorder.xsd
Purchase Order XML Schema
Used in the XML DB Course
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t example s the partial Purchase Order XML schema that is used in the XML DB
e n
The slide
r a nshows
al asoan
course
V n t file, purchaseOrder.xsd.
-XML
n
The Purchase Order XML schema demonstrates some key features of a typical XML
document:
• Global element PurchaseOrder is an instance of the complexType
PurchaseOrderType.
• PurchaseOrderType defines the set of nodes that make up a PurchaseOrder
element.
• LineItems element consists of a collection of LineItem elements.
• Each LineItem element consists of two elements: Description and Part.
• Part element has the following attributes: ID, Quantity, and UnitPrice.
For the complete code listing and graphical representation of the Purchase Order XML
schema, see Chapter 3 (“Using Oracle XML DB”) or Appendix A (“Oracle-Supplied XML
Schemas and Examples”) in the Oracle XML DB Developer’s Guide 12c Release 1 (12.1).

XML Fundamentals 1 - 10
Appendixes

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

Appendix A Table Descriptions

Appendix B Additional Information About Document Type Definitions,


XML Schemas, and XSLT 2.0
Appendix C Managing External Tools in JDeveloper

Appendix D Glossary
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 1 - 11
Class Account Information

• Your account IDs are hr and oe.


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

• The passwords match the account IDs.


• For the XML DB case study (optional), the account ID is
XDBOE and the password is oracle.
• Each machine has a stand-alone installation of Oracle
Database 12c Enterprise Edition Release 1 (12.1) for Linux
with access to the hr and oe sample schemas. a
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 1 - 12
Course Environment

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

– Oracle JDeveloper 12c Java and Studio Editions (used in


this course)
– Oracle SQL Developer 4.0 (optional)
– Oracle SQL*Plus (used as needed in this course)
• Oracle Database 12c (XML DB part of the database)
• hr and oe Oracle sample schemas with SYSDBA and a
XDBADMIN privileges to create and register XML schemas
h a s
) ฺrs ฺ
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
Many tools i n
t provide n s environment for executing SQL statements. Oracle provides several
an
e
al thatocan
tools r a
-tbe used to execute SQL statements. Some of the tools that are available for
V n n
use in this course are:
• Oracle JDeveloper: A free integrated development environment (IDE)
• Oracle SQL Developer: A free integrated development environment that simplifies the
development and management of Oracle Database (optional)
• Oracle SQL*Plus: A window or command-line application
• Oracle Database 12c (Oracle XML DB part of the database)
• hr and oe: Oracle sample schemas with SYSDBA and XDBADMIN privileges to create
and register XML schemas
Note: Most of the code and screen examples presented in the course notes are from output in
the JDeveloper 12c Java Edition environment. There are some instances where SQL*Plus,
(using a terminal window) was used instead of JDeveloper. Such instances are well
documented. In addition, Practice 9-2, titled “Creating a WebDAV Connection Using
JDeveloper,” uses Oracle JDeveloper Studio Edition 12c. Creating a WebDAV connection is
available only in Oracle JDeveloper Studio Edition.

XML Fundamentals 1 - 13
Entering SQL Statements by Using
Oracle SQL*Plus in a Terminal Window
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
Oraclen t
SQL*Plus a is s
n a command-line application with which you can submit SQL statements
l e
a PL/SQL
and t r
-blocks for execution and receive the results in an application or command
V
window. non
SQL*Plus is:
• Shipped with the database
• Installed on the database server system and accessed from an icon or the command
line

XML Fundamentals 1 - 14
Using Oracle JDeveloper
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s developers can create, edit, test, and debug PL/SQL code by using
t JDeveloper,
e n
With Oracle
r a n
al on-t GUI. Oracle JDeveloper is a part of Oracle Developer Suite and is also
a sophisticated
V n as a separate product.
available
To use JDeveloper with XML, you first create a database connection to enable JDeveloper to
access a database schema owner for the subprograms.
Note: For more information about using JDeveloper, see Appendix C.

XML Fundamentals 1 - 15
Entering SQL Statements and Coding PL/SQL
by Using Oracle SQL Developer
• Oracle SQL Developer is a free graphical tool that
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

enhances productivity and simplifies database


development tasks.
• You can connect to any target Oracle database schema by
using standard Oracle database authentication.
• SQL Developer is available as an optional tool in this
course. a
h a s
r s )
m sฺ eฺ
@ e uid
c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
SQLleDeveloper
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t Developer s is a free graphical tool designed to improve productivity and simplify
Oraclen
l e SQL
r a n
theadevelopment
V o n -t of everyday database tasks. With a few clicks, you can easily create and
debug n stored procedures, test SQL statements, and view optimizer plans.
SQL Developer, the visual tool for database development, simplifies the following tasks:
• Browsing and managing database objects
• Executing SQL statements and scripts
• Editing and debugging PL/SQL statements
• Creating reports
You can connect to any target Oracle database schema by using standard Oracle database
authentication. When connected, you can perform operations on objects in the database.

XML Fundamentals 1 - 16
Related Oracle University Courses:
education.oracle.com
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 1 - 17
Additional Information
Topic Website

XML DB Home Page http://www.oracle.com/technetwork/database-


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

features/xmldb/overview/index.html
Oracle Technology Network (OTN) http://www.oracle.com/technetwork/index.html

Education and Training http://education.oracle.com

Product Documentation http://www.oracle.com/technology/documentation

Product Downloads http://www.oracle.com/technology/software s a


h a
Product Articles s
http://www.oracle.com/technology/pub/articles ฺrs) ฺ
@ em uide
Product Support http://www.oracle.com/support
o v ic t G
t o r den
Product Forums s
aj s Stu
http://forums.oracle.com
a ฺ m hi
Product Tutorials t i n t
http://www.oracle.com/technetwork/tutorials/index.
n us e
a l ehtml
c ( v e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t lists Oracle s websites for education and training, documentation, software
e n
This slide
r a n
al onarticles,
downloads,
V -t support, forums, tutorials, and sample code.
n

XML Fundamentals 1 - 18
Oracle Database 12c Release 1 (12.1)
Documentation
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
You can n i n
taccessathe
n sOracle Database 12c documentation by using the following URL:
e
al on-t r
V
http://www.oracle.com/technology/documentation
n

XML Fundamentals 1 - 19
Oracle Database 12c Release 1 (12.1)
Documentation
Documentation Guide Name
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Oracle XML DB Developer’s Guide 12c Release 1 (12.1)

Oracle Database SQL Language Reference12c Release 1 (12.1)

Oracle Database PL/SQL Language Reference12c Release 1 (12.1)

Oracle Database PL/SQL Packages and Types Reference 12c Release 1 (12.1)

s a
Oracle SQL Developer User’s Guide Release 4.0
h a
Oracle Database Object-Relational Developer’s Guide 12c Release 1 (12.1) s ฺrs) ฺ
@ em uide
Oracle Database SQL JDeveloper Guide 12c Release 1 (12.1)
o v ic t G
t o r den
Oracle Text Reference 12c Release 1 (12.1) s
aj s Stu
a ฺ m hi 1 (12.1)
t i n e t
Oracle Text Application Developer’s Guide
a l en o us 12c Release

i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 1 - 20
Additional Resources: Using “Oracle by Example”
(OBE) in the Online Learning Library
OBE tutorials:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Provide step-by-step instructions for performing a variety


of new features using Oracle Database 12c
• Reduce the time that you spend investigating the required
steps to perform a task
• Use practical real-world situations so that you can gain
knowledge through valuable hands-on experience. You a
h a s
can then use the solutions as the foundation for
production implementation, dramatically reducing s ฺrs) ฺto
time
deployment. @ em uide
ic G
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 1 - 21
Additional Resources:
Oracle Learning Library (OLL)
You can access OBEs and other material by using the OLL at
http://www.oracle.com/oll.
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 1 - 22
Oracle XML DB Home Page:
http://www.oracle.com/technetwork/database-
features/xmldb/overview/index.html
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
You can n i n
taccessathe
n sOracle XML DB home page by using the following URL:
e
al on-t r
V
http://www.oracle.com/technetwork/database-features/xmldb/overview/index.html
n
This page contains useful information such as white papers, technical presentations,
demonstrations, and hands-on practices.

XML Fundamentals 1 - 23
Oracle JDeveloper Home Page:
http://www.oracle.com/technetwork/developer-
tools/jdev/overview/index.html
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 1 - 24
Additional Web Resources for XML
Topic Website
W3C XML Schema http://www.w3.org/XML/Schema
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

W3C XML Technology http://www.w3.org/standards/xml/

XML 1.0 W3C Recommendation http://www.w3.org/TR/xml/

W3C XML Schema www.w3.org/2001/XMLSchema

XML Schema Part 0: Primer http://www.w3.org/TR/xmlschema-0/

W3C XML Schema Structures http://www.w3.org/TR/xmlschema-1/

W3C XML Schema Datatypes http://www.w3.org/TR/xmlschema-2/


s a
W3C XML XPath Version 1.0 http://www.w3.org/TR/xpath/ h a
XPath Tutorial s ฺrs) ฺ
http://www.zvon.org/xxl/XPathTutorial/General/examples.html

XQuery Tutorial http://www.w3.org/TR/xquery/ @ em uide


o v ic t G
W3C Namespaces in XML http://www.w3.org/TR/xml-names/
t o r den
s
aj s Stu
Document Object Model (DOM)
a ฺ m
http://www.w3.org/DOM/
hi
t i n e t
XSL Transformations (XSLT)
en o us
http://www.w3.org/TR/xslt
a l
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 1 - 25
Course Files in the /home/oracle/labs Folder
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t i n
The /home/oracle/labs
n n s folder contains six folders: solns, lab, code_ex, demo,
l e
a onand t r a
- src.
V
classes,
n
Practices and their solutions are found in the /home/oracle/labs/lab and
/home/oracle/labs/solns folders. The solutions are named based on the lesson
number and practice question. For example, the sol_01_03.xml file is the solution code for
question 3 in Practice 1-1.
The code_ex folder contains the scripts or files for most (but not all) of the code examples in
the lessons. The code examples are named based on the lesson number and page number.
The demo folder contains a few demo scripts. The demo scripts are named based on their
functionality.

XML Fundamentals 1 - 26
JDeveloper Application and Projects
in the labs Folder
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
The /home/oracle/labs
l e n t r a n folder contains the XMLFundamentalsWorkspace JDeveloper
a onThis
application.
V - application contains several projects (as shown in the slide).
n

XML Fundamentals 1 - 27
Course Environment and Data
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Client Database

s a
) h a
r s
sฺ eฺ
m
e uid
c @
JDeveloper 12.1.2 Java and Studio Editions HRrando viOE Schemas
n t G
Mozilla Firefox Web browser a
o uXML
jstOracle t deDB
SQL*Plus a ฺ m his S
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
Client n t ina ansfe
Software
le n-JDeveloper
tr
V•a Oracle
no
(Java and Studio Editions) for creating, editing, and validating XML
documents
• Mozilla Firefox for viewing XML documents and transformations, and accessing the
Oracle XML DB folder functionality
• SQL*Plus to execute SQL or PL/SQL operations on the database data
• Java classes that use the Oracle XML Parser:
- The ShowXMLns class to display the effects of using XML namespaces
Database Software
• Oracle Database 12c with the Oracle XML DB features installed and enabled
Note: In this course, you use Oracle JDeveloper Java Edition 12.1.2 for most of the practices.
In the last practice in the course, Practice 9-2, titled “Creating a WebDAV Connection Using
JDeveloper,” you use JDeveloper Studio Edition 12.1.2 because creating a WebDAV is not
available in JDeveloper Java Edition.

XML Fundamentals 1 - 28
Practice 1-1: Overview

This practice covers using your web browser to access and


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

review some of the XML resources and documentation.

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

Oracle Database 12c: Use XML DB 1 - 29


Summary

In this lesson, you should have learned how to:


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

• Discuss the course objectives and agenda


• List the database and XML schemas used in the course
• List the appendixes
• Identify the relevant documentation and other resources

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t to annotates data by using markup to describe the data. Markup tags are user
XML isnused
e r a n
V al and
defined
o n -t therefore extensible to suit the requirements of the author or application that
are
createsnthe document.
XML standards are W3C recommendations. If you are adhering to the XML
recommendations, your XML documents must have their structure defined by a DTD or an
XML schema, thereby enabling the document to be validated and meaningfully processed by
all its consumers.
Extensible Stylesheet Language (XSL) processors can consume an XML document, enabling
it to be transformed and formatted into another type of document. Using XPath expressions,
XSL processors can search for the desired XML elements to be transformed and formatted
according to the rules specified in a stylesheet.
JDeveloper provides the tools needed to create, edit, perform well-formed checks on, and
validate XML documents. JDeveloper can be enabled to access external tools to perform
operations on XML documents.

XML Fundamentals 1 - 30
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Introduction to XML

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Define XML and the standards covered in this course


• Use JDeveloper to create, open, and edit XML and
XML-related documents
• View an XML document in JDeveloper, a web browser,
and a text editor
• Describe XML s a
h a
• Describe the structure of an XML document
s ฺrs) ฺ
• List the components of an XML document: elements,
@ em uide
attributes, and entities o v ic t G
t o r den
• Create a well-formed XML document s
aj s Stu
a ฺ m hi
i
• Use the XML Editor in JDeveloper
n t n e t
v a le to us
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
a sfe
n
Extensible t inMarkupa nLanguage (XML) has very simple yet strict syntax rules. To use XML
l e
a oand
efficiently t r
- create well-formed XML documents, you need to become familiar with the
V n n
document structure and components. This lesson introduces the XML document and focuses
on how to create well-formed documents.

XML Fundamentals 2 - 2
Lesson Agenda

• XML and the standards covered in this course


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

• Using JDeveloper to create, open, and edit XML and XML-


related documents
• View an XML document in JDeveloper, a web browser,
and a text editor
• Extensible Markup Language
– Structure of an XML document s a
h a
– Components of an XML document
s ฺrs) ฺ
– Creating a well-formed XML document
@ em uide

ic
Using the XML Editor in JDeveloper orov ent G
a jst Stud
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 2 - 3
Extensible Markup Language

Extensible Markup Language (XML) describes data objects


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

called XML documents that are composed of markup and data.

Web data
Markup and data a
h a s
s ฺrs) ฺ
XML document
@ em uide
o v ic t G
t o r den
s
aj s Stu
a ฺ m hi
n t i n e t XML processors
Custom tags
v a le to us
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n t ina data
XML describes n s fe called XML documents that:
objects
le composed
V•a Are n - tra of markup language for structuring the document data
no custom tags for data definition, transmission, validation, and interpretation
• Support
• Have become a standard way to describe data on the web
• Are processed by XML processors
XML was developed by an XML working group headed by the World Wide Web Consortium
(W3C) with the following design goals:
• XML is usable over the Internet.
• It supports a wide variety of applications.
• It is compatible with Standard Generalized Markup Language (SGML).
• XML can be processed using easy-to-write programs.
• It has a minimum number of optional features.
• XML is human-legible and reasonably clear.
• XML enables quick design preparation.
• It enables formal and concise design.
• XML documents are easy to create.
• XML documents can be verbose.

XML Fundamentals 2 - 4
Advantages of Using XML

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

• A simple and extensible way to describe data


• The ability to interchange data
• Simplified business-to-business communication
• The writing of smart agents
• The ability to perform smart searches
A sample XML document: s a
) h a
<?xml version="1.0"?> r s
sฺ eฺ
<books> m
e uid
c @
vi nt G
<title>Building Oracle XML Applications</title>
<title>Oracle XML Handbook</title>
r o
<title>Beginning XML Fourth Edition</title>
</books> a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s is its ability to perform data interchange. Because different groups of
XML’s n
e strongest
r a n
point
V al rarely
people
o n t
-standardize on a single set of tools, it takes a significant amount of work for two
n
groups to communicate. XML makes it easy to send structured data across the web so that
nothing gets lost in translation.
When using XML, you can receive XML-tagged data from your system and from another
system. Neither of the users has to know how the other user’s system is organized. If another
partner or supplier teams up with your organization, you do not have to write code to
exchange data with their system. You require them to follow the document rules defined in the
DTD. You can also transform those documents using XSLT.
When writing an agent, one of the challenges is to make sense of incoming data. A good
agent interprets information intelligently, and then responds to it accordingly. If the data sent
to an agent is structured with XML, it is much easier for the agent to understand exactly what
the data means and how it relates to other pieces of data it may already know.
The slide shows a simple example of an XML document containing a list of book titles. The
XML document can be embellished or restructured depending on how it will be used.

XML Fundamentals 2 - 5
XML Standards

Defines and Defines and


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

validates validates 5
Document type
definition (DTD) XML schema
Locates
/Catalog/Item
Uniquely identifies nodes
elements 4 XPath
1 Used by
http://www.hr.com/
s a
catalog XML language
Applies Transformed
h a
to
s ฺ r s)
6 m
eฺ
Processed
3 XML namespace by e
@ GuOutputi d
v i c
t o o ent document
rstylesheet
XSL/XSLT

7 a js Stud
n a ฺm this
i
nt u(DOM
XMLeprocessor s e and SAX API)
XQuery l
va e to
c (
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t XML s
l e n
The following
t r a nstandards are covered in this course:
V1.a Then o n- language specification defines the rules that govern XML document
XML
structure, and the XML processors must read them.
2. A document type definition (DTD) provides the definition and relationships of
elements contained in an XML document. A DTD validates an XML document.
3. XML namespaces provide a mechanism to distinguish elements with the same name
but different definitions used in the same XML document.
4. The XML Path Language (XPath) provides the syntax for searching an XML document.
XPath expressions are used in an XSL style sheet to match specific nodes.
5. An XML schema provides a way to describe the XML document structure using data
type definitions, and uses namespace support.
6. The Extensible Stylesheet Language (XSL) is implemented by XSL Transformations
(XSLT) to specify how to transform an XML document into another document. XSLT
uses an XML vocabulary for transforming or formatting XML documents.
7. XQuery is an XML query language that provides a data model for XML documents and
a set of operators to use with the data model.
The next two pages briefly discuss XML Programming Models or APIs, such as the
Document Object Model (DOM) and Simple API for XML (SAX), which are used by XML
processors for processing XML documents.

XML Fundamentals 2 - 6
Document Object Model

• Document Object Model (DOM):


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

– Defines programming interfaces for accessing and changing


an XML document as a tree structure
– Is defined by a W3C recommendation
• DOM API:
– Is platform- and language-independent
– Stores an entire XML document tree in memory a
– Can dynamically access and update the content, structure, h a s
and style of documents s ฺrs) ฺ
@ emto uXML
– Level 2 provides specialized interfaces dedicated ide
i c v t G
r o n
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t recommendeds by the W3C is a universal API for accessing the structure of XML
e n
The DOM
r a n
al onDOM
documents.
V -t provides a language-neutral and platform-neutral object model for web
n XML documents. DOM describes language-independent and platform-independent
pages and
interfaces to access and operate on XML components and elements. It expresses the
structure of an XML document in a universal, content-neutral way. You can build applications
to dynamically delete, add, and edit the content, attributes, and style of XML documents. DOM
makes it possible to create applications that work properly on all browsers, servers, and
platforms.
When you use the DOM API, the whole XML document is read into memory, making its entire
tree structure accessible to the API.
The DOM API Core standard has evolved and is implemented by most XML Parsers, which
typically support some level of DOM specification. Current DOM specification levels are:
• DOM Level 1 defines ways to navigate and manipulate the content of HTML or XML
documents.
• DOM Level 2 extends Level 1 by adding support for XML namespaces, filtered views,
ranges, events, and other features.
• DOM Level 3 extends Level 2 by defining how the DOM maps to the XML Infoset.

XML Fundamentals 2 - 7
Oracle XML Parser supports most of DOM Level 2 and some Level 3 features.
Note: Using the DOM API to process large documents can consume more memory resources
compared to working on the documents by using the stream-based processing approach
provided by SAX.
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 Fundamentals 2 - 8
Simple API for XML

Simple API for XML (SAX):


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

• Is an event-based API for reading XML documents


• Reads a document as a serialized (ordered) event stream
of data
• Uses events to notify programs when it detects document
objects such as start and end tags
• Does not store the document in memory and does not s a
support random-access manipulation h a
s ฺrs) ฺ
em uide
• Is a good choice for processing very large documents
@
• Is developed independently of W3C rovic nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tprocessing smodel is event-based. A program that uses SAX reads an XML
e n
The SAX
r a n
al oas
document
V n -at serialized stream of data, that is, one piece at a time. The SAX XML Parser
n read an entire XML document into memory and uses events to notify the program
does not
when it encounters a document object such as:
• A start tag
• An end tag
• Character data
• Processing instructions
Your program can then act on these events to save or process the recognized object. An
important feature of SAX is that the document is not read into memory, like the DOM model
requires. This makes SAX a very good choice when processing very large documents,
because the program does not consume as much memory as the DOM API.
Because of its popularity, SAX has become a de facto standard. Many XML Parsers provide
support for the API. Strictly speaking, SAX, which has been developed independently of the
W3C organization, has not been ratified as a standard or recommendation.
Note: Oracle XML Parser includes support for both the DOM API and the SAX API.

XML Fundamentals 2 - 9
Oracle XML Support

Oracle products provide support for XML in:


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

• Development tools

• Oracle Fusion Middleware


s a
) h a
• Database storage r s
sฺ eฺ
m
e uid
c @
OracleoXDK
r vi 12cnt G
a jsto Stude
Includes the Oracle
n a ฺm this
XML Database (XDB) i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t tools sand frameworks: Oracle XML Database (XDB), Oracle JDeveloper,
e n
Development
r a n
V al Internet
Oracle
o n -t File System (iFS), Oracle Application Development Framework (ADF) in
n Oracle Portal, Oracle Application Server Reports Services, and Oracle Dynamic
JDeveloper,
Services can be used to build XML applications.
Oracle Fusion Middleware: This runs in the Oracle Application Server environment that
provides a runtime platform and architecture for J2EE, web services, and applications that
can communicate, integrate, and collaborate with XML using a rich set of SOA-enabled
technologies.
Database storage: In Oracle Database 12c, Oracle XDB enables XML documents to be
stored in and retrieved from relational tables, XMLType columns, XMLType object tables, and
binary XML and object-relational tables.
Oracle XML Developer’s Kit (XDK): Oracle XDK is available in Oracle JDeveloper, Oracle
Application Server (part of the Oracle Fusion Middleware product family), and Oracle
Database 12c. Oracle XDK provides the following components, tools, and utilities:
• XML Parsers, XSLT processors, XSLT VM, XML schema processors
• XML JavaBeans, XML Class Generator, XSQL Servlet, XML SQL Utility (XSU)
• XML Pipeline Processor, TransX Utility

XML Fundamentals 2 - 10
XML in Oracle JDeveloper

Oracle JDeveloper is a development tool that:


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

• Is used for:
– Building Java, XML, and PL/SQL applications
– Debugging the applications
– Deploying Internet-enabled applications
• Includes Oracle Application Development Framework
(ADF) s a
h a
• Enables seamless Java, XML, XML
s ฺrs) ฺ
and SQL development of: em e
c@ t idJava
– Java for distributed code v i Gu
– XML for portable data t o ro den
s
aj s Stu
ฺ m
– SQL for querying na nthi
ti se SQL
e
al to u
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
Oraclen t ina anissfaepowerful development tool for building, debugging, and deploying
JDeveloper
V aleweb
J2EE,
o n - tr and Internet applications. Oracle JDeveloper:
services,
• Isnan integrated development environment (IDE) that provides end-to-end support for
developing, debugging, and deploying J2EE applications and web services
• Provides a flexible environment for creating, editing, and debugging XML applications
and supporting the latest XML and Internet standards
• Provides developers with a set of well-integrated tools to productively develop and
deploy applications by using Java, XML, PL/SQL, and SQL at low cost
• Maximizes developer productivity by supporting the complete development life cycle
from modeling, coding, and debugging to testing and profiling
• Provides access to source code control to manage the entire development process and
enable distributed development teams to work cooperatively
• Includes the Oracle Application Development Framework (ADF), which was previously
known as Business Components for Java. Oracle ADF is a 100% Java, XML-powered
application component framework that greatly simplifies the development, deployment,
and customization of Internet-enabled applications
This course uses the XML support that is available in JDeveloper 11.1.1.2.0.

XML Fundamentals 2 - 11
Building XML Applications
with Oracle Technology
• An XML application is an application process that operates
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

on XML data, either stored in or generated from a data


source.
• Oracle XML applications can be built in one of the
following ways:
– Non-programmatically
– Programmatically
s a
) h a
r s
sฺ eฺ
m
e uid
c @
Client Application
r o vi nDatabase
t G
server sto
a j t u de
a ฺ m his S
XML application nti
n et
v a l to us
e
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n t
The diagraminaillustrates
n s fewhere an XML application processing XML data (solid lines) can run
ale lines)
(dotted
V n - trathe client, middle tier, or the database. The data may also reside in the
on
Oracle n o
database in XML or relational form.
XML Applications Without Programming
To help application developers take advantage of XML, Oracle Application Server includes
the XML Developer’s Kit (XDK). The XDK is a standards-based set of components, tools, and
utilities that eases the task of building and deploying XML-enabled applications.
XML Applications by Using Programming
Applications can be developed with the APIs provided by the XDK. Web applications,
developed with JDeveloper, can use Java Database Connectivity (JDBC) to connect with an
Oracle database to create powerful yet relatively simple data-driven XML applications.
Oracle Database has built-in features for storing, manipulating, transforming, and validating
XML data. These features are accessible through the Oracle XDK and XML functionality
available to SQL and PL/SQL services in the database.

XML Fundamentals 2 - 12
XML in Service-Oriented Architecture

• Service-Oriented Architecture (SOA) is an architectural


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

style for integrating loosely coupled, interacting software


services.
• XML language:
– Is used by the standards that enable SOA
– Simplifies communication and interoperability between SOA-
enabled applications
a
– Provides ease of data representation has
s ฺrs) ฺ
@ em uide
Service request
o v icServicet G
Service r
to tuprovider de n
consumer a j s
Service response
a ฺ m his S
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t ina aArchitecture
Service-Oriented n sfe (SOA) is an architecture for the development of loosely coupled
l e
distributed - t
a onapplications.r It represents a collection of many services in the network. These
V n
services communicate with each other, and the communications involve either simple data
passing or two or more services coordinating some activity.
SOA enables loose coupling between interacting applications, agents, or services. Loose
coupling is possible by using standards-based technologies that enable platform
independence and interaction on the basis of the request-response architecture provided by
the Internet.
The function provided by a service is a unit of work performed by a service provider to
achieve the desired end results for a service consumer. The provider and consumer roles are
implemented through software components.
XML is used as a common representation format in SOA-enabled environments because of
its platform and language independence.
SOA-enabled systems involve the services and service interaction patterns. A service is a
business logic (a function or any processing unit) that is well defined, self-contained, and
does not depend on the context or state of other services. For example, a weather service
might provide weather-related information. Any application on the network can use the
weather service to get the weather information.

XML Fundamentals 2 - 13
Importance of XML in Web Services

• A web service is a technology that is based on a set of


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

standards for building interoperable distributed


applications.
• Web service standards such as SOAP, WSDL, and UDDI
are XML-based.

s a
Registration and Discovery:
Universal Description, Discovery and Integration (UDDI) ) h a
r s
sฺ eฺ
Service Description:
Web Services Description Language (WSDL)
m
e uid
c @
Messaging:
r o vi nt G
SOAP
a jsto Stude
aฺm thlanguage.
Representation:
n
XML is the universaltirepresentation
is
l en o us e
a
(v se t
i c
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n
Many businesses n s are becoming e-businesses. The greatest barrier to this
today
e r a
al on-t is application-to-application communication. An enterprise system usually has
transformation
V n
multiple applications, each running on different hardware and sometimes implemented in
different languages. These applications often need to exchange data with one another, and
developers must write additional code to make this integration possible. A web service is a
technology (based on a set of standards) that provides a solution for application-to-application
communication and enterprise application integration.
Using web services, clients and servers can communicate over standard Internet protocols
regardless of the platform or programming language. Developers can write their business
functionality in any language and on any platform.
To be exposed and accessed as web services, the applications must be developed according
to the standards used by web services. Web service standards make use of the core concept
of XML-based representation to carry out information exchange between service providers
and requestors.
Simple Object Access Protocol (SOAP) is a lightweight, XML-based protocol for exchanging
data in a distributed environment. The definition of SOAP is in XML and places no restriction
on the format.

XML Fundamentals 2 - 14
Web Services Description Language (WSDL) uses the XML format to describe web services.
It describes what functionality a web service offers, how it communicates, and where it is
accessible. Because WSDL is in standard XML format, the services developed can easily be
made available to thousands of users. Users must invoke the correct services with the
appropriate parameters. WSDL defines an XML grammar to specify the location of the service
and to describe the operations of a service.
UDDI is an XML-based registry that enables businesses to list themselves and their services
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

on the Internet.
A web service may be defined by a WSDL interface, and the WSDL interface may import XML
Schema files. The XML schema definitions may be used to describe the message exchanges
by operations defined by the WSDL for the web service.

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 Fundamentals 2 - 15
Creating an Application and Project in JDeveloper

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

Double-click or
click Next.

s a
3
) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a
t with s documents in JDeveloper, you need to create or open an application
e n
When working
r a nXML
V ala project.
with
o n -tAn application contains one or more projects. A project contains one or more
projectn
files.
To create a new application with a project, perform the following steps:
1. Select File > New or click the New Application icon in the Applications pane. The
New Gallery window is displayed.
2. Select Applications from the General category, and then double-click Java Desktop
Application in the Items section. The Create Java Application – Step 1 of 4 window
is displayed.
3. In the Create Java Application – Step 1 of 4 window, perform the following steps:
a. Enter an Application Name (this becomes the file name).
b. Enter, or browse to, the Directory name for the application file.
c. Click Next. The Create Java Application – Step 2 of 4 window is displayed.

XML Fundamentals 2 - 16
Creating an Application and Project in JDeveloper

4
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a a
a Mforsthe
4. Entertianname n ferproject. This is the file name that will be used for the project within
e n r a
l file nsystem.
Vathe o -t The .jpr extension is assumed, but not displayed. In this course, we are
n JDeveloper 12c; therefore, there is only one available project feature available,
using
Java. In the JDeveloper Studio edition, there are other types of project features available.
Accept the default selection, and then click Next. The Create Java Application – Step 3
of 4 window is displayed.
5. Your new project starts with a default package, Java source path, and an output directory.
Click Next. The Create Java Application – Step 4 of 4 window is displayed. Accept the
default selection, and then click Next.
Note
To open an existing application, select File > Open, and then locate and double-click a file
name with a .jws extension.

XML Fundamentals 2 - 17
Creating an Application and Project in JDeveloper
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

7
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a ra and then click Finish.
M feselection,
t a
6. Acceptinthe default
s
lennewly
7. aThe - t r an application and project are displayed in the Applications and Projects
created
non
V pane.

XML Fundamentals 2 - 18
Creating an XML Document in JDeveloper
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Double-click
1

s a
h a
s ฺrs) ฺ
@ em uide
o v ic 3 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
To create i n
t a newaXML
n s document in a project, perform the following steps:
l e r
-t the project name in the Applications Navigator, and then select New.
V1.a Right-click
o n
n New Gallery window is displayed.
The
2. Select the XML node from the General category, and then double-click XML Document
in the Items section. The Create XML File window is displayed.
3. In the Create XML File window, enter the File Name. Then enter, or browse to, a
Directory name to store the file, and click OK.

XML Fundamentals 2 - 19
Creating an XML Document in JDeveloper

5
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t XMLandocument
4. The
e n new
linitial content
t r is created as a child entry of the project in the Navigator. The
a -
V non is displayed in the JDeveloper XML Code Editor pane. The XML document
also contains the following line:
<?xml version='1.0' encoding='UTF-8'>
5. Click Save to save the new project.
UTF stands for Universal Character Set Transformation Format. XML documents can contain
international characters. To avoid errors, you should specify the encoding used, or save your
XML files as UTF-8. Character encoding (or character set) defines a unique binary code for
each different character used in a document. Unicode is an industry standard for character
encoding of text documents. It defines every possible international character by a name and a
number. The standard character encoding on the web is UTF-8. This line of text may be
different if JDeveloper is running on a different platform such as MS-Windows. In MS-
Windows, the encoding used is Windows-1252. In this course, you are using a Linux
environment.

XML Fundamentals 2 - 20
Adding an XML Document to a Project
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

2 a
h a s
r s )
m sฺ eฺ
@ e uid
c
vi nt G
r
to tudeo
a j s
3
a ฺ m his S
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t a sfe
inXML
To addnan
l e t r a n
document to a project, perform the following steps:

n o n- the project name in the Applications Navigator, and then select Project
V1.a Right-click
Properties from the context menu. The Project Properties window is displayed.
2. In the Project Properties window:
a. Click Add next to Java Source Paths to add the directory that contains the XML
document. The Choose Directory window is displayed. Navigate to the directory,
and then click Select.
b. Click Add next to the Included tab to add the XML document. The Add Include
Filters window is displayed.
c. Select the XML document that you want to add to the project, and then click OK.
3. The newly added XML document is added as a node to the project.

XML Fundamentals 2 - 21
Viewing the Contents of an XML Document

1. Use a web browser.


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

s a
2. Use a text editor. ) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t can display s the XML document in your web browser. In the course environment,
1. You
l e n r a n
Va Mozilla
o n -tFirefox is the default browser.
na. Open the Mozilla Firefox application as shown in the first example in the slide.
b. Navigate to the directory where the XML document resides.
c. Double-click the XML document file name. The browser displays the XML
document tree, which can be expanded or collapsed. Initially, the document tree is
expanded as shown in the slide example. To collapse the tree or a branch, click
the “+” sign at the beginning of the line. To expand the tree or a branch, click the
“-” sign at the beginning of the line.
2. You can also display the XML document using a text editor. In this course, you can
display the XML document using the gedit text editor as follows:
a. Navigate to the folder that contains the XML document as shown in the second
example in the slide.
b. Right-click the file name, and then select “Open with gedit” from the shortcut menu.
The XML document is displayed in the text editor.

XML Fundamentals 2 - 22
Viewing an XML Document in JDeveloper

1. Use a web browser external tool.


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

s a
2. Use JDeveloper’s Code Editor pane. ) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s command line to be initiated from the JDeveloper Tools menu.
tadd youranbrowser
You can
e n r
V al is done
This
o n -byt creating an External Tools command by using the Tools > External Tools
menu. n
In this course, JDeveloper is already set up to include Mozilla Firefox in the Tools menu, as
seen in the first screenshot in the slide. To launch the browser from the Tools menu:
1. Select the XML document in the Applications Navigator.
2. Select Tools > Firefox.
You can also right-click the XML file in the JDeveloper Navigator and XML Editor, and then
select Firefox from the shortcut menu.
Note: To learn how to create and access an external tool from within JDeveloper, see
Appendix C (“Managing External Tools in JDeveloper”).
You can also display the contents of an XML document using the Code Editor pane as
follows:
1. Locate the file in the Applications pane.
2. Double-click the file. The content of the file is displayed in the Code Editor window, as
shown in the second example in the slide.

XML Fundamentals 2 - 23
Quiz

Oracle JDeveloper is a development tool that:


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

a. Is used to build Java, XML, and PL/SQL applications


b. Is used to debug applications
c. Is used to deploy Internet-enabled applications
d. Includes Oracle Application Development Framework
e. Enables seamless Java, XML, and SQL development of
Java, XML, and SQL s a
) h a
f. Is used to develop and debug PL/SQL applicationssฺrs m ideฺ
e
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t b, c,ad,nse
Answer:na,
l e r
Va non-t

XML Fundamentals 2 - 24
Practice 2-1: Overview

This practice covers the following topics:


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

• Opening an existing course application and project with


JDeveloper
• Creating a basic XML document by using JDeveloper
• Opening an existing XML document in JDeveloper
• Viewing XML documents in a web browser
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s is to help you become familiar with the basic features of JDeveloper
tof this apractice
n
The goal
e r n
V alare needed
that
o n -t to create XML documents.
You usen an Internet browser (such as Firefox) to view simple XML documents and the default
visual presentation style of XML data in the browser. Initially, the browser displays the XML
document content in a tree-like structure.
Subsequent lessons and practices cover how to use the XML editing features of JDeveloper.

XML Fundamentals 2 - 25
Lesson Agenda

• XML and the standards covered in this course


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

• Using JDeveloper to create, open, and edit XML and XML-


related documents
• View an XML document in JDeveloper, a web browser,
and a text editor
• Extensible Markup Language
– Structure of an XML document s a
h a
– Components of an XML document
s ฺrs) ฺ
– Creating a well-formed XML document
@ em uide

ic
Using the XML Editor in JDeveloper orov ent G
a jst Stud
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 2 - 26
Extensible Markup Language

Extensible Markup Language (XML):


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

• Describes data objects called XML documents


• Is composed of markup language for structuring data
• Requires custom tags for definition, transmission,
validation, and interpretation of data
• Conforms to Standard Generalized Markup Language
(SGML) s a
) h a
• Has become a standard way to describe data on the ฺrsweb
s
• Is processed by XML processors em 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
XML isnatmarkup n s
language that provides a universal format for structured documents and
l e
a on the
data - t r a
V n o nweb.
Although XML documents look similar to HTML documents, they are very different.
• HTML is a markup language that is primarily used for formatting and displaying text and
images in a browser.
• XML is a markup language for structuring data rather than formatting information.
You use XML to create a document that contains structured data that can be used or
interpreted by other applications. The format or structure is straightforward and can be used
by any person or program that can read text.
Unlike HTML, the tags in XML are extensible, and so you can create your own tags as you
need them. HTML has a set of predefined formatting tags that you can use, but you cannot
create your own.
XML is part of the World Wide Web Consortium (W3C) standards.
Note: XHTML is a more refined version of HTML. XHTML stands for “Extensible Hypertext
Markup Language.” For more information, see the section of this lesson titled “Comparing
XML and HTML.”

XML Fundamentals 2 - 27
XML Documents Form a Tree Structure

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

<child>
<subchild>.....</subchild>
</child>
</root>

<?xml version="1.0"?>
<employees>
<employee>
<employee_id>120</employee_id>
s a
<last_name>Weiss</last_name>
) h a
<salary>8000</salary> r s
sฺ eฺ
</employee> m
e uid
<employee>
c @
<employee_id>121</employee_id>
r o vi nt G
<last_name>Fripp</last_name>
a jsto Stude
<salary>8200</salary>
n a ฺm this
</employee>
i
nt use
</employee>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s contain a root element. This element is “the parent” of all other
e n
XML documents
r a n
must
al oThe
elements.
V n -t elements in an XML document form a document tree. The tree starts at the
n then branches to the lowest level of the tree. All elements can have subelements
root and
(child elements).
The slide example displays employee information, which is self-descriptive. It contains
employee information wrapped in custom tags. Unlike HTML, XML does not have predefined
tags.

XML Fundamentals 2 - 28
XML Document Structure

An XML document contains the following parts:


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

<?xml version="1.0"?>
<!–- this is a comment --> Prologue

<employees>
... Root element
s a
</employees> h a
s ฺrs) ฺ
@ em uide
<?gifPlayer size="100,300"?> Epilogue ic G
r o v n t
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s the following parts:
An XML n
e document
l prologue, r a n
contains
V•a The o n -t which may contain the following information:
n- XML declaration (optional in XML 1.0; mandatory in XML 1.1)
- Document type definition (DTD), which is required only to validate the document structure
- Processing instructions and comments, which are optional
• The root element, which is also called the “document element” and contains all other elements
• An epilogue, which contains processing instructions and comments
Processing instructions give commands or information to an application that processes the XML data.
Processing instructions have the <?target instructions?> format, where target is the name of
the application that is expected to do the processing, and instructions consists of a string of
characters that embodies the information or commands for the application to process.
Processing instructions can be written in the prologue, epilogue, or root element of an XML document.

XML Fundamentals 2 - 29
Simple XML Document: Example

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

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


<!–- This is a simple XML document. --> Comment
<employees> Employees Root element
<employee>
<employee_id>120</employee_id>
<last_name>Weiss</last_name> Employee element
<salary>8000</salary> Employees
</employee> has (2)
Employee element child
<employee>
data content s a
<employee_id>121</employee_id> Employee
) h a
<last_name>Fripp</last_name> elements
r s
sฺ eฺ
<salary>8200</salary> m
e uid
c @
vi nt G
</employee>
</employees> r o
<?gifPlayer size="100,300"?>
a jsto Stude
n a ฺm this
i
ntEpilogue s e
l e u
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t example s a simple XML document uses nested elements to describe employee
e n
The slide
r a nof
V al Elements
data.
o n -t are identified by tag names such as employee, employee_id, and
n Tag names are distinguishable as markup, rather than data, because they are
last_name.
surrounded by angle brackets (< and >). In XML, an element includes a start tag
(<employees>), an end tag (</employees>), and all the markup and character data
contained between those tags. Tag names are case-sensitive (and their case must be
identical).
An XML document contains the following parts:
• The prologue, which may contain the following information: XML declaration, document
type definition (DTD), which is required only to validate the document structure, and
processing instructions and comments, which are optional
• The root element, which is also called the “document element” and contains all other
elements
• Any child elements (two employees in the example)
• An epilogue, which contains processing instructions and comments. Processing
instructions give commands or information to an application that processes the XML
data.

XML Fundamentals 2 - 30
XML Declaration

XML documents should start with an XML declaration.


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

An XML declaration:
• Looks like a processing instruction with the xml name
• Is optional in XML 1.0 but mandatory in XML 1.1
• Must contain one of the following version attributes:
– 1.0
– 1.1 s a
) h a
• Can include the following optional attributes: r s
sฺ eฺ
– Encoding m
e uid
c @
– standalone r o vi nt G
a jsto Stude
<?xml version="1.0" encoding="UTF-8"?>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t recommended s
l e n
It is generally
t r a n that you start an XML document with an XML declaration. If it is
a oit must
present, - be the first line in the document. Although the XML declaration resembles a
V n n
processing instruction, it is really a simple declaration. In the XML 1.0 specifications, an XML
document does not require the XML declaration. However, in the XML 1.1 specifications, the
XML declaration is mandatory.
Note: Not all tools and XML Parsers support the XML 1.1 syntax.
The only attribute that is mandatory in the declaration is the version attribute. Additional
attributes that can be added to the XML declaration include the following:
• encoding attribute, which is optional. By default, XML documents are encoded in the
UTF-8 format of the Unicode character set. The encoding for Windows is WINDOWS-
1252. This is useful for applications (such as those written in Java) that can handle
other encoding formats.
• standalone attribute, which is optional and may be set to the yes or no value. If this
attribute is omitted, the value is assumed to be no. Set the standalone value to:
- No, which indicates that the document may depend on an external DTD (covered
later in this course) to determine the document structure
- Yes, which indicates that the document exists entirely on its own, without
depending on other files such as a DTD

XML Fundamentals 2 - 31
Components of an XML Document

XML documents are storage units that contain:


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

• Parsed character data (PCDATA), which includes:


– The markup used to describe the data that it contains
– Character data described by the markup
<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="100">
<name>Rachael O&apos;Leary</name>
s a
</employee> h a
</employees>
s ฺrs) ฺ
@ em uide
• Unparsed character data (CDATA): Textual
o v ic or binary
t G
r n
e entered
information (graphic and sound data)
a jstotaken t u das
a ฺ m his S
n tin ]]> e t
<![CDATA[ ...unparsed data...
e
al to u s
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t ina aare
XML documents n fe units that contain parsed or unparsed data.
sstorage
a le n-tr
V no
Parsed character data (PCDATA) is textual information comprising:
• The markup that describes the data it contains. Markup includes:
- Elements to describe the data it contains such as the root element (employees)
and its child elements (employee, name)
- Attributes, which are name and value pairs (id="100") included in the start tag of
an element
- The entities (&apos;) representing any character data substituted in place of their
appearance
• Character data described by the markup components, such as the following:
- The value 100 assigned to the id attribute
- The data “Rachael O’Leary” described by the <name> element
- The &apos; entity, which represents the apostrophe (') character
Note: The element tree in an XML document defines its layout and logical structure.
The unparsed data embedded in CDATA sections can be used in an XML document to contain
textual data or encoded binary data such as graphic and sound files. A CDATA section starts
with <![CDATA[ and ends with ]]> characters. The information contained inside the CDATA
section is not parsed by an XML Parser and is taken as entered.
XML Fundamentals 2 - 32
XML Elements

• An XML element has a start tag, an end tag, and optional


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

data content such as employee and name.


• Tag names are case-sensitive.
• Start and end tags must be identical.

Tag name, start tag


Data content
s a
<employee>
h a
<name>Steven King</name>
s ฺrs) ฺ
</employee>
@ em uide
o v ic t G
Tag name, end tag
t o r den
s
aj s Stu
employee element
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t taga(for
• Anstart n sexample, <employee>) includes:
l e r
-t “<” character
Va n- onThe
- A case-sensitive tag name (employee), without leading spaces
- The “>” character
• An end tag (for example </employee>) includes:
- The “<” character
-A case-sensitive tag name that must be identical to the start tag name, but prefixed
with a slash. Leading spaces are not permitted.
- The “>” character
• Data content: It can also contain elements such as <name> in the slide example.
In summary, an XML element includes a start tag, an end tag, and everything in between.
Empty elements have no content between the start and end tags. In this case, a shortened
form can be used where the start tag name is followed by a slash (for example,
<initials/>).
Tag names are a descriptive term for an XML element and its content (for example,
employee). The tag name is known as the element type name.

XML Fundamentals 2 - 33
Empty Elements

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

• Contain no data
• May appear as a single tag

<initials></employee>

s a
) h a
r s
sฺ eฺ
<initials/> 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t have sno content between the start and end tags. In this case, a shortened form
l e n
Empty elements
r a n
can a
V be used o -t the start tag name is followed by a slash (for example, <initials/>).
where
n
n

XML Fundamentals 2 - 34
Markup Rules for Elements

• There is one root element, which is sometimes called the


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

top-level or document element.


• All elements:
– Must have matching start and end tags, or must be a self-
closing tag (an empty element)
– Can contain nested elements so that their tags do not
overlap
s a
– Have case-sensitive tag names that are subject to naming h a
conventions (starting with a letter, having no spaces,
s s) not
ฺrand
starting with the letters xml) em ideฺ
@ u
– May contain white space (spaces, tabs, o v
newic lines,
t Gand
r n
deof the element
jsto Stupart
combinations of these) that is considered
a
data content aฺm his tin se t
e n
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t a sfe
indocument
l e n
Every XML
t r a n must contain one root element (top-level or document element). XML
a onare
documents
V - hierarchical in structure with elements nested within others forming a
n tree. The start and end tags for elements must not overlap. For example:
document
<employee>
<first_name>Steven<last_name>king</first_name></last_name>
</employee>
Here, the <last_name> element overlaps the <first_name> element. This is not
permissible. The correct form is:
<employee>
<first_name>Steven</first_name><last_name>king</last_name>
</employee>
Element start and end tag names must be identical; that is, they are case-sensitive and they
must be in the same case. For example, <Employee>, <employee>, and <EMPLOYEE> are
distinct and different tag names.
Element tag names must start with a letter or an underscore (_) but not with numeric or
punctuation characters. Numeric, dash (-), and period (.) characters are allowed after the
first letter, but not white space. Tag names cannot start with the xml letter sequence or any
case-sensitive combination thereof, such as XML and Xml. White space, including new lines,
are considered part of the data; that is, no stripping of white space is done. However, XML
Parsers treat end-of-line characters as a single line-feed.
XML Fundamentals 2 - 35
XML Attributes

An XML attribute is a name-value pair that:


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

• Is specified in the start tag (after the tag name)


<?xml version="1.0" encoding="UTF-8"?>
<employees>
<employee id="100" name='Rachael O&apos;Leary'>
<salary>1000</salary>
</employee>
</employees> s a
) h a
• Has a case-sensitive name r s
sฺ eฺ
m
e within
id
• Has a case-sensitive value that must be enclosed i c @ G u
matching single or double quotationto rov dent
marks
a js Stu
• Provides additional information ฺ m histhe XML document
about
a
tin se t
or XML elements n
e
al to u
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n
Attributes t nasimple
iare n s fe
name-value pairs that are associated with a particular element. XML
l e
a omust
attributes t r a
- be specified after the start tag of an element or after the tag name of an empty
V
element. n n
Example: <employee id="100" email="SKING"/>
Attribute names are case-sensitive and follow the naming rules that apply to element names.
In general, spaces are not used, but they are allowed on either side of the equal sign.
Attribute names should be unique within the start tag.
The attribute values must be within matching quotation marks, either single or double. The
example in the slide shows the employee id attribute value enclosed within double quotation
marks and the name attribute value within single quotation marks. In the latter case, the
&apos; entity must be used to include the apostrophe (single quotation mark) character in
the name value.
Attributes provide additional information about the XML document’s content or other XML
elements. Attributes can be used for the following purposes:
• Describing how the XML document data is encoded or represented
• Indicating where the links or external resources are located
• Identifying and calling external processes such as applets and servlets
• Specifying an element instance in the document for facilitating a rapid search
Note: Attributes always have a value. For example, name="" has an empty string value.

XML Fundamentals 2 - 36
Naming Convention Rules
for Elements and Attributes
Element and attribute names:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Can contain the letters A–Z and a–z


• Can contain the numbers 0–9
• Can contain ideograms and non-English characters
• Can contain an _ (underscore), ‒ (dash), or . (period)
• Must start with a letter or an _
• Cannot start with a number, punctuation character, or the s a
) h a
letters xml (or XML, Xml, and so on) sฺrs m ideฺ
• Cannot contain spaces @ e
o
c
vi nt Gu
• r
Can use any name (no words are reserved)
to de a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a
t ideogram s(from Greek “idea” + “to write”) is a graphic symbol that represents an
e n
Note: An
r a n
V al or concept.
idea
o n -t Examples of ideograms include wayfinding signs, such as in airports and
n
other environments, where people may not be familiar with the language of the place they are
in. Ideograms are used worldwide regardless of how they are pronounced in different
languages.

XML Fundamentals 2 - 37
Using Elements Versus Attributes

<?xml version="1.0"?>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<employees> id, last_name, and


<employee> salary as elements
<id>100</id> 1
<last_name>King</last_name>
<salary>24000</salary>
</employee>
</employees>

s a
h a
<?xml version="1.0"?>
s ฺrs) ฺand
id, last_name,
<employees> em asuattributes
salary
@ ide
<employee id="100" last_name="King" ic G2
r o v n t
jsto Stude
salary="24000">
</employee>
ฺm thisa
</employees>
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t youasee stwo examples:
e n
In the slide,
l Example r n
V1.a Inemployee
o n -t 1, id, last_name, and salary are defined as elements within the
n element, whose parent is the employees element.
2. In Example 2, id, last_name, and salary are attributes of the employee element,
whose root element is employees.
Elements and attributes accomplish approximately the same thing. That is, they describe the
data. The examples shown are both valid and communicate the same basic information about
an employee.
Note: In general, elements are more robust and flexible to use than attributes.
The choice to use elements or attributes is often a matter of preference, but it takes some
time and experience to determine the best style to use for different contexts. The easiest and
most consistent approach is to use elements for data that the user of the document wants to
see, and attributes for metadata representing additional information about the data that the
document may require. The following are additional points to consider:
• Elements are more easily added as your requirements expand.
• Elements may be ordered, but attributes are returned unordered from the document.
• Elements can be structured and may contain other elements.
• Attributes are atomic; that is, they cannot be nested or have attributes of their own.

XML Fundamentals 2 - 38
XML Entity

• Is used as replacement text (substituted) when referencing


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

its name between an ampersand (&) and a semicolon (;)


• Has predefined names for special XML characters:

Entity Reference Description


&lt; Represents a less-than sign ( < )
&gt; Represents a greater-than sign ( > )
s a
&amp; Represents an ampersand character ( & )
h a
&quot; Represents a double quotation mark ( " ) s ฺrs) ฺ
&apos; Represents a single quotation mark or apostrophe @ em ( u' i)de
o v ic t G
t o r den
s
aj s Stu
ฺ m hi
a&lt; t1000</comment>
<comment>Salaries must not ibe
t n e
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
An XML n i n s of data storage that:
tentity isaanunit
l e t r
on- by a case-sensitive name
V•a Isnidentified
• Represents replacement text to eliminate a lot of typing in XML documents
• Is referenced by its name prefixed with an ampersand (&) and terminated by a
semicolon (;). The ampersand is referred to as an escape character.
The XML standard predefines entity names for the following special characters:
• The less-than sign (<) represented by the name &lt;
• The greater-than sign (>) represented by &gt;
• The ampersand (&) represented by &amp;
• The double quotation mark (") represented by &quot;
• The single quotation mark or apostrophe (') represented by &apos;
In the following invalid example, the XML Parser expects a tag name after the less-than (<)
sign:
<number>150<100</number>
This invalid syntax is corrected by using the &lt; built-in entity to represent the less-than
character, as in the following example:
<number>150&lt;100</number>
Note: User-defined entity names can be declared in a document type definition (DTD).
XML Fundamentals 2 - 39
XML Character Data (CDATA)

The CDATA section:


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

• Is not read by a parser


• Is passed to the application without change
• Is used to contain text that has several XML-restricted
characters such as <, >, &, ", and '
<script>
if(a &gt; b &amp; b &gt; c)
1 s a
{print(&quot; a is greater than c &quot;)} h a
</script> s ฺrs) ฺ
@ em uide
<script><![CDATA[
o v ic t G
if(a > b & b > c) t o r den
{print("a is greater than c")} s
aj s Stu
ฺ m i 2
]]> a
tin se t h
</script> e n
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n
The text t ina ainside
written n sfea CDATA section is not read by the parser and is passed to the
ale onwithout
application
V -tr modification.
n such as <, >, &, ', and " are restricted in XML. You must replace them with
Characters
entities.
If the text inside an element contains many of these restricted characters, replacing them with
entities makes the text unreadable and bulky. In such cases, you can write that portion of text
as a CDATA section.
For example, suppose that you want an element to contain the following script:
if (a > b & b > c)
{
print(“a is greater than c”)
}
Without using CDATA, you must write the element as shown in box 1 in the slide.
Alternatively, you can write the same text as CDATA as shown in box 2.

XML Fundamentals 2 - 40
XML Comments

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

• Start with <!--


• End with -->
• May appear anywhere in the character data of a
document, and before the root element
• Are not elements and can occupy multiple lines
• May not appear inside a tag or another comment s a
h a
s ฺrs) ฺ
em uide
<?xml version="1.0" encoding="UTF-8"?>
<!-- Comment: This document has information@ about
o v ic t G
employees in the company -->
t o r den
<employees> s
aj name S tu
<name>Steven King</name> <!-- a ฺ m
Full
h is -->
</employees> i n
nt use t
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na sa record of what is done in an XML document. They can appear
e n
XML comments
r n
are
a
al oinn-the
anywhere
V t XML document, except inside the following:
• nThe start or end tag of an element (They are thus not permissible in attribute values.)
• Another comment (That is, comments cannot be nested.)
Because comments are not considered to be XML elements, their appearance does not
violate the tree structure or one-root element requirement for well-formed XML documents.
The slide example shows a comment:
• Before the root element
• After the end tag of the </name> element
Note: In general, a comment must not appear before the XML declaration. However, some
XML Parsers, as in Internet Explorer, do not mark it as an error.
Applications must not depend on comments because they are intended not for processing
purposes but merely for making the XML document more meaningful to the people reading
their contents.

XML Fundamentals 2 - 41
Well-Formed XML Documents

Every XML document must be well-formed:


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

• It must have one root element.


• An element must have matching start and end tag names
unless it is an empty element.
• Elements can be nested but cannot overlap.
• All attribute values must be quoted.
• Attribute names must be unique in the start tag of an s a
h a
element.
s ฺrs) ฺ
• Comments and processing instructions do noteappear m ide
i @
c t Gu
inside tags. v
ro den
s t o
aj s S
• The < and & special characters cannot
m tu in the
appear
ฺ thi value.
tina or attribute
character data of an element
e n use
l
va e to
c (
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tdocument s be well formed to guarantee that it is correctly structured and
An XML
e n r a nmust
al otonthe-t rules defined in the slide. The slide contains a list of the most common rules
adheres
V n
for well-formed documents, but it is not a complete list.
Note: The less-than (<) and ampersand (&) characters are special in XML and cannot appear
as themselves within the character data part of an element, or the value of an attribute. In
character data and attribute values, an XML Parser recognizes:
• The less-than (<) sign as a character that introduces the start tag of another element
• The ampersand (&) as an escape character before an entity name terminated by a
semicolon (;)
Therefore, to include special characters in the character data of an element or attribute value,
you must use built-in XML entity names for the special characters. For example, use &lt; to
include the less-than character and &amp; for the ampersand character. The XML Parser
replaces the entity reference with its textual or binary representation, as discussed earlier in
this lesson.
Note: The XML 1.1 specification requires the XML declaration to appear at the beginning of
the document. However, the declaration is optional in XML 1.0.

XML Fundamentals 2 - 42
Comparing XML and HTML

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

– Is a markup language for describing data


– Contains user-defined markup elements
– Is extensible
– Is displayed as a document tree in a web browser
– Conforms to rules for a well-formed document
• HTML s a
h a
ฺrs) ฺ
– Is a markup language for formatting data in a web browser
s
– Contains predefined markup tags
@ em uide
– Is not extensible
o v ic t G
– Does not conform to well-formed jdocument s tor tudrules en
ฺ m a sS
t i n a
e t hi
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
The keyn i n
tdifference n s
between XML and HTML is that:
l e t r a
V•a XMLn onis-a markup language for describing data
• HTML is a markup language for formatting data
The slide covers other important differences between XML and HTML.
Note About XHMTL
The World Wide Web Consortium (W3C) has defined Extensible Hypertext Markup Language
(XHTML) as a successor to HTML.
XHTML is designed to conform with XML standards and well-formed document rules, and
provides a way to reproduce, subset, and extend HTML documents. An XHTML document is
a particular XML document instance intended for processing by a web browser.
Note: Not all browsers support XHTML documents, and different browsers often process
XHTML documents in different ways.

XML Fundamentals 2 - 43
Comparing XML and HTML: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

HTML document

s a
) h a
XML document 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
texampleaninsthe slide displays HTML code with predefined tags. HTML uses markup
e n
The first
tr web pages. The purpose of a web browser, such as Internet Explorer or
alto describe
tags
V n -
Firefox,nisoto read HTML documents and display them as web pages. The browser does not
display the HTML tags, but uses the tags to interpret the content of the page. Examples of
some of the predefined tags that are used in the first slide examples are <HEAD>, <TITLE>,
<P>, and <B>.
The second slide example displays an XML document that contains user-defined markup tags
such as <DOC>, <TITLE>, <SAYING>, and <FIGURE>.

XML Fundamentals 2 - 44
XML Development

XML documents can be developed by using:


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

• A simple text editor such as Notepad (Windows) or gedit


(Linux)
• A specialized XML editor such as XMLSpy
• Oracle JDeveloper XML-related features that include:
– XML Code Editor with code-insight for XML schema–driven
editing a
h a s
– Registration of external XML schemas
s ฺ rs)
– Validation of XML documents against XML schemas
em eฺ
i c @ Guid
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t manyawayss to develop XML documents—from using a simple text editor, such as
n
There are
e r n
al otonrich
Notepad,
V -t XML development tools, such as XMLSpy or Oracle JDeveloper.
n
Oracle JDeveloper includes:
• An XML Editor, which is a specialized XML schema–driven editor for editing XML
documents subject to XML language standards
• A code insight feature that enables JDeveloper to provide you with a choice of elements
or attributes that are relevant to the context of the editing location within the document
• The capability to register external XML schemas for use by the schema-driven editor
• The use of XML schemas to validate XML documents

XML Fundamentals 2 - 45
Creating an XML Document in JDeveloper
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

1 Double-click.

s a
h a
s ฺrs) ฺ
@ em uide
o v ic 3 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
To create i n
t a newaXML
n s document in a project, perform the following steps:
l e r
-t the project name in the Applications Navigator, and then select New.
V1.a Right-click
o n
n New Gallery window is displayed.
The
2. Select the XML node from the General category, and then double-click XML Document
in the Items section. The Create XML File window is displayed.
3. In the Create XML File window:
a. Enter the File Name.
b. Enter, or browse to, a Directory name to store the file.
c. Click OK.

XML Fundamentals 2 - 46
Editing XML Documents in Oracle JDeveloper
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
1 ) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t showsathesfollowing actions when you right-click an XML document in JDeveloper’s
n
The slide
e r n
al on-t
Navigator:
V
1. Then Open option causes the elements and data to be displayed hierarchically in the
Structure window. In the Structure window, perform the following steps:
- Click an element or attribute to navigate to it in the XML Editor.
- Double-click an element, attribute, or error to edit it in the XML Editor.
2. The Open option also causes the file’s contents to be displayed in the XML
(source/code) Editor window.
JDeveloper checks the XML syntax as you type in XML files. Errors appear with red squiggly
lines in the Code Editor and also in the Structure pane. You see a red square box near the
scroll bar if there are errors in the document. Alternatively, if there are no errors in the
document, you see a green square box. Note that when you place your cursor over the big
red square box, a pop-up with the message “Hold Ctrl to view” is displayed. Press and hold
the Ctrl key with the cursor over the big red box. You see a suggestion that can help you fix
the error in code.
Select File > Save to save your changes to the document.
Note: Select View > Structure to display the Structure window if it is not already open.

XML Fundamentals 2 - 47
Setting Preferences in Oracle JDeveloper

1
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
You can n i n s in JDeveloper to assist in writing XML documents.
tset preferences
n
l e r a
-t > Preferences. The Preferences window opens.
V1.a Select
n o n Tools
2. Select XML and JSP/HTML under the Code Editor node:
- Select the Required Attribute Insertion check box for the editor to automatically
insert the required attributes when entering a space after the start tag name.
- Select the End Tag Completion check box to enable the end tag to be
automatically inserted after completing the start tag.

XML Fundamentals 2 - 48
Quiz

The following statements are all valid:


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

<?xml version="1.0"?>
<Question>Is this legal?</Question> 1
<Answer>No</Answer>

<!-- An XML document -->


<?xml version="1.0"?> 2

<Question 4You="Is this attribute name correct"/> 3 a


h a s
<EMAIL [email protected]></EMAIL>
s ฺrs) ฺ4
@ em uide
<Question>Is this legal</question>
o v ic t G 5
r n
a jsto Stude
a. True
n a ฺm this
i
nt use
b. False l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
Answer:nb
l e r a n
V1.a Both o n -t
<Question> and <Answer> are top-level elements. You can have only one
n
document element per document.
2. In general, you cannot have a comment before the XML declaration, and the root
element is required.
Note: Internet Explorer allows a comment before the XML declaration.
3. The attribute name cannot start with a digit.
4. The attribute value is not enclosed within quotation marks.
5. Opening and closing tag names are case-sensitive and must match.

XML Fundamentals 2 - 49
Summary

In this lesson, you should have learned how to:


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

• Define XML and the standards covered in this course


• Use JDeveloper to create, open, and edit XML and
XML-related documents
• View an XML document in JDeveloper, a web browser,
and a text editor
• Describe XML s a
h a
• Describe the structure of an XML document
s ฺrs) ฺ
• List the components of an XML document: elements,
@ em uide
attributes, and entities o v ic t G
t o r den
• Create a well-formed XML document s
aj s Stu
a ฺ m hi
• Use the XML Editor in JDeveloper
n t i n e t
v a le to us
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n t inaa universal
XML defines n s fe standard for electronic data exchange. It provides a rigorous set of
rules
V n -trathe structure inherent in the data to be easily encoded and unambiguously
alethatoenable
n
interpreted using human-readable text documents. Unlike HTML, the tags are extensible. In
XML, you create your own tags as required, whereas HTML has a set of predefined formatting
tags that you use; you cannot create your own tags.
When creating an XML document that is well-formed, you:
• Provide a single root element
• Ensure that start and end tag names are matching
• Ensure that attributes appear in the start tag of an element
• Ensure that attribute names are unique in the start tag
• Use predefined entity names to replace their references with their textual definition when
used in the data content of XML documents
JDeveloper provides tools and features that enable rapid development and syntax checking of
XML documents.

XML Fundamentals 2 - 50
Practice 2-2: Overview

This practice covers the following topics:


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

• Writing an XML document by using the features of XML


Editor in Oracle JDeveloper
• Checking whether an XML document is well formed
• Testing an XML document with errors that violate the rules
of a well-formed document
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na stest your understanding of writing an XML document by:
l e n
In this practice,
ryou
a n
V•a Openingo n -tan existing XML document and cloning some of the elements provided in the
n
document as a template
• Checking the syntax of the document to ensure that it is well formed
After creating various errors in an XML document, you check and explain the results. There
are optional questions about XML documents that you can answer.

XML Fundamentals 2 - 51
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ฺ

Validating XML Documents


with Document Type Definitions

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Describe a document type definition (DTD)


• Compare an internal DTD with an external DTD
• Read and write a DTD
• Validate XML documents by using an internal DTD, an
external DTD, or their combination
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t focuses
This lesson n son the use of DTDs to control the validity of XML documents.
l e r a
Va non-t

XML Fundamentals 3 - 2
Lesson Agenda

• Document type definition


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

– DTD rules
– Content of a DTD
• Declarations for:
– Elements
– Attributes
– Entities s a
h a
• Validating XML against a DTD
s ฺrs) ฺ
• XML catalogs
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 3 - 3
What Is a Document Type Definition?

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

• Is the grammar for an XML document


• Contains the definitions of
– Elements
– Attributes
– Entities
– Notations a
h a s
• Contains specific instructions that the XML Parser )
interprets to check document validity r s
sฺ eฺ
e m
• May be stored in a separate file (external)ic@ Guid
• May be included in the document (internal) t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s of elements contained in an XML document that collectively specifies
A DTDn provides an
a list
l e
theastructure r
-oft the document. A DTD defines the set of markup items used in an XML
V n o n
document:
• Elements
• Attributes and their permissible values
• Entities (user-defined)
• Notations
Note: Notations are not frequently used. For more information, see Appendix B (“Additional
Information About Document Type Definitions, XML Schemas, and XSLT 2.0”).
The XML engine interprets the markup defined in DTD to check the document for validity. For
example, a DTD may specify that a department must have exactly one department
identification number and one name but may have multiple employee elements. An XML
document indicates to an XML Parser whether it is associated with a DTD and where to locate
the DTD.
A DTD may be found internally (inline) in the document, or externally identified by a uniform
resource locator (URL), which can be a file on disk. If the XML Parser does not encounter
errors, the XML document is guaranteed to be consistent with the definition.

XML Fundamentals 3 - 4
Reasons to Validate an XML Document

• Well-formed documents satisfy XML syntax rules, and not


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

the business requirements about the content and structure.


• Business rules often require validation of the content and
structure of a document.
• XML documents must satisfy the structural requirements
imposed by the business model.
• A valid XML document can be reliably processed by XML a
h a s
applications.
s ฺ rs)
• m idaneฺ
Validations can be done by using a DTD or byeusing
XML schema. @ Gu
vic ro dent
t
js Stu o
ฺm thisa
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t XML sdocument is one that meets all the rules of the XML specification. Your
e n
A well-formed
r a n
al omay
business
V n -trequire the validation of the actual structure and content of a document. What if
n
following only the syntax rules is not quite good enough? Your document must not only follow
the XML rules, but also satisfy the structural requirements imposed by your business model.
Here is an example:
Each <employee> element must consist of <employee_id>, <first_name>,
<last_name>, <email>, <phone_number>, and <department_id> elements. If an
<employee> element misses any of these elements, it is considered to be not valid.
You may also need to verify whether these elements have a valid value.
You can perform all these validations by using a DTD. The DTD was the first type of
mechanism available for validating XML document structure and content. However, it lacked
the capability of performing data type validations on the content. The XML Schema
recommendation supports validation for different data types, and is rapidly replacing DTDs as
a way to validate the XML document structure and content.
Note: Theoretically, anything that has not been explicitly permitted in a DTD is forbidden.
However, some parsers do not always enforce the rules defined by a DTD.
Using an XML schema for validation is covered in the lesson titled “Validating XML
Documents with XML Schemas.”

XML Fundamentals 3 - 5
General DTD Rules

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

• Must provide a declaration for items used in an XML


document such as:
– Elements
– Attributes
– Entities
• Is case-sensitive, but spacing and indentation are not s a
significant h a
s ฺrs) ฺ
• May use XML comment syntax for documentation, em ubut
comments cannot appear inside declarations c @ ide
r o vi nt G
• Forbids using anything that is not jexplicitly e
sto tudpermitted
ฺ m a sS
t i n a
e t hi
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t syntax
The general n sof a DTD is different from that of an XML document. To validate an XML
e
al oby
document r a
t a DTD, the rules in the slide must be followed.
-using
V n
n declarations form the definition of the elements, attributes, and entities that are used
The DTD
in an XML document. The definitions describe rules of how the XML document can be
constructed to be considered valid.
The element, attribute, and entity names are all case-sensitive and must match their use in
the XML document. Comments in the XML syntax are allowed but cannot appear inside any
declarations.
Note: Everything that is not explicitly permitted is forbidden. This means that when an XML
document is validated using a DTD, anything that is not declared or permitted by the DTD is
considered to be forbidden; that is, the document is considered to be invalid.

XML Fundamentals 3 - 6
Syntax for DTD Declarations

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

<!ELEMENT element_name content_model>

• Attributes:
<!ATTLIST element_name attribute_name attribute_type
default_value>

• Entities: s a
) h a
<!ENTITY entity_name "replacement text"> r s
sฺ eฺ
m
e uid
c @
r o vi nt G
• Notations:
a jsto Stude
<!NOTATION notation_name SYSTEM "text">
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
A DTDn consists ofndeclarations
a for the markup and data used in an XML document. The slide
l e t r
- syntax for declaring markup components:
a theongeneric
shows
V n
• Elements, which declare tag names and their content model. The content model for an
element includes other nested markup (elements) and/or character data.
• Attributes, which declare the attribute names for the containing element, the type of
attribute (not a data type), and optional default values
• Entities, which provide a mechanism for replacement text
• Notations, which are used in DTD attribute types to provide a way for identifying how
parsed or unparsed data is encoded, typically used by applications that can process the
data
Note: Notations are not frequently used in DTDs.
The next few pages of this lesson show examples of how to use each of these declarations,
and finally how to use DTD to validate the structure and content of an XML document.

XML Fundamentals 3 - 7
Example of a Simple DTD Declaration and
an XML Document That Conforms to the DTD
• Example of a simple DTD with element declarations:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!ELEMENT employees (employee)>


<!ELEMENT employee (name)>
<!ELEMENT name (#PCDATA)>

• A valid XML document based on the preceding DTD:

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


s a
<employees>
) h a
<employee> r s
sฺ eฺ
<name>Steven King</name> m
e uid
</employee>
c @
</employees>
r o vi nt G
a jsto Stude
n a ฺm this
i
ntbe defined.
s e
Note: All child elements must l e u
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t syntaxaforsdeclaring an element provides a way to specify cardinality (a number of
The DTD
e n r n
al on-for
occurrences)
V t usage of elements in the content model. Specifying cardinality is covered
n
after discussing how to declare elements later in this lesson. In the slide example, there is no
symbol after the child element. This indicates that one and only one child element is
mandatory.
The DTD in the slide declares three elements: employees, employee, and name. These
elements form a simple hierarchy, in which the <employees> element must contain one
<employee> element, which must contain one <name> element.
Notes
• The #PCDATA example represents text-only content without child elements. PCDATA
means “parsed character data.”
• The XML document does not reference the DTD in the slide example.

XML Fundamentals 3 - 8
Example of an XML Document
That Does Not Conform to the DTD
Using the same DTD, the following XML document is invalid
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

because there are two employee child elements:


<!-- The DTD document. -->
<!ELEMENT employees (employee)>
<!ELEMENT employee (name)>
<!ELEMENT name (#PCDATA)>

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


s a
<!-- This XML document does not conform to the DTD.-->
<employees> ) h a
<employee> r s
sฺ eฺ
<name>Steven King</name> m
e uid
c @
</employee>
r o vi nt G
jsto Stude
<employee>
<name>Ellen Abel</name>
ฺm thisa
</employee>
i n a
</employees>
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t DTD from sthe previous slide, the XML document in the slide is invalid because it
e n
Using the
r a n
al otwo
contains
V n t
-<employee> child elements.
n

XML Fundamentals 3 - 9
Reference the DTD in the XML Document

• After the XML declaration and before the root by using:


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

<!DOCTYPE employees [ ... ]>

• Externally with the SYSTEM or PUBLIC keywords:


<!DOCTYPE employees SYSTEM "employees.dtd">

<!DOCTYPE employees PUBLIC "-//formal-public-ID">


s a
h a
• Internally in the <!DOCTYPE root [...]> entry: s ฺrs) ฺ
<?xml version="1.0" encoding="UTF-8"?>
@ em uide
<!DOCTYPE employees [
o v ic t G
<!ELEMENT employees (#PCDATA)> t o r den
]> s
aj s Stu
a ฺ m hi
<employees>Employee Data</employees>
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
The XML i n
t document n sreferences the DTD to be validated using a specification syntax of:
e r a
al on-root-element-name
t
V
<!DOCTYPE
n
[declarations]>
The root element name must be used after the <!DOCTYPE keyword in the declaration. To
use an external DTD, replace [declarations] with:
• The SYSTEM keyword and a quoted string containing a URL or a relative path to a file
with a .dtd extension, such as employees.dtd in the slide, or a URL such as
http://www.myserver.com/dtd/employees.dtd
• The PUBLIC keyword and a quoted string containing a Formal Public Identifier (FPI).
For example, to reference the public XHTML identifier, use:
<!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
The last example in the slide uses an internal DTD with declarations placed between the
<!DOCTYPE root-element [ characters and the closing ]> characters.
Note: The <!DOCTYPE name [...]> specification appears after the XML declaration and
before the root element of the XML document. Public identifiers are seldom used. An XML
document can reference both an external DTD and an internal DTD. The internal DTD takes
precedence if an element is defined both externally and internally.

XML Fundamentals 3 - 10
Lesson Agenda

• Document type definition


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

– DTD rules
– Contents of a DTD
• Declarations for:
– Elements
– Attributes
– Entities s a
h a
• Validating XML against a DTD
s ฺrs) ฺ
• XML catalogs
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 3 - 11
Empty Element Declaration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!-- Empty element-->


1
<!ELEMENT job EMPTY>

<!-- Examples of empty elements-->

<job/>
<job><job/>
s a
<job id="ST_CLERK"/>
) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s building blocks of DTDs. An element declaration contains the
t are theabasic
n
Elements
e r n
al okeyword,
ELEMENT
V n -t followed by the element name and a content model. The content model
definesnwhat can be contained in the element. The four types of content models are:
1. Empty: This cannot contain subelements or text. However, an empty element may hold
attributes. Examples of an empty element include the following:
<job/>
<job></job>
<job id="ST_CLERK"/>

XML Fundamentals 3 - 12
Child Elements Declaration

<!-- Single element -->


<!ELEMENT employees (employee)>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!-- Ordered list (sequence) element -->


<!ELEMENT employee (employee_id,last_name,job_id)>

<!-- Single elements --> 2


<!ELEMENT employee_id (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>

<!-- Choice element -->


<!ELEMENT job_id (manager | worker)>
s a
) h a
<employees> r s
sฺ eฺ
<employee>
m
e uid
<employee_id>100</employee_id>
c @
<last_name>King</last_name>
r o vi nt G
jsto Stude
<job_id>
<manager>AD_PRES</manager>
</job_id> ฺm thisa
i n a
nt use
</employee>
</employees> l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a s building blocks of DTDs. An element declaration contains the
t are theabasic
n
Elements
e r n
al okeyword,
ELEMENT
V n -t followed by the element name and a content model. The content model
definesnwhat can be contained in the element. The four types of content models are:
2. Child elements: These are subelements enclosed within angle brackets, which can be:
- A single element name
- A sequence of element names containing a comma-separated list of names
whose order of appearance is significant, as in the following example:
The <employee> element must contain an <employee_id> element that
appears before the <last_name> element, which must appear before the
<job_id> element.
- A choice of element names, which is a list of names separated by a vertical bar
(|), each representing an alternative choice. For example, the <job_id> element
may contain either a <manager> element or a <worker> element, but not both.
- A combination of single element and a list, as in this example:
<!ELEMENT person (full_name | (first_name, last_name))>

XML Fundamentals 3 - 13
Mixed Elements Declarations
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!-- Mixed elements declarations. -->

<!ELEMENT last_name (#PCDATA)> 3


<!ELEMENT hire_date (date| (day,month,year))>
. . .

<!-- Examples of mixed elements. -->


. . . s a
<last_name>King</last_name>
) h a
<hire_date><date>January 15, 2009<date></hire_date> r s
sฺ eฺ
<hire_date> m
e uid
c @
<day>15</day>
r o vi nt G
jsto Stude
<month>January</month>
<year>2009</year>
ฺm thisa
</hire_date>
i n a
. . .
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s form of mixed content is a text-only element, known as parsed
t Theansimplest
l e n
3. Mixed:
r
Va character
o n -t data, using the #PCDATA keyword enclosed within parentheses. When the
n is specified as #PCDATA, the element can contain a text string but not
content
subelements. For example, using the last_name element declaration, the following
XML is valid:
<last_name>King</last_name>
Another example of mixed content is shown in the hire_date element declaration,
which can contain either parsed character data or a combination of date, month, and
year elements. The following XML elements will be valid for hire_date:
<hire_date><date>January 15, 2009<date></hire_date>
or
<hire_date>
<day>15</day>
<month>January</month>
<year>2009</year>
</hire_date>

XML Fundamentals 3 - 14
Any Element Declarations
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!-- Any -->


4
<!ELEMENT employee_id ANY>

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s model means that anything can be contained within the element; the
t This acontent
e n
4. Any:
r n
l modelnis-tvery loose. The ANY content model allows any element declared in the DTD to
Va be o in any order, using any number of occurrences. That is, the DTD does not
nused,
validate the element content.
Note: You should avoid using ANY because it entirely defeats the purpose of using a
DTD for validating the structure and content of an XML document.

XML Fundamentals 3 - 15
Specifying the Cardinality of Elements

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

• Indicate the number of children permitted


• Appear as suffixes
• Are placed after an element or group (using parentheses)
in the content model or after the content model

Symbol Description a
No symbol (default) Mandatory (one and only one) h a s
r s )
? (question mark) Optional (zero or one)
m sฺ eฺ
@ e uid
* (asterisk) Zero or more (optional) c
vi nt G
r o
+ (plus sign) One or more (mandatory)
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t model s
l e n
The content
t r a nindicates how to group subelements by using parentheses, and how many
a on-of each element are permitted. Cardinality (the number of permitted occurrences
occurrences
V
of eachnelement) is expressed using the following symbols:
• Question mark (?): Indicates an optional or single-valued subelement (zero to one)
• Asterisk (*): Indicates an optional or multivalued subelement (zero to many)
• Plus sign (+): Indicates a mandatory or many-valued subelement (one to many)
The symbols can appear in the content model specification as a suffix with the individual
elements, a group, and the entire content model. By default, the absence of a symbol means
that the element, group, or content model must appear at least once; that is, it is mandatory.

XML Fundamentals 3 - 16
Specifying the Cardinality of Elements: Examples

<!-- An <employees> element can contain zero or many <employee> -->


<!-- elements: -->
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!ELEMENT employees (employee*)>

<!-- A <department> element must contain at least one or many -->


<!-- <employee> elements: -->

<!ELEMENT department (employee+)>

s a
<!–- An <employee> element must have one <last_name> and <salary> -->
h a
ฺrs) ฺ
<!-- elements, and an optional <commission_pct> element: 
s
<!ELEMENT employee (last_name,salary,commission_pct?)>
@ em uide
o v ic t G
t o d n many -->
rand optionally
e
<!–- The <jobs> element must have one president, s
aj s Stu
<!-- managers or clerks: -->
a ฺ m h i
t i n e t
<!ELEMENT jobs (president , (manager
a l en o| uclerk)*)> s
v
c ( nse t
v i
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
The slide
n t na nsshow
iexamples fe how to specify the cardinality of elements using the cardinality
ale on-tra
symbols:
V 1. Ann <employees> element can contain zero or many <employee> elements:
<!ELEMENT employees (employee*)>
2. A <department> element must contain at least one or many <employee> elements:
<!ELEMENT department (employee+)>
3. An <employee> element must have one <last_name> and <salary> element, and
an optional <commission_pct> element:
<!ELEMENT employee (last_name,salary,commission_pct?)>
4. The <jobs> element must have one president, and (optionally) many managers or
clerks:
<!ELEMENT jobs (president , (manager | clerk)*)>

XML Fundamentals 3 - 17
Attribute Declarations: Syntax and Requirements

<!ATTLIST element-name attribute-name attribute_type default>


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

Attribute declarations require:


• An element name
• An attribute name
• An attribute type, specified as:
CDATA, Enumerated, ENTITY, ENTITIES, ID, IDREF, s a
IDREFS, NMTOKEN, NMTOKENS, or NOTATION h a
s ฺ r s)
• An attribute default, specified as: m ideฺ
e
@ Gu
#IMPLIED, #REQUIRED, #FIXED, or a literal
vic value
t o ro dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t specifyanadditional
l e n
Attributes
t r information about an element, and they form a name-value pair
a ona -particular property of the element. Declaring an attribute requires:
describing
V
• n
An element name identifying the element to which the attribute belongs
• An attribute name specifying the name of the attribute
• An attribute type set to one of the 10 possible types of attributes listed in the slide.
These are discussed on the next page.
• An attribute default that specifies a mandatory, or optional, value using:
- #IMPLIED indicating that the attribute is optional
- #REQUIRED indicating that the attribute must be specified (mandatory)
- #FIXED indicating that the attribute is a constant, that is, a supplied single value
- A literal value enclosed within quotation marks that is used as a default value
when no value is specified in the XML document

XML Fundamentals 3 - 18
Attribute Declarations: Syntax and Requirements

<!-- Syntax. -->


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

<!ATTLIST element-name attribute-name attribute_type default>

<!-- Example. -->


<!ELEMENT employee (employee_id, last_name)>

<!ATTLIST employee manager_id CDATA #IMPLIED>

s a
h a
<employee manager_id="100"> s ฺrs) ฺ
<employee_id>101</employee_id>
@ em uide
<last_name>Kochhar</last_name>
o v ic t G
</employee>
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
Example
n i n
tof XMLabynsusing the manager_id attribute declaration:
e tr
al on-manager_id="100">
V
<employee
n
<employee_id>101</employee_id>
<last_name>Kochhar</last_name>
</employee>

XML Fundamentals 3 - 19
CDATA and Enumerated Attribute Types

• CDATA: For character data values


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

<!ELEMENT employee (employee_id, last_name)>


<!ATTLIST employee manager_id CDATA #IMPLIED>

<employee manager_id="102">
<employee_id>104</employee_id>
<last_name>Ernst</last_name>
</employee>

• Enumerated: For a choice from a list of values s a


h a
<!ELEMENT employee (employee_id, last_name)> s ฺrs) ฺ
<!ATTLIST employee gender (male|female) #IMPLIED> em
c @ u ide
r o vi nt G
<employee gender="male">
a j sto Stude
<employee_id>104</employee_id> m
<last_name>Ernst</last_name> ina
ฺ this
</employee>
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
CDATAn
e Attribute
r a n
Type
V alCDATA
The o n -t indicates that the attribute value can be set to any character data. Most
type
n
attributes use the CDATA type. The example in the slide declares manager_id as an attribute
of the employee element. The manager_id attribute may have any character data as its
value. The sample XML code in the slide, under the manager_id attribute declaration, shows
an example of using the attribute.
Enumerated Attribute Type
The Enumerated type provides a choice from a list of allowable values for the attribute. The
choices of values in the list:
• Are enclosed within parentheses
• Are separated by the vertical bar (|) character (spaces are optional)
• Can be case-sensitive strings (without quotation marks) or numeric values
For example, the employee element is given a gender attribute with male or female as its
possible values. In the slide, the XML code under the gender attribute declaration shows an
example of using the attribute.

XML Fundamentals 3 - 20
NOTATION Declaration and Attribute Type

The NOTATION attribute type represents a name of a


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

NOTATION that is declared in the DTD.


<!-- You can declare a NOTATION using the following syntax. -->
<!NOTATION notation_name SYSTEM "text">

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


<!DOCTYPE photos [
<!ELEMENT photos (image+)>
<!ELEMENT image EMPTY>
s a
<!NOTATION gif SYSTEM "image/gif">
) h a
<!NOTATION jpeg SYSTEM "image/jpeg">
<!ATTLIST image r s
sฺ eฺ
source CDATA #REQUIRED m
e uid
c @
type NOTATION (gif | jpeg) #REQUIRED>
r o vi nt G
jsto Stude
]>
<photos>
<image source="myphoto.gif" type="gif"/> ฺm thisa
i n a
nt use
<image source="mypet.jpg" type="jpeg"/>
</photos> l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t for the s
The syntax
l e n t r a n
NOTATION declaration is shown in the first code example in the slide. This
a aoname
defines
V n n- for a notation that can be used as:
• An attribute value for a NOTATION attribute type
• A name after an NDATA keyword in an unparsed external ENTITY declaration (covered
on the following pages)
The second code example in the slide shows an XML document using an internal DTD that
declares:
• The <photos> element contains one or more <image> elements
• Each <image> element can have two attributes:
- A source containing a character data such as a file name
- A type, declared as a NOTATION attribute type, whose value must be a choice of
the notation names such as gif and jpeg, as listed in the content model
• A notation name for gif and jpeg in their NOTATION declarations
Note: The DTD example also illustrates how to declare more than one attribute for an
element (for example, the source and type attributes for the image element). Also,
NOTATION declarations are not commonly used.

XML Fundamentals 3 - 21
Specifying Default Attribute Values

• A quoted default attribute value:


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

– Can be specified in the DTD after the attribute type


<!ELEMENT employee (employee_id, last_name)>
<!ATTLIST employee department_id (10|60|90) '90'>

– Is not specified when using the #IMPLIED or #REQUIRED


keywords
– Is required in the DTD when you use the #FIXED keyword a
h a s
<!ELEMENT employee (employee_id, last_name)>
r s )
<!ATTLIST employee manager_id CDATA #IMPLIED>
m sฺ eฺ
<!ATTLIST employee min_salary CDATA #FIXED '4000'> e
i c @ Guid
t o r ov ent
• An attribute value is mandatory injs
a the XML
S t uddocument
when you use the #REQUIRED
n a ฺmkeyword. t his
t i e
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t declaration
The attribute n s in a DTD enables you to specify default attribute values after the
e
al otype
attribute t r a
-enclosed within quotation marks.
V n n
The first DTD example in the slide specifies a default value of 90 for the department_id
attribute. If the XML document does not specify a department_id attribute for an employee
element, the default is assumed.
The second DTD example demonstrates:
• The manager_id attribute declared as an #IMPLIED value. The attribute declaration in
the DTD does not require a default value, and the XML document may or may not
supply a value. A default is not applied if the attribute is excluded from the employee
element.
• The min_salary attribute as a #FIXED attribute, in which case a quoted default value
is mandatory in the declaration. When the min_salary attribute is used, it must have
the same value as declared in the DTD. A #FIXED attribute value is constant and
cannot be changed in the XML document.
If an attribute declaration uses the #REQUIRED keyword, it forces the XML document to use
the attribute and specify a value in its element.

XML Fundamentals 3 - 22
Entities in XML

• Entity types predefined by XML standards:


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

– Built-in entities
– Character entities
• Entity types that can be declared in the DTD:
– General entities
– Parameter entities
• Entity references for: s a
a
– Built-in, character, and general entities can be made rins)anh
XML document or the DTD by using: m sฺ eฺ
@ e uid
&entity-name; c
vi nt G
r
to DTD o e using:
– Parameter entities can be made in
a jsthe t u dby
a ฺ m his S
tin se t
%entity-name;
e n
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n
Accordingt intoatheaWorld
n sfeWide Web Consortium (W3C) standard, predefined entities in XML
ale on-tr
include:
V n entities representing these special characters:
• Built-in
- The less-than sign (<) represented by &lt;
- The greater-than sign (>) represented by &gt;
- The ampersand (&) represented by &amp;
- The single quotation mark or apostrophe (') represented by &apos;
- The double quotation mark (") represented by &quot;
• Character entities representing any Unicode character specified in the XML
recommendation, which is based on Unicode 3.0. Character entities are formed using
an ampersand (&) followed by a hash (#), then a Unicode character number, and finally
a terminating semicolon (;). For example, &#169 denotes the copyright symbol (©). The
character number can be a decimal or a hexadecimal Unicode value. Hexadecimal
values are prefixed by an x. For example, &#xA9 represents the copyright symbol (©).
An entity reference to a parameter entity is done in a DTD. However, references to built-in,
character, and general entities can be done in the XML document and a DTD, as in this
example:
<Company>&quot;Oracle Corporation&quot;</Company> instead of
<Company>"Oracle Corporation"</Company>
XML Fundamentals 3 - 23
General Entity Declarations

• Internal entity declaration (parsed):


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

<!ENTITY entity-name "replacement text">

<!ENTITY company "Oracle Corporation">


<!ENTITY Emperor "Alexander &quot;The Great&quot;">
<!ENTITY president "<employee>
<last_name>King</last_name>
</employee>">

• External entity declarations s a


h a
– Parsed (text stored in external location): s ฺrs) ฺ
<!ENTITY entity-name SYSTEM "file.ext | URL"> @ em uide
o v ic t G
– Unparsed (requires a notation declaration): t o r den
s
aj notation> S tu
<!ENTITY entity SYSTEM "file.ext" ฺNDATA
a m his
<!NOTATION notation SYSTEM "text"> t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
Generaln i n
tentities:ans
l e r
be-tdeclared
V•a Must
• no n in a DTD before you use it in the XML document
May be used in the DTD as well
• Are used to create reusable sections of replacement text that can represent entire
paragraphs, strings, or even entire documents
• Can be declared:
- Internally (direct), with replacement text held within the DTD declaration
- Externally, with replacement text stored outside in the DTD in a file or a web
address (via URL). External entities are used to include large quantities of data
and are declared with the SYSTEM keyword before a file name or a URL.
To reference a general entity, prefix the name with an ampersand (&) and terminate it with a
semicolon (;). An XML Parser replaces an entity reference with its replacement text. For
example, consider the following:
<organization>
&company; is a software company
</organization>
This expands to:
<organization>
Oracle corporation is a software company
</organization>

XML Fundamentals 3 - 24
Well-Formed Parsed Entities
The replacement text of parsed internal or external entities must consist of well-formed XML
content. These entities can be used as replacement text inside XML elements or attributes,
and other DTD declarations. For example, in the slide, the entity declarations for company,
Emperor, and president are all (well-formed) parsed internal entities.
However, the following examples are not well-formed and therefore, invalid:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!ENTITY starttag "<emp>"> <!-- must contain end tag -->


<!ENTITY endtag "</emp>"> <!-- must contain start tag -->
<!ENTITY ref "change to &ref;"> <!-- recursive not allowed -->
Note: The last example in the notes is recursive. You cannot have a recursive entity.
Parsed External Entities
Examples of parsed external entities can be another file containing, or a URL referencing,
data that conforms to well-formed XML data.
s a
Example:
h a
<?xml version="1.0"?>
s ฺrs) ฺ
<!DOCTYPE employees [
@ em uide
<!ENTITY employeeData SYSTEM "employee.xml">
o v ic t G
t o r den
]> s
aj s Stu
<employees>
a ฺ m hi
t i n e t
&employeeData;
a l en o us
</employees>
/ p y
i c (v se t
The employee.xml document
t o rov can l i c n
econtain:
<employee>
a js ble
a M fera
<last_name>King</last_name>
n t i n n s
e
al onExternal
</employee>
-t r a
V
Unparsed
n Entities
There is often a requirement to include data that is not well-formed in an XML document such
as:
• HTML text that has tags that are not paired, do not overlap, and must not be interpreted
as XML elements
• Image data to be processed by the XML application
• Text data containing special XML characters that must not be interpreted
Using an unparsed external entity is one way to include data that will be processed and not
validated by an XML Parser. To convert a parsed external entity declaration into an unparsed
external entity declaration, add:
• The NDATA keyword after the file name or URL
• A notation name after the NDATA keyword
The notation name must be declared in a <!NOTATION ..> declaration, as shown in the
example in the slide.
Note: External entities can make the DTD more manageable by reducing the size of the entity
declarations if the replacement text is very extensive (because there are no size limits).

XML Fundamentals 3 - 25
Parameter Entities

• Are declared as:


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

<!ENTITY % entity-name "replacement text">

– Requires a space after the percent (%) symbol


– Must be declared before being referenced in the DTD
– Must be defined in an external DTD
• Can have replacement text in an external location by using:
<!ENTITY % entity-name SYSTEM "file.dtd | URL ">
s a
) h a
<!ENTITY % employee_elements "last_name, salary"> r s
sฺ eฺ
<!ENTITY % employee_elements SYSTEM "empelm.txt"> m
e uid
c @
• Are referenced in the DTD by using: rovi n t G
%entity-name;
a jsto Stude
n a ฺm this
<!ELEMENT employee (%employee_elements;)> i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t a parameter s entity requires a percent (%) sign and a space after the ENTITY
Declaring
e n r a n
al oand
keyword
V n t
-before the entity name. Referencing a parameter entity requires that you use the
percentn(%) sign followed by the entity name (no space required) and terminated with a
semicolon (;). A parameter entity must be declared:
• In an external DTD, and therefore, cannot be declared in an internal DTD
• Before they are referenced in the DTD
Using Parameter Entities
If the content model of multiple elements is identical, use a parameter entity to represent the
structure of the content model, thereby avoiding repetitive typing. Changing the content model
can be done in one place. For example, consider the following elements:
<!ELEMENT employee (id, firstname, lastname)>
<!ELEMENT manager (id, firstname, lastname)>
Instead of declaring them individually, you can declare a parameter entity and reference it to
achieve the same result as shown in the following example:
<!ENTITY % personcontent "(id, firstname, lastname)">
<!ELEMENT employee %personcontent;>
<!ELEMENT manager %personcontent;>

XML Fundamentals 3 - 26
Complete DTD: Example

The employees DTD document, code_03_27_as.dtd


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

<!ELEMENT employees (employee+)>


<!ELEMENT employee (employee_id, last_name)>
<!ATTLIST employee manager_id CDATA #IMPLIED
department_id (10|60|90) '90'>
<!ELEMENT employee_id (#PCDATA )>
<!ELEMENT last_name (#PCDATA )>

The XML document referencing the DTD, code_03_27_as.dtd


s a
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE employees SYSTEM "code_03_27_as.dtd" [ ) h a
<!ENTITY title "Mr"> r s
sฺ eฺ
]> m
e uid
c @
vi nt G
<employees>
<employee manager_id="100" department_id="10"> r o
<employee_id>100</employee_id>
a jsto Stude
<last_name>&title; king</last_name>
n a ฺm this
</employee>
i
nt use
</employees>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t showsaancomplete
l e n
This slide
t r example of an XML document and an associated DTD that
a on-some of the features of the DTD declarations covered in this lesson. In particular,
summarizes
V n illustrates:
the example
• Combining an external DTD and an internal DTD, as highlighted in the slide, where the
ENTITY declaration is added to the internal DTD subset
• Declaring elements such as employees, employee, employee_id, and last_name
• Specifying a cardinality symbol (+) for employee elements
• Using the (employee_id, last_name) sequence and #PCDATA content models
• Declaring multiple attributes such as manager_id and department_id for the
employee element
• Using default attribute values for department_id
Note: JDeveloper automatically validates an XML document against a DTD if the SYSTEM ID
in the XML instance document points to a local DTD file. You see validation errors in the
Structure pane.

XML Fundamentals 3 - 27
Lesson Agenda

• Document type definition


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

– DTD rules
– Contents of a DTD
• Declarations for:
– Elements
– Attributes
– Entities s a
h a
• Validating XML against a DTD
s ฺrs) ฺ
• XML catalogs
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 3 - 28
Validating XML Against a DTD

The oraxml command-line utility or XML Parser:


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

• Requires xmlparserv2.jar in the CLASSPATH


• Uses the –dtd option for full validation with a DTD:

java oracle.xml.parser.v2.oraxml –dtd emp.xml

• Produces the following message when the XML document


is valid: a
as ) h
The input XML file is parsed without errors
r s
sฺ eฺ
m
e uid
using DTD validation mode.
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t utility issa command-line interface to the Oracle XML Parser that is supplied with
The oraxml
l e n r a n
theaOracle
V o n -t Developer’s Kit (XDK). The oraxml command-line utility can be used to
XML
n
validate an XML document against a DTD using the following syntax:
java oracle.xml.parser.v2.oraxml –dtd emp.xml
The –dtd option specifies that the supplied XML document (emp.xml) must be validated
against the DTD specified in the <!DOCTYPE> declaration contained in the XML file.
To run the oraxml command-line utility, ensure that xmlparserv2.jar is added to the
Java CLASSPATH, as in the following example:
cd labs/lab
export CLASSPATH=
/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2/xmlparserv2.jar
java oracle.xml.parser.v2.oraxml –dtd emp.xml
Note: Due to space restrictions on this page, the export command is listed on two lines. When
you enter the export command in a terminal window, make sure there are no spaces in
CLASSPATH=/home/…

XML Fundamentals 3 - 29
Using XML Catalogs

• Catalog files are text files that describe a mapping from


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

generic addresses or names to specific local directories on


a given machine.
• A catalog can be used to locate DTDs, XML schemas,
system entity files, and stylesheet files during processing.
• Catalog files add flexibility to your setup.
s a
) h a
r s
sฺ eฺ
` m
@ e uid
` c
vi nt G
r o
`
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s refer to external entities such as the public and/or system identifiers
XML documents
e n
l document r a n
typically
t type definition (DTD). These external relationships are expressed using
on- identifiers (URIs), typically as uniform resource locators (URLs).
forathe
V
uniformnresource
Relying on URLs to directly identify resources to be retrieved often causes problems for end
users:
• If they are absolute URLs, they work only when you can reach them. Relying on remote
resources makes XML processing susceptible to both planned and unplanned network
down time. The URL http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd is not
very useful if you are not connected to the network.
• If they are relative URLs, they are useful only in the context where they were initially
created. The URL ../../xml/dtd/docbookx.xml is not useful anywhere except on the
system where it was initially created.
One way to avoid these problems is to use an entity resolver or a URI resolver. A resolver can
examine the URIs of the resources being requested and determine how best to satisfy those
requests. An XML catalog is an XML document that provides a mapping from generic
addresses to specific local directories on a given machine. For example, a catalog can be
used to locate DTDs, XML schemas, system entity files, and stylesheet files during
processing. Catalog files add a degree of flexibility to your setup. After you have set up your
scripts to use catalog files, if you rearrange things or transfer to a different system, you can
just edit the catalog file to remap all the old paths to new locations.
Note: XML schemas and stylesheets are covered in later lessons in this course.
XML Fundamentals 3 - 30
Benefits of Using XML Catalogs

Use catalogs to make your XML setup more portable and more
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

flexible. With catalogs, you can:


• Use PUBLIC identifiers in your DOCTYPE declarations in
your files instead of using SYSTEM identifiers to find the
DTD, XML schemas, or system entity files
• Use stable Internet URL addresses for your DTD SYSTEM
identifier or stylesheet path name, but let the catalog a
resolve them to path names on the local machine h a s
s)
• Test new releases of the stylesheets or DTD bymsฺr ฺ
temporarily changing your catalog to pointicto
e uide
@themG
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s identifiers in your DOCTYPE declarations in your files, and do not
t stableaPUBLIC
• Use
l e n r n
Va worryo n -t using the SYSTEM identifier to find the DTD file or system entity files. The
about
n identifier can be mapped to a specific location on a given machine with a
PUBLIC
catalog. If you move your files to a new machine, you do not have to edit them all to
change the SYSTEM reference. You edit only the catalog’s mapping of the PUBLIC
identifier.
• Use stable Internet URL addresses for your DTD system identifier or stylesheet path
name, but let the catalog resolve them to path names on the local machine. By doing so,
you get efficient processing if they are available locally, and it falls back to network
access only if it has to.

XML Fundamentals 3 - 31
Referring to External DTDs
You can refer to an external DTD by using either system identifiers or public identifiers, which
are briefly mentioned earlier in this lesson.
System Identifiers
A system identifier enables you to specify the location of an external file containing DTD
declarations. It consists of two parts: The keyword SYSTEM and a URI reference pointing to
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

the document’s location. A URI can be a file on your local hard drive, a file on your network, or
a file available on the internet. The syntax has already been shown in this lesson.
Public Identifiers
Similar to system identifiers, the public identifier begins with the keyword PUBLIC followed by
a specialized identifier. However, instead of a reference to a file, public identifiers are used to
identify an entry in a catalog. Following the identifier string, you can include an optional
system identifier too. This enables the processor to find a copy of the document if it cannot
resolve the public identifier. When you include the optional system identifier, the SYSTEM a
keyword is not required. A valid document type declaration that uses a public identifier might
h a s
resemble the following: r s )
m sฺ eฺ
<? xml version = "1.0" ?>
@ e uid
<!DOCTYPE concept PUBLIC "-//OASIS//DTD DITA Concept//EN"
v i c G
"concept.dtd">
t
. . . r o
to tude n
The public identifier "-//OASIS//DTD DITA Concept//EN" a j s S to be resolved to a local
a ฺ m his needs
DTD by the catalog file. Even the system identifier
n t in se t " concept.dtd " can be resolved to another
file by the catalog, maybe to the same lfile
a e name o ubut in a specific path.
v
( catalog t
XML editors and other tools use
o v i c the
e n seproject
to find the DTDs and XML schemas. Some use
the “resolver” Java classofrom
t r the
l i c
Apache to resolve public and system IDs using
catalogs. The resolver s
aIf jthere le to resolve the public identifier in the DOCTYPE declaration by
first tries
b
using the catalog.
a M f e r a
is no match, it tries to resolve the system identifier. If there is no
match,n i n
it tuses then s
system identifier as the actual file location.
l e t r a
Va non-

XML Fundamentals 3 - 32
Quiz

A document type definition (DTD):


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

a. Is the grammar for an XML document


b. Contains the definitions of elements, attributes, entities,
and notations
c. Contains specific instructions that the XML Parser
interprets to check the document’s validity
d. May be stored in a separate file (external) a
h a s
e. May be included in the document (internal) )
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t b, c,ad,nse
Answer:na,
l e r
Va non-t

XML Fundamentals 3 - 33
Summary

In this lesson, you should have learned how to:


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

• Use internal and external DTDs


• Read and write DTD declarations for:
– Elements
– Attributes
– Entities
– Notations s a
) h a
• Validate XML documents with a DTD by using the oraxml r s
command-line utility m sฺ eฺ
@ e uid
c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s grammar for a valid structure of an XML document. This is similar in
A DTDn
e establishes
r a nthe
al toonhow
concept
V -t a database schema establishes the structure of a relational database. When
an XMLndocument follows the rules declared in the DTD, the XML document is said to be
“valid.” The DTD declares the following structural items:
• Elements and their content models such as parsed character data (#PCDATA) for
nonmarkup text, element names, sequences, or choices. The content model and
elements can be given cardinality to specify the number of occurrences permitted by
using the following suffixes:
- + for a mandatory (one or many) occurrence
- * for an optional (zero or many) occurrence
- ? for an optional (zero or one) occurrence
Note: The absence of a suffix means that there is only one occurrence.
• Attributes that represent the additional properties of an element. Attributes can have
valid values, and you can specify whether attributes are optional or mandatory as well.
• Entities that can be used to substitute their name with replacement text, which can be
defined internally in the DTD or obtained from external sources such as a file or URL
Validation can be performed using the Oracle XML Parser oraxml command-line utility.

XML Fundamentals 3 - 34
Practice 3: Overview

This practice covers the following topics:


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

• Associating a DTD with an XML document


• Validating an XML document by using an external DTD
• Adding internal DTD declarations to extend the XML
document structure and content

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n
In this practice, youn sare provided with a DTD for employees to test your understanding of
e
al an oexternal
using r a
-t DTD to validate an XML document. Tasks include:
V n n
• Associating the external DTD with an existing XML document
• Validating the XML document
• Correcting validation errors
• Extending the DTD by using internal DTD declarations
Note: The practice uses the oraxml command-line utility to validate the XML document.
Although Internet Explorer does not implement strict DTD validation, it is used so that you can
view the resulting XML document tree (especially to see the resulting document after the
entities are replaced).

XML Fundamentals 3 - 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 non-t
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Modularizing XML with Namespaces

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Describe XML namespaces


• Explain the need for XML namespaces
• Use XML namespaces in an XML document
• Use XML namespaces with prefixes
• Manage the scope of XML namespace declarations
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n
XML namespaces
t r a nrepresent a group or collection of names. XML namespaces are the
a toonthe- problem of ambiguity and name collisions. In this lesson, you learn the basics
solution
V
of: n
• Declaring an XML namespace with or without prefixes
• Using the XML namespaces in an XML document

XML Fundamentals 4 - 2
What Is an XML Namespace?

An XML namespace:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Is identified by a case-sensitive internationalized resource


identifier (IRI) reference (URL or URN)
• Provides universally unique names for a collection of
names (elements and attributes)

<departments> s a
<employee>
) h a
<name> r s
sฺ eฺ
<employee_id>
m
e uid
@
<department_id>
c
<name>
r o vi nt G
<location_id>
<salary>
a jsto Stude
n a ฺm this
i
nt uhttp://hr.com/departments
s e
http://hr.com/employees l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tnamespace: s
An XML
l e n r a n
n-t by an internationalized resource identifier (IRI), which is a case-sensitive
V•a Isnidentified
o
string of characters identifying a resource
• Provides a universally unique name for a collection of XML names made of element and
attribute names
The IRI is a string of characters that can be formatted as:
• A uniform resource indicator (URI) or uniform resource locator (URL), such as the web
address http://hr.com/employees
Note: The URL, or web address, represents a unique string and is not checked to be a
valid or existing web address.
• A uniform resource name (URN) such as urn:hr:departments. A URN starts with
the letters urn, followed by a colon (:), a namespace identifier (NID) (hr), a colon (:),
and a namespace-specific string (NSS) (departments).
The example in the slide shows two collections of XML element and attribute names, each
identified by a unique XML namespaces IRI. If the XML namespace is not used, the <name>
element for an employee would be indistinguishable from the <name> element of the
department. Applying the XML namespace qualifies each <name> element by making them
unambiguous, especially if the documents are merged.

XML Fundamentals 4 - 3
Benefits of Using XML Namespaces

Using an XML namespace resolves name collisions or


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

ambiguities in an XML document.


Which name?
<?xml version="1.0"?>
<!-- The department document. -->
<department> <?xml version="1.0"?>
<name>Executive</name>
</department> <!-- The two documents combined. -->
<department>
<name>Executive</name>
If the documents are merged <employee>
s a
<name>
) h a
<?xml version="1.0"?>
<first_name>Steven</first_name>
<last_name>King</last_name> r s
sฺ eฺ
<!-- The employee document. --> </name> m
e uid
<employee> <salary>2000</salary>
c @
<name>
<first_name>Steven</first_name>
</employee>
r o vi nt G
<last_name>King</last_name> </department>
j s to tude
</name>
<salary>2000</salary>
ฺ a
m his S
</employee> a
tin se t
e n
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t ina anare
XML namespaces sfedesigned to provide universally unique names for elements and
ale oXML
attributes.
V n -trnamespaces:
n name ambiguity and collision problems that can occur when XML document
• Resolve
fragments are combined, or multiple elements have similar type and attribute names in
the same XML document
• Allow code modules to be invoked for specific elements and attributes
If two companies are interchanging XML messages, they must come to a common agreement
on the meaning of the element and attribute names in the messages. This can be achieved by
two means:
• Defining the meaning, format, and domain of every required element and attribute
• Recognizing the element and attribute names without ambiguity
The first can be accomplished by using an XML schema definition, which is discussed in the
lesson titled “Validating XML Documents with XML Schemas.” The second is solved by using
XML namespaces to qualify element names.
The examples in the slide illustrate the combining of a <department> element with one of its
<employee> elements. Each document is different and defines a <name> element. Each
<name> element has a different content model and must be interpreted by an application in a
different way. The problem does not arise when the elements exist in separate documents.

XML Fundamentals 4 - 4
Declaring XML Namespaces

Declare an XML namespace:


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

• With the xmlns attribute in an element start tag


– Assigned an IRI (URL, URI, or URN) string value
– Provided with an optional namespace prefix
• With a namespace prefix after xmlns: to form qualified
element names
<dept:department
s a
xmlns:dept="urn:hr:department-ns">
h a
...
s ฺrs) ฺ
em uide
</dept:department>

• Without a prefix to form a “default namespace”


@
ic t G
o
r denv
s t o
<department xmlns="http://www.hr.com/departments">
ฺ m aj s Stu
...
t i n a
e t hi
en o us
</department>
a l
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
An XML n i n
tnamespace n sis declared:
l e r a
-t tag of an XML element by using the xmlns attribute, which has:
V•a Inntheonstart
- A value representing the unique XML namespace string (or IRI)
- An optional prefix, providing an abbreviation for the XML namespace string
• With a namespace prefix to explicitly qualify elements and attribute names. The
namespace prefix is declared after xmlns: and is used with an XML element to create
a qualified name. An element name without a prefix is called the local name. A prefix
can also be used to qualify an XML attribute name.
• Without a prefix to form a default namespace, which implicitly qualifies the element
name and its children that are not explicitly qualified by a prefix
Note: Attributes that are specified without a prefix are not associated with an XML
namespace.
The examples in the slide show two XML namespace declarations:
• The urn:hr:department-ns string, which is assigned the dept prefix. The prefix is
used to qualify the department element name in the start and end tags.
• The http://www.hr.com/departments string, which is a default namespace
Note: An XML namespace string does not need to reference an actual document or page.

XML Fundamentals 4 - 5
XML Namespace Prefixes

A namespace prefix:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• May contain any XML character except a colon


• Can be declared multiple times as attributes of a single
element, each with different names whose values can be
the same string or a different string
• Can be overridden in a child element by setting the value
to a different string, as in this example: a
h a s
r s )
<?xml version="1.0"?>
m sฺ eฺ
<emp:employee xmlns:emp="urn:hr:employee-ns">
@ e uid
<emp:last_name>King</emp:last_name> c
vi nt G
<emp:address xmlns:emp="urn:hr:address-ns">
r o
500 Oracle Parkway
a jsto Stude
</emp:address>
</emp:employee>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tnamespace sprefix qualifies a local element and attribute name by a particular XML
An XML
e n r a n
al on-The
namespace.
V t example in the slide qualifies the last_name element by using the emp
n
prefix representing the urn:hr:employee-ns XML namespace. The address element also
uses the emp prefix (which is qualified by a different namespace) that references
urn:hr:address-ns, overriding the parent element definition for the XML namespace. The
following example shows two different prefixes (A and B) being used with the same XML
namespace:
<A:department xmlns:A="http://www.a.org/">
<A:employees>
<B:name xmlns:B="http://www.b.org/">
<B:first_name>Steven</B:first_name>
<B:last_name>King</B:last_name>
</B:name>
</A:employees>
</A:department>
Note: It is best to avoid using the same prefix on elements that have a parent-child
relationship. It is best to avoid using the same prefix for different namespace declarations
because it can be confusing to read and can create problems when processing the document.

XML Fundamentals 4 - 6
XML Namespace Declarations: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<?xml version="1.0"?>
<department xmlns="urn:hr:department-ns" Default namespace
xmlns:emp="urn:hr:employee-ns">

<name>Executive</name> 1 employee namespace


<emp:employee>
<emp:name> 2
<emp:first_name>Steven</emp:first_name>
<emp:last_name>King</emp:last_name>
s a
</emp:name>
) h a
</emp:employee> r s
sฺ eฺ
<emp:employee> m
e uid
<emp:name>
c @
<emp:first_name>Neena</emp:first_name>
r o vi nt G
<emp:last_name>Kochhar</emp:last_name>
a jsto Stude
</emp:name>
n a ฺm this
</emp:employee>
i
nt use
</department>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
The slide i na
t showsaannsexample of a <department> element containing two <employee>
e n
al oThe
elements.
V n -tr<department> element shows an example of declaring two XML
n a default namespace and another namespace with the emp prefix. The <name>
namespaces:
element is used for:
• The department name containing the Executive text
• The employee name containing the <first_name> and <last_name> child elements
Without using XML namespaces in the document, the <name> element will be ambiguous to a
processor and can possibly be treated as the same type, even though the department and
employee names are semantically and structurally different.
Using the XML namespaces removes the ambiguity for each <name> element, which allows
them to be processed differently by an XML application. In the example in the slide, note the
following:
• The unqualified <department> and <name> elements are implicitly qualified by the
default namespace, urn:hr:department-ns, declared in the start tag of the
<department> element.
• All <employee> elements and their children, including the <emp:name> element, are
explicitly qualified with the emp prefix, which is associated with the
urn:hr:employee-ns XML namespace.

XML Fundamentals 4 - 7
Scope of XML Namespace Declarations

<?xml version="1.0"?>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<root-level xmlns="urn:demo:default-ns"
xmlns:one="urn:hr:one-ns"
xmlns:two="urn:hr:two-ns">

<level-one id="1">A</level-one>
<one:level-one id="2">
<one:level-two>B</one:level-two>
<two:level-two>C</two:level-two>
</one:level-one>
s a
h a
<one:level-one xmlns:one="urn:one:override-ns">ฺrs)
e ms ideฺ
<level-two xmlns="">D</level-two>
v i c@ t Gu
<level-two>E</level-two>
t o ro den
<level-two xmlns:two="urn:two:override-ns">F
s
aj s Stu
</level-two>
a ฺ m hi
</one:level-one>
t i n e t
</root-level>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
The scope i n s or prefixed XML namespace declaration extends from the beginning of
t of a default
n
e r a
l tagnin-t which it appears to the end of the corresponding end tag, unless it is
theastart
V no
overridden in a child (descendant) element by an inner declaration.
Note: In the case of an empty element, the scope of the XML namespace is the element itself.
The default namespace declaration applies to all nonprefixed element names within its scope.
In the slide, the default namespace applies to:
• The <level-one id="1"> element
• The <level-two> element containing the texts E and F because the element
containing the text F does not use a prefix
The attribute value for an XML namespace declaration may be the empty string. An XML
namespace with an empty string has the effect of removing any association of the default
namespace or prefix with any XML namespace name: for example, the <level-two>
element containing the text D in the slide. In this case, additional declarations may be made to
declare the default namespace or prefix again.
Note: It is an error if an element uses a prefix without its XML namespace declaration in
scope.

XML Fundamentals 4 - 8
ShowXMLns Tool

1
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t the s source code and class have been provided in your project for use in
l e n
In this course,
r a n
Java
theapractices.
V o n -tUsing these, you can examine the effects of applying XML namespaces to
elements n in an XML document. The Java class has been configured as an external tool in
JDeveloper.
The tool displays the effects of using XML namespace declarations in an XML document by
showing the expanded name as a pseudo attribute for each element in the XML document.
The printed results can be seen in the JDeveloper Message Log window.
Notes
• The ShowXMLns tool is written in Java by using the Document Object Model (DOM) API
that is available in the Oracle XML Parser (xmlparserv2.jar). The source code is
included in your course project.
• For additional information on how to set an external tool, click Help on any screen in the
External Tool wizard.

XML Fundamentals 4 - 9
Invoking the ShowXMLns Tool
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Right-click
1 the XML file. 3
2
Select
ShowXMLns.

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
To invoke i n
t this tool:
n s
l e r a
-t an XML document in the Navigator (or in the XML Code Editor).
V1.a Right-click
n o n
2. Select ShowXMLns from the context menu.
3. The tool displays the effects of using XML namespace declarations in an XML document
by showing the expanded name as a pseudo-attribute for each element in the XML
document. The printed results can be seen in the JDeveloper ShowXMLns - Log
window.
To run the tool from the command line, execute the following command:
set CLASSPATH=D:\JDeveloper\lib\xmlparserv2.jar;D:\labs\classes
java ShowXmlNS filename.xml

XML Fundamentals 4 - 10
Quiz

You can declare an XML namespace with:


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

a. The xmlns attribute in an element start tag


b. A namespace prefix after xmlns: to form qualified
element names
c. A prefix to form a “default namespace”

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t b ans
Answer:na,
e -tr an XML namespace:
alcan odeclare
V
You
n n
• With the xmlns attribute in an element start tag:
- Assigned an IRI (URL, URI, or URN) string value
- Provided with an optional namespace prefix
• With a namespace prefix after xmlns: to form qualified element names
• Without a prefix to form a “default namespace”

XML Fundamentals 4 - 11
Summary

In this lesson, you should have learned how to:


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

• Declare an XML namespace


• Declare a default XML namespace
• Use an XML namespace with or without a prefix
• Qualify element and attribute names with an XML
namespace
• Manage the scope of XML namespace declarations 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tnamespace sis a string identifier that is used to qualify a collection of element and
An XML
e n r a n
al onames.
attribute
V n -t If an element has a namespace prefix, it has two parts:
• The n local name (that is, the element name itself)
• The qualified name, which includes the local name and the XML namespace prefix used
When the element is combined with the XML namespace string associated with its prefix, it is
called the expanded name, forming the universal name of the element.
An XML namespace is declared using the xmlns attribute in the start tag of an element. The
xmlns attribute, which may or may not be followed by a colon and prefix name, has a case-
sensitive value representing an XML namespace string or IRI (that is, a URL, URI, or URN
that is not checked as a valid web address). A default namespace is declared and applied
when the prefix is not used; otherwise, elements must be qualified by their appropriate
prefixes.
The scope of a namespace declaration includes the element in which it is declared and its
descendants, unless overridden by another namespace declaration in a child element. The
namespace string value may be an empty string, which effectively cancels the namespace.

XML Fundamentals 4 - 12
Practice 4: Overview

This practice covers the following topics:


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

• Modifying an XML document to contain XML namespace


declarations and prefixes
• Using the ShowXMLns tool to examine the effects of using
XML namespaces in an XML document

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n
In this practice, youn stest your understanding of writing an XML document with XML
e
al on-t
namespaces. r a
V n

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

Validating XML Documents


with XML Schemas

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Describe an XML schema


• Create an XML schema document
• Reference an XML schema in an XML document
• Create an XML schema document by using:
– Built-in data types
– Simple types
– Complex types with sequences and choices s a
) h a
• Validate XML documents with an XML schema by using: r s
sฺ eฺ
– Oracle JDeveloper m
e uid
c @
– The oraxml command-line utility
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t you s about XML schemas, including:
l e n
In this lesson,
r a nlearn
V•a Validating
o n -t an XML document against an XML schema
n
• The syntax for writing an XML schema document (XSD) using the elements defined in
the XML Schema namespace

XML Fundamentals 5 - 2
Lesson Agenda

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

– Benefits
– Example
– Referencing an XML schema in an XML document
• Components of an XML schema
• Declaring a <simpleType> component
• Declaring a <complexType> component s a
h a
• Declaring attributes
s ฺrs) ฺ
• Using the JDeveloper XML Schema Editor @em uide
o v ic t G
• Validating an XML document by using: o r den
s t
– Oracle JDeveloper
ฺ m aj s Stu
– The oraxml command-line t i n autility thi
l e n use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 5 - 3
What Is an XML Schema?

• Is an XML-based alternative to DTD


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

• Is an XML language that defines and validates the


structure of XML documents
• Is stored in an XML schema document (XSD)
• Defines components such as:
– Simple and complex type definitions
XSD
– Element and attribute declarations s a
Validates
) h a
• Builds on the DTD functionality while r s
providing XML namespaces, built-in, m sฺ eฺ
@ e uid
simple, and complex data types support vi c t G
r o n
a jsto Stude
n a ฺm this
n t i s e References
a e
l to u XML
v
c ( nse
v i
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
The W3C n t na Schema
iXML n s fe Definition Language is an XML language (or vocabulary) that is used
in a
V
e schema
anl XML n - tra document (XSD) to describe and constrain the content of XML
no The XSD is used to validate the structure of an XML document.
documents.
An XML document, whose structure is based on the definitions in an XML schema, is called
an instance document of that XML schema. The slide shows an XML schema document
stored separately from the XML instance document that it describes and validates.
The introduction of XML schema allowed XML technology to represent data types in a
standard format. The data types give a precise way of specifying the type of content that can
be held in the elements and attributes of an XML document. A document type definition (DTD)
provided no mechanism for specifying data types in a way that a database user may require.
The XML schema definition file builds on the DTD functionality while providing XML
namespace and data type support.
Note: In this course, we use the term XML schema (lowercase s) to refer to any XML schema
that conforms to the W3C XML Schema (uppercase S) Recommendation. Because an XML
schema is used to define a class of XML documents, the term instance document is often
used to describe an XML document that conforms to a particular XML schema.

XML Fundamentals 5 - 4
W3C XML Schema Recommendation: Overview

• An XML schema can be considered to be the metadata


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

that describes a class of XML documents.


• The term instance document is often used to describe an
XML document that conforms to a given XML schema.
• The W3C Schema working group publishes an XML
Schema, often referred to as the “Schema for Schemas.”
– This XML Schema provides the definition, or vocabulary, of a
the XML Schema language. h a s
s
– All valid XML schemas can be considered to be members ฺrs) ฺ of
the class defined by this XML Schema. @ em uide
o vic ntthat
– An XML schema is therefore an XMLordocument
G
jst XML
conforms to the class defined byathe t u de published
Schema
a ฺ m his S
at: http://www.w3.org/2001/XMLSchema.
in t nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t XML Schema s Recommendation defines a standardized language for specifying the
The W3C
e n r a n
al ocontent,
structure,
V n -t and certain semantics of a set of XML documents. The XML Schema
n
Recommendation is described at http://www.w3.org/TR/xmlschema-0/.
XML Instance Documents
Documents conforming to a given XML schema can be considered to be members or
instances of the class defined by that XML schema. Consequently, the term instance
document is often used to describe an XML document that conforms to a given XML schema.
The most common use of an XML schema is to validate that a given instance document
conforms to the rules defined by the XML schema.

XML Fundamentals 5 - 5
Benefits of XML Schemas

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

• Unify both document and data modeling


• Validate XML documents
• Are created using XML
• Support the Namespace Recommendation
• Allow validation of text elements’ content based on built-in
or user-defined data types s a
h a
ฺrs) ฺ
• Allow modeling of object inheritance and type substitution
s
• Allow easy creation of complex and reusable @ em uide
content
models o v ic t G
or en jst Stud
ฺm thisa
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tuse anaXML s schema to specify the structure of XML documents and validate them.
You can
e n r n
al omost
Although
V n -t XML documents are well-formed, they may not be “valid.” A valid document is
n and conforms to specific rules defined by either a DTD or an XML schema.
well-formed
The benefits of using an XML schema for validation are the following:
• They are easily created using XML, as defined by the W3C XML Schema language.
• They support the W3C Namespace Recommendation.
• They can be used to validate the content of text elements based on built-in and user-
defined data types.
• They allow the creation of complex data type models that can be reused.
• They support object inheritance and substitution in types.
In the world of web publishing, a single page that displays the local weather, stock quotes,
horoscopes, and specific news channels based on user preferences can involve dozens of
queries made to underlying databases and application servers. These queries are made via
SQL, the standard object-relational query language, or via some programmatic interface that
ultimately calls SQL. Because both SQL and programming languages (such as Java) are
strongly typed, they return information that possesses type, structure, constraints,
relationships, and so on. The structural and data typing aspects of XML schema can help
exploit the generation of viewable documents from databases.

XML Fundamentals 5 - 6
XML Schema Versus DTD

• XML schema:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

– Is more powerful and flexible than a DTD


– Supports namespaces (not supported in a DTD)
– Is written in XML syntax
– Is extensible
– Provides data type support
• DTD: s a
– Provides the ENTITY functionality that is not supported ) h
by
a
r s
sฺ eฺ
XML Schema m
e d
– Can be embedded in an XML document ic@ Gui
– Is written in SGML t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s a set of new names called vocabulary, as well as the structure and
tschemaadefines
An XML
e n r n
al typesonfor-telements, types, attributes, and attribute groups. A DTD defines a vocabulary of
data
V
namesn for elements and attributes containing either a text string or a combination of text
strings and child elements.
Applications that process the same XML documents may want to represent their data
elements differently. Using XML namespaces helps applications to distinguish between
different definitions of the same element name used in different contexts in a single
document. XML schema seamlessly supports namespaces to distinguish definitions. A DTD
does not directly support namespaces or provide support for “typing” of data. Because a DTD
declares names associated with textual contents, it poses problems when you try to model
dynamic content that comes from strongly typed systems such as databases. An XML
schema is ideal for database systems because it provides for data type integrity and the
maintenance of bound conditions.
A DTD is written using syntax that is different from an XML document. Consequently, parsers,
tools, and programs dealing with a DTD must implement the specific rules that govern the
DTD. An XML schema allows models to be written using XML syntax, which allows the same
programs that read the data to also read the definition of the data.

XML Fundamentals 5 - 7
XML Schema Document: Example

A simple XML schema uses:


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

• A required XML namespace string, with an xs prefix,


http://www.w3.org/2001/XMLSchema
• The <schema> element as its document root
• The <element> element to declare an element
<?xml version="1.0"?>
<!-- This is the XML schema document, XSD. -->
<xs:schema
s a
xmlns:xs="http://www.w3.org/2001/XMLSchema">
) h a
<xs:element name="departments" type="xs:string"/>
</xs:schema> r s
sฺ eฺ
m
e uid
c @
<?xml version="1.0"?>
<!-- This is a valid XML instance based onro vi XML nschema
t G -->
to tuchild de -->
the
<!-- document, XSD. The element cannotjscontain
<!-- elements -->
ฺ a
m his S
a
tin se t
<departments>Finance</departments>
e n
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t inain athensslide
The example
fe shows a simple XML schema document that declares a single
ale called
element
V n - trdepartments. The XML schema document (XSD) uses the required XML
no value http://www.w3.org/2001/XMLSchema, which is assigned the xs
namespace
namespace prefix. The xs prefix is used to qualify the schema, element, and string
names to ensure that the XSD document structure conforms to the W3C XML Schema
language recommendation; that is, the XSD itself is a valid document.
The XML Schema language elements used in the example are:
• The <xs:schema> element, which is the root element for the XSD and contains the
definitions for the structure of the XML instance document
• The <xs:element> element, which declares a root element name (departments) for
the XML instance document (an example of which is shown below the XSD code)
• The <xs:string> value, which is set as the data type for the contents of the
departments element in the XML instance document. In this case, the data can be any
string but not markup; that is, no child elements are permitted.
Although the example is a very simple XML schema document, the resulting XML instance
document structure that can be created is not very useful.
Note: Using a namespace prefix, such as xs or xsd, is recommended but not required.

XML Fundamentals 5 - 8
Validating an XML Document
with an XML Schema Document
In the XML instance document, use the XML namespace
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

http://www.w3.org/2001/XMLSchema-instance, and
reference the XML schema using either of the following:
• noNamespaceSchemaLocation attribute
• schemaLocation attribute
noNamespaceSchemaLocation="file.xsd"
XML XSD
s a
schemaLocation="namespace file.xsd"
) h a
validates r s
sฺ eฺ
m
e uid
c @
<?xml version="1.0"?>
r o vi nt G
sto Stude
<departments xmlns:xsi=
"http://www.w3.org/2001/XMLSchema-instance"
a j
n a ฺm this
xsi:noNamespaceSchemaLocation="departments.xsd">
Finance i
nt use
</departments>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t an XML sinstance document using an XML schema, the XML instance document
e n
To validate
r a n
al othen-http://www.w3.org/2001/XMLSchema-instance
declares
V t XML namespace in its
n
root element to enable either of the following attributes to be used:
• The noNamespaceSchemaLocation attribute, whose value is a file name or URL
string identifying the location of the XML schema document
• The schemaLocation attribute, whose value is a namespace/file-location pair that
identifies the location of an XML schema using a specific XML namespace string, a
space, and a file location (file name or URL) of the XML schema document
These attributes are a hint to the XML Parser to validate the XML document using the XML
schema document identified by their value.
Note: The http://www.w3.org/2001/XMLSchema-instance namespace must be given
a namespace prefix such as xsi. The prefix allows the schemaLocation or the
noNamespaceSchemaLocation attribute to be recognized by the XML Parser.
The code example uses the noNamespaceSchemaLocation attribute, which indicates that
the XML document does not need to declare any XML namespace for its elements. For
example, the departments element is not required to be qualified by an XML namespace.
However, it is recommended that you use the schemaLocation attribute.

XML Fundamentals 5 - 9
Referencing an XML Schema
with the schemaLocation Attribute
• The XML schema defines the targetNamespace value.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<?xml version="1.0"?> <!-- departments.xsd -->


<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
XSD
targetNamespace="http://www.hr.com/departments">
<xs:element name="departments" type="xs:string"/>
</xs:schema>

• The XML document references targetNamespace in a


schemaLocation and the default namespace. h a s
r s )
<?xml version="1.0"?> <!-- XML document -->
m sฺ eฺ
<departments xmlns = "http://www.hr.com/departments"
@ e uid
xmlns:xsi="http://www.w3.org/2001/XMLschema-instance" c
vi nt G
xsi:schemaLocation= t r
o udeo XML
"http://www.hr.com/departments departments.xsd"> j
a sSs t
a ฺ m h i
Finance
t i n e t
</departments>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t i n
It is recommended
n n s you use the schemaLocation attribute rather than the
that
e
al on-t r a attribute. Using schemaLocation forces a specified XML
V
noNamespaceSchemaLocation
n
namespace to be used in an XML instance document. This helps to eliminate ambiguity when
referencing elements from multiple namespaces in the XML schema and instance documents.
The XML schema document in the example, departments.xsd, uses a
targetNamespace attribute to define the XML namespace that must be used in the XML
instance document.
The XML instance document references the XML schema for validation by using the
schemaLocation attribute to specify the following two parts in its value:
• The targetNamespace string as defined in the XML schema document
• The file name, or URL location, of the XML schema document
Note: The targetNamespace attribute value is also declared as the default namespace in
an xmlns attribute of the <departments> element in the XML instance document.
It is recommended that you declare targetNamespace in an xmlns attribute with a
namespace prefix in the XML schema. When you do this, the XML schema can
unambiguously refer to, reuse, or extend types and elements declared in the same XML
schema document.

XML Fundamentals 5 - 10
Quiz

The advantages of using an XML schema over a DTD are that


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

the XML schema:


a. Is more powerful and flexible than a DTD
b. Supports namespaces
c. Is written in XML syntax
d. Is extensible
e. Provides data type support s a
) h a
f. Is written in SGML sฺrs m ideฺ
e
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t b, c,ad,nse
Answer:na,
l e r
Va non-t

XML Fundamentals 5 - 11
Lesson Agenda

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

– Benefits
– Example
– Referencing an XML schema in an XML document
• Components of an XML schema
• Declaring a <simpleType> component
• Declaring a <complexType> component s a
h a
• Declaring attributes
s ฺrs) ฺ
• Using the JDeveloper XML Schema Editor @em uide
o v ic t G
• Validating an XML document by using: o r den
s t
– Oracle JDeveloper
ฺ m aj s Stu
– The oraxml command-line t i n autility thi
l e n use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 5 - 12
Components of an XML Schema

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

<element>
<restriction>
<attribute>

<simpleType> <list>

<complexType> <union>

<group> <sequence> s a
<group>
) h a
<choice> r s
sฺ eฺ
m
e uid
c @
r o vi nt G<all>

a jsto Stude
<attributeGroup>
<attributeGroup>
n a ฺm this
i
nt use
a l e o
<attribute>

i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
The rootn i n
tof an XML n sschema document is the <schema> component, which can contain one
or a
e
more -t r
l declarations a for:
V n o n
• An <element> for XML elements, which can contain attributes or be structured as
<simpleType> or <complexType>
• An <attribute> representing a named attribute XML element
• A <simpleType>, which is an atomic built-in data type, or structured as a <list> or
<union> of other components such as <enumeration> components
• A <complexType>, which defines a complex structure that may be composed of other
components such as a <sequence>, <choice>, or <group>. For example, a
<complexType> is needed to declare a structure with child elements, and can define
type structures used for deriving other types or used as a user-defined type.
• A <group>, which names a structure composed of a <sequence>, <choice>, or an
<all> component
• An <attributeGroup> defining a list of attributes
Note: An XML schema contains more component types than are discussed in this course,
which covers <element>, <attribute>, <simpleType>, <complexType>, and
<sequence> components. The <attribute> item is duplicated for clarity in the slide.

XML Fundamentals 5 - 13
XML Schema Components: Example

<?xml version="1.0"?> 1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!-- XSD document. -->


<xsd:schema 2 3
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="region">
<xsd:complexType> 4
<xsd:sequence> 5
<xsd:element name="region_id"
type="xsd:int"/>
<xsd:element name="region_name" 6
s a
type="xsd:string"/>
h a
ฺrs) ฺ
</xsd:sequence>
</xsd:complexType> s
</xsd:element> <?xml version="1.0"?> @ em uide
</xsd:schema> <!-- XML document. -->
o v ic t G
t o r den
s
<region xmlns:xsi="http://...">
aj s Stu
a ฺ m
<region_id>1</region_id>
hi
i n t
<region_name>Europe</region_name>
t e
a l en o us
</region>

i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t foraXML
The example n s schema contains the following components:
e
l XML r
-t declaration
V1.a The
n o n
2. The <schema> root element with the namespace declaration
xmlns:xsd="http://www.w3.org/2001/XMLSchema" to indicate that the
elements used in the XML schema document, which are specified in the W3C XML
Schema Language recommendation, define the structure of an XML instance document.
The namespace prefix, such as xsd, is optional but recommended. When the prefix is
declared, XML schema elements must use the prefix: for example, <xsd:schema>.
3. The <xsd:element> declaration within the <schema> element, which specifies a root
element name for the XML instance document: for example, region
4. The <xsd:complexType> definition within <xsd:element>, which is needed to
indicate that the <region> element contains a sequence of child elements named
<region_id> and <region_name>
5. <xsd:sequence>, which defines the order of occurrence of the two element
declarations that it contains, and forms the content model for the region element
6. The two <xsd:element> declarations for the child elements region_id and
region_name, based on the built-in int and string data type, respectively

XML Fundamentals 5 - 14
<schema> Declaration

The <schema> declaration is the root element of every XML


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

schema. It contains namespace information, defaults, and the


version.
<schema targetNamespace="URI" XSD
targetNamespace ="URI"
attributeFormDefault="qualified"|"unqualified"
elementFormDefault="qualified"|"unqualified"
version="version number">
. . .
s a
h a
<?xml version="1.0"?> XML ฺrs)
<xsd:schema m s eฺ
xmlns:xsd="http://www.w3.org/2001/XMLschema">@ e u id
i c G
rov dent
...
t
s Stu
jused o
Specifies that the elements and data types ฺ a
m his in the schema come
from the "http://www.w3.org/2001/XMLSchema" a
tin se t namespace and
should also be prefixed withaxs: e n
l to u
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t ina aelement
The <schema>
n n sfe is the root element in an XML schema. It contains namespace
ale onelement
information,
V -tr or attribute defaults, and any version for the vocabulary. Here is an
n
example:
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace ="http://www.oracle.com/namespace"
xmlns:target="http://www.oracle.com/namespace"
attributeFormDefault="qualified"
elementFormDefault="qualified"
version="1.0">
The XML Schema namespace is http://www.w3.org/2001/XMLSchema. The following
are valid XML Schema namespace declarations:
<schema xmlns="http://www.w3.org/2001/XMLSchema">
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
The namespace prefix has no significance and can be anything that you choose. The prefixes
in the preceding examples are most commonly used. An XML schema declares vocabularies;
therefore, the optional targetNamespace attribute helps to uniquely identify a vocabulary,
and requires a matching namespace declaration to be used with references to declarations
within the same XML schema.

XML Fundamentals 5 - 15
Declaring a Target Namespace
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace ="http://www.oracle.com/namespace"
xmlns:target="http://www.oracle.com/namespace">
or
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

targetNamespace="http://www.oracle.com/namespace"
xmlns:target="http://www.oracle.com/namespace">
Note: Declaring the targetNamespace value as a default namespace, or using a
namespace prefix as xmlns:target="http://www.oracle.com/namespace", in the
root element allows the XML schema document to reference vocabulary names declared in
the same XML schema document. This is necessary when you declare names that are
designed to be referenced by other declarations (and the names are then extended or used to
derive types for other components).
s a
Declaring Defaults
) h a
You can modify the default values of declarations appearing within the XML schema
s r s
ฺ to theฺ by
setting the attributeFormDefault and elementFormDefault attributes
qualified or unqualified (the default) value. These attributes @ emtheuqualification
control ide of
v i c t G
elements and attributes, with a namespace, in the instance document.
t o ro den
An element or attribute is qualified if it has an associated s tu as in the following
aj namespace,
S
example: a ฺ m his
t i n e t
l en o us
<emp:employee xmlns:emp="http://www.hr.com/employee">
a
<first name>Steven</first
<first_name>Steven</first_name>
i c (v name>
s e t
<last_name>King</last_name>
t o rov licen
</emp:employee> a js ble
a M fera
n
In this example:
i
nt <employee> n s
•aleThe - t r a element is qualified by a namespace
V• The n
no<first_name> and <last_name> elements are unqualified; that is, they have no
associated namespace
In the following example, all elements are implicitly qualified by the default namespace:
<employee xmlns="http://www.hr.com/employee">
<first_name>Steven</first_name>
<last_name>King</last_name>
</employee>
The last example can be explicitly expressed as shown here:
<emp:employee xmlns:emp="http://www.hr.com/employee">
<emp:first_name>Steven</emp:first_name>
<emp:last_name>King</emp:last_name>
</emp:employee>

XML Fundamentals 5 - 16
Global and Local Declarations

• Global declarations:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

– Appear as direct children


<schema ...
of the <schema> element xmlns:t="http://...">
– Can be reused within the <!– Global -->
XML schema document <element name="A"/>
• Local declarations: <complexType name="B">
– Are valid in the context in <!-- Local -->
s a
which they are defined <sequence>
h a
– Are not children of the
<element ref="t:A"/>
s ฺ r s)
<schema> element e m ideฺ
<element name="C"/>
</sequence>
i c @ Gu
– Can reference global type ro v
</complexType> nt
declarations by using a a j sto Stude
ฺ m</schema>
namespace prefix n a this
n ti se
e
al to u
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t a sfe
inschema
l e n
In the XML
t r a n document, a component declaration is:
V•a Global
n o n-if it is declared as a direct child of the <schema> element, such as the
<element> and <complexType> components in the slide
• Local if it is declared within a component other than the <schema> element; that is, it is
not a direct child of the <schema> element
In the slide, the global declarations are:
• The element A, which can appear as a root element in an XML instance document
• The complex type B, whose structure contains other local type declarations
The local types are:
• B, whose type is derived from the reference to global element A
• The element C, a simple local component
To enable a local element, such as element A, to reference a global declaration within the
same XML schema document, it is necessary to declare an XML namespace (preferably with
a namespace prefix such as t) with the same value as the targetNamespace attribute in
the start tag of the <schema> element, as in the following example:
<schema targetNamespace="http://www.hr.com/departments"
xmlns:t="http://www.hr.com/departments"...>

XML Fundamentals 5 - 17
Referencing Global Type Declarations
Within the Content Model
<?xml version="1.0" encoding="UTF-8"?> XSD
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:trg="http://www.hr.com/departments"
targetNamespace="http://www.hr.com/departments"
elementFormDefault="qualified">
<xsd:element name="department_id" type="xsd:int"/>
<xsd:element name="department_name" type="xsd:string"/>
<xsd:complexType name="departmentType">
<xsd:sequence>
<xsd:element ref="trg:department_id"/>
<xsd:element ref="trg:department_name"/>
</xsd:sequence>
s a
</xsd:complexType>
) h a
<xsd:element name="departments"> r s
sฺ eฺ
<xsd:complexType>
m
e uid
<xsd:sequence>
c @
<xsd:element name="department"
r o vi nt G
jsto Stude
type="trg:departmentType" maxOccurs="unbounded"/>
</xsd:sequence>
</xsd:complexType> ฺm thisa
i n a
nt use
</xsd:element>
</xsd:schema>
l e
c ( va e to
r o vi ens
j s o ©e2014,
tCopyright
l licOracle and/or its affiliates. All rights reserved.
M a rab
t a sfe
inexample
l e n
This partial
t r a n in the slide shows how to reference global type declarations within the
V a model.
content
n o n-
The XML schema reuses global type declarations in the ref and type attribute values, which
reference the names of the global component. It is recommended that an XML namespace
prefix, such as trg (with a namespace value set in the targetNamespace), should be used
to ensure that the global type name references are unique within the document.
In the slide example, the global type names are:
• department_id and department_name, which are referenced in the ref attributes
of the element declarations in the complexType called departmentType
• departmentType referenced in the type attribute of the department element that is
a child (local) type in the departments global element

XML Fundamentals 5 - 18
An XML Instance Document That Conforms
to the Preceding XML Schema: Example
<?xml version='1.0' encoding="UTF-8"?> XML
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!– This is an XML instance document that conforms -->


<!– to the XML schema on the previous slide. -->

<departments
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=
"http://www.hr.com/departments code_04_13n.xsd"
xmlns="http://www.hr.com/departments">
<department> s a
<department_id>10</department_id>
) h a
<department_name>Administration</department_name> r s
sฺ eฺ
</department> m
e uid
c @
</departments>
r o vi nt G
jsto Stude
...

ฺm thisa
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t example s a partial XML instance document that conforms to the XML schema
e n
The slide
l previous r a nshows
in a
V the
o n -t slide.
n schema elements are declared using the xsd prefix.
Note: All

XML Fundamentals 5 - 19
Declaring an Element

A simple element is an XML element that can contain only text.


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

It cannot contain any other elements or attributes.


• Declare a simple <element>:
– A name attribute to specify the tag name
– A type attribute to specify the content allowed
<xsd:element name="first_name" type="xsd:string"/> XSD

XML s a
<first_name>Steven</first_name> <!-- XML -->
) h a
<element r s
sฺ XSD
Fullname="name-of-element"
syntax m eฺ
e i d
type="global-type | built-in-type"
v i c@ t Gu
ref="global-element-name"
form="qualified | unqualified" t o ro den
minOccurs="non-negative number" s
aj s Stu
maxOccurs="non-negative number |ฺm
default="default-value" t i n a unbounded"
e t hi
fixed="fixed-value">
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
The XML n i n
t Schema n s
language uses the <element> component to declare an XML element.
l e t r a
V n o n- of <element> are:
a attributes
The
• The name attribute for specifying the tag name for the element
• The type attribute to specify the content type, or type of data contained in the element
• The ref attribute for referencing global type names in the schema document
• The form attribute that can be used to override the elementFormDefault attribute
value used in the <schema> element
• The minOccurs attribute that specifies the minimum number of times the element must
appear in the XML instance document. A value of zero indicates that the element is
optional.
• The maxOccurs attribute that indicates the maximum number of times the element can
appear in the XML instance document. A value of unbounded indicates that there is no
limit to the number of element occurrences.
• The default attribute that specifies a default value for the element if it does not appear
in the XML instance document. The default is not applied to an element with a value.
• The fixed attribute, which indicates that the content of the element must be equal to
this value
Note: The example specifies that the first_name element contains string data. The
string type is a built-in data type in the XML Schema language.

XML Fundamentals 5 - 20
Built-In XML Schema Data Types

The XML schema language provides built-in data types such


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

as:
• string
• int
• decimal
• Many others (boolean, float, and so on)
Examples: s a
h a
<xsd:element name="employee_id" type="xsd:int"/> XSD
s ฺrs) ฺ
@ em uide
<employee_id>100</employee_id> XML vic t G
r o n
<xsd:element name="salary" type="xsd:decimal"/> a jsto Stude XSD
n a ฺm this
i
nt use XML
<salary>1000.50</salary>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t types s can be specified in the type attribute:
Built-inndata
l e t r a n
that

on-
Va nSimpleType Example or Explanation

string “this is a string”


boolean true, false
float single-precision floating point

double double-precision 64-bit floating


point
decimal 123.45
integer ‒12345; 0; 12345
nonPositiveInteger 12345; 0
negativeInteger ‒12345
int 123456789
short 12345

XML Fundamentals 5 - 21
SimpleType Example or Explanation
byte 123
nonNegativeinteger 0; 123456

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

unsignedInt 1234567801

unsignedShort 12345

unsignedByte 123

positiveInteger 123456

date 2000-02-16 s a
h a
time 15:55:00.000
s ฺrs) ฺ
m ide
e(5:55
timeinstant 2000-12-31T05:55:00.000
i c @ G u
am,
v
ro den
Coordinated Universal Time)t
s t o
timeduration
ฺ m aj s Stu (5 years; 1 month;
P5Y1M4D10H20M1.2S
i n4adays; 20 h
t i
minutes; 1.2 seconds)
t e
en o-02-16T5:55:00
recurringinstant
v a l t us (February 16th every year
i (
c nse at 5
5:55
55 AM C
Coordinated
di d Universal
U i l Time)
Ti )
v
ro lice
long
j s t o e 1234567890123
a ab l
binary M r Bit pattern
i na s f e
l e nt tran
uri-reference standard URL
Va ID* n -
no XML 1.0 Compatibility

IDREF* XML 1.0 Compatibility

ENTITY* XML 1.0 Compatibility

NOTATION* XML 1.0 Compatibility

language en-US (see xml:lang in XML 1.0)

IDREFS* XML 1.0 Compatibility

ENTITIES* XML 1.0 Compatibility

NMTOKEN* XML 1.0 Compatibility

NMTOKENS* XML 1.0 Compatibility

XML Fundamentals 5 - 22
Lesson Agenda

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

– Benefits
– Example
– Referencing an XML schema in an XML document
• Components of an XML schema
• Declaring a <simpleType> component
• Declaring a <complexType> component s a
h a
• Declaring attributes
s ฺrs) ฺ
• Using the JDeveloper XML Schema Editor @em uide
o v ic t G
• Validating an XML document by using: o r den
s t
– Oracle JDeveloper
ฺ m aj s Stu
– The oraxml command-line t i n autility thi
l e n use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 5 - 23
Declaring a <simpleType> Component

<simpleType>:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Is a derived type that extends built-in or other types


• Provides three primary derived types:
– <restriction>, <list>, and <union>
• Has facets (properties) such as maxInclusive
<xsd:simpleType name="empid">
<xsd:restriction base="xsd:positiveInteger">
s a
<!–- for positiveInteger the minimum is 1 -->
) h a
<xsd:maxInclusive value="1000"/> r s
sฺ eฺ
</xsd:restriction> m
e uid
</xsd:simpleType>
c @
<xsd:element name="employee_id" type="empid"/>
r o vi nt G
a jsto Stude
<!–- The following is a valid XML a m snippet.-->
ฺcode his
t i n e t
<employee_id>999</employee_id>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t i n
The <simpleType>
n n s declaration includes atomic or built-in types provided by the XML
e r a
al oRecommendation.
Schema -t An example of a built-in type is integer, which requires numeric
V n
data inn
a document instance.
A <simpleType> declaration can be:
• For a derived type that extends a built-in or other simpleType declarations
• For an element that contains data and may not contain attributes or child elements
• Refined by using facets or properties
In the example in the slide:
• A <simpleType> called empid is declared as a <restriction> type
• The <restriction> uses the base attribute to identify the built-in type from which the
<simpleType> is derived
• The base type is positiveInteger, representing nonnegative values
• The maxInclusive facet sets a maximum value of 1000 for this data type
• The <element> for employee_id references the empid <simpleType> as its data
type, restricting employee_id elements to contain positive integers that are less than
or equal to 1000

XML Fundamentals 5 - 24
Facets or properties that can be used in simpleType declarations:

Facet Description

enumeration An allowable value in an enumerated list

fractionDigits The number of digits to the right of a


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

decimal point in a numeric type


length The number of items in a list type, or
characters in a string type
maxExclusive A maximum value, excluding the value
listed
maxInclusive A maximum value, including the value listed

s a
maxLength Maximum number of items in a list type, or h a
characters in a string type
s ฺrs) ฺ
minExclusive A minimum value, excluding @ emvalueuilisted
the de
c
vi nt G
r o
minInclusive sto including
A minimum jvalue,
a t u dethe value listed
a ฺ m his S
n in senumber
tMinimum t of items in a list type, or
minLength e
al characters
o u in a string type
( v t
o v ic nse
pattern
s t o r l i ce Restriction of string type based on a regular
M aj rable expression

t ina ansfe
totalDigits
n The total number of digits in a numeric value
l e -t r
Va whiteSpace
n o n
Control of how whitespace is treated within
a type. Values are preserve, replace,
and collapse.

XML Fundamentals 5 - 25
Using the <enumeration> Facet

<!-- Example: To restrict the job_id element value by -->


<!–- using the <enumeration> facet in the .XSD: -->
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

. . .
<xsd:element name="job_id">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="AC_MGR"/>
<xsd:enumeration value="AD_PRES"/>
<xsd:enumeration value="FI_MGR"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
s a
. . .
h a
s ฺ rs)
<!-- The XML element can contain only one of the values --> m
e i d eฺ
<!-- specified in the enumeration list, as in this example
v i c@ t Gu
-->
. . .
<job_id>AC_MGR</job_id> <!-- is valid --> t o ro den
s
a-->j s Stu
<job_id>XX_MGR</job_id> <!-- is not valid
a ฺ m hi
t i n e t
en o us
. . .
a l
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
Example n t i n n s
l e
Toarestrict
r a
-tjob_id element value by using the <enumeration> facet:
V n o n
the
<xsd:element name="job_id">
<xsd:simpleType>
<xsd:restriction base="xsd:string">
<xsd:enumeration value="AC_MGR"/>
<xsd:enumeration value="AD_PRES"/>
<xsd:enumeration value="FI_MGR"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:element>
The XML element can contain only one of the values specified in the enumeration list, as in
this example:
...
<job_id>AC_MGR</job_id> <!-- is valid -->
<job_id>XX_MGR</job_id> <!-- is not valid -->
...

XML Fundamentals 5 - 26
Using <list> and <union>
simpleType Declarations
• Declaring a <list>:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<xsd:element name="email">
<xsd:simpleType>
<xsd:list itemType="xsd:string"/>
</xsd:simpleType>
</xsd:element>

• Declaring a <union>: a
h a s
<xsd:simpleType name="mgrTypes">…</xsd:simpleType>
r s )
<xsd:simpleType name="repTypes">…</xsd:simpleType>
m sฺ eฺ
...
@ e uid
<xsd:element name="job_id"> c
vi nt G
<xsd:simpleType> r o
<xsd:union memberTypes="mgrTypes repTypes">
a jsto Stude
</xsd:simpleType>
n a ฺm this
</xsd:element> i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
The first i na
texampleaninsthe slide shows a declaration of a simpleType that provides a <list>
e n
l forn-the tr <email> element, which is shown in an XML snippet that uses a space-
of a
strings
V nolist of email strings. In the <union> example, suppose that mgrTypes and the
separated
repTypes are declared as follows:
<xsd:simpleType name="mgrTypes">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="AC_MGR"/>
<xsd:enumeration value="FI_MGR"/>
</xsd:restriction>
</xsd:simpleType>

<xsd:simpleType name="repTypes">
<xsd:restriction base="xsd:string">
<xsd:enumeration value="AC_REP"/>
<xsd:enumeration value="HR_REP"/>
</xsd:restriction>
</xsd:simpleType>
Then the following are valid XML snippets for the <job_id> element:
<job_id>AC_MGR</job_id>
<job_id>HR_REP</job_id>
XML Fundamentals 5 - 27
Lesson Agenda

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

– Benefits
– Example
– Referencing an XML schema in an XML document
• Components of an XML schema
• Declaring a <simpleType> component
• Declaring a <complexType> component s a
h a
• Declaring attributes
s ฺrs) ฺ
• Using the JDeveloper XML Schema Editor @em uide
o v ic t G
• Validating an XML document by using: o r den
s t
– Oracle JDeveloper
ฺ m aj s Stu
– The oraxml command-line t i n autility thi
l e n use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 5 - 28
Declaring a <complexType> Component

The <complexType> declaration:


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

<xsd:complexType name="..." mixed="true | false">


...
</xsd:complexType>

• Must be identified by a name attribute if it is global


(Otherwise, it is an anonymous complex type.)
• Provides a content model that can contain: a
h a s
– Simple content )
r s
sฺ eฺ
– A <sequence> declaration m
– A <choice> declaration @ e uid
c
vi nt G
– A reference to a global <group> toro
– An <all> declaration a js Stude
n a ฺm this
• Can allow mixed or empty i content
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na sdeclaration is a powerful construct that enables you to create simple to
A <complexType>
e n r a n
al ostructures.
complex
V n -t A <complexType> that is declared globally in the XML schema
n must be identified by a name attribute. A locally declared <complexType> is
document
called an anonymous complex type, where the name attribute is not required. Setting the
mixed attribute to true allows both text and element content to be included in the content for
the element.
The <complexType> declaration specifies different content models that enable the content
to be mixed, empty, or contain element hierarchies. A content model can contain:
• Simple content, for data without child elements
• A <sequence> declaration, to specify an ordered sequence of child elements
• A <choice> declaration, for providing a choice of child elements
• A reference to a global <group>, to reuse a group of elements to define the complex
type structure
• An <all> declaration, to allow all elements in the content model to be used in any order
Note: Examples of the <sequence>, <choice>, and empty content models are discussed
on the following pages.

XML Fundamentals 5 - 29
Declaring a <sequence>

• Defines an ordered sequence of elements


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

• Must be contained within a <complexType>


<xsd:element name="department">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="department_id"
type="xsd:int"/>
<xsd:element name="department_name"
s a
type="xsd:string"/>
) h a
<xsd:element name="manager_id"
r s
sฺ eฺ
type="xsd:int" minOccurs="0"/>
m
e uid
<xsd:element name="location_id"
c @
type="xsd:int" minOccurs="0"/>
r o vi nt G
</xsd:sequence>
</xsd:complexType> a jsto Stude
</xsd:element>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t isaused s within <complexType> to declare an ordered list of elements. The
n
<sequence>
e r n
V al on-telement in the slide may contain the element sequence of <department_id>
department
n by <department_name>, optionally followed by <manager_id>, which, in turn,
followed
can be optionally followed by <location_id>.
Note: The minOccurs attribute is zero for manager_id and location_id. Either of these
elements may be omitted. However, if both appear, they must be entered in the sequence
shown in the preceding paragraph. For example, the following XML element is valid:
<department>
<department_id>10</department_id>
<department_name>Administration</department_name>
</department>
However, the following is invalid because <location_id> is before <manager_id>:
<department>
<department_id>20</department_id>
<department_name>Marketing</department_name>
<location_id>1800</location_id> <!-- Error -->
<manager_id>201</manager_id>
</department>

XML Fundamentals 5 - 30
Declaring a <choice>

• Defines a choice of alternative elements


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

• Must also be contained within a <complexType>


<xsd:complexType name="employeeType">
<xsd:choice>
<xsd:element name="full_time"
type="xsd:string"/>
<xsd:element name="part_time"
type="xsd:string"/>
</xsd:choice>
</xsd:complexType>
s a
<xsd:element name="employee">
h a
ฺrs) ฺ
<xsd:complexType>
<xsd:sequence> s
<xsd:element name="full_name"
@ em uide
type="xsd:string"/>
<xsd:element name="contract"
o v ic t G
type="employeeType"/> t o r den
</xsd:sequence> s
aj s Stu
</xsd:complexType>
a ฺ m hi
</xsd:element>
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t enables n sone of a selection of components to be included in the XML instance
<choice>
e r
al onA-<choice>
document. t a is declared within a <complexType>. Assume that the Human
V n department wants to identify employees as full-time or part-time staff. The
Resources
example in the slide declares an element for the type of employee, called employeeType,
which can contain either a <full_time> or a <part_time> element, but not both at the
same time. The following example is a snippet of valid text in an XML document:
<employee ...>
<full_name>Steven King</full_name>
<contract><full_time>Permanent</full_time></contract>
</employee>
The following example is invalid:
<employee ...>
<full_name>Steven King</full_name>
<contract><!-- Error can't contain both children -->
<full_time>Permanent</full_time>
<part_time>Six Months</part_time>
</contract>
</employee>

XML Fundamentals 5 - 31
Declaring an Empty Element

• Can be declared using a <complexType> declaration


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

without elements or a content model


• Typically contains attributes

<xsd:element name="departments">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="department"
maxOccurs="unbounded"/> s a
<xsd:complexType>
) h a
<xsd:attribute name="department_id" r s
sฺ eฺ
type="xsd:int"/>
<xsd:attribute name="department_name" m
e uid
c @
<xsd:complexType>
type="xsd:string"/>
r o vi nt G
</xsd:element>
</xsd:sequence> a jsto Stude
</xsd:complexType>
n a ฺm this
</xsd:element> i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t element s not allow text between the start and end tags. Empty elements are
e n
An empty
r a ndoes
V al useful
mostly
o n -tfor elements with only attributes (that is, elements that do not have text content).
n an empty element without attributes in the XML schema, use:
To declare
<xsd:element name="marker">
<xsd:complexType/>
</xsd:element>
The valid XML syntax for marker is <marker></marker> or <marker/>. The example in
the slide declares departments containing one or more department elements, each with a
department_id and department_name attribute.
Note: Text content is not allowed within each department element.
A valid XML instance document, using the XML schema in the slide, can contain:
<departments>
<department department_id="10"
department_name="Administration"></department>
<department department_id="20" department_name="Marketing"/>
</departments>
Note: The second <department> element does not use the end tag format.

XML Fundamentals 5 - 32
Using Element Wildcards

Element wildcard declarations:


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

• Allow inclusion of elements without much control


• Provide a way to include elements from:
– Within the XML schema and its namespace
– Another namespace
• Are declared using <any>:
s a
<xsd:complexType name="commentType">
) h a
<xsd:sequence>
<xsd:element name="author"/> r s
sฺ eฺ
<xsd:any namespace="##any" m
e uid
c @
processContents="lax"
r o vi nt G
jsto Stude
minOccurs="0" maxOccurs="unbounded"/>
</xsd:sequence>
ฺm thisa
</xsd:complexType>
i n a
l e nt use
<xsd:element name="comments" type="commentType"/>

c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s a form of element wildcard, which declares an element that does not
twant toause
You may
e n r n
al itsocontent
control
V n -t when including any elements declared in the XML schema or another
n Declare an element wildcard by using <any>, with the following key attributes:
namespace.
• The namespace attribute with a value of:
- ##any allows elements from all namespaces
- ##other allows elements from namespaces other than targetNamespace
- ##targetNamespace allows elements from targetNamespace
- ##local allows well-formed elements not qualified by a namespace
-A space-separated list of namespace URIs allows elements from the specified
namespaces
• The processContents attribute with a value of:
- skip indicates to the processor to skip wildcard elements in an instance document
- lax attempts the validation of the content elements if their declaring XML can be
accessed. However, errors are not flagged.
- strict (the default) validates the content if the declaring XML schema can be
accessed, and errors are flagged
Example: Use an element wildcard to create elements that you want to contain HTML/XHTML
tags as part of formatting instructions for the data within the element.

XML Fundamentals 5 - 33
Lesson Agenda

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

– Benefits
– Example
– Referencing an XML schema in an XML document
• Components of an XML schema
• Declaring a <simpleType> component
• Declaring a <complexType> component s a
h a
• Declaring attributes
s ฺrs) ฺ
• Using the JDeveloper XML Schema Editor @em uide
o v ic t G
• Validating an XML document by using: o r den
s t
– Oracle JDeveloper
ฺ m aj s Stu
– The oraxml command-line t i n autility thi
l e n use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 5 - 34
Declaring Attributes

Declare an <attribute>:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Identified by the name attribute


• With the type attribute restricted to built-in or user-defined
simple types

<xsd:attribute name="department_id" type="xsd:string"/>

s a
Full Syntax
) h a
<attribute r s
sฺ eฺ
name="name-of-attribute" m
e uid
c @
vi nt G
type="global-type | built-in-type"
ref="global-attribute-declaration" r o
form="qualified | unqualified"
a j sto Stude
used="optional | prohibited | required"
n a ฺm this
default="default-value" i
nt use
l e
va e to
fixed="fixed-value">
c (
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t declared s using an <attribute> component are identified by a name and type
Attributes
e n r a nby
alis based
that
V o n t a built-in or user-defined <simpleType>. An attribute can be declared in
-on
n
three ways:
• As a local type contained within another component such as an <element> or an
<attributeGroup>
• As a global type that can be referenced, extended, and reused in a local type or as part
of an <attributeGroup>
• As a reference to an existing global type
In the slide, department_id is a global declaration if it appears as a direct child of the
<schema> component. Otherwise, it is a local type if nested within an element other than
<schema>. Other attributes of the <attribute> component are:
• ref, which derives the type from an existing global attribute declaration
• form, which indicates whether the attribute must be qualified by a namespace prefix or
not (the default)
• used, which indicates that the attribute is optional (the default), required, or
prohibited (not allowed)
• default, which defines a default value for the attribute when it is optional and not
specified
• fixed, which defines a constant value for the attribute

XML Fundamentals 5 - 35
Attribute Declarations: Example

1. Based on a <simpleType>:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<xsd:attribute name="department_id">
<xsd:simpleType>
<xsd:restriction base="xsd:positiveInteger">
<xsd:maxInclusive value="100"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>

2. With a default value: s a


) h a
<xsd:attribute name="department_id" r s
sฺ eฺ
type="xsd:int" default="10"/> m
e uid
c @
3. Referencing a global type: r o vi nt G
a jsto Stude
<xsd:attribute ref="department_id"/>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
The first i na s
texampleandeclares the department_id attribute using a <simpleType>
l e n t r
a onThe
declaration.
V - <simpleType> uses a <restriction> to constrain the data type values
n
to being positive integer values less than or equal to 100.
The second example declares the department_id as a simple integer type with a default
value of 10. The default value is assumed if the attribute is not included in the XML document.
The third example declares an attribute that references the department_id attribute type
declared in the first example. The attribute reference example assumes that department_id
is uniquely declared in the default namespace of the same XML schema document.
Each of the examples in the slide can be declared globally or nested (locally) as part of:
• An element declaration
• A <complexType> declaration. An example of this is shown in the section titled
“Declaring an Empty Element,” which contains attribute declarations.
• An <attributeGroup> declaration, as discussed in the section titled “Declaring and
Referencing an <attributeGroup>”

XML Fundamentals 5 - 36
Declaring and Referencing
an <attributeGroup>
Declare an <attributeGroup>:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Identified by the name attribute


• Containing one or more <attribute>s or references to
<attributeGroup>s
<xsd:attributeGroup name="departmentGroup">
<xsd:attribute name="name" type="xsd:string"/>
<xsd:attribute name="id" type="xsd:int"/>
</xsd:attributeGroup>
s a
) h a
r s
sฺ eฺ
Reference in a <complexType> or <attributeGroup>: m
e uid
c @
<xsd:element name="department">
r o vi nt G
<xsd:complexType>
a jsto Stude
<xsd:attributeGroup ref="departmentGroup"/>
n a ฺm this
</xsd:complexType>
i
nt use
</xsd:element>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s is a collection of attribute declarations. Although an
n
<attributeGroup>
e r a n
V al on-t
<attributeGroup> can be declared locally, it is more useful when it is declared globally so
that thengroup can be reused in different components.
An <attributeGroup>:
• Has a name that can be referenced by another <attributeGroup> declaration
• Can contain <attribute> declarations
• Can contain another <attributeGroup> declaration. However, recursive and indirect
circular references are not valid. The following examples are not permitted:
<attributeGroup name="group1">
<attributeGroup ref="group1"/>
</attributeGroup>
or
<attributeGroup name="groupA">
<attributeGroup ref="groupB"/>
</attributeGroup>
<attributeGroup name="groupB">
<attributeGroup ref="groupA"/>
</attributeGroup>
XML Fundamentals 5 - 37
Documenting the XML Schema

Document the XML schema by using:


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

• XML comments
• Attributes from other namespaces
• An <annotation> declaration:
– Provides an <appinfo> element
– Contains a <documentation> element
– Can be included within all other component types s a
) h a
r s
sฺ eฺ
<xsd:annotation>
<xsd:appinfo source="file:///d:/labs/anno.xml"/> e
m
i c @ Guid
<xsd:documentation> v t
A <b>big</b> comment about this Schematoro e n
</xsd:documentation> a js Stud
</xsd:annotation>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a s ways to document your XML schema. Although XML comments are
t lists different
e n
The slide
r a n
al ofornreading
valuable
V -t the XML schema document, most processors will not report the XML
n
comments.
You can include references to attributes from namespaces other than the target namespace
of the XML schema document. These attributes are ignored by the Schema processor and
can be used to pass information to other applications, and act as comments to the human
reader of the document.
The best form of comment is to use the <annotation> declarations, which can be nested
within most other XML schema components as a form of documentation. As shown in the
slide, an annotation can contain any element from any namespace. For example, the <b> and
</b> elements represent bold text in the HTML/XHTML namespace. The <appinfo> element
is optional and can include a source attribute to identify an external file for application
processing or documentation. Different XML schema processors may ignore or treat
<appinfo> contents differently.
Note: When you create an XML schema document in JDeveloper, it automatically adds a
sample <annotation> element as a child of the <schema> component.

XML Fundamentals 5 - 38
Practice 5-1: Overview

This practice covers the following topics:


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

• Creating and validating an XML schema document for the


regions.xml document manually
• Creating and validating an XML schema document for the
employees.xml document with the visual XML Schema
Editor in JDeveloper
• Associating the regions.xml document with the a
regions.xsd XML schema and validating the XML ) ha
s
instance document s ฺrs ฺ
@ em uide
• Associating the employees.xml document ic with Gthe
o v n t
r de the XML
employees.xsd XML schema and s tovalidating
j tu
instance document ฺma is S
n t ina se th
v a le to u
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n t ina you
In this practice, n s fe a simple XML schema document that is used to validate XML
develop
instance
V n -tra You use JDeveloper to create the XML schema document and manually
ale odocuments.
add XML n schema components to define the XML schema structure.

XML Fundamentals 5 - 39
Lesson Agenda

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

– Benefits
– Example
– Referencing an XML schema in an XML document
• Components of an XML schema
• Declaring a <simpleType> component
• Declaring a <complexType> component s a
h a
• Declaring attributes
s ฺrs) ฺ
• Using the JDeveloper XML Schema Editor @em uide
o v ic t G
• Validating an XML document by using: o r den
s t
– Oracle JDeveloper
ฺ m aj s Stu
– The oraxml command-line t i n autility thi
l e n use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 5 - 40
Creating an XML Schema Document
in JDeveloper
1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

2
5 a
h a s
r
ฺ ฺs )
4 m s
Right-click.
@ e uide
c
vi nt G
r o
a jsto Stude 6
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t an XML s
l e n
To create
t r a n
schema document:
V1.a Select
n o na- project node and select File > New. Alternatively, you can right-click the project
node and select New from the shortcut menu.
2. In the New Gallery window, expand the General category and select the XML node.
3. Double-click XML Schema. Alternatively, you can select XML Schema and click OK.
4. In the Create XML Schema window, enter the XML schema file name and click OK.
5. The XML schema document is created and opened in the XML Schema Editor, which
provides two editing tabs:
a. A Design tab to graphically edit the XML schema
b. A Source tab to textually edit the document by using an XML editor that is aware
of the XML Schema recommendation elements and attributes. Initially, on the
Design tab, view the <schema> element with a default targetNamespace
attribute and a single child element called exampleElement that is created.
6. Add XML schema components visually on the Design tab. Perform either of the
following:
- Right-click an item and select a component from the shortcut menu.
- Drag Schema Components from the Component Palette to the Design window.

XML Fundamentals 5 - 41
Using the JDeveloper XML Schema Editor
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

1
s a
) h a
r s
sฺ eฺ
m
e uid
i c @ G 3
o v n t
4 j s tor tude
ฺ m a sS
t i n a
e t hi
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
As shown i n s the XML Schema Editor can be used visually by using the Design tab
t in theaslide,
n
e r
al Alterations
view. -t made to the Design tab alter the source code by adding, modifying, or
V o n
n the associated XML elements in the document, which can be viewed on the Source
removing
tab.
When you edit on the Design tab:
1. Components can be:
- Moved by using drag-and-drop techniques
- Added to other components by right-clicking a component and selecting an object
from the context-sensitive menu
- Clicked to change their name in place, or by using the Property Inspector
2. The Component Palette provides XML components that can be dragged to the Design
window
3. The Property Inspector displays properties that can be altered to set component
properties such as minOccurs, maxOccurs, and type attribute values
4. The Structure pane displays the hierarchical structure of the document and indicates
validation errors. The Structure pane can be used to navigate between components and
add, modify, and delete components.
Click the Source tab to use the text-based XML Editor to view or make changes.
Note: For help on a component, select the node and press F1.
XML Fundamentals 5 - 42
Using the JDeveloper
XML Schema‒Aware XML Editor
To use the features of the XML schema‒aware editor:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

1. Register an XML schema with JDeveloper.


2. Associate an XML document with an XML schema.
3. Use the XML schema-aware code-insight and validate the
XML menu features.

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t areadiscussed
(Thesensteps
l e t r n in detail on the following pages.)
o n-
V1.a InnJDeveloper, register the XML schema document, which can be located in a file on
disk or from a URL on the web.
2. Create an XML document associated with an XML schema.
3. The code-insight automatically becomes aware of the registered XML schema, and you
can start using it to rapidly create an XML instance document.
Note: JDeveloper has a list of preregistered XML schemas (such as the XML Schema
Recommendation) that enable you to rapidly and visually create your own XML schema
documents.

XML Fundamentals 5 - 43
Registering an XML Schema with JDeveloper
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

1
s a
) h a
s r
ฺ ฺs
2 m
e uide
c @
r o vi nt G
jsto Stude 6
4
a
ฺm this
t i n a
l e 5n u s e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tschemaacan sbe registered or imported into JDeveloper, thereby enabling the XML
An XML
e n r n
al to obecome
Editor
V n -t aware of the XML schema.
n an XML schema with JDeveloper:
To register
1. Select Tools > Preferences from the menu to open the Preferences window.
2. Click the XML Schema node.
3. Click Add to open a dialog box for entering the XML schema location.
4. In the Add Schema dialog box, click the Browse button to find the schema that you want
to register.
5. Click OK to add the new XML schema to the User Schemas for XML Editing list.
6. Click OK to close the Preferences window. The new XML schema is now registered.
JDeveloper automatically validates the XML schema when you add it.
To use the XML schema–aware editing feature after the XML schema is registered:
1. Create an XML document.
2. Enter the root element of the document and associate it with an XML schema by
declaring the default namespace (or a namespace prefix) with the same value as the
targetNamespace specified in the appropriate XML schema document.
You can use a simple wizard to create an XML document from an XML schema. In the wizard
steps, you can select the target namespace for the XML schema, and the root element.
XML Fundamentals 5 - 44
Creating an XML Document from an XML Schema
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t an XML s
l e n
To create
t r a n
document from an XML schema in JDeveloper:
V1.a Select
n o na- project node and select File > New. Alternatively, you can right-click the project
node and select New from the context menu.
2. In the New Gallery window, expand the General category, and then select the XML
node.
3. Double-click XML Document from XML Schema. The Create XML Schema
Document from the XML Schema wizard is launched.
4. Click Next to continue.
5. Specify the file name, directory, and schema location. You can use either a schema that
is available in the file system or a schema that is already registered with JDeveloper.
6. Select the target namespace for the XML schema, the root element. Then click Finish.
The XML document instance is created. This XML document has all the elements
defined in the XML schema that are needed to instantiate the XML document.
Note: You can also choose to specify other options, such as the depth of the XML document
to be generated in terms of element levels, and encoding.

XML Fundamentals 5 - 45
Using the XML Schema–Aware XML Editor

• Code-insight is invoked when a less-than (<) character,


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

space, or colon (:) is typed.


• Drag elements from the Component Palette.

s a
2 3 ) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tXML schema s is registered with JDeveloper, the XML Editor becomes aware of its
e n
After the
r a n
al onThe
vocabulary.
V -t JDeveloper code-insight feature can then provide you with a context-
n selection of elements, attributes, and namespace prefixes.
sensitive
To use the schema-ware XML editor:
1. Create the root element, and assign the XML schema namespace as defined in the XML
schema in a default (or prefixed) namespace declaration in the instance document.
2. Enter a less-than (<) character and wait for a second, after which JDeveloper code-
insight displays the elements that are appropriate to that context. In the slide, the child
element of the <department> element includes the department_id,
department_name, location_id, manager_id, and elements from the xsi:
namespace (if the departments XML schema permits).
3. Alternatively, perform a drag-and-drop operation with an element from the Component
Palette if it has been configured using the Tools > Configure Palette menu.
Notes
• You perform the preceding step 1 if you do not use the wizard to create an XML
document from an XML schema.
• Entering a less-than (<) character causes elements to be displayed; adding a space
after the start tag causes a list of attributes to be displayed for the element; and entering
a colon (:) after a namespace prefix displays elements in the namespace (if accessible).
XML Fundamentals 5 - 46
Lesson Agenda

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

– Benefits
– Example
– Referencing an XML schema in an XML document
• Components of an XML schema
• Declaring a <simpleType> component
• Declaring a <complexType> component s a
h a
• Declaring attributes
s ฺrs) ฺ
• Using the JDeveloper XML Schema editor @em uide
o v ic t G
• Validating an XML document by using: o r den
s t
– Oracle JDeveloper
ฺ m aj s Stu
– The oraxml command-line t i n autility thi
l e n use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 5 - 47
Validating an XML Document with
Its XML Schema in JDeveloper
To validate an XML document with its XML schema:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• In the XML document, right-click one of the following:

s a
1 ) 2h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
Application Navigator a jsto StXML udeEditor
n a ฺm this
• Select Validate XML from
i
nt theucontext s e menu.
a l e o
v
c ( nse t
v i
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n
After an t inadocument
XML n s feis associated with an XML schema, the XML instance document can
beavalidated
V tra the XML schema.
le n-against
noshows two ways to validate the XML document in JDeveloper:
The slide
• Right-click the XML document name in the Navigator pane, and then select Validate
XML from the context menu (example 1 in the slide).
• Right-click the Source tab of the XML Editor and select Validate XML from the context
menu (example 2 in the slide).
Note: The Validate XML menu item is unavailable when the XML document is not associated
with an XML schema. You can also use the oraxml command-line utility to validate an XML
document against an XML schema.

XML Fundamentals 5 - 48
Validating an XML Document with its XML
Schema with the oraxml Java Utility
The oraxml Java command-line utility:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Does not require registering the XML schema


• Validates an XML instance document against an XML
schema specified in the schemaLocation or
noNamespaceSchemaLocation attribute values
• Requires:
– xmlparserv2.jar in the CLASSPATH
s a
– The –schema option ) h a
r s
sฺ eฺ
– The XML document file name m
e uid
c @
java oracle.xml.parser.v2.oraxml -schema test.xml r o vi nt G
a j sto Stude
The input XML file is parsed without
a ฺ m errors h i s using schema
validation mode.
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t
The oraxml
n inautility,
n s fe
which is a command-line interface for the Oracle XML Parser (in
V ale on-tra can be used to validate an XML document against an XML schema
xmlparserv2.jar),
n Invoke the oraxml utility by using the following command:
document.
java oracle.xml.parser.v2.oraxml –schema test.xml
Note: The java executable located in the Java Runtime Environment (JRE) binary directory
must be in the PATH. The xmlparserv2.jar file located in the JDeveloper library directory
must be in the CLASSPATH as follows:
export
CLASSPATH=/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2/xmlparserv2.jar
The –schema option indicates that the test.xml file must be validated against the XML
schema specified in the root element of the XML document. For example, if the XML schema
is contained in a file called test.xsd, the XML instance document contains the following:
<test
xmlns:xsi="http://www.w3.org/2001/XMLschema-instance"
xsi:schemaLocation="http://www.demo.com/test test.xsd"
xmlns="http://www.demo.com/test">
Sample data
</test>

XML Fundamentals 5 - 49
Applications for XML Schema

• E-commerce
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Web publications and syndication


• Enterprise application integration (EAI)
• Process control and data acquisition

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 usXML e schema validates
a l e o the XML information exchanged.
( v t
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t ina ansInfeboth business-to-business and business-to-consumer e-commerce,
• E-commerce:
le isn-largely
tr dynamic. XML schemas map robustly to server-driven data models such
Va data
o
nSQL:1999,
as which provides user-defined types, inheritance, references between
types, and collections of types among others. Libraries of schemas define business
transactions within markets and between parties. XML schemas validate a business
document and provides access to its information set.
• Web publications and syndication: The key to syndication on the web is the highly
customizable distribution between publishing and syndication services. Collections of
XML documents with complex relations (such as cross-references and kind-of relations)
among them will be the norm. Protocols (such as Simple Object Access Protocol, or
SOAP) that are built on XML can take advantage of the complex structural aspects of
XML schema.
• Enterprise application integration (EAI): The essence of an EAI hub-and-spoke
architecture is the dynamic data exchange between loosely coupled applications. DTDs
cannot fully describe today’s exchange data models. XML schema capability for
metadata interchange simplifies EAI and is an important optimization technology.
• Process control and data acquisition: In multivendor, distributed systems such as
those in plant automation, security, devices, and traffic routing, XML schema can aid
outgoing and incoming message validity. Controllers can determine which parts of
messages they understand, when to ignore information, and when to raise errors.
XML Fundamentals 5 - 50
Converting a DTD to an XML Schema

<employee>
<employee_id>120</employee_id> XML
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<last_name>Weiss</last_name> document
</employee>

<!ELEMENT employee (employee_id, last_name)> DTD


<!ELEMENT employee_id (#PCDATA)>
<!ELEMENT last_name (#PCDATA)>

s a
) h a
<xsd:complexType name="employeeType">
r
sฺ eฺs
<xsd:sequence>
m
e uid
<xsd:element name="employee_id" type="xsd:int"/>
i c @ GXML
<xsd:element name="last_name" type="xsd:string"/>
o
r de v n t schema
</xsd:sequence>
s t o
</xsd:complexType>
ฺ m aj s Stu
t i n a
e t hi
<xsd:element name="employee"
a l entype="employeeType"/>
o us
v
c ( nse t
v i
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
The XML n t na in n
icode s
the
e
ffirst example conforms to both the DTD and the XML schema in the
l e t r a
- parser that uses the DTD can only discern that the elements are strings. By
a A validating
slide.
V nonschema, a validating parser can discern that employee_id is an integer and
using XML
last_name is a string.
The slide presents a simple example that is easily converted by manually entering the XML
schema, provided that you understand the structure represented by the DTD. There are
several ways to automate the conversion of a DTD to an XML schema:
• Using JDeveloper
• Writing your own application with an API that provides the capabilities to convert a DTD
to an XML schema, such as in the Oracle XML Developer’s Kit (XDK)
Note: The XDK provides API calls to enable the development of an application that converts
a DTD to an XML schema.
Oracle XML SQL Utility (XSU) can generate an XML schema document from a database table
definition.

XML Fundamentals 5 - 51
Quiz

The oraxml Java command-line utility:


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

a. Requires registering the XML schema


b. Validates an XML instance document against an XML
schema specified in the schemaLocation or
noNamespaceSchemaLocation attribute values
c. Requires xmlparserv2.jar in the CLASSPATH
d. Requires the –schema option and the XML document file s a
name ) h a
rs 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t c, dans
Answer:nb,
l e r
Va non-t

XML Fundamentals 5 - 52
Summary

In this lesson, you should have learned how to:


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

• Describe an XML schema


• Create an XML schema document with:
– Element declarations
– Attribute declarations
– Custom simpleType and complexType declarations
– Annotations s a
) h a
• Use the XML schema built-in data types r s
m sฺ eฺ
• Associate an XML schema with an XML instance
@ e uid
document c
vi nt G
r o
• Validate XML documents with anaXML jsto schema t u de by using
JDeveloper and the oraxml a ฺ m his S tool
command-line
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t ina you
In this lesson, n s fe about XML schema creation. You also learned about validating an
learned
aledocument
XML
V n - tra
using an XML schema.
no
An XML schema can express complex structures using:
• element declarations with data type support
• attribute declarations with data type support
• simpleType and complexType declarations
• group and attributeGroup declarations
Comparatively, a DTD expresses relatively simple structures with the following typical
shortcomings:
• Lack of support for complex structural schemas
• No support for XML namespaces
• No provision for data types
• Content models that specify part-of relations, but specify kind-of relations only implicitly
or informally
An advantage that XML schema offers is extensibility. An XML schema can be refined and
extended by authors, who add their own elements, attributes, and constraints. This
extensibility enables XML schemas to create open content models.

XML Fundamentals 5 - 53
Practice 5-2: Overview

This practice covers the following topics:


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

• Using the JDeveloper XML Schema Editor to create an


XML schema for the employees.xml document
• Registering the employees.xsd XML schema in
JDeveloper, and then associating and validating the
employees.xml document with its XML schema
• Creating a new employee by using the XML schema– a
h a s
aware editor ) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na suse the JDeveloper XML Schema Editor to modify the XML schema
e n
In this practice,
ryou
a n
al o(which
document
V n -t is registered in JDeveloper) and to validate the XML instance document.
n

XML Fundamentals 5 - 54
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Navigating XML Documents with XPath

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Describe XML Path Language (XPath)


• Construct a location path
• Use XPath expressions and functions
• Search an XML document by using an XPath expression

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t you s about basic XML Path Language (XPath) syntax, abbreviated
e n
In this lesson,
r a nlearn
al on-and
expressions,
V t functions. You use JDeveloper’s XPath Search feature to search an XML
documentn by using XPath expressions.

XML Fundamentals 6 - 2
Lesson Agenda

• XML Path Language (XPath)


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

• XPath expressions
– Location path expression
– Location steps
– XPath axes
– Node test types
– Abbreviated and unabbreviated XPath expressions s a
h a
ฺrs) ฺ
– XPath predicates
s
• XPath functions
@ em uide
• Testing XPath expressions 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 6 - 3
What Is XML Path Language?

XPath is a language for finding information (elements and


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

attributes) in an XML document. XPath:


• Treats XML documents as trees of nodes
• Uses path expressions to select nodes or node-sets in an
XML document
• Is named after the path notation it uses for navigating
through the hierarchical structure of an XML document a
h a s
• Uses a compact, non-XML syntax to form expressions rs) for
s ฺ
use in URI and XML attribute values e m ideฺ
• Fully supports XML namespaces v i c@ t Gu
t o ro den
• Is designed to be used by XML applications s
aj s Stu such as XSLT
and XPointer a ฺ m hi
t i n e t
• Contains a library of l en o ufunctions
standard
a s
v c ( se t
i
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
XML Path i n
t Language n s(XPath) is primarily used to address parts (nodes) of an XML document.
e
al models
XPath r a
-tan XML document as a tree of nodes, and expresses a navigation path through
V n o n
the hierarchical structure of an XML document. XPath:
• Is named after the path notation it uses for navigating through the structure of an XML
document
• Uses a compact, non-XML syntax to form expressions that are used within URI and
XML attribute values
• Facilitates the manipulation of string, number, and Boolean values
• Operates on the abstract, logical structure of an XML document, which is called the
document data model, rather than its surface syntax
In addition to its use for addressing parts of an XML document, XPath fully supports XML
namespaces and provides a natural expression language subset for pattern matching, which
is extensively used in the W3C XSLT Recommendation, and XPointer.
Note: XSLT is XML Stylesheet Language Transformation, the language used to transform an
XML document into another XML document. XPointer is XML Pointer Language, which is
used as a fragment identifier for any uniform resource locator (URL) reference that locates a
resource with a MIME type of text/xml or application/xml.

XML Fundamentals 6 - 4
XPath 1.0 and XPath 2.0

• XPath is available in two versions: XPath 1.0 and XPath


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

2.0.
• The major difference between the two versions lies in the
following:
– XPath 1.0 supports node-sets.
– XPath 2.0 supports sequences.
• XQuery and XPath 2.0 share the same data model. a
h a s
• XPath 2.0 is a syntactic subset of XQuery.
s ฺrs) ฺ
• This lesson focuses on XPath 1.0. em de @ Gui
i c
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s language such that the operands of various expressions, operators,
XPath nis ta strongly
a ntyped
e
alfunctions
and r
-tmust conform to designated types. It is a case-sensitive expression language.
V n o n
Typically, it is used by an XSLT Processor, where the context node changes as the processor
navigates through the XML document nodes. An XSLT Processor is guided by template
instructions that contain XPath expressions that are used to match nodes in the tree.
Notes
• The XPath 2.0 data model is different from the XPath 1.0 model. W3C specifies XQuery
1.0 and the XPath 2.0 data model, which is the data model of XSLT 2.0, XPath 2.0, and
XQuery.
• XPath 2.0 supports all the simple primitive data types that are built into XML Schema.
• For more information about additional features in XPath 2.0, see:
- The section titled “XPath 2.0.” in this lesson
- The website at http://www.w3.org/TR/xpath/

XML Fundamentals 6 - 5
XPath Terminology: Node Types

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

• Attribute
• Text
• Namespace
• Processing instruction
• Comment
• Document node 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na streated as trees of nodes. The topmost element of the tree is called the
e n
XML documents
r a n
are
V alelement.
root
o n -t
n there are seven kinds of nodes (as listed in the slide).
In XPath,

XML Fundamentals 6 - 6
XPath Terminology: Family Relationships

Family Relationship Description


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

Parent Each element and attribute has one parent.


Children Element nodes may have zero or more children.
Siblings These are nodes that share the same parent.
Ancestors These include a node's parent, parent's parent, and so on.
Descendants These include a node's children, children's children, and
so on.
s a
<?xml version="1.0" encoding="UTF-8"?>
h a
<bookshop>
s ฺrs) ฺ
em uide
<book category="CHILDREN">
<title>Love You Forever</title> @
ic t G
<author>Robert Munsch</author>
o
r denv
<year>1995</year>
s t o
<price>4.98</price>
ฺ m aj s Stu
</book>
t i n a
e t hi
</bookshop>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
Examples i n
t of family n s
relationships in the XML document in the slide are as follows:
l e t r a
V•a Then n- of the <bookshop> element is the <book> element.
ochild
• The children of the <book> element are the <title>, <author>, and <year>
elements.
• The <title>, <author>, <year>, and <price> elements are siblings.
• The descendants of the <bookshop> element are the <book>, <title>, <author>,
<year>, and <price> elements.
• The ancestors of the <title>, <author>, <year>, and <price> elements are the
<book> and <bookshop> elements.

XML Fundamentals 6 - 7
XPath Model

1 / Document root node


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

2 <departments> Root element node

4
3 <department> Num="1" 4 <department> Num="2"

s a
h a
Attribute node
ฺrs) ฺ
<department_id> <department_name> 3 . . .
s
3 @ em uide
o v ic t G
10
5 Administration 5 t o r den
s
aj s Stu
a ฺ m hi
t i
n us n e t
Text nodes ale
c ( v e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t ananXML s document as a tree of nodes. The graphic in the slide depicts the
XPath n models
e
al otypes
different r
-t of nodes as seen by XPath in an XML document:
V n n
1. Document root node, which is a virtual document root that is the parent of the entire
XML document containing the XML declaration, the root element and its children,
comments, and processing instructions at the beginning and end of the document
2. Root element node (for example, the <departments> element)
3. Element nodes (for example, the <department>, <department_id>, and
<department_name> elements. The root element node is also an element node.)
4. Attribute nodes (for example, the num="1" node)
5. Text nodes (for example, the nodes containing the text 10 and Administration)
Note: In the XPath model, the document root is not the same as the root element node.
XPath can also address comment nodes, processing instruction nodes, and namespace
nodes, which have not been shown in the diagram. XPath defines a way to compute a string
value for each type of node, some of which have names. Because XPath fully supports the
XML Namespaces Recommendation, the node name is modeled as a pair that is known as
the expanded name, which consists of a local part, and a namespace URI, which may be
null.
XPath allows various kinds of expressions to be nested with full generality.

XML Fundamentals 6 - 8
Lesson Agenda

• XML Path Language (XPath)


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

• XPath expressions
– Location path expression
– Location steps
– XPath axes
– Node test types
– Abbreviated and unabbreviated XPath expressions s a
h a
ฺrs) ฺ
– XPath predicates
s
• XPath functions
@ em uide
• Testing XPath expressions 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 6 - 9
XPath Expressions

An XPath expression:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Is the primary construct in XPath


• Is evaluated to yield an object whose type can be:
– A node-set, a Boolean, a number, or a string
• Is evaluated within a context (starting point) that includes:
– A node called the context node
– The context position and the context size
s a
– A function library ) h a
r s
sฺ eฺ
• Can be a location path (the most important and m
commonly
e uid
c @
used expression type)
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t language sprovides several kinds of expressions that may be constructed from
e n
The XPath
r a n
al onsymbols,
keywords,
V -t and operands. Generally, an operand forms another kind of expression.
n expression is evaluated to yield an object of the following basic types:
An XPath
• A node-set: An unordered collection of nodes, excluding duplicates
• A Boolean: A true or false result
• A number: A floating-point number
• A string: A sequence of Universal Coded Character Set (UCS) characters
XPath expressions are evaluated in a context, which consists of:
• A node, called the context node
• A pair of nonzero positive integers known as the context position and context size
• A set of variable bindings
• A function library
• A set of namespace declarations in scope for the expression
The most important and commonly used type of expression is the location path, which is
discussed on the next page.

XML Fundamentals 6 - 10
Location Path Expression

• All legal XPath code can be called expressions.


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

• An XPath expression that returns a node-set is called a


location path.
• The location path expression is one of the following:
– Relative location path
— Is made up of one or more location steps that specify navigation
directions to nodes in an XML document
— Is relative to the starting point (called the context node) a
– Absolute location path h a s
Always starts from a standard point, the root node, /, followed r s )
ฺ ฺ by

s
a relative location path
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t pathanissone of the most important XPath expressions that gives an application
The location
e -tr in an XML document to locate a set of nodes. A location path can be
al ondirections
navigation
V nthe following:
either of
• An absolute location path, which starts with a slash (/) and is followed by a relative
location path
• A relative location path, which is made up of a sequence of one or more location
steps that are separated by a slash (/). Location steps are composed from left to right
by selecting or navigating to a set of nodes, relative to the context node.

XML Fundamentals 6 - 11
Location Path Expression: Example

Find the department_id of every department element that is a child


of the departments element.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

/departments/department/department_id XPATH
expression
<?xml version="1.0" encoding="UTF-8"?>
<departments>
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
</department> XML
</departments> s a
<department num="2">
) h a
<department_id>20</department_id> r s
sฺ eฺ
<department_name>Marketing</department_name> m
e uid
</department>
c @
</departments>
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t pathanexpression
l e n
The location
t r shown in the slide is an absolute path that uses the document
a as the
root - node. An absolute location path always starts with a slash (/), and is
context
V
followed nobyna relative location path that comprises the following location steps:
1. Go to the <departments> root element relative to the document root.
2. Go to the first child <department> element of the <departments> node.
3. Go to the <department_id> child element of the <department> node.
If the sample XML document includes more than one <department> element and child
<department_id> element, the XPath expression matches multiple <department_id>
nodes or a set of <department_id> nodes. The result is the entire element from the start to
the end tags, plus the data between them. As shown in the highlighted portion of the slide, the
result includes <department_id>10</department_id> and
<department_id>20</department_id>.

XML Fundamentals 6 - 12
Results of Location Path Expressions

A location path evaluates to a result that is:


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

• Empty
• A single node
• A set of nodes
<?xml version="1.0" encoding="UTF-8"?>
<departments>
<department num="1">
s a
<department_id>10</department_id>
h a
ฺrs) ฺ
<department_name>Administration</department_name>
</department> s
</departments>
@ em uide
<department num="2">
o v ic t G
<department_id>20</department_id> t o r den
<department_name>Marketing</department_name> s
aj s Stu
a ฺ m hi
</department>
t i n e t
</departments>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
When you i n
t evaluate n slocation path expressions, you obtain one of the following results:
l e -t r a
V•a Empty
n o n
• A single node
• A set of nodes (more than one node)
The slide shows a skeleton XML document of departments and various location path
expressions.

XML Fundamentals 6 - 13
Results of Location Path Expressions: Examples
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Absolute
Relative

2as a
s ) h
r
sฺ eฺ
m
e uid
c @
Absolute
r o vi Relative
n t G
a jsto Stude 3
ฺ m i s
n t ina se th
v a le to u
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
The slide
n t na the
ishows n s fe
departments.xml document and various location path expressions
and
V
e resulting
altheir n - tra nodes by using the Oracle JDeveloper XPath Search tool. The relative
no
location paths in the examples assume that the context node is the document root:
• The first example is an absolute location path expression that selects the document root,
and results in the entire document matching the expression.
• The second example shows a relative and an absolute location path that select the root
element node as their result.
• The third example provides a relative and absolute location path selecting the set of
department child elements of the root element.
In the second example, departments is called a location step. In the last example,
departments is the first location step, which is followed by a slash, and then the
department location step, thus forming a compound XPath expression.

XML Fundamentals 6 - 14
Location Steps in XPath Expressions

• Form the address of a specific node, or a set of nodes, to


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

• Contain three parts:


– An axis: Defines the tree relationship between the selected
nodes and the current node
– A node test: Identifies a node within an axis
– Zero or more predicates: Is used to further refine the
selected node-set a
h a s
axis::node-test[predicates]
r s )
• Have both an abbreviated and an unabbreviated m sฺ eฺ
form
@ e uid
• Act like a filter for a selected node or node-set c
vi nt G
r o
• Are separated by / to form a compound a jsto Slocation t ude path
expression n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t a location s path is an important XPath expression that comprises one or more
e n
As identified,
r a n
V al steps
location
o n -trelative to the context node. Each location step is separated by a slash (/) to
form a n
compound location path that:
• Specifies the address or location of a node or a set of nodes (node-set)
• Evaluates to a result that is empty, a single node, or a set of nodes
For example, department/department_name addresses all the department_name child
elements of the department element that is a child of the context node. Each location step
consists of—and is defined—three parts:
• An axis, which specifies the relationship between the selected nodes and the context
node
• A node test, which specifies the node type of the selected nodes
• Zero or more predicates or conditions that use arbitrary expressions to filter the set of
selected nodes
The axis produces a list of nodes that is then filtered by using the node test (which is limited to
filtering based on node type and node name). The predicates are then applied, if present, to
further reduce the node-set.
The location step can be specified in unabbreviated or abbreviated form. The unabbreviated
form is seldom used because it is quite verbose, using full axis names followed by a double
colon and then the node test. Axes names are discussed in the section titled “XPath Axes.”
XML Fundamentals 6 - 15
XPath Axes

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

parent/ancestor

preceding-sibling

self Context node


...
child/descendant
s a
descendant h a
s ฺrs) ฺ
attribute @ em uide
o v ic t G
namespace t o r den
s
aj s Stu
a ฺ m
following-sibling hi
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
Document i n s Axis Names (in Lowercase) Relative to a Context Node in the
t TreeaNode
n
e
al on-t
Diagram r
V n for the children of the context node
• child
• descendant for descendants of the context node
• parent for the parent of the context node (The document root does not have a parent.)
• ancestor for the ancestors of the context node
• following-sibling for all the following siblings of the context node
• preceding-sibling for all the preceding siblings of the context node
• following for all nodes appearing after the context node in document order
• preceding for all nodes that are before the context node in document order
• attribute for the attributes of the context node (empty for nonelement nodes)
• namespace for the namespace nodes of the context node (empty for nonelement nodes)
• self axis for the context node itself
• descendant-or-self for the context node and its descendants
• ancestor-or-self axis for the context node and its ancestors (and the root node)
Note: The following-sibling and preceding-sibling axes are empty for attribute or
namespace context nodes. The descendant, preceding, and following axes exclude
attribute or namespace nodes; following excludes descendants and preceding
excludes ancestors.
XML Fundamentals 6 - 16
XPath Node Test Types

An XPath node test can be:


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

• A node-name such as:


– The element name
– The attribute name if prefixed with an @ symbol
– A qualified name with a namespace prefix
• An asterisk (*)
• The text() type a
h a s
• The processing-instruction() type
s ฺ rs)
• The comment() type e m ideฺ
• The node() type v i c@ t Gu
t o ro den
• Separated by the vertical bar (|) a s
toj form multiple
tu matches
a ฺ m his S
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n
The node t na thatnissfspecified
itype e
as the node test for a location step can be:
l e t r a
on-
V•a Annode-name representing either a qualified or an unqualified element name. If it is
prefixed with an @ symbol, the node-name represents an attribute name.
• An asterisk (*) to match all element or attribute node names
• The text() type to select text nodes
• The processing-instruction() type for processing instruction nodes
• The comment() type for obtaining the comment nodes
• The node() type to match any of the preceding nodes
When the vertical bar (|) is used as a separator, node tests can be combined to create a rule
that matches multiple node types. For example, using /text()|comment() matches either
the text or comment nodes of the root element.
Note: If the nodes in the XML document declare an XML namespace with a prefix, the node
name must be qualified by the namespace prefix in the XPath expression.

XML Fundamentals 6 - 17
Selecting Nodes by Using
Abbreviated XPath Expressions
Expression Description
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

nodename Selects all nodes with the name "nodename"

/ Selects nodes from the root node


// Selects nodes in the document from the current node that match the
selection no matter where they are in the document
. Selects the current node
.. Selects the parent of the current node
@ Selects attributes
s a
Matches any element node ) h a
*
r s
sฺ eฺ
@* Matches any attribute node 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t pathaexpressions
XPath n
l e uses
t r n to select nodes in an XML document. The node is selected by
a oanpath.
following
V - The table in the slide lists some of the commonly used path expressions.
n

XML Fundamentals 6 - 18
Selecting Nodes by Using
Abbreviated XPath Expressions: Examples
Abbreviated XPath Results
Expression
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

departments Selects the department children elements of the context node

employee/salary Selects the salary children elements of the employee child


elements of the context node
* Selects all element children of the context node

. Selects the context node

Selects the parent node of the context node


..
s a
Selects the salary child element from the parent of the context h a
ฺrs) ฺ
../salary
node
s
@id Selects the id attribute of the context node
@ em uide
o v ic childt elements
G of
Selects the num attribute from the department
r n
jsto Stude
department/@num
the context node
a
ฺm oftthe
text() Selects all text node children
i n a h iscontext node
//salary e nt elements
Selects all salary
l u s eof the context node or its descendents
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t path scan be expressed by using abbreviated syntax rather than verbose
e n
Every location
r a n
al on-t syntax. The table in the slide provides some examples of how to select nodes
unabbreviated
V n abbreviated XPath expressions.
by using

XML Fundamentals 6 - 19
Abbreviated and Unabbreviated
XPath Expressions
Abbreviated XPath Unabbreviated XPath Expression
Expression
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

departments child::departments

employee/salary child::employee/child::salary

* child::*

. self::node()

.. parent::node()
s a
../salary parent::node()/child::salary
) h a
r s
sฺ eฺ
@id attribute::id
m
e uid
c @
department/@num child::department/attribute::num
r o vi nt G
text() child::text()
a jsto Stude
//salary a ฺm this
/descendant-or-self::node()/child::salary
n
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s shows the abbreviated and unabbreviated XPath expression examples
t in the aslide
The table
e n r n
al the oprevious
from
V n -t page:
n
• child::departments selects the department child elements of the context node.
• child::employee/child::salary selects the salary child elements of the
employee child elements of the context node.
• child::* selects all element children of the context node.
• self::node() selects the context node.
• parent::node() selects the parent node of the context node.
• parent::node()/child::salary selects the salary child element from the parent
of the context node.
• attribute::id selects the id attribute of the context node.
• child::department/attribute::num selects the num attribute from the
department child elements of the context node.
• child::text() selects all text node children of the context node.
• /descendant-or-self::node()/child::salary selects all salary elements of
the context node or its descendents.
Note: child::node() selects all the children of the context node, whatever their node type.
Using attribute::* selects all the attributes of the context node.

XML Fundamentals 6 - 20
Using abbreviated syntax:
• name selects the name element children of the context node
• / selects the document root, which is always the parent of the document element
• text() selects all text node children of the context node
• comment() selects all the comments node children of the context node
• node() selects all the child node types of the context node
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• @name selects the name attribute of the context node


• @* selects all the attributes of the context node
• . selects the context node
• .. selects the parent of the context node
• ../@lang selects the lang attribute of the parent of the context node
Note: The most important abbreviation allows the child axis and the double colon (::) to be
omitted from a location step. In effect, child is the default axis. a
Other Common XPath Expressions h a s
r s )
• /employee matches the employee element at the document root. sฺ
• department/department_name matches the department_name e m element
i d eฺ as a
direct child of the department element. v i c@ t Gu
t o ro den element as a
• departments//department_id matches the department_id s
aj level. S tu
descendant of the departments element,ฺat
a m any
is
hdescendants
• .//employee matches the employee t
n usi nelement
e t
as of the current node.
p va l e
• department
p
(
_name | department
c e
_id tothe
matches either the department
p _name or the
department_id element
r o vi ens
as children of context node.

j s to le lic
M a rab
n t ina ansfe
le r
Va non-t

XML Fundamentals 6 - 21
Abbreviated XPath Expression Examples:
employees.xml Document
<?xml version="1.0"?>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<!-- Employee data -->


<employees>
<employee employee_id="100">
<name>
<first_name>Steven</first_name>
<last_name>King</last_name>
</name>
<salary>24000</salary>
</employee>
<employee employee_id="101">
s a
<name>
) h a
<first_name>Neena</first_name>
<last_name>Kochhar</last_name> r s
sฺ eฺ
</name> m
e uid
c @
<salary>18000</salary>
r o vi nt G
jsto Stude
</employee>
</employees>
ฺm thisa
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s document in the slide is used in the XPath expression examples on the
The employees.xml
e n r a n
altwo opages.
next
V n -t
n

Oracle Database 12c: Use XML DB 2 - 22


Selecting Nodes by Using
Abbreviated XPath Expressions: Examples
Abbreviated XPath Expression Results
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

//last_name <last_name>King</last_name>
<last_name>Kochhar</last_name>
employees/employee/ Steven
name/first_name/text() Neena

employees/salary <salary>24000</salary>
<salary>18000</salary>
<?xml version="1.0"?>
<!-- Employee data -->
<employees>
s a
<employee employee_id="100">
) h a
<name>
<first_name>Steven</first_name>
<last_name>King</last_name> r s
sฺ eฺ
</name> m
e uid
<salary>24000</salary>
c @
</employee>
<employee employee_id="101">
r o vi nt G
jsto Stude
<name>
<first_name>Neena</first_name>

ฺm this
<last_name>Kochhar</last_name> a
</name>
i n a
nt use
<salary>18000</salary>
</employee>
l e
va e to
</employees>

c (
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t path scan be expressed by using abbreviated syntax rather than verbose
e n
Every location
r a n
al on-t syntax. The table in the slide provides some examples of how to select nodes
unabbreviated
V n abbreviated XPath expressions.
by using
Note: The context node is the document root. <first_name> and <last_name> are
children of <name>. The comment is a child of the document root.

XML Fundamentals 6 - 23
Selecting Nodes by Using
Abbreviated XPath Expressions: Examples
Abbreviated XPath Expression Results
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

employees/employee/@employee_id employee_id="100"
employee_id="101"
comment() <?xml version="1.0"?>

<?xml version="1.0"?>
<!-- Employee data -->
<employees>
s a
<employee employee_id="100">
) h a
<name>
<first_name>Steven</first_name>
<last_name>King</last_name> r s
sฺ eฺ
</name> m
e uid
<salary>24000</salary>
c @
</employee>
<employee employee_id="101">
r o vi nt G
jsto Stude
<name>
<first_name>Neena</first_name>

ฺm this
<last_name>Kochhar</last_name> a
</name>
i n a
nt use
<salary>18000</salary>
</employee>
l e
va e to
</employees>

c (
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t path scan be expressed by using abbreviated syntax rather than verbose
e n
Every location
r a n
al on-t syntax. The table in the slide provides some examples of how to select nodes
unabbreviated
V n abbreviated XPath expressions.
by using

XML Fundamentals 6 - 24
Abbreviated XPath Expression Examples:
departments.xml Document
<?xml version='1.0' encoding='UTF-8'?>
<departments>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id>200</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>1800</location_id>
</department>
s a
<department num="3">
h a
ฺrs) ฺ
<department_id>30</department_id>
<department_name>Purchasing</department_name>
s
em uide
<manager_id>114</manager_id>
<location_id>1700</location_id>
</department> @
ic t G
<department num="4">
o
r denv
<department_id>40</department_id>
s t o
<department_name>Human Resources</department_name>
ฺ m aj s Stu
hi
<manager_id>203</manager_id>
<location_id>2400</location_id>
t i n a t
</department>
l en o us e
</departments>
a
(v se t
i c
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t expression
Each XPath
n n s on the following pages is applied to the departments.xml
e
al othat
document r a
-t is shown in this slide.
V n n

Oracle Database 12c: Use XML DB 2 - 25


XPath Predicates

• Are Boolean expressions in brackets that are evaluated for


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

each node in the node-set:


//department[department_name="Administration"]

• With a numeric result, are converted to a Boolean by using s a


h a
the position() function: ฺrs)
/departments/department[2]
e ms ideฺ
/departments/department[position()=2]
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t in anaXPath
A predicate n s expression is a condition contained in brackets that appears after the
e r
al testoofna-tlocation step. The predicate is evaluated as a Boolean expression for each
node
V
node inna node-set. If the condition is true, the node is included; otherwise, the node is
excluded from the results. Predicates, such as query expressions, filter the results.
• The first example in the slide returns the descendant <department> elements that
contain a <department_name> element whose text node is Administration.
• The second example in the slide specifies a numeric value, which can be computed.
Numeric results are converted to a Boolean expression by using an equality comparison
of the value to the return value of the position() node-set function. The position()
function determines the proximity position of a member node in a node-set. The
proximity position is a numbered position of the node in the sequence in which it
appears in a node-set. Proximity position numbers start at 1.

Oracle Database 12c: Use XML DB 2 - 26


XPath Predicates

• May be provided with each location step:


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

//department[@num<3]/department_id[.="10"]

• May be combined with logical operators: a


h a s
//department[@num>2 and @num<=4]/department_name
r s )
m sฺ eฺ
@ e uid
c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a s in the slide returns the department_id node that has a value of 10
t first example
• The
e n
l that r a n
Va and o n -tis the child of the department element with the num attribute value of 3.
• nThe second example in the slide uses the and logical operator (in lowercase) to
combine predicate expressions, which can be written by using an implied and operation,
as in the following example:
//department[@num>2][@num<=4]/department_name
You can combine predicates with an or logical operator as follows:
//department[department_id="10" or (@num>1 and @num<4)]

Oracle Database 12c: Use XML DB 2 - 27


Operators in XPath Expressions in
Precedence Order
Expression Description
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Comparison operators <=, <, >=, >, =, !=


Logical operators not, and, or

Mathematical operators *, div, mod, +, -

Literal strings Literal strings are quoted with single or double quotation marks.

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
The slide i n s
t lists theanoperators that can be used in predicates and XPath expressions. The
l e
a onoperators
comparison - t r include:
V n
• Less than or equal to (<=), or less than (<)
• Greater than or equal to (>=), or greater than (>)
• Equal to (=), and not equal to (!=)
• A combination of multiple predicates by using a lowercase and, lowercase or, or both
The mathematical operators include:
• The asterisk (*) for multiplication
• The lowercase div for division
• The lowercase mod for the remainder of a division
• The plus sign (+) for addition
• The minus sign (-) for subtraction
When using XPath in XSLT Style Sheet documents, the “<” character must be entered by
using its entity representation, that is, using &lt;. In addition, literal strings in expressions,
which are usually delimited by single or double quotation marks, must use entity names to
enter quotation marks, that is, either &quot; or &apos; as appropriate.
Predicates can be combined as follows: [predicate1][predicate2]. This is the same as
using an and operation: [predicate1 and predicate2].
XML Fundamentals 6 - 28
Lesson Agenda

• XML Path Language (XPath)


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

• XPath expressions
– Location path expression
– Location steps
– XPath axes
– Node test types
– Abbreviated and unabbreviated XPath expressions s a
h a
ฺrs) ฺ
– XPath predicates
s
• XPath functions
@ em uide
• Testing XPath expressions 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 6 - 29
XPath Functions

• Are used in predicates or expressions:


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

function-name(arguments,...)

• Have a name followed by parentheses, and zero or more


arguments, as in the following example:
position()

s a
) h a
• May return one of the following four types: r s
sฺ eฺ
– Boolean
m
e uid
c @
– Number r o vi nt G
– Node-set a jsto Stude
n a ฺm this
– String i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na sused in predicates and expressions to yield one of the following four
XPath n
e functions
r n
are
a
V al types:
result
o n -t
• AnBoolean function can operate on Boolean expressions to return a true or false
result.
• A number function is used to operate on numbers, and returns floating-point values.
• A node-set function operates on a set of nodes selected by a location path expression,
and returns a result, usually a numeric value.
• A string function operates on Unicode strings values.
The position() function is an example of a node-set function. Functions in each of these
categories are discussed on the next few pages.
Note: XPath 2.0 provides more functions than XPath 1.0. XPath 2.0 shares its function library
with XQuery 1.0. For more information, see the lesson titled “Working with XQuery.”

XML Fundamentals 6 - 30
Boolean Functions

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

boolean(o) Converts to a Boolean

not(b) Returns true if its argument is false and vice versa

true() Returns true

false() Returns false

Find any department that does not have a manager.


s a
/departments/department[boolean(not(manager_id))]
) h a
r s
sฺ eฺ
m
e uid
c @
The partial output
r o vi shows n t G
that
... departments
a j s to 14tuthrough de 27
domnot have aSmanager.
n a ฺ this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tfunctions s
Boolean
l e n t r a n
manipulate expressions to return either a true or false value. The
V a on-function converts its argument to a Boolean value and returns a result, where:
boolean()
• n
Nonzero numbers are converted to true, and a 0 value returns false
• A node-set is false if empty; otherwise, it is true
• Zero-length strings return false; otherwise, a true value is returned, as in the
following examples:
- boolean('false') returns true.
- boolean() returns false.
The not() function returns the reverse of its Boolean argument. That is, it returns true
when its argument is false, and false if its argument is true. For example,
not(@salary>10000) returns true if the salary is less than 10,000.
The true() function always returns true.
The false() function always returns false.
The example in the slide locates the <department> elements that do not contain any
<manager_id> child elements. Although the boolean() function is not required in the
example, it shows that XPath functions may be nested within the predicate expression.
Note: The example in the slide uses data from the rows of the DEPARTMENTS table in XML
format as seen in the partial results.

XML Fundamentals 6 - 31
Number Functions

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

number(o) Converts the argument to a number

sum(ns) Returns the sum for each node in a node-set

ceiling(n) Returns the smallest integer that is greater than a given number

floor(n) Returns the largest integer that is less than a given number

round(n) Returns the closest integer to a number

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t functions s can be used to manipulate numeric data. The number functions are the
e n
The number
r a n
al on-t
following:
V
• Then number() function converts its argument to a number. For example,
number('6.75') returns the numeric value 6.75.
If the argument is a Boolean expression, true is converted to a value of 1 and false
returns a value of 0. If the argument is not convertible to a number, this function returns
NaN (not a number).
• The sum() function accepts a node-set as an argument and returns the result of the
sum of the nodes. Each node is first converted to a string value, and then to a number
before being summed up.
• The ceiling() function returns the smallest integer that is greater than its argument.
For example, ceiling(2.35) returns a value of 3.
• The floor() function returns the largest integer that is less than its argument. For
example, floor(2.35) returns a value of 2.
• The round() function returns the number rounded to its nearest integer. For example,
round(2.35) returns the value 2, and round(2.65) returns the value 3.

XML Fundamentals 6 - 32
Node-Set Functions

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

last() Returns the context size

position() Returns the context position

id(o) Returns elements by their unique ID

count(ns) Returns the number of nodes in a node-set

local-name(ns) Returns the local node name (unqualified)

s a
//department[position()=1]
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
//department[last()]/department_name sto
a j t u de
a ฺ m his S
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t inafunctions
The node-set n s fethat manipulate and return information about a set of nodes include:
le last()
V•a The n - tra function that returns the number of nodes in the context node-set
• Thenoposition() function that returns the context position. For example,
//employee[position()=2] returns the second employee descendant node.
• The id() function that accepts a string containing one or more space-separated ID
values as the argument. It returns a node-set containing all nodes in the document that
have those ID values, which are declared as an ID attribute type in the DTD; that is, the
attribute is not named but is declared as a unique ID attribute type.
• The count() function is similar to last(). It gives a count of the number of nodes in
its node-set argument. For example, count(//employee) gives you the number of
employee elements in the document.
• The local-name() function returns the local name, without a namespace prefix, of the
first node in the node-set argument. If no argument is provided, the name of the context
node is returned.
The first example in the slide locates the first department element by its position in the node-
set. This is equivalent to //department[1]. The second example locates the last
department in the node-set, which can be written as:
//department[count(//department)]/department_name
Note: The node-set functions that are not listed in the slide are:
• namespace-uri(), which returns the namespace URI string of a node
• name(), which returns the qualified name of a node
XML Fundamentals 6 - 33
String Functions

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

string(o) Converts an object to a string

concat(s,s,...) Concatenates its arguments

substring(s,n,n) Returns a substring of a string argument from a start position to


a length

contains(s,s) Accepts two arguments and returns true if the first argument
contains the second

starts-with(s,s) Takes two arguments and returns true if the first argument starts s a
with the second; otherwise, returns false h a
s ฺ r s)
string-length(s) Returns the length of a string
e m ideฺ
Return the department elements with the department_name v i c@ tchild
G u
nodes
t o r o en
that start with the uppercase letter A.
a js Stud
n a ฺm this
n t i
/departments/department[starts-with(department_name,
e
'A')]

v a le to us
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
t ina afunction
The string()
n n sfe converts any type of data to a string.
e
alconcat() - tr function returns the concatenation of its arguments. For example,
V
The
no n
concat('This is ','cool') returns the string 'This is cool'.
The substring() function takes three arguments: the string to evaluate, a start position,
and a length. If the length is omitted, the return string contains all the characters from the start
position to the end of the evaluated string, as in the following examples:
• substring('employee',1,3) returns the string 'emp'.
• substring('Administration',6) returns 'istration'.
The contains() function searches for the presence of the second argument within the first.
If it is present, it returns true; otherwise, it returns false:
• contains('Steve King','ing') is true.
• contains('Neena Kocchar','charm') is false.
The starts-with() function returns true if the first argument starts with the second;
otherwise, it returns false. For example, starts-with ('Administration', 'Adm')
returns true.
The string-length() function returns the length of its argument as an integer value. For
example, string-length('King') returns a value of 4.
The example in the slide returns the <department> elements with the
<department_name> child nodes that start with the uppercase letter A.

XML Fundamentals 6 - 34
XSLT and XPath

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

• Transforms XML into plain text, HTML, or XML


• Specifies transformation rules in elements with attributes
that use XPath expressions
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="//department_name">
s a
<html>
) h a
<body>
r s
sฺ eฺ
<p><xsl:value-of select="."/></p>
m
e uid
<xsl:apply-templates/>
c @
</body>
r o vi nt G
</html>
</xsl:template> a jsto Stude
n a ฺm this
<xsl:template match="*/text()"/>
i
nt use
</xsl:stylesheet>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tStylesheet sLanguage (XSL) document is an XML document with elements that define
An XML
e n r a n
al on-t rules. The slide shows sample XSL document elements such as:
transformation
V n
• The <xsl:template> element with a match attribute containing an XPath expression
that defines the set of nodes to which the template rule applies
• The <xsl:value-of> element with the select attribute containing an XPath
expression
• The <xsl:value-of> element, which outputs the text value of the specified node
relative to the context node that matches its template XPath expression
The <xsl:template> element tells the XSLT processor how to locate specific element
nodes in the input document, and provides the rules to create the output data as the contents
specified between the start and end tags of the <xsl:template> element. In the example,
you search for department_name elements and use the current node (.) to output the value
of department_name.

XML Fundamentals 6 - 35
Note: The XSLT Specification states that XSLT processors must provide a default template
rule that causes text nodes to be output if no matching template rules exist. Suppress the
output of unprocessed text nodes by including, at the end of the XSLT Style Sheet, a template
rule that matches all the text nodes that do not output data. For example, as shown in the
slide, use:
<xsl:template match="*/text()"/>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Note: The example in the slide provides a context where XPath expressions can be used.
XSLT is covered in detail in the lesson titled “Using XSL Transformations to Transform XML
Documents.”

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 Fundamentals 6 - 36
Lesson Agenda

• XML Path Language (XPath)


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

• XPath expressions
– Location path expression
– Location steps
– XPath axes
– Node test types
– Abbreviated and unabbreviated XPath expressions s a
h a
ฺrs) ฺ
– XPath predicates
s
• XPath functions
@ em uide
• Testing XPath expressions 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 6 - 37
Testing XPath Expressions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

1 2 s a
h a
s ฺrs) ฺ
@ em uide
o v ic 3 t G
t o r den
To view the results of XPath s
aj s Stu
ฺ m
expressions, use Search >
XPath Search in JDeveloper. ntin
a
e thi
v a le to us
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
a sfe
t
To viewntheinresults
a nof XPath expressions, use:
l e t r
V•a The n n- Search option in JDeveloper
oXPath
• An XSL stylesheet to transform an XML document that can be viewed in an XML-
enabled browser
The screenshots in the slide show the following steps to use JDeveloper’s XPath Search
option:
1. Open an XML document in JDeveloper, and then select Search > XPath Search.
2. In the XPath Search – Log pane that appears, specify the XPath expression in the
Expression field as shown in the slide example, and then click OK.
3. In the Results section of the Search – Log pane, view the results of applying an XPath
expression to your XML document.
Note: In the XPath Search – Log pane, XPath version 2.0 is selected by default. In the slide
example, XPath 1.0 was selected from the drop-down list.

XML Fundamentals 6 - 38
XPath 2.0

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

• Is a data model based on sequences


• Supports XML Schema data types
• Provides many string and numeric functions, date and time
functions, regular expression functions, and string
manipulation functions
• Shares its function library with XQuery 1.0 s a
h a
• Supports conditional logic ฺrs)
e ms 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
XPath n i n s is an extension of XPath 1.0. Every XPath 2.0 expression returns a
t syntactically
2.0 n
e r
al onA-sequence
sequence. t a is an ordered collection of items. The items can be either nodes from
V
an XMLndocument or atomic values. An atomic value is an instance of one of the built-in data
types defined by XML Schema such as integers, strings, dates, and decimals.
XPath 2.0 serves as the foundation for XSLT 2.0 and XQuery 1.0. Both XSLT 2.0 and XQuery
1.0 languages use XPath 2.0 as their path language.
XPath 2.0 uses the same nodes as XPath 1.0, but the root node is called the document node
in XPath 2.0.
The data model underlying XPath 2.0 is different from the XPath 1.0 data model. The XPath
1.0 data model is based on node-sets, whereas the XPath 2.0 data model is based on
sequences.
Notes
• XPath 2.0 and XQuery 1.0 share the same XML Schema–based data model. For more
information, see the lesson titled “Working with XQuery.”
• Every valid XPath 1.0 syntax is valid in XPath 2.0. Like XPath 1.0, in XPath 2.0, a path
consists of location steps separated by a slash (/). But XPath 2.0 location steps identify
items in a sequence instead of nodes in a tree.
• In XPath 1.0, a node-set cannot have duplicates, whereas in XPath 2.0, sequences can
contain duplicates.
XML Fundamentals 6 - 39
Note: To declare a sequence in XPath, separate the items with commas and enclose the
whole sequence in parentheses:
('you', 'I', 'we'), ('they', 'we')
XPath 1.0 supports four expression types. But, with support for XML Schema primitive types
in XPath 2.0, you have the advantage of using more simple types.
XPath 2.0 adds many functions to perform tasks such as string manipulation and handling
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

date and time. For example, the current-date() and current-time() functions return
the current date and current time in a standard format, respectively.
XPath 2.0 supports conditional logic:
if(/po:PurchaseOrder/po:Seller = 'ABC') then 'Yes' else 'NO'
The expression in parentheses first evaluates to true or false. Depending on this
evaluation result, the expression returns the then or else section.
Note: You learn how to use some of the XPath 2.0 functions in the lesson titled “Working with
XQuery.” 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 Fundamentals 6 - 40
Quiz

XPath is primarily used to address parts (nodes) of an XML


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

document. XPath models an XML document as a tree of nodes


and expresses a navigation path through the hierarchical
structure of an XML document.
a. True
b. False
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t
Answer:na i n n s
l e r a
Va non-t

XML Fundamentals 6 - 41
Summary

In this lesson, you should have learned how to:


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

• Describe XPath and its document model


• Construct a location path with location steps
• Use axes, node-tests, and predicates in XPath
expressions
• Use XPath functions in predicates
• Use the XPath Search feature in JDeveloper s a
) h a
• Describe XPath 2.0 features 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t you s the XPath syntax that is used to form an expression that addresses
e n
In this lesson,
r a nlearned
al oroset
a node
V n -tof nodes (node-set) in an XML document.
n document model comprises an XML document tree, which begins at the virtual
The XPath
document root that contains everything in an XML document, including:
• The XML header
• The root element node and its children
• Comments
• Processing instructions
You can construct XPath expressions to navigate to any node in the XML document tree. The
most important and common XPath expression is called a location path. The location path is
made up of one or more location steps separated by a slash (/). Each location step, which
can be expressed in an abbreviated or unabbreviated format, has three parts:
• The axis that defines the direction of navigation relative to a context node
• The node-test that defines the set of nodes of interest
• Zero or more predicates, which filter the set of nodes processed
XPath is meaningful in processing contexts such as XSLT and XPointer. The XPath Search
feature in JDeveloper allows you to view the results of applying an XPath expression on your
XML document.
XML Fundamentals 6 - 42
Practice 6: Overview

This practice covers testing XPath expressions by using the


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

XPath Search feature in 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 6 - 43
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ฺ

Transforming XML Documents


with XSL Transformations

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Describe XSL and XSL Transformations (XSLT)


• Transform an XML document by using XSLT
• Use key XSLT elements
• Create, apply, and call templates
• Sort and filter an XML document
• Use parameters with templates s a
h a
• Perform an XSLT process in JDeveloper s ฺrs) ฺ
• Use the oraxsl command-line utility @ em uide
ic o v t G
r n
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t you s about Extensible Stylesheet Language (XSL) and its rules and syntax
l e n
In this lesson,
r a nlearn
foratransforming
V o n -t XML documents.
n
References
• http://www.topxml.com/xsl/XSLTRef.asp
• Beginning XML, Fourth Edition (Hunter et al., Wrox Publications)

XML Fundamentals 7 - 2
Lesson Agenda

• Extensible Stylesheet Language (XSL)


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

– Using an XSLT Style Sheet with an XML document


– Viewing the transformed document
• Creating and applying template rules
• Sorting an XML document
• Conditional processing
• Using parameters with templates s a
h a
• Performing the XSLT process s ฺrs) ฺ
– Using JDeveloper @ em uide
– Using the oraxsl command-line utility 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 7 - 3
What Is XSL?

Extensible Stylesheet Language (XSL) has two parts:


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

• XSL Transformations (XSLT)


• XSL Formatting Objects (XSL-FO)

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
XSL 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i
XML documents n n s structure and, unlike HTML, do not provide formatting or display
have
e
al on-XSL
instructions. r a
t adds the capability of transforming an XML document into another
V n containing XML with a different structure, or into an HTML document with
document
formatting tags.
XSL has the following parts:
• XSL Transformations (XSLT) is an XML application that processes the rules contained
in an XSLT Style Sheet. The rules in an XSLT Style Sheet can be applied to any XML
document.
• XSL Formatting Objects (XSL-FO) is an XML application that is used to describe the
precise layout of text on a page. XSLT is used to generate XSL-FO elements that are
output to another XML document. Additional processing is needed to create the final
output form such as a portable document format (PDF) document.
This lesson focuses on XSLT. The screenshot in the slide depicts an XML document and an
XSLT Style Sheet document being processed by an XSL Processor. The XSL Processor is an
XML application that applies transformation rules to the XML document as specified in the
XSL document and produces a new document as the result.
Note: XSLT is available in two versions. XSLT 1.0 is more widely used and implemented.
XSLT 2.0 is a revised version of the XSLT 1.0 recommendation. For more information about
XSLT 2.0, see the section titled “XSLT 2.0.”
XML Fundamentals 7 - 4
XSL Transformations

Input XML XSL Output XML


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

document Processor document

XSL
stylesheet s a
) h a
r
ฺ ฺs
sOutput
m
e uide
Matching
<?xml version="1.0"?>
i c @ G data
<xsl:stylesheet ...>
o v n t
rule
<xsl:template match="XPath"/>
j s t or ude
<!-- output information
m a --> S t
</xsl:template> a
n ฺ t h is
t i e
</xsl:stylesheet>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n
XSL Transformations n s (XSLT) is an XML application, or XSL Processor, that processes rules
e
alspecify r a
-t to transform one XML document into another XML document. The rules are
that
V o nhow
n in an XSLT document called an XSLT Style Sheet, which itself is an XML document
contained
and must be well-formed.
The slide shows that an XSLT Style Sheet contains one or more template rules. Each
template rule has two parts:
• A match pattern, which is specified as an XPath expression in an attribute of an
<xsl:template> element
• Template data, appearing between the start and end tags of the <xsl:template>
element, which contains the output information. The template data typically contains
XML and HTML elements mixed with the XSLT rules.
The XSL Processor compares the elements in the XML input document with the template rule
pattern in the XSLT Style Sheet, and writes the template data for matching rules to the output
tree, typically, another XML document. Essentially, the transformation process operates on
the tree data structure of a source (input) XML document and produces a tree of nodes as its
output.
Notes
• XSL-FO elements are output elements found in <xsl:template> rules.
• XSLT 2.0 enables you to transform textual data that is not well-formed XML. XSLT 2.0
enables multiple-output documents.
XML Fundamentals 7 - 5
XSLT Style Sheet

An XSLT Style Sheet is an XML document containing:


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

• An <xsl:stylesheet> root element that declares:


– The xsl namespace prefix
– The http://www.w3.org/1999/XSL/Transform
mandatory namespace URI
• One or more <xsl:template> elements and other XSL
elements that define transformation rules a
h a s
<?xml version="1.0"?>
r s )
<xsl:stylesheet version="1.0"
m sฺ eฺ
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
@ e uid
... c
vi nt G
r o
jsto Stude
<xsl:template match="/"> ... </xsl:template>
<xsl:template match="..."> ... </xsl:template>
</xsl:stylesheet> ฺm thisa
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tStyle Sheet s is an XML document that uses elements from the XSLT vocabulary to
An XSLT
e n r a n
V al otransformation
describe n -t rules. The document element of every XSLT Style Sheet is the
n
<xsl:stylesheet> element, whose content is a set of one or more XSL elements that
define the template rules describing the transformation to be performed.
Note: <xsl:transform> is a synonym for <xsl:stylesheet>.
The <xsl:stylesheet> element declares the mandatory namespace attribute
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" that is used to qualify the
elements in an XSLT Style Sheet. If you do not provide this exact namespace prefix and URI,
the XSLT Processor simply ignores the rules in <xsl:template>, <xsl:for-each>,
<xsl:value-of>, and other XSL elements that are qualified with the xsl prefix. Therefore,
it does not recognize them as XSLT instructions.
Each stylesheet rule, called a template, contains a match pattern that is specified as an XPath
expression that is compared against the nodes in the source XML document. An XSL
template rule is represented by an <xsl:template> element with a match attribute that is
assigned a "pattern" string containing an XPath expression.
Note: To use XSLT 2.0, you must specify 2.0 in the version attribute of the
<xsl:stylesheet> element.

XML Fundamentals 7 - 6
XSLT Style Sheet: Example

<?xml version="1.0"?> 1
<xsl:stylesheet version ="1.0"
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 2
<xsl:template match="/"> 3
<html>
<body>
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<xsl:apply-templates/>
</table>
</body>
</html> 5 4 s a
</xsl:template>
) h a
r s
sฺ eฺ
<xsl:template match="employee"> m
e uid
<tr>
c @
<td><xsl:value-of select="employee_id"/></td>
r o vi nt G
sto Stude
<td><xsl:value-of select="last_name"/></td>
<td><xsl:value-of select="salary"/></td>
a j
</tr>
n a ฺm this 6
</xsl:template> i
nt use
</xsl:stylesheet>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t document sin the example, called a style sheet, shows the following components:
e n
The XSLT
l standard r a n
V1.a The o n -t XML document declaration (because a stylesheet is an XML document)
n
2. The mandatory XML namespace declaration that uses the xsl namespace prefix, and
the URI http://www.w3.org/1999/XSL/Transform namespace
3. The <xsl:template> rule that matches the root of the source XML document
4. The template data that contains the HTML tags to be written to the output document
5. The <xsl:apply-templates> element that instructs the XSLT Processor to apply
template rules, if any, to the child elements of the root in the source document. The
<xsl:apply-templates> element is replaced by any output generated for matching
child element template rules.
6. The <xsl:template> rule for matching the <employee> elements in the source
document with the template data containing the HTML table row (<tr>...</tr>) tags
that enclose three table data (<td>...</td>) tag pairs. Each table data tag pair
encloses an <xsl:value-of select="..."/> rule that inserts the value of the
source document element selected by the XPath expression, which is typically the name
of the source document element.
The XML document that uses this XSLT Style Sheet is shown in the slide titled “Viewing the
Transformed Document.”

XML Fundamentals 7 - 7
Using an XSLT Style Sheet
with an XML Document
An XML document uses an <?xml-stylesheet...?>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

processing instruction:
• After the XML declaration and before the root element of
the XML document
• Containing two pseudoattributes:
– The type value is the MIME type for the stylesheet.
– The href value is the URL of the source XSLT Style Sheet
s a
document. ) h a
s ฺrs ฺ
<?xml version="1.0"?>
@ em uide
<?xml-stylesheet type="text/xsl" href="emp.xsl"?>
o v ic t G
<employees> t o r den
. . . s
aj s Stu
a ฺ m hi
</employees>
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
An XSLT
n i n
tStyle Sheetn s is applied to an XML document by adding the xml-stylesheet
e r
al oninstruction
processing -t a in the XML document prologue that is before the document root
V n The xml-stylesheet processing instruction provides two pseudoattributes:
element.
• The type value is the Multipurpose Internet Mail Extensions (MIME) media type for the
type of stylesheet. For XSL stylesheets, the type value must be either
application/xml or text/xml. However, the XSL Processor in Internet Explorer
does not recognize application/xml as the type value, but accepts a value of
text/xsl as shown in the example in the slide. A MIME media type value of
text/css is applicable to a cascading style sheet (CSS).
• The href value is a URI string referencing the name of the stylesheet document.
The xml-stylesheet processing instruction is required for a browser to invoke its XSLT
Processor to apply the style sheet to the XML document tree.
Note: The type pseudoattribute value of text/xsl, which is a fabrication of Microsoft, is not
a formally registered MIME type. However, this course uses the text/xsl MIME type to
make it easier to observe the results of XSLT. The processing instruction is not required by
the Oracle command-line XSLT Processor. Therefore, the MIME type used is irrelevant to the
Oracle command-line XSLT Processor.

XML Fundamentals 7 - 8
Viewing the Transformed XML Document

Perform one of the following:


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

• Open the XML document in the web browser.


• View the oraxsl command-line processor output.
• Use JDeveloper.
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="emp.xsl"?>
<employees>
<employee>
s a
<employee_id>100</employee_id>
) h a
<last_name>King</last_name>
<salary>24000</salary>
r s
sฺ eฺ
</employee> m
e uid
c @
<employee>
r o vi nt G
jsto Stude
<employee_id>101</employee_id>
<last_name>Kochhar</last_name>
<salary>18000</salary>
ฺm thisa
i n a
nt use
</employee>
</employees>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t showsathe ssource XML document and the result of applying the XSLT Style Sheet
n
The slide
e r n
al the osection
from
V n -t titled “XSLT Style Sheet: Example.” When you open the XML document with
n Internet Explorer, it automatically applies the XSLT Style Sheet specified in the XML
Microsoft
document processing instruction and renders the result shown.
Alternatively, you can use the oraxsl command-line interface to apply an XSLT Style Sheet
to an XML document. To invoke oraxsl, use:
Export
CLASSPATH=/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2/xml
parserv2.jar
java oracle.xml.parser.v2.oraxsl f.xml f.xsl f.html
The oraxsl parameters in order are:
1. The source XML document (f.xml)
2. The XSLT Style Sheet document (f.xsl)
3. The output file name (f.html). If this is omitted, the output appears on standard output.
Note: If the result file contains HTML, you can view it in a browser. The oraxsl utility is
discussed in the section titled “Using the oraxsl Utility.”
You learn how to view the transformed document by using JDeveloper in the section titled
“Performing the XSLT Process in JDeveloper.”
XML Fundamentals 7 - 9
Creating an XSL File in JDeveloper

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

s a
4 ) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G 3
a jsto Stude
5
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t provides sthe XML schema–aware XML editor, which you can use to create, check
l e n
JDeveloper
r a n
theasyntax
V of,-tand validate an XSL stylesheet document.
n
o
n an XSL stylesheet in JDeveloper:
To create
1. Right-click an existing project node and select New > From Gallery (or select File >
New > From Gallery from the menu).
2. In the New Gallery window, click XML under the General node in the Categories
section, and then double-click XSL Style Sheet in the Items section.
3. When the Create XSL File window is displayed, enter a file name in the File Name field
and click OK.
4. JDeveloper creates a skeleton XSL file containing:
- An XML declaration
- The <xsl:stylesheet> root element with its required XML namespace prefix
xsl and the URI http://www.w3.org/1999/XSL/Transform
- An empty <xsl:template> element with a rule matching the document root
5. Start editing the XSLT document by making use of the code-insight features of the XML
schema–aware XML editor. The screenshot shows code-insight providing a list of
suitable elements after entering the text <xsl: inside the <xsl:template> element.

XML Fundamentals 7 - 10
Lesson Agenda

• Extensible Stylesheet Language (XSL)


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

– Using an XSLT Style Sheet with an XML document


– Viewing the transformed document
• Creating and applying template rules
• Sorting an XML document
• Conditional processing
• Using parameters with templates s a
h a
• Performing the XSLT process s ฺrs) ฺ
– Using JDeveloper @ em uide
– Using the oraxsl command-line utility 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 7 - 11
Creating Template Rules

A template rule:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Is created by using the <xsl:template> elements with:


– A match attribute with an XPath pattern value
– The output template containing formatting instructions to
produce a result tree
<xsl:template match="pattern">
output-template
</xsl:template>
s a
a
• Is applied when a node in the XML input documentฺrs) h
matches the XPath pattern in the rule e ms ideฺ
v i c@ t Gu
<xsl:template match="/">
t o ro den
A simple text string
s
aj s Stu
</xsl:template>
a ฺ m hi
t i n e t
Match ppattern determines a l encontext
its o us node.
v
c ( nse t
v i
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n
Templatet na determine
irules n s fe what the XSLT Processor outputs when an input node matches the
ale oEach
template.
a
-trtemplate rule is defined by the <xsl:template> element. The
V n n
<xsl:template> element contains:
• A match attribute that specifies the rule pattern, as an XPath expression, to recognize
nodes from the XML input document
• The output template between the start and end <xsl:template> tags. The output
template contains instructions for formatting the result document tree. The output
template can be empty to suppress the output for a node, or include any or a
combination of text, XML, HTML, and XSLT elements with additional instructions for the
XSLT Processor and XSL-FO elements.
The example in the slide transforms the entire XML input document into a simple text string
with the template rule matching the document root, and the output document containing the
text: A simple text string. The rule in the slide processes the root element and not any
other nodes from the input document. To process the child nodes of the document root, its
template rule must include an <xsl:apply-templates/> rule.
The general principle used by an XSLT Processor is that when a template rule matches an
input node, the output template instructions are copied to the resulting output tree.
Note: The matching node becomes the context node for the template rule.

XML Fundamentals 7 - 12
Obtaining Input Text with <xsl:value-of>

The <xsl:value-of> element:


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

• Has the select attribute that contains an expression,


typically an XPath expression
• Inserts the string (text) value of the expression
• Is used in an <xsl:template> element
<xsl:template match="region">
<xsl:value-of select="."/>
s a
</xsl:template>
) h a
r s
sฺ eฺ
m
e uid
<region num="1">
i c @ G
<id>1</id> r o vEurope n t
sto Stude
1
<name>Europe</name>
a j
</region>
n a ฺm this
n t i s e Output document
XML input document e
al to u
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t a sfe
inwant
Often, nyou atoninsert content from the input document into output results, unlike some of
l e t r
V
element on-examples
theaprevious
ninserts
that replace the input with a literal string. The <xsl:value-of>
into the output tree a string or text value that is calculated from the XPath
expression specified in the select attribute. Suppose that you want to output:
• Element text: <xsl:value-of select="name"/> produces Europe.
• Attribute values: <xsl:value-of select="@num"/> produces a 1.
• Numeric or Boolean expressions:
<xsl:value-of select="@num + 2"/> produces a 3, or <xsl:value-of
select="boolean(id)"/> produces the string true.
The <xsl:value-of> element syntax is:
<xsl:value-of select=“expression”
disable-output-escaping=“yes|no”/>
The example in the slide uses the current node (.) XPath expression in the template
matching nodes with the name region from the XML input document. The result that is
written to the output document contains the concatenated text value of the <region>
element and its children. The disable-output-escaping attribute values are:
• yes, which outputs an & character for an &amp; in the input, and > for an &lt;
• no (default), which outputs &amp; and &lt; as themselves (if in the input text)

XML Fundamentals 7 - 13
Applying Template Rules

Use <xsl:apply-templates/> to recursively process


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

children of the context (current) node. 1


<xsl:template match="/">
Document root Document root
</xsl:template>
/
<xsl:template match="/"> Output
Document root Document
A <xsl:apply-templates/> 2
B </xsl:template> s a
) h a
B D
<xsl:template match="A">
Root element A Document r s
sฺ eฺ A
root
C <xsl:apply-templates/> Root m
e uid
element
D </xsl:template>
i c @
Element GB
<xsl:template match="B"> rov Element
C
n t
Element B j s to tude B
</xsl:template>
ฺ m a sS
Input XML
t i n a
e t hi
document
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t each
As discussed, n srule is called a template because the literal text, elements, and attributes
in a
e r a
l bodynof-tthe rule act as a blueprint for constructing a part of the result tree. The XSLT
the
V no constructs the content of a rule’s template in the result tree whenever it processes
Processor
a source node that matches the rule’s pattern.
To begin, the XSLT Processor reads the source document from top to bottom, starting at the
document root, working down to the children by using a preorder traversal. Templates are
activated in the order in which they match the elements encountered during the traversal.
Therefore, a template for a parent node is activated before a template matching its children.
However, to activate templates for child elements, the <xsl:apply-templates/> XSLT
element must be included in the template of the parent node.
Notes
• The <xsl:apply-templates/> element is an instruction to the XSLT Processor to
recursively process all the children of the context node. Without this rule, child elements
may not be processed unless the default template rule is invoked. Default template rules
are discussed in the section titled “Default Template Rules.”
• The data output from the child node templates is inserted into the location where the
<xsl:apply-templates/> appears.

XML Fundamentals 7 - 14
The slide illustrates the same XML input document tree being processed by two different
XSLT Style Sheets:
1. In the first example, the resulting output document contains only the literal text,
Document root, which is contained in the template rule for the document root. In this
example, the <xsl:apply-templates/> element is not used; therefore, no other
nodes from the input document tree are processed. In addition, the template rule for the
document root overrides the built-in default template.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

2. In the second example, the output document contains data from each of the templates in
the XSLT Style Sheet. All templates are invoked because the <xsl:apply-
templates/> element is used in the templates for templates matching most of the
parent nodes in the tree. However, the child elements of node B (that is, C and D) are not
processed without including the </xsl:apply-templates/> in the template rule
for B.
The following sequence of events occurs in the second example in the slide:
1. XSLT Processor reads the XML input document starting with the document root node.
s a
h a
ฺrs) ฺ
2. It finds the template rule for the document root, with match="/", copies the template
s
data to the output tree, and writes the literal text “Document root” to the output tree
first.
@ em uide
v ic t G
3. It encounters the <xsl:apply-templates/> element inside the document root
o
template rule. t o r den
s
aj s Stu
ฺ m
4. It fetches the document root’s child element, root element A, from the XML input
a hi
document. t i n e t
a l en o us
5. It finds the matching template rule for node A, with match="A", and copies its template
i c (v se t
data to the output tree, appending the text “Root element A” to the result.
t o rov licen
6. The XSLT Processor encounters another <xsl:apply-templates/> element in the
a js ble
template rule for node A.
a M fera
i n s
7. The first child, element B, of parent node A is processed by the template rule, with
n t n
l e r a
match="B", appending the text “Element B” to the output results.
Va non-t
8. Because there is no more to process in the template rule for B, processing continues
with the next child node B of parent element A, appending another line of the text
“Element B” to the output results.
9. Because there are no more direct child elements of node A, the processing continues in
the context of the rule for element A, which has no more to process either.
10. Processing returns to the context of the template for the document root, the parent of
node A, which also has no more template data to be processed.
11. The result document tree is copied to the output destination, possibly the browser
display window, or a file containing the result data.

XML Fundamentals 7 - 15
Controlling Template Activation Order

<xsl:apply-templates> activates one of the following:


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

• Templates matching child nodes by default


• Templates matching its optional select attribute

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


<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
s a
<xsl:apply-templates select="//region"/>
) h a
</xsl:template>
r s
sฺ eฺ
<xsl:template match="regions">
m
e uid
<h1>Regions</h1><xsl:apply-templates/>
c @
</xsl:template>
r o vi nt G
<xsl:template match="region">
a jsto Stude
<p><xsl:value-of select="."/></p>
n a ฺm this
</xsl:template>
i
nt use
</xsl:stylesheet>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t previously,s the default behavior of <xsl:apply-templates> is to process
As discussed
e n
l nodes r a n
theachild
V o n -t of the context node. Alternatively, you can add a select attribute to the
n
<xsl:apply-templates/> element to control the specific template to be activated relative
to the context node.
The example in the slide makes use of the select attribute in the <xsl:apply-
templates/> element. The select attribute specifies an expression that is used to activate
a template rule whose expression is region in both the select and match attributes.
Note: The select attribute of <xsl:apply-templates/> is typically the same as, but
does not have to be identical to, the match attribute in the activated template rule. However,
the two expressions must identify the same node-set for the correct template rule to be
activated.
The result of the transformation contains a line of text for each region ID and name, excluding
the Regions header, because the template matching regions is not activated.
If select="//region" is absent from <xsl:apply-templates/> in the template rule for
the document root, the transformation will include a level one heading with the title Regions
before the lines for each region ID and name pairs.

XML Fundamentals 7 - 16
Template Rules and Priorities

• The rule with the highest priority is applied.


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

• Or the rule that appears last is used if the priority equals


another.
<?xml version='1.0' encoding='UTF-8'?>
<xsl:stylesheet version ="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
Document root
</xsl:template>
<xsl:template match="/" priority="1">
s a
Apply this document Root
) h a
<xsl:apply-templates/> r s
sฺ eฺ
</xsl:template> m
e uid
<xsl:template match="department">
c @
Department Data <!-- default priority -0.5 -->
r o vi nt G
jsto Stude
</xsl:template>
<xsl:template match="department">
ฺm thisa
I WIN! <!-- default priority -0.5 -->
i n a
</xsl:template>
l e nt use
va e to
</xsl:stylesheet>

c (
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tStyle Sheet s usually has multiple template rules declared. As shown in the slide, the
An XSLT
e n r a n
V al pattern
match
o n -t for more than one template rule can be applicable to the same node. To deal
npotential conflict, an XSLT Processor assigns a default priority to each template rule,
with this
unless the author of the XSLT Style Sheet explicitly specifies a priority attribute in the
start tag of the <xsl:template> declaration. The default priority is:
• A -0.5 value if the match pattern is a simple node-test such as the element name
• A 0 value if the pattern uses a node name qualified by a namespace prefix, or the
processing-instruction() function (for example, dept:department)
• A 0.25 value for patterns that use a namespace prefix with a wildcard node-test (for
example, dept:*)
• A 0.5 value for all other patterns (for example, /departments, which uses an
absolute XPath pattern)
Note: If more than one template rule has a pattern that matches the same node, the rule with
the highest priority is applied. If multiple patterns share the highest priority, an error occurs or
the rule that appears last in their order of appearance is used. For example, the rule for the
department pattern that contains the text literal I WIN! in the slide is selected because it
appears last, even though it has the same priority as the previous one.

XML Fundamentals 7 - 17
Default Template Rules

XSLT provides built-in template rules:


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

• That are applied for nodes without a matching template


rule in the XSLT Style Sheet
• For different types of nodes in an XML document such as:
– The element and root nodes:
<xsl:template match="*|/">
<xsl:apply-templates/>
s a
</xsl:template>
h a
s ฺrs) ฺ
– The text and attribute nodes:
@ em uide
o v ic t G
<xsl:template match="text()|@*">
t o r den
<xsl:value-of select="."/> s
aj s Stu
</xsl:template>
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t form
The simplest ofsan XSLT Style Sheet is:
n
e r a
al version="1.0"?>
-t
V
<?xml
n o n
<xsl:stylesheet version ="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
An empty XSLT Style Sheet creates an output document that contains the text values for all
the nodes in the source document. This occurs because most XSLT Processors implement
default template rules called built-in templates. The slide shows the first built-in template rule
matching the document root (/) and all element nodes (*) of the source document tree. The
second template rule matches all the text nodes (text()) and attribute nodes (@*).
The XSLT Processor uses the first built-in template rule to process the document root and all
its descendant nodes because of the presence of the <xsl:apply–templates/> element.
When a text (or attribute) node is encountered, the second template invokes the
<xsl:value-of> element to output its string value.
The resulting output document contains all the text values from the input document.
Note: Explicit template rules added to the XSLT Style Sheet override the default built-in rules.
However, nodes without explicit template rules can match the default template rules, which
often cause unwanted text to appear in the output document.

XML Fundamentals 7 - 18
Effects of Default Template Rules

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


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

<xsl:stylesheet version ="1.0"


xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<tr><th>Id</th><th>Name</th><th>Salary</th></tr>
<xsl:apply-templates/>
</table> <xsl:template match="*|/">
</body> <xsl:apply-templates/>
s a
</html> </xsl:template>
) h a
</xsl:template>
<xsl:template match="employee"> r s
sฺ eฺ
<tr> m
e uid
c @
vi nt G
<td><xsl:value-of select="employee_id"/></td>
<td><xsl:value-of select="last_name"/></td>
r o
<td><xsl:value-of select="salary"/></td>
a jsto Stude
</tr>
</xsl:template>
n a ฺm this
i
nt use
</xsl:stylesheet>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s of default template rules, the example in the slide assumes that the
t the aeffects
n
To illustrate
e r n
al oroot
document
V n -t contains the employees root element with one or more employee children.
The XSLTn Style Sheet declares template rules that match the document root and employee
nodes, but not the employees element. In this case, the processing steps are as follows:
1. The document root template outputs the HTML table header row and invokes the
<xsl:apply-templates/> element in the HTML table after the header row.
2. The default template is invoked for the employees root element, which does not have
an explicit template rule, as seen by the dotted arrows. The default template outputs
white space and invokes the <xsl:apply-templates/> element to process the
employee child elements.
3. Each employee node matches its template rule that outputs the text value for
employee_id, last_name, and salary in the cells of an HTML table row.
Note: After the source document and stylesheet are constructed and before they are
processed by an XSLT Processor, the text nodes are stripped if they contain only white space
characters. Stripping a text node removes it from the tree. The XSLT Style Sheet can specify:
• The <xsl:strip-space elements="tokens"/> element to define a list of space-
separated elements for which the spaces are stripped
• The <xsl:preserve-space elements="tokens"/> element to define a list of
space-separated elements in which the spaces are preserved

XML Fundamentals 7 - 19
Looping with <xsl:for-each>

The <xsl:for-each> element:


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

• Provides a select attribute containing an expression to


identify a node-set
• Applies its template for each node in the node-set
• Is nested in an <xsl:template> element
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
s a
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
) h a
<xsl:template match="departments"> r s
sฺ eฺ
<xsl:for-each select="department"> m
e uid
<p><xsl:value-of select="."/></p>
c @
</xsl:for-each>
r o vi nt G
</xsl:template>
a jsto Stude
</xsl:stylesheet>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s element iterates through all the nodes in the node-set and matches
The <xsl:for-each>
l e n r a n
theaexpression
V o n -t that is specified in its select attribute. The template rule is applied once for
n in the node-set. The XSLT Style Sheet in the slide can be applied to the following
each node
XML:
<?xml version="1.0"?>
<departments>
<department>
<department_id>10</department_id>
<department_name>Administration</department_name>
</department> ...
</departments>
When applying the XSLT Style Sheet, the processing steps are as follows:
1. The default template rule matches the document root and executes an <xsl:apply-
templates/> causing the departments root element template to be activated.
2. The template rule of the departments root element is activated with the <xsl:for-
each> element matching all the department child elements.
3. The <xsl:for-each> element is applied for each department node.
The resulting output document contains one line for each department element, with the text
values of its child elements concatenated, as in the following example:
<p>10 Administration</p> XML...
Fundamentals 7 - 20
Specifying Output Formats

The <xsl:output> element:


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

• Specifies the output format of the result tree


• Must be a child of the <xsl:stylesheet> element
• Has a method attribute value that must be one of the
following:
– XML, html, text
– A qualified name that does not define behavior a
h a s
r s )
<xsl:output method="xml" media-type="text/xml"/> sฺ
e m ideฺ
v i c@ t Gu
t o ro den
<xsl:output method="html" s
aj s Stu
omit-xml-declaration="yes" a ฺ m hi
t i n e t
media-type="text/html"/>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
You cann i n
tuse XSLT n s create the result tree in different output formats such as:
to
l e r a
-t method to xml, which is the default, unless the output tree root element is
V•a XML:
n o nSet
<html>. The xml output type can be used for XHTML output.
• HTML: Set method to html to inform the XSLT Processor to convert empty elements,
such as <br/> or <br></br>, into their unpaired HTML form (that is, <br>).
Note: The XSLT Style Sheet must use one of the empty element formats for unpaired
HTML tags to ensure that the stylesheet is well-formed.
• Text: Set method to text to output documents with a text/plain MIME type.
• Other formats: Other formats, such as Wireless Markup Language (WML), can be
parsed by Oracle JDeveloper:
<xsl:output method="wml"
media-type="text/x-wap-wml" indent="yes"/>
The <xsl:output> element provides several attributes, such as suppressing the output of
the XML declaration by using omit-xml-declaration="yes".
Note: The XSLT Processor may process the output method specified, but is not required to
do so. Therefore, different XSLT Processors may produce different results. If the stylesheet
does not contain an <xsl:output> element, the html method is used if the root element in
the output is <html>, independent of text case. Otherwise, the xml method is used.

XML Fundamentals 7 - 21
Attribute Value Templates

An attribute value template is:


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

• An XPath expression (enclosed in braces) whose result is


used as an attribute value
• Used to include input document information in the attribute
values of XSLT elements, and the output
<region>
<region_id>1</region_id>
<region_name>Europe</region_name>
</region> s a
) h a
<xsl:template match="/"> r s
sฺ eฺ
<regions><xsl:apply-templates/></regions> m
e uid
c @
vi nt G
</xsl:template>
<xsl:template match="region">
r o
<region id="{region_id}" name="{region_name}"/>
</xsl:template> a jsto Stude
n a ฺm this
i
nt use
l e
<regions><region id="1" name="Europe"/></regions>

c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t value s
l e n
The attribute
t r a ntemplate is a powerful technique to include node names (element and
a onames)
attribute - or their values in the attribute values of the XSLT elements in the stylesheet.
V n n
Using an attribute value template creates dynamic output results that are determined by the
information from the input document, such as attribute values for elements in the result
document, as shown in the example in the slide. You can use the <xsl:element> element
in the template to create output elements based on the element names from the input
document, as discussed in the section titled “Creating Elements with Attributes.”
The example in the slide shows three related documents:
• Part of an XML input document showing one <region> element and its children
• The XSLT Style Sheet with templates to transform the input document by converting the
child elements of the <region> element into attribute values in the output document.
The template rules hard-code the output element names for <regions> and
<region>. However, the template rules use the attribute value template technique to
obtain the text values of the <region_id> and <region_name> elements from the
input document. The text values are obtained from the input document to set the values
for their corresponding attributes id and name in the <region> element of the output
document.
• The resulting output document that contains the results after the transformation, with the
element text from the input document formatted as attribute values in the output

XML Fundamentals 7 - 22
Creating Elements with Attributes

• To create an element, use <xsl:element>:


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

<xsl:element name="region">Japan</xsl:element>

<region>Japan</region>

• To create an attribute, use <xsl:attribute>:


– Inside an <xsl:element>: s a
) h a
<xsl:element name="region"> r s
sฺ eฺ
<xsl:attribute name="id">5</xsl:attribute>Japan m
e uid
c @
</xsl:element>
r o vi nt G
a jsto Stude
<region id="5">Japan</region>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t creating s output documents, use <xsl:element> to dynamically create new
If you are
e n r a n
XML
al oAs
elements.
V n t
-shown previously and in this slide, you can do either of the following:
• n
Explicitly provide element and attribute names.
• Define rules so that the element and attribute names are constructed from the nodes in
your XML input document at processing time.
This slide and the next slide show three ways to create an element and the results, with or
without attributes:
1. A <region> element without attributes that contains a text value of Japan
2. A <region> element containing the text Japan, and a single attribute called id with a
value of 5

XML Fundamentals 7 - 23
Creating Elements with Attributes

• To create an attribute, use <xsl:attribute>:


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

– Inside the <xsl:attribute-set> element used in the


use-attribute-set attribute of <xsl:element>:

<xsl:attribute-set name="region-info">
<xsl:attribute name="id">5</xsl:attribute>
<xsl:attribute name="name">Japan</xsl:attribute>
</xsl:attribute-set>
<xsl:template match="region[1]">
s a
<xsl:element name="{local-name()}"
h a
ฺrs) ฺ
use-attribute-sets="region-info">
</xsl:element> s
</xsl:template> em ide
v i c@ t Gu
t o ro den
<region id="5">Japan</region> 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t i n
3. An<region> n s
element with an id and a name attribute. This example uses an
l e - t r a
Va <xsl:attribute-set> element that declares the id and name attributes and their
non The template creating the output element derives the element name from the
values.
context node by using the local-name() XPath function, and generates the attributes
by referencing the region-info attribute in use-attribute-sets.
Note: The attribute set hard-codes its attribute names and values in the example. The design
can be made more flexible with the use of the <xsl:parameter> elements.

XML Fundamentals 7 - 24
Lesson Agenda

• Extensible Stylesheet Language (XSL)


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

– Using an XSLT Style Sheet with an XML document


– Viewing the transformed document
• Creating and applying template rules
• Sorting an XML document
• Conditional processing
• Using parameters with templates s a
h a
• Performing the XSLT process s ฺrs) ฺ
– Using JDeveloper @ em uide
– Using the oraxsl command-line utility 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 7 - 25
Sorting an XML Document

The <xsl:sort> element can be used:


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

• To sort nodes in the input document before the nodes are


processed
• In the <xsl:apply-templates> or <xsl:for-each>
elements
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
s a
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
h a
ฺrs) ฺ
<xsl:template match="/employees">
<xsl:for-each select="employee"> s
<xsl:sort select="last_name"/>
@ em uide
<p><xsl:value-of select="."/></p>
o v ic t G
</xsl:for-each>
t o r den
</xsl:template> s
aj s Stu
</xsl:stylesheet> a ฺ m hi
t i n e t
• Multiple times to specify a l enadditional
o us sort criteria
v
c ( nse t
v i
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
Suppose
n t inatheaexample
that n sfe in the slide is applied to the following XML document:
le -tr
Va non<employees>
<employee num="1">
<employee_id>100</employee_id>
<last_name>King</last_name>
</employee>
<employee num="2">
<employee_id>101</employee_id>
<last_name>Kochhar</last_name>
</employee>
<employee num="3">
<employee_id>102</employee_id>
<last_name>De Haan</last_name>
</employee>
</employees>
The output shows employee data sorted by their last names in the following sequence:
102 De Haan
100 King
101 Kochhar
XML Fundamentals 7 - 26
Conditional Processing with <xsl:if>

• <xsl:if> is processed when the test attribute


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

evaluates to true.
• The test attribute is a Boolean expression.

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
s a
<h2>Departments with a manager</h2>
) h a
<xsl:for-each select="//department">
<xsl:if test="manager_id"> r s
sฺ eฺ
<p><xsl:value-of select="."/></p> m
e uid
c @
</xsl:if>
r o vi nt G
jsto Stude
</xsl:for-each>
</xsl:template>
ฺm thisa
</xsl:stylesheet>
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t rules can sperform conditional processing by using the <xsl:if> or
Template
e n r a n
V al on-t elements. Both these elements provide a test that evaluates an XPath
<xsl:choose>
n as a Boolean result of true or false. The XPath expression is converted to a
expression
true or false value. If the XPath expression evaluates to:
• A numeric result of 0, it is converted to false; otherwise, it is considered true
• A string result with a length greater than 0, it is converted to true; otherwise, it is
false
• A node with a value, it is considered true; otherwise, an empty or nonexistent node is
false
The example in the slide creates an HTML paragraph for each <department> element
containing a <manager_id> child element with a value. Any <department> element
without a child or empty <manager_id> element is excluded.
Note: There are no <xsl:else> or <xsl:else-if> constructs in XSLT. To emulate an
“else” scenario, use the <xsl:choose> element, as discussed in the section titled
“Conditional Processing with <xsl:choose>.”

XML Fundamentals 7 - 27
Conditional Processing with <xsl:choose>

<xsl:choose> has one or more <xsl:when> elements and


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

(optionally) one <xsl:otherwise>.


<xsl:template match="/">
<xsl:for-each select="//employee">
<p><xsl:value-of select="last_name"/>,
<xsl:choose>
<xsl:when test="salary &lt; 10000">
<font color="red">
<xsl:value-of select="salary"/>
s a
</font>
) h a
</xsl:when>
<xsl:otherwise> r s
sฺ eฺ
<font color="blue"> m
e uid
c @
vi nt G
<xsl:value-of select="salary"/>
</font>
r o
</xsl:otherwise>
a jsto Stude
</xsl:choose></p>
</xsl:for-each>
n a ฺm this
</xsl:template> i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s element may contain:
The <xsl:choose>
e n
l orn-more r a n
t <xsl:when> elements, each with a condition in its own test attribute
V•a One
n o
• An optional <xsl:otherwise> element
The <xsl:when> elements are evaluated in their order of appearance; the first <xsl:when>
with a true condition is instantiated, and the remaining elements are not processed. If all
<xsl:when> conditions yield a false result, none are processed, unless an
<xsl:otherwise> element is provided. The <xsl:otherwise> element must appear last
and is processed if no <xsl:when> condition returns true.
The example in the slide uses a single <xsl:when> element to check the salary value of an
employee. If the salary is less than 10,000, the salary is displayed in red; otherwise, the salary
is displayed in blue. The example emulates an “if-then-else” scenario.
Note: The example uses the &lt; built-in entity reference for the less-than operator to
ensure that the XSLT Style Sheet is well-formed.

XML Fundamentals 7 - 28
Modes

• Allow the processing of the same input XML element more


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

than once
• Are applied by using the mode attribute in
<xsl:template> and <xsl:apply-templates>
• Are implemented by:
– Defining mode in <xsl:template>:
<xsl:template match="department" mode="toc"> ...
s a
h a
ฺrs) ฺ
<xsl:template match="department" mode="body"> ...
<xsl:template match="department "> ...
s
@ em uide
– Referencing mode in <xsl:apply-templates>:
o v ic t G
t o r den
<xsl:apply-templates s
aj s Stu
select="department" mode="toc"/>
a ฺ m hi
t i n e t
The first template is matched a and
l enprocessed.
o us
v
c ( nse t
v i
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
Modesn t ina aaway
provide n sftoe process the same input XML content more than once. This is
ale oby:
achieved
V n -tr
• n
Specifying the mode attribute in the <xsl:template> definitions
• Referencing the mode value of a template by using the mode attribute of the
<xsl:apply-templates> element
The mode attribute value is a string to describe the mode (such as toc for table of contents,
and body to represent the body of information) for transformation to the output tree.
After a mode attribute is defined in a template rule, you can specify mode in one or more
instances of <xsl:apply-templates>, which ensures that the XSLT Processor applies the
template by matching the specified mode value.
The example in the slide shows three incomplete template rules, with the first two specifying
their mode attribute values of toc and body, respectively. When the <xsl:apply-
templates> element example is encountered that matches the employee nodes, the
element targets the template defined with the mode value toc. In this case, only the first of
the template rules is applied because the node and mode name match the apply template
request.

XML Fundamentals 7 - 29
Example of Using Modes

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

<xsl:template match="/departments">
<html>
<body>
<h1>Department Report</h1>
<ol><xsl:apply-templates
select="department" mode="toc"/></ol>
<xsl:apply-templates
select="department" mode="body"/>
</body>
</html>
</xsl:template>
s a
<xsl:template match="department" mode="toc">
) h a
<a href="#id_{department_id}">
r s
sฺ eฺ
<li><xsl:value-of select="department_name"/></li>
</a> m
e uid
c @
</xsl:template>
r o vi nt G
jsto Stude
<xsl:template match="department" mode="body">
<a name="#id_{department_id}">
ฺm thisa
<h2><xsl:value-of select="department_name"/></h2>
i n a
nt use
</a>...<xsl:apply-templates mode="body"/>...
</xsl:template>
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t example s the <xsl:apply-templates> element to specify a mode for the
e n
The slide
r a nuses
al otonbe-t applied, and specifies a mode name in the target <xsl:template> elements.
template
V n uses an XML input document of department data to generate a web report.
The example
To process an XSLT Style Sheet:
1. The template rule matching the root element /departments generates the HTML
document structure with the report header Department Report, and generates an
ordered list of department names by requesting <xsl:apply-templates> using the
mode value toc.
2. <xsl:template> with the mode toc is invoked to generate an HTML list of elements.
This list contains the department name as a hypertext link referencing an anchor that is
named with the literal text #id_ prefixed with the department_id node text.
3. The root element template rule processes another <xsl:apply-templates>
element. This time, it does so by using the mode value of body.
4. The template rule with mode="body" generates the body of the report, which is
composed of HTML anchor tags (hyperlinks). The department name is formatted as an
HTML header in the anchor text. The name attribute of each anchor tag is set to the
same value that is generated for the hypertext link (href attribute) created by the
template with mode="toc".
5. The template with a body mode applies its child templates with the mode of body.
Note: The hypertext link and anchor values are formed by using an attribute value template.
XML Fundamentals 7 - 30
Example of Using Modes
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<ol>
<xsl:apply-templates
select="department"
mode="toc"/></ol>

s a
) h a
<xsl:apply-templates
r s
sฺ eฺ
select="department" m
e uid
c @
mode="body"/>
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t showsathe ssample HTML results for the XSLT Style Sheet shown in the previous
n
The slide
e r n
V al Theostylesheet
slide. n -t shows the <xsl:apply-templates> elements with their
n
corresponding output. A snippet of the department XML document is:
<?xml version='1.0' encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="code_07_32_n.xsl"?>
<departments>
<department num="1">
<department_id>10</department_id>
<department_name>Administration</department_name>
<manager_id>200</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>1800</location_id>
</department>
...
</departments>
XML Fundamentals 7 - 31
The preceding report is generated by using the following XSLT Style Sheet:
<?xml version='1.0' encoding='windows-1252'?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/departments">
<html>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<body>
<h1>Department Report</h1>
<ol>
<xsl:apply-templates
select="department[position()&lt;5]"
mode="toc"/>
</ol>
s a
<xsl:apply-templates
h a
select="department[position()&lt;5]"
s ฺrs) ฺ
mode="body"/>
@ em uide
</body>
o v ic t G
</html>
t o r den
</xsl:template> s
aj s Stu
a ฺ m hi
t i
<xsl:template match="department" mode="toc">n e t
<a href="#id_{department_id}"> a l en o us
i c (v se t
<li><xsl:value-of select="department_name"/></li>
</a> t o rov licen
a js ble
M fera
</xsl:template>
a
t i n n s
<xsl:template match="department" mode="body">
n
l e r a
Va non-t
<a name="#id_{department_id}">
<h2><xsl:value-of select="department_name"/></h2>
</a>
<table border="1">
<tr><xsl:apply-templates mode="body"/></tr>
<tr><xsl:apply-templates/></tr>
</table>
</xsl:template>
<xsl:template match="department/*" mode="body">
<th><xsl:value-of select="local-name()"/></th>
</xsl:template>
<xsl:template match="department/*">
<td><xsl:value-of select="."/></td>
</xsl:template>
</xsl:stylesheet>

XML Fundamentals 7 - 32
Calling Templates by Name

• Create a template by specifying the name attribute:


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

...
<xsl:template name="deptlist">
<!-- instructions to process -->
</xsl:template>
...

• Call a template by name:


s a
...
h a
<xsl:template match="/departments">
s ฺrs) ฺ
em uide
<html>
<body>
@
ic t G
<h1>Department Report</h1>
o
r denv
<ol><xsl:call-template name="deptlist"/></ol>
s t o
<xsl:apply-templates mode="body"/>
ฺ m aj s Stu
</body>
t i n a
e t hi
...
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t rule awith
A template
n n sa name attribute can be instantiated by referencing its name in an
e
al on-t r rule. The slide shows:
V
<xsl:call-template>
n
• Assigning the name attribute value of deptlist to the template rule
Note: A template without a match attribute cannot have a mode attribute.
• Calling the template by using the <xsl:call-template> element with the name
attribute value deptlist that identifies the desired template rule to be instantiated
Note: The called template does not have a match attribute. Therefore, its processing
instructions must use the XSLT commands that set the context, as in the following example:
<xsl:template name="deptlist">
<xsl:for-each select="//department">
<a href="#id_{department_id}">
<li><xsl:value-of select="department_name"/></li>
</a>
</xsl:for-each>
</xsl:template>
Using named templates is an effective way to create a stylesheet to be used with multiple
XML documents. Each XML document applies named templates for the default formatting
appearance according to company standards, but produces different content.
XML Fundamentals 7 - 33
Lesson Agenda

• Extensible Stylesheet Language (XSL)


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

– Using an XSLT Style Sheet with an XML document


– Viewing the transformed document
• Creating and applying template rules
• Sorting an XML document
• Conditional processing
• Using parameters with templates s a
h a
• Performing the XSLT process s ฺrs) ฺ
– Using JDeveloper @ em uide
– Using the oraxsl command-line utility 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 7 - 34
Creating and Using Parameters

• Define template parameters with <xsl:param>:


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

<xsl:template name="deptlist">
<xsl:param name="loc_id"/>
<xsl:for-each
select="//department[location_id=$loc_id]">
<a href="#id_{department_id}><li><xsl:value-of
select="department_name"/></li></a>
</xsl:for-each>
</xsl:template>

s a
• Pass parameters with <xsl:with-param> in an
) h a
sฺrs
<xsl:apply-templates> or <xsl:call-template>:
m ideฺ
e
<xsl:template match="/departments">
v i c@ t Gu
<ol><xsl:call-template name="deptlist">
t o ro den
<xsl:with-param name="loc_id" s
aj s Stu
select="department[1]/location_id"/>
a ฺ m hi
</xsl:call-template></ol>
t i n e t
</xsl:template>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
Definingn t i n
parameters n sis similar to creating arguments for methods or procedures in a modular
e
al on-language.
programming t r a
V
The firstnexample in the slide defines the template named deptlist with one parameter called
loc_id. The parameter value is used by prefixing its name with a dollar sign ($) in the template
body. The example uses $loc_id to filter the department node-set identified by the <xsl:for-
each> element to departments with a specified location_id.
The second example uses <xsl:with-param> to specify the name parameter and its value
determined by the select attribute when calling the deptlist template that uses the
<xsl:call-template> element.
Guidelines
• The <xsl:param> element must be the first item in a template, and you can define one or
more parameters.
• Prefix parameter names with a dollar sign ($) to use the parameter values in the template.
• Provide default parameter values by using either the template body (between the start and
end <xsl:param> tags), or by using the select attribute.
• Provide parameter values with <xsl:with-param> either in the template body or by using
the select attribute.
Note: The <xsl:param> and <xsl:with-param> elements define their value by using either
the select attribute or their template bodies, but not both at the same time.

XML Fundamentals 7 - 35
Lesson Agenda

• Extensible Stylesheet Language (XSL)


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

– Using an XSLT Style Sheet with an XML document


– Viewing the transformed document
• Creating and applying template rules
• Sorting an XML document
• Conditional processing
• Using parameters with templates s a
h a
• Performing the XSLT process s ฺrs) ฺ
– Using JDeveloper @ em uide
– Using the oraxsl command-line utility 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 7 - 36
Performing the XSLT Process in JDeveloper
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

1 2

3 s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this 4
5 i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s process in JDeveloper and view the transformed document. The slide
trun theaXSLT
You can
e n r n
al ouses
example
V n -tcode_07_37_s.xsl. The code_07_37_s_input.xml XML document is
n into a code_07_37_s_output.html file.
transformed
To run the XSLT process in JDeveloper:
1. Right-click the XSL file and select Run from the context menu.
2. In the XSLT Settings window, specify the following:
- File name and location of the XML file to use as the source document (input XML
file) in the transformation process
- File name and location of the file (output file) where the results tree will be entered
- (Optional) The XSLT source path and XSLT options (if any)
Then click OK.
3. When the running of the project is completed, you will see a message in the Log
window. The output file, code_07_37_s_output.html file, is displayed in the
Applications pane.
4. View the output file by using your web browser.
Notes
• Make sure that the project is active to start the target XSL file.
• Browse to the location of the output file, and then double-click the output file name to
view the HTML file in your web browser as seen in the slide.
XML Fundamentals 7 - 37
Using the oraxsl Utility

The oraxsl command-line utility:


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

• Transforms XML documents with an XSLT Style Sheet


• Writes the result to a file or standard output
• Requires:
– A java executable in PATH
– xmlparserv2.jar file in CLASSPATH
s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
java oracle.xml.parser.v2.oraxsl a jsto Stude
departments.xml
departments.xsl result.html naฺm this
e n ti se
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
isin
t a sfe interface that is used to apply a stylesheet on XML documents. To
oraxsl
a
use e n
loraxsl, - t r an that the following environment variables are set:
a command-line
ensure
V non
• export
CLASSPATH=/home/oracle/jdeveloper/modules/oracle.xdk_12.1.2/xmlp
arserv2.jar for XSLT 2.0 support.
• PATH must contain the directory where the java executable is located.
To use the simplest form of invoking the oraxsl utility, enter:
java oracle.xml.parser.v2.oraxsl [opts] source stylesheet [result]
The command-line parameters are:
• opts, representing zero or more options
• source, representing the file name of the input source XML document
• stylesheet, representing the file name of the XSLT Style Sheet
• result, which is an optional file name for the result data. If it is omitted, the results are
written to the command window or standard output.
Note: You can configure the oraxsl utility as an external tool in JDeveloper. This course
uses only the command-line interface. For more information about how to use external tools,
see the appendix titled “Managing External Tools in JDeveloper.”

XML Fundamentals 7 - 38
The oraxsl utility can process multiple XML documents and accept the following command-
line options that dictate how it must behave:
• -d directory, which specifies a directory with files to be transformed. (The default
behavior is to process all files in the directory.) If only a certain subset of the files in that
directory (for example, one file) needs to be processed, this behavior must be changed
by using -l and by specifying only those files that need to be processed. You can also
change the behavior by using the -x or -i option to select files based on their
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

extension.
• -debug, which enables the debug mode. By default, the debug mode is turned off.
• -e error_log, which specifies a file into which errors and warnings are written
• -h, which prints the oraxsl invocation syntax and options
• -i source_extension, which specifies the file extensions to include (used in
conjunction with –d)
• -l xml_file_list, which specifies a list of files to be transformed
s a
a
• -o result_directory, which defines a location to place the result files. This must
h
be used with the -r option.
s ฺrs) ฺ
• -p param_list, which specifies a list of parameters
@ em uide
o v ic t G
• -r result_extension, which specifies the extensions to be used for result files. If -d
t o r den
or -l is specified, the option of –r must be specified. For example, if the extension is
s
aj s Stu
specified as out and an input document is foo, the result file is called foo.out. By
a ฺ m hi
default, results are placed in the current directory.
t i n e t
a l en o us
• -s stylesheet, which specifies the stylesheet file to use. This must be used if the -d
or -l
l option is specified
specified.
i c (v se t
t o rov licen
• -t num_of_threads, which controls the number of threads to use for processing the
js ble
transformations. Using multiple threads can provide performance improvements when
a
M fera
processing multiple documents.
a
t i n n s
• -v, which enables a verbose mode that displays some debugging information that could
n
l e r a
Va non-t
help in tracing any problems encountered
• -w, which causes warnings to be displayed. By default, warnings are turned off.
• -x source_extension, which defines files with extensions to be excluded (used in
conjunction with the -d option)

XML Fundamentals 7 - 39
XSLT 2.0

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

• Is a W3C recommendation
• Uses the XPath 2.0 and XQuery 1.0 data model
• Supports XML Schema data types
• Provides features such as the following:
– Grouping data
– Generating multiple outputs s a
) h a
– Temporary tree r s
sฺ eฺ
– Character mapping 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t uses the s data model as XPath 2.0 and XQuery. XSLT 2.0 uses XPath 2.0 to
e n
XSLT 2.0
r a nsame
al andonprocess
locate
V -t content within an XML document’s logical hierarchy.
To usenXSLT 2.0, set the version attribute in your stylesheet as follows:
<? xml-stylesheet version="2.0" ... ?>
XSLT 2.0 has new elements that help with grouping tasks, which are difficult to perform in
XSLT 1.0. XSLT 2.0 allows multiple documents to be output.
By using XSLT 2.0, you can transform textual data that is not well-formed XML.
XSLT 2.0 allows you to define custom functions. XSLT 2.0 includes the xsl:function
element. xsl:function declares the name, parameters, and implementation of a user-
defined function that is used within XPath expressions in a given style sheet.
Note: Some elements, such as xsl_value-of, produce different results in XSLT 2.0.
Therefore, you must specify the version of the transformation.
For more information about XSLT 2.0 features and examples, see the appendix titled
“Additional Information About Document Type Definitions, XML Schemas, and XSLT 2.0.”

XML Fundamentals 7 - 40
Quiz

An XSLT Style Sheet is an XML document containing:


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

• An <xsl:stylesheet> root element that declares:


The xsl namespace prefix
The http://www.w3.org/1999/XSL/Transform
mandatory namespace URI
• One or more <xsl:template> elements and other XSL
elements that define transformation rules a
h a s
a. True
s ฺrs) ฺ
b. False em e
c@ t Guid
v i
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t
Answer:na i n n s
l e r a
Va non-t

XML Fundamentals 7 - 41
Summary

In this lesson, you should have learned how to:


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

• Create an XSL stylesheet containing XSLT elements


• Use XSLT to transform an XML document by using a
browser and the oraxsl command-line utility
• Use <xsl:template> to create a template rule
• Use <xsl:apply-templates> for recursion through
child template rules s a
h a
• Call named templates with <xsl:call-template>,
s ฺrs)with
and without parameters e m ideฺ
v i c@ t G
• Apply sort criteria to nodes in an XML rdocument by
u
using
t o o en
the <xsl:sort> element ajs tud m his S
a ฺ
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t ina ansf(XSLT)
XSL Transformations
e
occur when a node in the source matches a template rule
l e
a The t r
-content of that rule is then written to the result tree. Given a source tree and a
non
pattern.
V
style sheet, the XSLT Processor carries out the transformation described by the rules in the
style sheet. An XSL Style Sheet contains XSLT elements such as:
• <xsl:template> to create a template rule, without and with parameters defined by
using an <xsl:param> element
• <xsl:apply-templates> for recursion through child template rules
• <xsl:call-template> to call named templates
• <xsl:value-of> to obtain the value of a node in the XML document
• <xsl:for-each> to loop through a node-set
• <xsl:if>, or <xsl:choose>, <xsl:when>, and <xsl:otherwise> for conditional
processing
Parameter values are supplied to templates by using the <xsl:with-param> element.
Sorting is accomplished by including one or more <xsl:sort> elements in a template rule to
sort the input node-set before they are processed. Some web browsers transform an XML
document by using the information provided in an <?xsl-stylesheet?> processing
instruction. You can run the XSLT process in JDeveloper and view the transformed document.
The oraxsl command line accepts an XML document and the XSLT Style Sheet as
command-line arguments to perform the transformation.

XML Fundamentals 7 - 42
Practice 7: Overview

This practice covers the following topics:


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

• Writing a simple XSLT Style Sheet to transform


department data in an XML document
• Modifying an existing XSLT Style Sheet to process an XML
document containing employee information
• (Optional) Transforming an XML document by using
oraxsl to work with the <xsl:output> element a
h a s
• (Optional) Using the oraxsl utility to transform one s XML
s ฺ r )
document into another XML document em eฺ
i c @ Guid
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na screate or modify existing XSLT documents to transform:
e n
In this practice,
l departments ryou
a n
V•a The o n -t XML document into a web page by using the browser XSLT
n
Processor
• The employees XML document into a web page by using the browser XSLT Processor
If there is time, you use oraxsl to transform:
• The departments XML document into an HTML page (You also work with the
<xsl:output> element.)
• The departments XML document into another XML document, converting the child
elements into attributes of the same node

XML Fundamentals 7 - 43
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ฺ

Working with XQuery

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• Describe XQuery, its features, and its applications


• Describe the data structures and fields of an XQuery data
model
• Describe XQuery support in JDeveloper
• Use XQuery expressions
• Use XQuery functions and operators a
as ) h
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t a query s
XQuerynis
l e t r a nlanguage that supports querying data from structured and unstructured
a sources.
data - In this lesson, you are introduced to XQuery and become familiar with its
V non
terminology, its data model and type system, and various expressions and functions. You
learn how to use XQuery in Oracle Database 12c.

XML Fundamentals 8 - 2
Lesson Agenda

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

– Introduction
– Applications and features
– Terminology
– Data model
– Syntax rules
• XQuery support in JDeveloper s a
h a
• XQuery expressions
s ฺrs) ฺ
• XQuery functions and operators
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 8 - 3
XQuery: Review

• XQuery is the W3C standard language that is designed for


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

querying (finding and extracting elements and attributes).


• XQuery is to XML what SQL is to database tables.
• XQuery is built on XPath expressions.
• XQuery 1.0 and XPath 2.0 share the same data model and
support the same functions and operators.
• By using XQuery, you can query both structured and s a
unstructured data: h a
rs) ฺ
– Relational databases e ms ideฺ
– XML documents v i c@ t Gu
toro tuden
– Other data sources with XML datajsview
ฺ m a sS
t i n a
e t hi
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t the W3C
XQuerynis n sstandard language that is designed for querying, transforming, and
e
al onXML
accessing r a
-t and relational data. XQuery is similar to SQL in many ways. Like SQL is
V n for querying structured, relational data, XQuery is designed especially for querying
designed
unstructured, XML data from a variety of data sources.
You can use XQuery to query XML data wherever it is found, whether it is stored in the
database tables, or available through web services. In addition to querying XML data, you can
use XQuery to construct XML data.
Oracle XML DB supports a native XQuery compilation engine that can parse and compile
XQuery expressions into SQL-native structures for evaluation. This native execution
significantly improves the performance of XQuery expressions in Oracle Database.

XML Fundamentals 8 - 4
XQuery Terminology

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

– Elements, attribute, text, namespace


– Processing instructions, comments, document nodes
• Family relationship
– Parent, children, siblings, ancestors, and descendants

s a
<?xml version="1.0" encoding="UTF-8"?>
h a
ฺrs) ฺ
<!-- Employees data -->
<emp:employees xmlns:emp="urn:hr:employee-ns">
s
<employee employee_id="100">
<name>
@ em uide
<first_name>Steven</first_name>
o v ic t G
<last_name>King</last_name>
t o r den
</name>
<salary>24000</salary> s
aj s Stu
</employee>
a ฺ m hi
. . .
t i n e t
en o us
</employees>

a l
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 8 - 5
What Is XQuery?

An XQuery referring to heterogeneous sources


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

s a
) h a
r s
sฺ eฺ
m
e uid
c @
XQuery result ro NeweXML
vi nt data G
A simple XQuery:
a jsto Stud
n a ฺm this
doc("code_08_dept.xml")/departments/department[department_name
i
nt use
='Purchasing']
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t an XML s language developed by the World Wide Web Consortium (W3C) and
XQuerynis
e r a nquery
alon other
built
V o n t standards such as XML, namespaces, XSLT, XPath, and XML Schema.
-W3C
n
XQuery 1.0 is a W3C recommendation.
XQuery is designed to query XML data from both XML files and relational databases. Just as
SQL is the query language for structured data that is expressed as relational tables, XQuery
is designed to work with the XML data model and be a comprehensive query language for
data that is expressed in XML. You can use a single XQuery on multiple and heterogeneous
data sources to transform the query result into new XML data.
XQuery consists of a set of possible expressions that are evaluated and return values (which,
for XQuery, are sequences).
Example: The slide displays a simple XQuery that selects all department nodes from the
code_08_dept.xml document, where department_name equals Purchasing. The
doc() function is used to specify the XML document that you want to query.

XML Fundamentals 8 - 6
Applications of XQuery
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Query XML transformation

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
XML generation
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tXQueryatonsquery, generate, and transform XML content.
You use
l e n tr
V•a XML n -
Querying: XML can map a large number of different data models. XQuery thus
no a mechanism for extracting information from relational databases, files, and
provides
web services simultaneously. The key feature of XQuery as a query language is its
conciseness and simplicity when compared to other querying approaches (such as
XSLT and DOM-based approaches).
• XML generation: XQuery enables you to construct XML data by using the result of a
query.
• XML transformation: XQuery can construct XML as a result of evaluating a query
expression, in many cases more expressively and efficiently than XSL Transformations
(XSLT). XQuery enables you to scan a hierarchy of elements. You can use it to apply
transformations at each level of the hierarchy by defining a recursive function.

XML Fundamentals 8 - 7
Features of XQuery

• Enables you to query both structured and unstructured


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

data
– Relational databases
– XML documents
– Other data sources with XML data view
• Is protocol independent
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s structured and unstructured types of data. It can be seamlessly merged
XQuerynhandles
e r a n
both
alSQLorelational
with
V n -t queries to handle all query scenarios. XML data can be generated using
XQuerynresults. XQuery is protocol independent. As a result, queries are evaluated on any
type of system with predictable results.
Notes
• XSLT uses XML syntax, whereas XQuery uses non-XML syntax. You can use both
XQuery and XSLT to add nodes, and both of them require XPath to select nodes to
process. XSLT 2.0 and XQuery 1.0 are based on the same foundation XPath 2.0.
• You learned about the XMLType data type in the lesson titled “Introduction to Oracle
XML DB.” XML data can be stored in Oracle Database using the XMLType data type.
XQuery provides support for XMLType. A single XQuery can join multiple data sources
and obtain the result from the query in XMLType.

XML Fundamentals 8 - 8
XQuery Terminology

Common terms used in XQuery:


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

• Node
• Atomic value
• Item
• Relationship of nodes
• Expression
• Sequence 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t treats sXML documents as trees of nodes. The root node is also known as the
l e n
• XQuery
r a n
Va document
o n -t node. The seven kinds of nodes are listed in the section titled “XQuery Data
n
Model.”
• An atomic value is an instance of one of the built-in data types defined by XML Schema
such as integers, strings, dates, and decimals. An atomic value cannot have parent or
children nodes.
• An item may be a node or an atomic value.
• A relationship in a document between nodes can be seen through the parent, the
children, the siblings, the ancestors, and the descendants.
• XQuery is a functional language, which is made up of expressions that can be nested
and return values. An expression is the basic building block of XQuery.
• Sequences are ordered collections of zero or more items. Every value in XQuery is a
sequence. An XQuery sequence can contain items of any XQuery type, which includes
numbers, strings, Boolean values, dates, as well as various types of XML node.

XML Fundamentals 8 - 9
XQuery Data Model

An XQuery data model:


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

• Is an abstract representation of one or more XML


documents
• Differs from the relational model
– Has hierarchy
– Supports node identity
• Represents a document as a tree of nodes s a
h a
• ฺrs)
Consists of sequences of atomic values or XML data
e ms 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
An XQuery i n
t dataamodel
n s is a hierarchical data model that represents an XML document as a
e
alof nodes.
tree r
-tUnlike the relational model, an XQuery data model has hierarchy and supports
V o n
identitynby treating the order of a node as one of its significant properties.
An XQuery data model has seven kinds of nodes, each having a unique node ID:
• Document node: Parent of the entire XML document containing the XML declaration and
all the elements (including the root element)
• Element node
• Attribute node
• Text node
• Namespace node
• Processing instruction node
• Comment node
An XQuery data model consists of a collection of ordered expressions called sequences. A
sequence consists of one or more items (a value or node) delimited by using the comma
operator. Items are typed using a rich type system that is based on the types of XML Schema.
Note
• XQuery 1.0, XPath 2.0, and XSLT 2.0 share the same data model.
• The type system is a major change from that of XPath 1.0, which is limited to simple
scalar types such as Boolean, number, and string.
XML Fundamentals 8 - 10
Conversion of XML into Query Data Model
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

4. Serialization

1. Parsing

s a
Parser Query data model
) h a
Infoset r
sฺ eฺs
m
e uid
c @
r o vi nt G
2. Validation
a jsto Stude 3. Mapping
n a ฺm this
Schema i
nt Post-Schema s e Validation
a l e o u
( v t Infoset (PSVI)

o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
Beforen ina can
a tquery n s
be
feprocessed, the XQuery compilation engine converts an XML document
V
e
to aldata model
n - trainstance as follows:
no the XML document by using the XML Parser, which generates an XML
1. Parses
Information Set (Infoset)
2. Validates the parsed document against one or more schemas, and creates the
information structure called the Post-Schema-Validation Infoset (PSVI)
3. Transforms the PSVI into a data model instance
4. Transforms the query result into an XML representation from the query data model, in a
data model called serialization
XQuery is defined in terms of the data model, but it does not place any constraints on how the
data model instances are constructed.

XML Fundamentals 8 - 11
XQuery Syntax Rules

• XQuery is case-sensitive.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Comments are delimited by (: and :).


• Elements, attributes, functions, and variables must be valid
XML names.
• The string value can be in single or double quotation
marks.
• A variable is defined with $ followed by a name. 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
The slide i n s of the syntax rules that you must know when writing XQuery code.
t showsasome
n
e
al keywords r
-t and names are case-sensitive, and generally in lowercase. You use
V
XQuery o n
n and smiley faces to define comments in an XQuery:
scowling
(:This is an example. :)
You define an XQuery variable with $ followed by a name: $employee

XML Fundamentals 8 - 12
Lesson Agenda

• XQuery features and applications


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

– Introduction
– Applications and features
– Terminology
– Data model
– Syntax rules
• XQuery support in JDeveloper s a
h a
• XQuery expressions
s ฺrs) ฺ
• XQuery functions and operators
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 8 - 13
XQuery Support in JDeveloper

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

5
3
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 4
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t provides san XQuery editor and an execution environment. JDeveloper supports
e n
JDeveloper
r a n
V al XQuery
creating
o n -t files, syntax highlighting of XQuery syntax in the code editor, and executing
XQuerynfrom within the IDE. The slide illustrates the steps to create an XQuery file and to run
the XQuery expression against an XML document in JDeveloper.
1. Select New > XQuery File (if you already created an XQuery file previously).
2. In the Create XQuery File window, enter the file name in the File Name field, and then
click OK.
3. JDeveloper creates an XQuery file. Enter your XQuery expression and save the file.
4. Right-click the XQuery file and select Run from the context menu.
5. View the XQuery results in the Log window.
In the slide example, the XQuery expression selects the department names for all department
nodes from the code_08_dept.xml document.
You can modify the XQuery expression to select all department nodes from the
code_08_dept.xml document, where department_name equals “Purchasing.” as
follows:
doc("code_08_dept.xml")/departments/department[department_name="Purc
hasing"]

XML Fundamentals 8 - 14
Lesson Agenda

• XQuery features and applications


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

– Introduction
– Applications and features
– Terminology
– Data model
– Syntax rules
• XQuery support in JDeveloper s a
h a
• XQuery expressions
s ฺrs) ฺ
• XQuery functions and operators
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 8 - 15
XQuery Expressions

XQuery expressions are of the following types:


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

• Primary
• Sequence
• FLWOR
• Path
• Arithmetic
• Logical s a
h a
• Conditional s ฺrs) ฺ
• Quantified @ em uide
ic G
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t areathe sbasic building blocks of XQuery. They are case-sensitive.
l e n
Expressions
r n
V•a Primary
o n -texpression: Includes literals, variable references, context item expressions,
n
constructors, and function calls
- A literal is an atomic (scalar) value. XQuery supports two types of literals: numeric
and string.
- A variable name starts with a dollar sign ($) (for example, $num).
• Sequence expression: Can be constructed by using the comma operator; may contain
a series of atomic values or nodes
• FLWOR expression: Can be used to query XML data and is considered similar to the
SELECT-FROM-WHERE statements in SQL
• Path expression: Is used to locate nodes in trees by using XPath
• Arithmetic expression: Is used to write expressions with arithmetic operators
• Logical expression: Can be written by using logical operators
• Conditional expression: Also supports IF-THEN-ELSE
• Quantified expression: Generates a Boolean based on a sequence’s content

XML Fundamentals 8 - 16
Primary Expressions: Variables

• Variables can be used for:


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

– Temporary storage of data


– Manipulation of stored values
– Reusability
• Variable names start with a dollar sign:
– $num

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tuse variabless for the following purposes:
You can
l e n r a n
V•a Temporary
o n -t storage of data: Data can be stored temporarily in one or more variables
nvalidating data input and for later processing.
for
• Manipulation of stored values: Variables can be used for calculations and other data
manipulations without accessing the database.
• Reusability: After they are declared, variables can be used repeatedly in other
statements.
A variable name starts with a dollar sign ($), as in the example: $num.

XML Fundamentals 8 - 17
Primary Expressions: Literals

A literal is a value that is not represented by an identifier:


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

• String literal
– $cname := "SCOTT"
• Numeric literal
– $ItemCode := 3568

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t an explicit snumeric, character, string, or Boolean value that is not represented by
A literalnis
l e r a n
t are two types of literals:
anaidentifier.
V o n -There
n literals: Characters, numerals, spaces, and special symbols that must be
• String
enclosed in quotation marks, as in the following examples:
“I”, “Hello”, “what’s up”
• Numeric literals: Integers, decimals, and exponents, as in the following examples:
30568, 30568.5, 30568.5E-2

XML Fundamentals 8 - 18
Primary Expressions: Constructors

A constructor creates an XML structure in a query:


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

• Direct constructor
• Computed constructor
– Computed element constructor
– Computed attribute constructor
– All document node constructors
– All text node constructors 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tcreate a sstructures in a query by using constructors. These are provided for
You can
e n r n
XML
al oattribute,
element,
V n -t document, text, comment, and processing instruction nodes. There are two
types ofnconstructors:
• Direct constructor: The name of the constructed element is a constant. The
constructor creates an element node. For example, the XQuery expression <a>33</a>
constructs the XML element <a>33</a>.
• Computed constructor: This can also be used to create nodes. A computed
constructor begins with a keyword that identifies the type of node to be created:
element, attribute, document, text, processing instruction, or comment.
- A computed element constructor creates an element node, allowing both the
name and the content of the node to be computed.
- A computed attribute constructor creates a new attribute node with its own
node identity.
- All document node constructors are computed constructors. The result of a
document node constructor is a new document node with its own node identity.
- All text node constructors are computed constructors. The result of a text node
constructor is a new text node with its own node identity.

XML Fundamentals 8 - 19
The following example shows the use of computed element and attribute constructors in a
simple case in which the names of the constructed nodes are constants.
element book {
attribute isbn {"isbn-0060229357" },
element title { "Harold and the Purple Crayon"},
element author {
element first { "Crockett" },
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

element last {"Johnson" }


}
}

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 Fundamentals 8 - 20
Sequence Expressions

• XQuery is a sequence-manipulation language.


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

• A sequence expression is a list of items that can be XML


nodes or simple content (number, date).
• A comma operator is used to concatenate two values or
sequences.
• A singleton sequence acts as a single item.
• A sequence expression is never nested.
s a
("Kiran", "Kumar", "XML", "Fundamentals")
) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t a general ssequence-manipulation language. An XQuery sequence expression
XQuerynis
e r a n
al oton-sequences
evaluates
V t of homogeneous or heterogeneous items. An item can be a simple
n
value (numbers, strings, Boolean values, dates, and so on), and it can also be one of the XML
node types (such as element, attribute, text, and so on).
The example in the slide shows the construction of a sequence with four atomic values. The
comma operator (,) concatenates two values or sequences, and parentheses ( ) facilitate
grouping.
A singleton sequence behaves in the same way as a single item:
(42) = 42
You cannot nest sequences. For example, (1, 2, (3, 4, (5), 6), 7) is treated as
(1, 2, 3, 4, 5, 6, 7).

XML Fundamentals 8 - 21
FLWOR Expressions

• The acronym FLWOR (pronounced “flower”) represents the


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

following XQuery clauses:


– for
– let
– where
– order by
– return a
h a s
• FLWOR supports iteration and binding of variables. )
r
ฺ ฺs
• FLWOR constructs a sequence in any order. ems
c @ u ide
for $identifier in expr(expr)
r o vi nt G
let $identifier := expr(expr)
where conditional_expr a jsto Stude
a m his
ฺ"descending")
order by identifier("ascending"n|
n t i e t
return expr_containing_result e
al to u s
( v
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t inaFLWOR
The acronym
n a n s fe
(pronounced “flower”) represents the XQuery clauses for, let, where,
V ale by,
order n -
andtrreturn. A FLWOR expression has at least one for or let clause and a
returnnoclause; single where and order by clauses are optional.
• for: Is analogous to the from clause of a SQL SELECT query. The for clause enables
you to iterate across a range of sequence values and bind each one of them to one or
more bind variables. At each iteration, the variables are bound in the order in which they
appear.
• let: Is analogous to the SQL SET statement. You can use the let clause to define
variables and assign them, in turn, during iteration through a for clause. You can bind
one or more variables. Just as with FOR, a variable can be bound by let to a value that
is computed by using another variable that is listed previously in the binding list of let
(or an enclosing for or let).
• where: Filters the for and let variable bindings according to some condition. This is
similar to a SQL WHERE clause.
• order by: Arranges the result (returned by the WHERE clause) in ascending or
descending order
• return: Constructs a result from the ordered, filtered values. This is the result of the
FLWOR expression as a whole, which can be compared to a SELECT statement. It is a
flattened sequence.

XML Fundamentals 8 - 22
FLWOR Expression Clauses

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

for Iterates across a range of sequence values and binds one or more
variables to each of any number of values. This is similar to the FROM
clause of a SQL SELECT query.
let Defines variables and assigns them, in turn, during iteration through a
for clause. This is analogous to the SQL SET statement.
where Filters the for and let variable bindings according to some condition.
This is similar to a SQL WHERE clause.
s a
order by Arranges the result (returned by the WHERE clause) in ascending or
h a
descending order
s ฺ rs)
return Constructs a result from the ordered, filtered values. Thism
e e
is the result
d ฺ
of the FLWOR expression as a whole, which can be@
c compared i
uto a
v i t G
SELECT statement. It is a flattened sequence.
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t i n
The components
n n
of sa FLWOR expression are as follows:
l e r a
-t for clause enables you to iterate across a range of sequence values and
V•a for: o n
The
n one or more variables to each of any number of values.
bind
Example:
for $i in (1,2,3,4)
return <project>{$i} </project>
The output of this example is:
<project>1</project><project>2</project><project>3</project><pr
oject>4</project>
Note: There can be multiple for clauses in the same FLWOR expression.
• let: You can define variables and assign values to them by using let in a for clause.
Example:
let $s := (<a/>, <b/>, <c/>)
return <out>{$s}</out>
The output of this example is:
<out><a/><b/><c/></out>

XML Fundamentals 8 - 23
• where: You can provide a condition, which works like a SQL WHERE clause:
where $i/price < 45
• order by: You can sort the result of the where filtering:
order by $i/department_name
• return: This gives you the final result after ordering and filtering of values:
return $i/department_name
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Note that using a FLWOR expression (with order by) is the only way to construct a sequence
in any order other than document order.
Note: In XQuery, nodes that are created when parsing an XML document are in an order
known as document order.

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 Fundamentals 8 - 24
departments.xml Document: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

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


<!–- This document is used by the XPath expressions on the
next few pages. -->
<departments>
<department num="1"><department_id>10</department_id>
<department_name>Administration</department_name>
</department>
<department num="2"><department_id>20</department_id>
<department_name>Marketing</department_name>
s a
</department>
) h a
<department num="3"><department_id>30</department_id>
<department_name>Purchasing</department_name> r s
sฺ eฺ
</department>
m
e uid
c @
<department num="4"><department_id>40</department_id>
r o vi nt G
jsto Stude
<department_name>Human Resources</department_name>
</department>
ฺm thisa
</departments>
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s document in the slide is used in the XPath expression examples on
The departments.xml
e n
l fewn-pages.
t r a n
theanext
V n o

XML Fundamentals 8 - 25
Using FLWOR Expressions

XQuery fragment (FLWOR expression):


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

Use the doc() XQuery function to


open the code_08_dept.xml file.

for $i in doc("code_08_dept.xml")//department
let $j := 20
where $i/department_id>$j
order by $i/department_name s a
return $i/department_name/text() ) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
Human Resources
a jsto Stude
Purchasing
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t in athensslide displays a FLWOR expression to extract department names that have
The example
e n
l than tr
V aless
IDs n -20.
no of the FLWOR expression clauses:
Explanation
• The FOR clause assigns all department elements in code_08_dept.xml to a variable
named $i.
• With the LET clause, $j is assigned the value 20.
• The WHERE clause is used to filter $i so that it retains only those departments that have
department_id values less than $j.
• The text value of the resultant department is returned by using the RETURN clause.
• The ORDER BY clause causes the output to be displayed in ascending order of
department_name.

XML Fundamentals 8 - 26
Path Expressions

• A path expression is used to locate nodes in trees by using


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

• A path always returns a sequence of distinct nodes in


document order.
• A path consists of a series of steps.
• A step may contain an axis, a node test, and zero or more
predicates.
s a
axis::node-test[predicates]
) 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s is an important part of building XQuery expressions. XQuery path
tpath expressions
e n
Building
r a n
al on-are
expressions
V t used to navigate the structure of an XML document and are similar to XPath.
n common task that is performed by using path expressions is to locate nodes by
The most
identifying their location in the hierarchy of an XML document.
Each path expression contains a series of one or more steps separated by a slash (/) or
double slash (//). Each step returns a sequence of nodes. Each location step consists of and
is defined by three types:
• An axis specifying the relationship between the selected nodes and the context node. In
XQuery, six axes are defined:
- attribute::
- child::
- descendent::
- descendent or self::
- parent::
- self::

XML Fundamentals 8 - 27
• A node test specifying the node type of the selected nodes. The node type that is
specified as the node test for a location step can be:
- A node-name representing either a qualified or an unqualified element name. If it
is prefixed with an @ symbol, node-name represents an attribute name.
- An asterisk (*) to match all element or attribute node names
- The text() type to select text nodes
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

- The processing-instruction() type for processing instruction nodes


- The comment() type for obtaining the comment nodes
- The node() type to match any of the preceding nodes
You can use the vertical bar (|) as a separator to combine node tests to create a rule
that matches multiple node types. For example, using /text()|comment() matches
either the text or the comment nodes of the root element.
Note: If the nodes in the XML document declare an XML namespace with a prefix, the
node name must be qualified by the namespace prefix in the path expression. a
• h a s
Zero or more predicates, or conditions that use arbitrary expressions to filter the set of
ฺrs) ฺ
selected nodes. A predicate is contained in brackets and is evaluated as a Boolean
s
em uide
expression for each node in a node set. If the condition is true, the node is included;
@
otherwise, the node is excluded from the results.
o v ic t G
t o r den
- The position() function determines the proximity position of a member node
s
aj s Stu
in a node set. Proximity position is a numbered position of the node in the
a ฺ m hi
sequence in which it appears in a node set. Proximity position numbers start at 1.
t i n e t
Path: l en o us
/departments/department[2]
a
O t t:
Output <department
d i c (v se t
num="2">
v en
ro<department_id>20</department_id>
t o l i c
a js ble
M fera
<department_name>Marketing</department_name>
a
n t i n n s </department>
l e r a
-t location step can contain a predicate. The example produces the
Va n- onEach <department_id> element containing the text string 10 if it is in a
<department> element whose num attribute value is less than 3.
Path: //department[@num<3]/department_id[.="10"]
Output: <department_id>10</department_id>
- You can use the or and and logical operators to combine predicate expressions.
Path: //department[@num>2 and @num<=4]/department_name
Output: <department_name>Purchasing</department_name>
<department_name>Human Resources
</department_name>

XML Fundamentals 8 - 28
Using Path Expressions to Filter
XML Data: Example
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

for $i in doc("code_08_dept.xml")//department
where $i/department_name = "Administration"
return $i

<department num="1"> s a
h a
ฺrs) ฺ
<department_id>10</department_id>
s
<department_name>Administration</department_name>
</department> em de @ Gui
i c
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t in athensslide shows the use of a path expression in XQuery. The FLWOR
The example
e n
expression
V -tr descendant <department> elements that contain a
al onreturns
n
<department_name> element whose text node is Administration.

XML Fundamentals 8 - 29
Using Conditional Expressions

if test_expr
then expr1
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

else expr2

for $i in doc("code_08_dept.xml")//department
where $i/department_id < 30
return <Result>
<department_id> {$i/department_id/text()}
</department_id>
<department> { if ($i/@num = 1)
then "Applicable"
else "NA" }
s a
</department>
</Result> ) h a
s r
ฺ ฺs
<Result> m
e uide
c @
vi nt G
<department_id>10</department_id>
<department>Applicable</department>
r o
</Result>
<Result> a jsto Stude
<department_id>20</department_id>
n a ฺm this
<department>NA</department> i
nt use
</Result> l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i n
XQuerynsupports
a s
anconditional expression based on the keywords if, then, and else. Each
l e - t r
a of theonexpression
part
a is itself an arbitrary expression. For instance, in the following
V n
conditional expression, each of the subexpressions can be any XQuery expression:
if ($po1/price < $po2/price)
then $po1
else $po2
In a conditional expression, condition_expr should evaluate to a Boolean value or to a
type that can be cast to Boolean. If the Boolean value is true, the result of the entire
conditional expression is the same as the result of evaluating expr1; otherwise, it is the same
as the result of evaluating expr2.
The output for the query example in the slide is shown in the slide.

XML Fundamentals 8 - 30
Quantified Expressions

• XQuery supports two kinds of quantification:


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

– Universal: every
– Existential: some
• The value of a quantified expression is always true or
false.
• Quantified expressions are shortcuts for using FLWOR
expressions. a
h a s
r s )
m sฺ eฺ
@ e uid
c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t expression s that uses some evaluates to true if at least one of the test expressions
A quantified
e n r a n
V althe Boolean
has
o n -t value true. When using every, the quantified expression is true only if
n
every evaluation of the test expression evaluates to true.

XML Fundamentals 8 - 31
Using Quantified Expressions
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

every $i in
doc("code_08_dept.xml")//department/department_id 1
satisfies $i< 40

--------
false

s a
) h a
some $i in r s
sฺ eฺ
doc("code_08_dept.xml")//department/department_ide m
i c @ Gu2id
rov dent
satisfies $i < 20
t
js Stu o
ฺm thisa
--------
i n a
true
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t 1ainnthe
1. Example s slide shows the use of the universal quantifier every. The department
e n r
l inncode_08_dept.xml
Va IDs o -t are 10, 20, 30, and 40; that is, not all values of
n
department id are less than 40. The output is therefore false.
2. Example 2 shows the use of the existential quantifier some. The query returns true if
there exists at least a single matching pair. Because there is at least one department
with department_id less than 20, the output is true.

XML Fundamentals 8 - 32
Lesson Agenda

• XQuery features and applications


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

– Introduction
– Applications and features
– Terminology
– Data model
– Syntax rules
• XQuery support in JDeveloper s a
h a
• XQuery expressions
s ฺrs) ฺ
• XQuery functions and operators
@ 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 8 - 33
XQuery Functions

The XQuery functions include the following:


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

• Constructor functions
• Functions on numeric values such as fn:abs(),
fn:ceiling(), fn:floor(), and fn:round()
• Functions on string values such as fn:concat(),
fn:substring(), fn:lower-case(), and fn:upper-
case()
s a
• Aggregate functions such as fn:count(), fn:avg(), h a
s ฺ rs)
fn:max(), fn:min(), and fn:sum() em eฺ
i c @ Guid
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s functions for string values, numeric values, sequence manipulation,
XQuerynincludes
e r a n
built-in
al ovalues,
Boolean
V n -t and so on.
n
Constructor functions
Every built-in atomic type has an associated constructor function.
xs:string($arg as xs:anyAtomicType?) as xs:string
xs:boolean($arg as xs:anyAtomicType?) as xs:boolean
Functions on Numeric Values
• fn:abs(): Returns the absolute value of the argument
• fn:abs(10.5) returns 10.5.

• fn:abs(-10.5) returns 10.5.


• fn:ceiling(): Returns the smallest number with no fractional part that is greater than
or equal to the argument
fn:ceiling(10.5) returns 11.
fn:ceiling(-10.5) returns -10.
• fn:floor(): Returns the largest number with no fractional part that is less than or
equal to the argument
fn:floor(10.5) returns 10.
fn:floor(-10.5) returns -11.

XML Fundamentals 8 - 34
• fn:round(): Rounds to the nearest number with no fractional part
fn:round(2.5) returns 3.
fn:round(2.4999) returns 2.
Functions on String Values
• fn:concat(): Accepts two or more arguments and casts them to xs:string, and
then returns the xs:string that is the concatenation of the values of its arguments
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

after conversion
let $v1 := "I plan to go to New"
let $v2 := "Delhi in September"
return concat($v1, $v2)
Output
"I plan to go to NewDelhi in September"
• fn:substring(): Accepts a source string and returns a substring located at a a
specified position
h a s
fn:substring("motor car", 6) returns "car".
s ฺrs) ฺ
em areuiselected.
Characters starting at position 6 of the literal to the end of the source string
@ de
fn:substring("metadata", 4, 3) returns "ada". c
vi nt G
r o
Characters at positions greater than or equal to 4 and less
a jstothan t deselected.
7uare
Aggregate Functions a ฺ m his S
fn:count(): Returns the number oftitems in
n t
in sea sequence

l e u
Assume $seq1 = (98.5, 98 3(va98.9).
98 9) to
(98 5 98.3,
o v ic nse
fn:count($seq1) returns
s t o r 3 .
l i ce
fn:count($seq1[.
M aj >ra100])ble returns 0.

n t ina anReturns
fn:avg(): sfe the average of a sequence of values
le $seq2r = (3, 4, 5).
Va non-t
Assume
fn:avg($seq2) returns 4.
• fn:max(): Returns the maximum value from a sequence of comparable values
fn:max((3,4,5)) returns 5.
fn:max((5, 5.0e0)) returns 5.0e0.
• fn:min(): Returns the minimum value from a sequence of comparable values
fn:min((3,4,5)) returns 3.
fn:min((5, 5.0e0)) returns 5.0e0.
• fn:sum(): Returns the sum of a sequence of values
Assume $seq3 = (3, 4, 5).
fn:sum($seq3) returns 12.
Note: The default prefix for the function namespace is fn:. You can call these built-in
functions without specifically declaring the namespace prefix.

XML Fundamentals 8 - 35
XQuery Operators

The XQuery operators on numeric values include the following:


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

• op:numeric-add()
• op:numeric-subtract
• op:numeric-multiply()
• op:numeric-divide()
• op:numeric-mod()
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t on Numeric s Values
l e n
Operators
r a n
o n -t
V•a op:numeric-add(): Backs up the + operator and returns the arithmetic sum of its
n
operands: ($arg1 + $arg2)
op:numeric-add($arg1 as numeric, $arg2 as numeric) as numeric
• Op:numeric-subtract: Backs up the – operator and returns the arithmetic difference
of its operands: ($arg1 - $arg2)
op:numeric-subtract($arg1 as numeric, $arg2 as numeric) as numeric
• op:numeric-multiply(): Backs up the * operator and returns the arithmetic product
of its operands: ($arg1 * $arg2)
op:numeric-multiply($arg1 as numeric, $arg2 as numeric) as numeric
• op:numeric-divide(): Backs up the div operator and returns the arithmetic
quotient of its operands
op:numeric-divide($arg1 as numeric, $arg2 as numeric) as numeric
• op:numeric-mod(): Backs up the mod operator; returns the remainder resulting from
dividing $arg1 (the dividend) by $arg2 (the divisor)
op:numeric-mod($arg1 as numeric, $arg2 as numeric) as numeric

XML Fundamentals 8 - 36
XQuery Comparison and Boolean Operators

• Xquery comparison operators on numeric values:


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

– op:numeric-equal()
– op:numeric-less-than()
– op:numeric-greater-than()
• Operators on Boolean values:
– op:boolean-equal()
– op:boolean-less-than() a
h a s
– op:boolean-greater-than() s )
r
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t Operators s on Numeric Values
l e n
Comparison
r a n
o n -t
V•a op:numeric-equal(): Returns true only if the value of $arg1 is equal to the value
n
of $arg2
op:numeric-equal($arg1 as numeric, $arg2 as numeric) as xs:boolean
• op:numeric-less-than(): Returns true only if $arg1 is less than $arg2
op:numeric-less-than ($arg1 as numeric, $arg2 as numeric) as
xs:boolean
• op:numeric-greater-than(): Returns true only if $arg1 is greater than $arg2
op:numeric-greater-than($arg1 as numeric, $arg2 as numeric) as
xs:boolean
Operators on Boolean Values
• op:boolean-equal(): Returns true if both arguments are true or if both
arguments are false
op:boolean-equal($value1 as xs:boolean, $value2 as xs:boolean) as
xs:boolean

XML Fundamentals 8 - 37
• op:boolean-less-than(): Returns true if $arg1 is false and $arg2 is true;
otherwise returns false
op:boolean-less-than($value1 as xs:boolean, $value2 as xs:boolean)
as xs:boolean
• op:boolean-greater-than(): Returns true if $arg1 is true and $arg2 is false;
otherwise returns false
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

op:boolean-greater-than($value1 as xs:boolean, $value2 as


xs:boolean) as xs:boolean

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 Fundamentals 8 - 38
Useful Websites About XML
Topic Website
W3C XML Schema http://www.w3.org/XML/Schema
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

W3C XML Technology http://www.w3.org/standards/xml/

XML 1.0 W3C Recommendation http://www.w3.org/TR/xml/

W3C XML Schema www.w3.org/2001/XMLSchema

XML Schema Part 0: Primer http://www.w3.org/TR/xmlschema-0/

W3C XML Schema Structures http://www.w3.org/TR/xmlschema-1/

W3C XML Schema Datatypes http://www.w3.org/TR/xmlschema-2/


s a
W3C XML XPath Version 1.0 http://www.w3.org/TR/xpath/ h a
XPath Tutorial s ฺrs) ฺ
http://www.zvon.org/xxl/XPathTutorial/General/examples.html

XQuery Tutorial http://www.w3.org/TR/xquery/ @ em uide


o v ic t G
W3C Namespaces in XML http://www.w3.org/TR/xml-names/
t o r den
s
aj s Stu
Document Object Model (DOM)
a ฺ m
http://www.w3.org/DOM/
hi
t i n e t
XSL Transformations (XSLT)
en o us
http://www.w3.org/TR/xslt
a l
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 8 - 39
Useful W3C Websites About XQuery

• http://www.w3.org/TR/xpath-datamodel/
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• http://www.w3.org/TR/xpath-functions/#comp.numeric
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 8 - 40
Quiz

Which of the following statements are true about XQuery?


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

a. Is developed by W3C and built on other W3C standards,


such as XML, namespaces, XSLT, XPath, and XML
Schema
b. Queries XML data from both XML files and relational
databases
c. Works with the XML data model and is a comprehensive a
h a s
query language for data that is expressed in XML
s ฺ rs)
d. Consists of a set of possible expressions that e m ideฺ
are
evaluated and return values @ Gu
vic ro dent
t
js Stu o
ฺm thisa
i n a
l e nt use
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t b, c,adns
Answer:na,
l e r
Va non-t

XML Fundamentals 8 - 41
Summary

In this lesson, you should have learned how to:


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

• Describe XQuery, its features, and its applications


• Describe the data structures and fields of an XQuery data
model
• Describe XQuery support in JDeveloper
• Use XQuery expressions
• Use XQuery functions and operators a
as ) h
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n r a n
Va non-t

XML Fundamentals 8 - 42
Practice 8: Overview

This practice covers the following topics:


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

• Creating XQuery files in JDeveloper


• Using XQuery expressions:
– Path
– FLWOR
– Conditional
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a s is to use XQuery expressions to retrieve data from XML documents.
tof this apractice
n
The goal
e r n
V alcreate
You
o n -t files in JDeveloper, execute them, and view the results.
XQuery
n

XML Fundamentals 8 - 43
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ฺ

Introduction to Oracle XML DB

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this lesson, you should be able to:


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

• List the XML features in the database


• Explain the architecture of the Oracle XML DB
components
• List the benefits of the components
• Describe Oracle XML DB support for XQuery
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t you
In this lesson, n s about Oracle XML DB and its components. You also learn about
learn
e
al XMLType,
using r a
-t as well as the XML Schema and Repository.
V o n
n more information, see the Oracle XML DB Developer's Guide 12c
Note: For
Release 1 (12.1).

XML Fundamentals 9 - 2
Lesson Agenda

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

• Features of Oracle XML DB


• XMLType
• XML Schema support in Oracle Database
• XML DB Repository
• Benefits of Oracle XML DB
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 9 - 3
What Is Oracle XML DB?

Oracle XML DB:


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

• Is a high-performance XML storage and retrieval


technology
• Provides standard access methods for navigating and
querying XML
• Is a merger of XML and relational database technology
s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto Stude
Repository n a ฺm this
i
nt use
XMLType
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t DB aisnashigh-performance XML storage and retrieval technology that is available
OraclenXML
e
althe Oracle
with - tr12c server. It fully absorbs the W3C XML data model into the Oracle server,
V n
no standard access methods for navigating and querying XML. Oracle XML DB
and provides
combines the advantages of relational database technology with XML technology.
Oracle XML DB:
• Is a native XML database, which enables you to build standards-based XML
applications and implementations
• Provides a storage-independent, content-independent, and programming language–
independent infrastructure to manage data. Although it is closely integrated with the
Oracle SQL engine, it introduces newer concepts never before present in relational
databases. For example, it builds XML Document Object Model (DOM) into the Oracle
kernel so that most of the operations on XML data can be performed as a part of regular
database processing.
• Is designed to store and retrieve XML objects by using relational and hierarchical
access APIs, and provides fine access control and configuration management for the
XML objects
• Implements popular protocols such as FTP, HTTP, and Web-based Distributed
Authoring and Versioning (WebDAV) to improve data access performance, and is
designed around open standards and implemented to provide different views and
intuitive access to a single repository
XML Fundamentals 9 - 4
When to Use Oracle XML DB
Achieving Faster Storage and Retrieval of Complex XML Documents
Users today face a performance barrier when storing and retrieving complex, large, or many
XML documents. Oracle XML DB provides very high performance and scalability for XML
operations. The major performance features are:
• Native XMLType
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Lazily evaluated virtual DOM support


• Database-integrated XQuery, XPath, and XSLT support
• XML Schema–caching support
• Indexing (both full-text and XML)
• Hierarchical index over the XML DB Repository
Integrating Applications
With Oracle XML DB, you can access data from disparate systems through gateways and be
s a
able to combine them into one common data model. This reduces the complexity of
) h a
developing applications that must deal with data from different stores. r s
sฺ eฺ
When Your Data Is Not XML m
e uid
c @
XMLType views provide a way for you to wrap existing relational
r o viand structured
n t G data in XML
format. This is especially useful if, for example, your legacy
j s to datatuisdnote in XML but you need to
migrate to an XML format. Using XMLType views,m a
ฺ youando not S
s need to alter your application
code. To use XMLType views, you must firstin a
register t h i
XML Schema with annotations that
n t e
represent a bidirectional mapping between
v a uscreate
le thetoXML Schema data types and either SQL data
types or binary XML encoding types
i c ( se
types. You can then an XMLType view that conforms to
this schema (mapping) by o
r v
providing annunderlying query that constructs instances of the
e
appropriate types. js t o l i c
MaFidelity a b le
Support fora
t i n
DOM
s f er
a len ofnObject
Document
- t ranModelXML (DOM) fidelity is a data-integrity mechanism for retaining the
V no
structure a retrieved document compared to the original XML document for DOM
traversals.
All elements and attributes declared in the XML Schema are mapped to separate attributes in
the corresponding SQL object type. Some information is not represented directly by these
elements or attributes. To assure the integrity and accuracy of this data, when regenerating
XML documents stored in the database, Oracle uses DOM fidelity.
DOM fidelity refers to how identical the returned XML documents are compared to the original
XML documents, particularly for purposes of DOM traversals.
XMLType achieves DOM fidelity by maintaining information that SQL or Java objects normally
do not provide for, such as:
• The ordering of child elements and attributes
• Unstructured content declared in the Schema
• Undeclared data in instance documents such as processing instructions, comments,
and namespace declarations
• Support for basic XML data types that are not available in SQL (Boolean, QName, and
others)
• Support for XML constraints (facets) that are not supported directly by SQL

XML Fundamentals 9 - 5
Oracle XML DB

XML Services:
• XML Schema Registration
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Create Tables
Oracle Database • Insert, Delete, and Update XMLType tables
• Index XML
Oracle XML DB • Query XML
• Validate XML
XMLType • Transform XML
Tables, Retrieve / Generate XML Using XMLType APIs
Columns,
• SQL, Java, PL/SQL, C, and C++
and Views
s a
) h a
XML Services: r s
sฺ eฺ
Oracle m
XML DB
• Foldering
@ e uid
• Versioning
i c G
• ACL Security rov t
Repository
n
e XMLType APIs
sto SXML
Retrieve / jGenerate
a t udUsing
ฺmJava,thand
• SQL,
n a is PL/SQL
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t DB aisnasset of Oracle Database technologies that are related to high-performance
OraclenXML
e
al oofnXML
handling -tr data: storing, generating, accessing, searching, validating, transforming,
V n and indexing. It provides native XML support by encompassing both the SQL and
evolving,
XML data models in an interoperable way. Oracle XML DB is included as part of Oracle
Database. You learn about the key features that enable you to perform these operations in
the pages that follow.
Oracle XML DB Key Components
• XMLType storage: XML data, including XML Schema definition files, can be stored in the
XMLType columns and tables.
• Oracle XML DB Repository: This enables you to organize and manage database
content in the form of files and folders, which are called resources. Oracle XML DB
implements popular protocols such as FTP, HTTP, and Web-based Distributed
Authoring and Versioning (WebDAV) to improve data access performance, and is
designed around open standards and implemented to provide different views and
intuitive access to a single repository. The Oracle XML DB Repository can be accessed
by using Internet protocols such as WebDAV, FTP, and HTTP, and is as fast as
accessing data from any file system.

XML Fundamentals 9 - 6
Oracle XML DB: Benefits

• Unified relational data and XML content:


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

– Enhances native database support for XML


– Stores and manages structured, unstructured, and semi-
structured data
– Offers transparent XML and SQL interoperability
– Exploits database features
– Exploits XML features a
• Faster storage and retrieval of complex XML documents: h a s
r s )
– Higher performance of XML operations m sฺ eฺ
@ e uid
– Higher scalability of XML operations c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tRelational sData and XML Content with Oracle XML DB
e n
Unifying
r a n
V al application
Most o n -t data and web content are stored in a relational database or a file system, or
n
in a combination of both. When stored in relational storage, XML loses DOM fidelity. In
industry, structured data is currently moving to XML for transport on the Internet to add
structure to the content. Data is becoming more semi-structured. As the volume of XML data
that is transported grows, the cost of regenerating these XML documents grows, and the
storage methods become less effective for accommodating XML content. With Oracle XML
DB, you have enhanced native support for XML.
With Oracle XML DB, you can store and manage structured, unstructured, and pseudo- or
semi-structured data by using a standard data model and standard SQL and XML. You can
store structured data in structured storage such as tables and columns, and unstructured data
in large objects (LOBs). Oracle Database 11g introduced binary XML storage.
By using Oracle XML DB, you have complete transparency and interchangeability between
XML and SQL. With Oracle XML DB, you can perform:
• XML operations on structured data (such as tables)
• SQL operations on XML documents

XML Fundamentals 9 - 7
Oracle Database has the following key database capabilities for working with XML:
• Indexing and search: Applications use queries such as “find all product definitions
created between March and April 2002,” a query that is typically supported by a B-tree
index on a date column. Oracle XML DB can enable efficient structured searches on
XML data, saving content-management vendors the need to build proprietary query
APIs to handle such queries.
• Updates and transaction processing: Fast updates of subparts of records are
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

possible, with minimal contention between the users who are trying to update. File or
CLOB storage cannot provide the granular concurrency control that Oracle XML DB
does.
• Managing relationships: Data with any structure typically has foreign key constraints.
XML data stores generally lack this feature, so you must implement any constraints in
application code. With Oracle XML DB, you can constrain XML data according to XML
schema definitions, and therefore achieve control over relationships that structured data
has always enjoyed.
s a
h a
• Multiple views of data: Most enterprise applications need to group data together in
)
r s
sฺ eฺ
different ways for different modules. This is why relational views are necessary: to allow
m
e uid
for these multiple ways to combine data. By allowing views on XML, Oracle XML DB
c @
types of applications). r o vi nt G
creates different logical abstractions on XML (for example, for consumption by different

a jsto Stude
• Performance and scalability: Users expect data storage, retrieval, and query to be
a ฺm this
fast. Loading a file or CLOB value, and parsing, are typically slower than relational data
n
i
nt use
access. Oracle XML DB dramatically speeds up XML storage and retrieval.
l e
( va e to
• Ease of development: Databases are foremost application platforms that provide
c
r o vi ens
standard, easy ways to manipulate, transform, and modify individual data elements.
s to le lic
Even though typical XML Parsers give standard read access to XML data, they do not
j
M a rab
provide an easy way to modify and store individual XML elements. Oracle XML DB
n t ina ansfe
supports several standard ways to store, modify, and retrieve data. These include XML

V ale on-tr
Schema, XQuery, XPath, DOM, and Java.
n
If the drawbacks of XML file storage force you to break down XML into database tables and
columns, there are several XML advantages that you still have:
• Structure independence: The open content model of XML cannot be captured easily in
the pure tables-and-columns world. XML schemas allow global element declarations,
not just scoped to a container. Therefore, you can find a particular data item regardless
of where in the XML document it moves to as your application evolves.
• Storage independence: When you use relational design, your client programs must
know where your data is stored, in what format, what table, and what the relationships
are among those tables. By using XMLType, you can write applications without that
knowledge, and database administrators can map structured data to physical table-and-
column storage.

XML Fundamentals 9 - 8
• Ease of presentation: XML is understood natively by web browsers, many popular
desktop applications, and most Internet applications. Relational data is generally not
accessible directly from applications. Additional programming is required to make
relational data accessible to standard clients. Oracle XML DB stores data as XML and
makes it available as XML outside the database. No extra programming is required to
display database content.
• Ease of interchange: XML is the language of choice in business-to-business (B2B)
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

data exchange. If you are forced to store XML in an arbitrary table structure, you are
using some kind of proprietary translation. Whenever you translate a language,
information is lost and interchange suffers. By natively understanding XML and
providing DOM fidelity in the storage/retrieval process, Oracle XML DB affords a clean
interchange.
Users today face a performance barrier when storing and retrieving complex, large, or many
XML documents. Oracle XML DB provides high performance and scalability for XML
operations. The major performance features are Native XMLType, XQuery, XPath, and XSLT a
a s
support, indexing, both full-text and XML, and a hierarchical index over Oracle XML DB
h
Repository.
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 Fundamentals 9 - 9
An XML schema uses vocabulary defined by the schema for schemas to create a collection of
XML Schema type definitions and element declarations that comprise vocabulary for
describing the contents and structure of a new class of XML documents, the XML instance
documents that conform to that XML schema.
Note
In this course, the term “XML schema” (lowercase “s”) is used to reference any XML schema
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

that conforms to the W3C XML Schema (uppercase “S”) Recommendation. Because an XML
schema is used to define a class of XML documents, the term “instance document” is often
used to describe an XML document that conforms to a particular XML schema.
XMLType Storage Models
XMLType is an abstract data type that provides different storage models to best fit your data
and your use of it. As an abstract data type, your applications and database queries gain
flexibility: the same interface is available for all XMLType operations. Because different
storage (persistence) models are available, you can customize performance and functionality
to best fit the kind of XML data that you have and the pattern of its use. One key decision to s a
make when using Oracle XML DB for persisting XML data as XMLType is determining which ) h a
storage model to use for which XML data. r s
sฺ eฺ
m
e uid
XML/SQL Duality
c @
A key objective of Oracle XML DB is to provide XML/SQL duality. r o vi XMLnprogrammers
t G can
s t o d e
leverage the power of the relational model when working
m ajworking
with XML
S tu content and SQL
programmers can leverage the flexibility of XML
n a ฺ
when
t h is with relational content.
Thereby, you can use the most appropriate i
nttoolsuforsea particular business problem.
l e
XML/SQL
/SQ duality
dua ty means
ea s tthat
at the
t e same
c
sa
( vae datae to be eexposed
ca
can posed as rowso s in a tab
table
eaandd
o v
manipulated using SQL, or exposed i as
n s
nodes in an XML document and manipulated using
r c e
li transformation. Access and processing techniques are
jsto and
techniques such as DOM
independent of theaunderlyingb l
XSL
estorage format.
M r a
ina provide
These features
n t n s fesimple solutions to common business problems. For example:
You
V n tra XML DB SQL functions to generate XML data directly from a SQL query.
e use-Oracle
alcan
You cannothen transform the XML data into other formats, such as HTML, by using the
database-resident XSLT Processor.
You can access XML content without converting between different data formats, by using
SQL queries, online analytical processing (OLAP), and business-intelligence and data
warehousing operations.
You can perform text, spatial data, and multimedia operations on XML content.
SQL/XML Standard Functions
Oracle XML DB provides the SQL functions that are defined in the SQL/XML standard.
SQL/XML functions fall into two groups:
• Functions that you can use to generate XML data from the result of a SQL query. In this
course, these are called SQL/XML publishing functions. They are also sometimes called
SQL/XML generation functions.
• Functions that you can use to query and access XML content as part of normal SQL
operations. In this course, these are called SQL/XML query and access functions.

XML Fundamentals 9 - 10
Using SQL/XML functions, you can address XML content in any part of a SQL statement.
These functions use XQuery or XPath expressions to traverse the XML structure and identify
the nodes on which to operate. The ability to embed XQuery and XPath expressions in SQL
statements greatly simplifies XML access.
Automatic Rewriting of XQuery and XPath Expressions
SQL/XML functions and XMLType methods use XQuery or XPath expressions to search
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

collections of XML documents and to access a subset of the nodes contained within an XML
document. In many cases, Oracle XML DB is able to automatically rewrite such expressions
to code that executes directly against the underlying database objects.
Overview of Oracle XML DB Repository
Oracle XML DB Repository is a component of Oracle Database with which you can handle
XML data by using a file, folder, or URL metaphor. The repository contains resources, which
can be either folders (directories, containers) or files.

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 Fundamentals 9 - 11
Implementing Oracle XML DB
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Repository s a
XMLType
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
Folders Access control i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t DBaKey s Components
OraclenXML
e r n
V al oprovides
XMLType n -t native XML storage and retrieval capability in the database because it is
stronglynintegrated with SQL. XML data, including XML Schema definition files, can be stored
in XMLType columns and tables. Physical storage of the XML data can be in LOBs, object-
relational (O-R) structures, or a hybrid of LOBs and O-R structures.
Oracle XML DB Repository is a component of Oracle Database 12c that is optimized for
handling XML data as files in a file system. Oracle XML DB Repository provides foldering,
access control, versioning, and other services for XML resources. Oracle XML DB Repository:
• Enables the creation of folders forming a “file system” structure in the database
• Stores data resources (database objects) such as XML, XSL, and XML Schema
documents in folders
• Can be used for fast access and manipulation of XML content, based on path names to
the XML data stored in the database
Oracle XML DB Repository can be accessed by using Internet protocols such as WebDAV,
FTP, and HTTP, and is as fast as accessing data from any file system.

XML Fundamentals 9 - 12
Lesson Agenda

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

• Features of Oracle XML DB


• XMLType
• XML Schema support in Oracle Database
• XML DB Repository
• Benefits of Oracle XML DB
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 9 - 13
Oracle XML DB: Features

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

XMLType Combines SQL and XML features

XML Schema Constrains XML documents to registered XML schemas

DOM Fidelity Preserves integrity of documents

XML Schema storage Provides structured storage to store XML

XML Schema validation Provides schema-compliant document structure


s a
) h a
XML Piecewise update Updates individual elements 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a
t is a nativesdata type that is used to store and manipulate XML. Multiple storage
XMLType
e n r a n
al on-t as structured storage and binary XML storage—are available with XMLType.
options—such
V n
Administrators can choose a storage option that meets their requirements. The structured
XML storage option is a decomposition of XML into underlying table and column structures
(automatically created and managed by Oracle) for better SQL query and manipulation
performance. Oracle Database 11g introduced the binary XML storage model for XMLType.
You learn more about the storage options in the section titled “XMLType Storage Models.”
With XMLType objects, you can perform SQL operations on the XML data it contains, such as:
• Queries, online analytical processing (OLAP) functions, and SQL/XML operations
• XPath searches and XSL Transformations (XSLT)
You can build regular SQL indexes or Oracle Text indexes on XMLType for high performance
of queries in a very broad spectrum of applications.
XPath search syntax can be used, embedded in a SQL statement or as part of an HTTP
request, to query XML content in the database. XPath expressions can be used to specify
parts of your document when you create XML indexes for XPath searches, for fast access to
content in XML documents.

XML Fundamentals 9 - 14
Oracle XML DB: Features

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

XPath Search Provides XPath syntax for querying

XML Indexes Enable faster access to documents

XML Transformations Transform XML documents using XSLT

PL/SQL interface Enables access and manipulation of data

XML Views Offer views over relational data or fragments


s a
) h a
Schema caching Minimizes access time r s
sฺ eฺ
Oracle XML DB Enables foldering; provides security by using m
e accessid
Repository control lists (ACLs); provides storage i c @
for G u
resources
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s for XMLType
XSL Transformations
e n r a n
V alXSLTonto-transform
Use
t XML documents through a SQL operator. These XSL transformations
providenhigh performance because they reside in the database.
PL/SQL Interface
Use DOM and other APIs for accessing and manipulating XML data. You can get static and
dynamic access to XML.
XMLType Views
You can use XMLType views to create permanent aggregations of various XML document
fragments or relational tables. You can also create views over heterogeneous data sources by
using Oracle Gateways.
Oracle XML DB Schema caching maintains structural information for registered XML
Schema documents such as element names, data types, and storage locations. The Schema
cache minimizes access time to the XML Schema and storage costs.
As previously discussed, Oracle XML DB Repository can be used for foldering whereby you
can view the XML content stored in Oracle XML DB as a hierarchy of directory-like folders.
The repository supports access control lists (ACLs) for any XMLType object.

XML Fundamentals 9 - 15
Oracle XML DB: Features

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

XML generation Provides high-performance generation of XML

XML Indexes Enables managing features such as configuration


parameters, repository resources

Enterprise Manager Enables querying, transforming, and accessing XML and


support relational data

PL/SQL interface Provides appropriate tools for a particular business s a


problem ) h a
r s
sฺthrough
XQuery language Enables applications to access Oracle Database
e m d e ฺ
support web services c@ t Gu i
v i
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t i n
XML generation
n s
isnpossible by using SQL/XML functions such as SYS_XMLGEN and
l e t r a
a on- which provide native, high-performance generation of XML from SQL queries.
V
SYS_XMLAGG,
n
Oracle XML DB provides functions that conform to the ANSI/ISO SQL/XML standard.
SQL/XML functions are used to create an XML document or fragments containing data from
columns in a SQL query.
You can use Oracle Enterprise Manager to manage the following Oracle XML DB features:
• Configuration parameters
• Repository resources
• Repository access control lists
• XML schemas
• XMLType tables and columns
XQuery is the XML Query language developed by W3C. Oracle XML DB supports the latest
version of the XQuery language specification, W3C XQuery 1.0 recommendation. It is similar
to SQL in many ways, but just as SQL is designed for querying structured, relational data,
XQuery is designed especially for querying XML data from a variety of data sources. You can
use XQuery to query XML data wherever it is found, whether it is stored in database tables or
is available through web services, and so on. In addition to querying XML data, you can use
XQuery to construct XML data. You learn more about XQuery in the lesson titled “Working
with XQuery.”
XML Fundamentals 9 - 16
XML programmers can leverage the power of the relational model when working with XML
content and SQL programmers can leverage the flexibility of XML when working with
relational content.
The SQL/XML duality allows you to use the most appropriate tools for a particular business
problem. SQL/XML duality means that the same data can be exposed as rows in a table and
manipulated by using SQL and XQuery, or exposed as nodes in an XML document and
manipulated by using techniques such as XQuery rewrite, DOM, and XSL transformation.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Your applications can access Oracle Database by using native Oracle XML DB web services.
One available service allows you to issue SQL and XQuery queries and receive results as
XML data. Another service provides access to all PL/SQL stored functions and procedures.
You can customize the input and output document formats when you use the latter service.
Web Services Description Language (WSDL) is automatically generated by the native
database web services engine.

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 Fundamentals 9 - 17
Lesson Agenda

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

• Features of Oracle XML DB


• XMLType
• XML Schema support in Oracle Database
• XML DB Repository
• Benefits of Oracle XML DB
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 9 - 18
XMLType: Overview
XMLType:
• Is an abstract data type for the native handling of XML
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

data in the database


• Can be used as the data type for a column or an object
table
• Stores XML content
– Can be used in PL/SQL stored procedures as parameters,
return values, and variables s a
– Can represent an XML document in the database, so its)ish a
accessible in SQL s ฺr ฺ
m e e id
• Provides a SQL API with functions that operate
v i c@ ton
G uXML
data t o ro den
j s u a s St
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
XMLType
l e r a
n-t data type that stores XML content. The term native indicates that it is the
V•a Isnaonative
natural way of storing XML and it is more efficient than storing it as serialized text.
• Can be used as the data type of a column in a table or view, or as the data type of an
object table
• Is designed to store XML content in an optimal way
• Can be used in PL/SQL stored procedures as parameters, return values, and variables
• Can represent an XML document in the database, so it is accessible in SQL
• Provides a SQL API framework that has built-in functions that operate on XML content.
For example, you can use XMLType functions to create, extract, and index XML data
that is stored in the Oracle database.
• Provides its functionality through a set of APIs that are available to PL/SQL, Java, C++,
and C
In Oracle XML DB, you can store an XML document in two ways: as an XMLType column in a
relational table, or as an XML object in an XMLType table.
With the capabilities of XMLType, you can leverage the power of the relational database while
working in the context of XML. Similarly, you can leverage the power of XML standards while
working in the context of a relational database.

XML Fundamentals 9 - 19
Using XMLType

Use XMLType to:


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

• Store and query XML data


• Provide efficient XPath access
• Shield applications from storage models
• Prepare for future optimizations

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t whenever s you want to use the database for persistent storage of XML data.
e n
Use XMLType
r a n
V alXMLType
Use o n -t to use SQL queries on a part of the document or on the entire document.
n
XMLType features include strong typing inside SQL statements and PL/SQL functions. Strong
typing ensures that the values passed are XML values and not arbitrary text strings.
The XML schema language defines 47 scalar data types. This provides for strong typing of
elements and attributes. Oracle XML DB leverages this strong typing feature of XML schema
to process XML data safely and efficiently.
XMLType uses the built-in C XML Parser and Processor. Therefore, it provides better
performance and scalability when used inside the server.
Because Oracle Database is natively aware that XMLType can store XML data, better
optimizations and indexing techniques are possible. By writing applications to use XMLType,
you can achieve these optimizations and enhancements, and preserve them in future
releases without needing to rewrite the applications. By using XMLType instead of the
standard relational or binary XML storage, applications can accommodate various storage
alternatives while minimizing changes to the SQL statements in the application.

XML Fundamentals 9 - 20
XMLType Storage Models

Oracle XML DB provides two storage models for XMLType


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

tables and columns:


• Binary XML storage
– XMLType data is stored in a post-parse, binary format that is
specifically designed for XML data.
– Binary XML is compact, post-parse, XML schema–aware
XML. This is also referred to as post-parse persistence.
s a
• Object-relational storage
) h a
– XMLType data is stored as a set of objects. r s
sฺ eฺ
m
e object-
id
– This is also referred to as structured storage i c @and
G u
based persistence.
t o rov dent
– You can embed CLOB storagemwithin a js object-relational
S tu
storage. This is called hybrid n ฺ
a storage. t his
t i e
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n i n
t is a native
n sserver data type that enables the database to understand that a column
XMLType
or a e
l contains
table r a
-t XML data. This is similar to the DATE data type that enables the Oracle
V
database o n
n to understand that a column contains a date value. XMLType is an abstract data
type that provides different storage models to best fit your data and your use of it. As an
abstract data type, your applications and database queries gain flexibility, because the same
interface is available for all XMLType operations. You can optimize performance and
functionality by selecting a storage model that best fits the kind of XML data that you have
and the pattern of its use. The purpose of abstraction in XMLType is to have a single data
type from your application’s point of view.
XMLType tables and columns can be stored in the following ways:
• Binary XML storage: The XMLTYPE data is stored in post-parse binary format that is
specifically designed for XML data. Binary XML is compact, post-parse, and XML
schema–aware XML. The biggest advantage of Binary XML storage is its flexibility. You
can use it for schema less documents, or when the schema allows for high availability.
This is also referred to as post-parse persistence.

XML Fundamentals 9 - 21
Object-Relational storage: The XMLTYPE data is stored relationally as a set of objects. This
is also referred to as structured storage and object-based persistence. The biggest advantage
of object-relational storage is that it provides the best performance in structured cases. The
query performance matches that of relational tables, and updates can be performed in-place.
It also provides relational-like schema evolution capability.
In object-relational XMLType storage, an XML document is broken up and stored object-
relationally, but you can choose to store one or more of its XML fragments as embedded
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

CLOB instances. A typical use case for this is mapping an XML-schema complexType or a
complex element to CLOB storage, because you generally access the entire fragment as a
unit. This is sometimes called hybrid storage. For example, you can embed CLOB storage
within o-r storage.
Notes
• Each of the storage models has its own advantages and disadvantages in different
dimensions, such as performance and flexibility. No single storage model is right for all
use cases.
s a
h
• For additional information about the available XMLType storage models, see the a
following resources: s ฺrs) ฺ
- Oracle XML DB Developer’s Guide 12c Release 1 (12.1)
@ em uide
o v ic t G
- The white paper titled “Oracle XML DB: Choosing the Best XMLType Storage
t o r den
Option for Your Use Case” on OTN: s
aj s Stu
a ฺ m
http://www.oracle.com/technetwork/database/features/xmldb/xmlchoosestorage-
hi
v1-132078.pdf 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 Fundamentals 9 - 22
Lesson Agenda

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

• Features of Oracle XML DB


• XMLType
• XML Schema support in Oracle Database
• XML DB Repository
• Benefits of Oracle XML DB
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 9 - 23
XML Schema Support
in Oracle Database 12c
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

DBMS_XMLSCHEMA

s a
h a
Local
s rs)
Global

schema
e mschema
i d eฺ
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
Oraclen t i n
Database n s XML schema support. You can perform the following tasks:
provides
l e r a
-tW3C-compliant local and global XML schema.
V•a Register
n o n
• Validate your XML documents against registered XML schema definitions.
• Generate XML schema from object types.
• Reference an XML schema that is owned by another user.
• Explicitly reference a global XML schema when a local XML schema exists with the
same name.
• Generate a structured database mapping from your XML schemas during XML schema
registration. This includes generating SQL object types, collection types, and default
tables, and capturing the mapping information by using XML schema attributes.
• Specify a particular SQL type mapping when there are multiple legal mappings.
• Create XMLType tables, views, and columns based on registered XML schemas.
• Perform manipulation (data manipulation language) and queries on XML schema–based
XMLType tables.
• Automatically insert data into default tables when schema-based XML instances are
inserted into the Oracle Database Repository by using FTP, HTTP, or SQL.

XML Fundamentals 9 - 24
XML Schema and Oracle XML DB

• Creating XML schema–based tables and columns


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

• Creating constraints on XML tables and columns


• Using queries and DML on XML tables and columns
• Using Oracle XML schema annotations
• Generating SQL types
– A SQL object type is generated for each ComplexType that
is defined in the XML schema. s a
h a
s ฺ s)
– The definition of the SQL object mirrors the definitionrof
ComplexType.
e m ideฺ
– Each child element and attribute definedvbyi c@ G u
ComplexType
t is
o
tor type. n
mapped to an attribute of the SQLsobject de aj s Stu
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
Creatingn i n n s
t XML Schema–Based Tables and Columns
l e t r a
V
You
n n- XMLType tables based on an XML schema that is already registered. Hidden
a can ocreate
columns are created corresponding to the object type to which the primary key element is
mapped. In addition, an XMLExtra object column is created to store top-level instance data,
such as namespaces declarations and so on.
Creating Constraints on XML Tables and Columns
You can create unique and referential integrity constraints on XML schema–based XMLType
tables. The constraints are enforced every time a change is made to the stored XML
documents.
Querying and DML on XML Tables and Columns
New instances can be inserted into an XMLType table. The XMLType table can be queried by
using the XPath-based SQL operators. The XPath rewrite mechanism rewrites queries that
involve XPath arguments to SQL operators to access the underlying object attribute columns
directly, thereby avoiding construction of XML followed by subsequent XPath evaluation.

XML Fundamentals 9 - 25
Using Oracle Schema Annotations
Using Oracle XML DB, application developers and database administrators can annotate the
XML schema because they are given greater control over the set of objects that is generated
from the XML schema.
Generating SQL Types
During the schema registration process:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Oracle XML DB generates a SQL object type for each ComplexType defined in the
XML schema
• The definition of the SQL object mirrors the definition of ComplexType
• Each child element and attribute defined by ComplexType is mapped to an attribute of
the SQL object type

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 Fundamentals 9 - 26
XMLType and XML Schema

• When creating an XMLType instance, you can specify that


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

it conforms to a preregistered XML schema.


• An XML schema is registered by using the
DBMS_XMLSCHEMA package.

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
In Oracle
n i n
t Database n s12c, you can create XMLType tables and columns. For example, you
e
al theooption
have r a
-t of specifying that they:
V n n
• Conform to a preregistered XML schema
• Are stored in the object-relational format specified by the XML schema
You can also choose to wrap existing relational and object-relational data into XML format by
using XMLType views.
You can store an XMLType object as:
• XML schema–based objects, which can be stored in object-relational format in tables,
columns, and views
• Non-XML-schema-based objects, which are stored as binary XML
Mapping from an XML instance document to object-relational or binary XML storage is
defined by registering your XML schema in Oracle Database 12c by using the
DBMS_XMLSCHEMA package. You must register the XML schema before you store XML
schema–based instance documents. To reference a registered XML schema, use its URL.
You can also store an XMLType object in binary XML format.

XML Fundamentals 9 - 27
Oracle XML DB supports the use of the W3C XML schema in two ways:
• Automatic validation of instance documents
• Definition of storage models
To use a W3C XML schema with Oracle XML DB, the XML schema document must be
registered with the database. After you register an XML schema, you can create XMLType
tables and columns that conform to the schema.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

XML schemas are registered by using methods provided by the DBMS_XMLSCHEMA PL/SQL
package. XML schemas can be registered as global or local schemas. This is covered later in
this lesson.

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 Fundamentals 9 - 28
XML Schema Management

• The XML schema functionality is available through


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

DBMS_XMLSCHEMA.
• You can use the DBMS_XMLSCHEMA subprograms to do
the following:
– Register an XML schema.
– Delete a previously registered XML schema.
– Recompile a previously registered XML schema. a
– Generate an XML schema.
s) has
– Evolve an XML schema. s ฺ r
m deฺ@ e ui
c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t schema s
The XML
l e n t r a n
functionality of Oracle Database12c is available through the PL/SQL-
a opackage,
supplied - DBMS_XMLSCHEMA, which is a server-side component that handles the
V n n
registration of XML schema definitions for use by Oracle Database 12c applications.
You can use the REGISTERSCHEMA procedure to register an XML schema that is specified as
a VARCHAR2 or XMLType. The REGISTERSCHEMA procedure registers an XML schema by
associating an XML schema document with a URL.
You can re-compile an already registered XML schema. This is useful to bring a schema in an
invalid state to a valid state by using the COMPILESCHEMA procedure.
You can generate an XML schema from an object type by using the GENERATESCHEMA or
GENERATESCHEMAS procedures.
You can delete a registered XML schema by using the DELETESCHEMA procedure. When you
attempt to delete an XML schema, DBMS_XMLSCHEMA checks whether the current user has
the appropriate privileges and whether there are any dependents.
A major challenge when using an XML schema is how to deal with changes in the content or
structure of XML documents. For example, new elements or attributes may need to be added
to an XML schema definition. In such cases, you perform XML schema evolution so that new
requirements are accommodated, while existing instance documents remain valid and
existing applications can continue to run.

XML Fundamentals 9 - 29
Note: Oracle XML DB supports the following kinds of XML schema evolution:
• In-place schema evolution: In-place schema evolution makes changes to an XML
schema without requiring existing data to be copied, deleted, and reinserted.
• Copy-based schema evolution: With copy-based schema evolution, all instance
documents that conform to the XML schema are copied to a temporary location in the
database. The old XML schema is deleted, the modified XML schema is registered, and
the instance documents are inserted into their new locations from the temporary area.
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 Fundamentals 9 - 30
Storage and Access Infrastructure

• When registering an XML schema, Oracle Database


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

creates the following:


– Types
– Default tables
• This is controlled by arguments to the
registerSchema() function.

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
n
While registering aanschema, Oracle Database also performs the following steps to facilitate
l e t r
a oaccess,
storage,
V n n- and manipulation of XML instances that conform to the XML Schema:
1. Creating types: When XML schema–based data is stored, its storage data types are
derived from the XML schema data types by using a default mapping and, optionally, by
using the mapping information that you specify by using XML schema annotations. For
binary XML storage, XML schema types are mapped to binary XML encoding types. For
object-relational storage, XML schema registration creates the appropriate SQL object
types for the structured storage of conforming documents.
2. Creating default tables: As part of XML schema registration, Oracle Database
generates default XMLType tables for all root elements. You can also specify any
column-level and table-level clauses and constraints for use during table creation.
You can control the creation of database objects and suppress their generation by arguments
to the registerSchema()function.
Use xdb:SQLType to specify the name of the SQL type to be generated. Set
xdb:defaultTable to the empty string ("") to suppress the creation of the default table for a
particular root element.
You can see the objects created by the registerSchema() function by writing a query that
uses the USER_OBJECTS data dictionary view.

XML Fundamentals 9 - 31
Validating XML

Oracle XML DB provides the following methods to validate XML


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

documents:
• Member functions
– XMLisValid()
– isSchemaValid()
– isSchemaValidated()
• Member procedures s a
– setSchemaValidated() ) h a
r s
sฺ eฺ
– schemaValidate() m
e uid
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
a l e o Valid Invalid
( v t
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n
It is oftent na nstofeknow whether a particular document conforms to a specific XML
inecessary
ale (that
schema
V n - trawhether it is valid with respect to a specific XML schema). By default, Oracle
is,
Database no12c ensures that XMLType instances are well-formed. In addition, for schema-
based XMLType instances, Oracle Database 12c performs a few basic validation checks.
Because full XML schema validation (as specified by the W3C) is an expensive operation,
when XMLType instances are constructed, stored, or retrieved, they are also not fully
validated.
To validate and manipulate the “validated” status of XML documents, several functions and
SQL operators are provided. These functions can be used to check whether the XML
documents that are being sent for input to the database conform to specific XML schemas
(that is, whether they are “valid”). However, this is not the same check as that performed by
XML Parsers, which check whether the XML documents are well formed.
Schema validation in the server uses the same functionality as XDK Schema validation.
Therefore, if you are using XDK in the middle tier, and then you call
setSchemaValidated(), it is equivalent to validating in the server.
Validating typically applies to XML schema–based XMLType instances. However, you can
also perform a validation check on unstructured XMLType instances.

XML Fundamentals 9 - 32
Lesson Agenda

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

• Features of Oracle XML DB


• XMLType
• XML Schema support in Oracle Database
• XML DB Repository
• Benefits of Oracle XML DB
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 9 - 33
Oracle XML DB Repository: Overview

• Provides hierarchical abstraction for data


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

• Provides file system functionality


• Provides path names to objects (similar to file systems)
• Is useful for content-oriented applications

s a
) h a
Folder r s
sฺ eฺ
m
e uid
c @
Resource
r o viLink nt G
a jsto Stude
ACL a
Pathnname ฺm Contentst his
t i e
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
Oraclen i n s the functionality of a file system in the database that forms a
t DB aprovides
XML n
e
al ofor
repository r
-tdata that is modeled via XML. The Oracle XML DB file system model maps path
V
namesn
n
or URLs to database objects of XMLType, and provides management facilities for
these objects. A database object that is mapped to the file system is called a resource. Oracle
XML DB resources are database objects with another (navigational) access path in addition to
SQL.
Oracle XML DB provides a generic access control list (ACL) mechanism that is usable by
applications, but provides significantly more functionality for objects mapped to the Oracle
XML DB file system. ACLs are usable on a per-object (per-row) basis and shareable across
table boundaries. ACLs can be seen as a particular implementation of database row-level
security.
Oracle XML DB also provides support for standard Internet protocols [FTP, HTTP(S), and
WebDAV] as a means of accessing its file system. XML elements are mapped to database
tables, XMLType tables, registered XML schema–based tables with XMLType columns, and
views. These can be easily viewed and accessed in Oracle XML DB Repository. Data in
XMLType tables and tables that contain XMLType columns can be stored in character large
objects (CLOBs) or natively in structured XML storage. Data in XMLType views can be stored
in local tables or remote tables that are accessed by using DBLinks. Both XMLType tables
and views can be indexed by using B*-tree, Oracle Text, function-based, or bitmap indexes.

XML Fundamentals 9 - 34
What Is the XML DB Repository?
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t isasimilar s to a directory structure in the database. You can access it by using
n
The repository
e r n
aldesktop
any
V o n t (such as a browser) or by creating a WebDAV connection (as you do in this
-tool
npractice). In the Address field, you can specify a database resource path. This maps
lesson’s
to a web folder so that you can view the database as a hierarchy. Access control applies so
that when you enter the path name, you are prompted for the password.
The documents in each folder are listed just as any file system document. However, the
documents are internally stored in rows and columns in the database.
The slide shows the contents of the PurchaseOrders table in the OE schema that is viewed
by using WebDAV connection as explained on the next few pages. On the right, you see the
contents of the table as .xml files. These files can be accessed by using SQL and any editor
(such as Microsoft Word) or browser.

XML Fundamentals 9 - 35
Mapping a Web Folder to
Oracle XML DB Repository
OE oe
2
3
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

s a
) h a
4 r s
sฺ eฺ
m
e u4id
c @
r o vi nt G
Click the drill-
j s to tude
down icon.
ฺ a
m his S
a
tin se t
e n
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
na folder
To mapnatiweb n s feOracle XML DB Repository:
to
le n-NX
V1.a Innyour trasession, select Connect to Server from the Places menu. The Connect to
o
Server window is displayed.
2. Complete the Connect to Server window as follows:
a. Service type: WebDAV (HTTP)
b. Server: localhost
c. Port: 8080
d. Folder: /home/OE
e. User Name: OE (upper case)
Then click Connect. The Authentication window is displayed.
1. In the Authentication window, enter oe (lower case) in the Password field, and then
click Connect. The web folder is displayed as shown in the slide. In addition, an icon for
the mapped web folder, WebDAV on localhost, is created on the desktop.
2. To view the content of the PurchaseOrders table, click the drill-down icon next to
PurchaseOrders.

XML Fundamentals 9 - 36
Mapping a Web Folder to the
Oracle XML DB Repository
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t showsathescontents of the PurchaseOrders table in the OE schema that is viewed
The slide
e n
l a nmapped r n
byausing
V o -t web folder. On the right, you see the contents of the table as .xml files.
These nfiles can be accessed by using SQL and any editor such as gedit in Linux.

XML Fundamentals 9 - 37
Oracle XML DB Repository: Features

The repository has the following features:


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

• Foldering
• Access control list (ACL)
• Protocol support
• Versioning

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t A afolder s is a node or directory in the repository hierarchy that contains or can
l e n
• Folder:
r n
Va contain
o n -at resource. A folder is also a resource.
n control list (ACL): ACL restricts access to a resource or resources. Oracle
• Access
XML DB uses ACLs to restrict access to any Oracle XML DB resource.
• Protocol support: Oracle XML DB allows access through Internet protocols such as
FTP and HTTP(S) as well as WebDAV (an extension of HTTP) to store and retrieve
XML data in the database.
• Versioning: With Oracle XML DB, you can maintain different versions of the same
resource.
In addition to containing resources, a folder resource can contain links to other resources
(files or folders). These are known as repository links or folder links.
Note: ACL is a set of database users who are allowed access to one or more specific
resources.

XML Fundamentals 9 - 38
Repository Support for Internet Protocols

• Internet protocols:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

– Access data through a URL


– Are mapped to a path name in the repository
– Return the content of the resource
• The repository supports the FTP, HTTP, and WebDAV
protocols.
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s for Internet protocols provides an alternative means of accessing
t DB asupport
Oraclen
e XML
r n
al oton-Oracle
resources
V t Net Services (Net8). Oracle Net Services is optimized for record-oriented
n Internet protocols are designed for stream-oriented data such as binary files or XML
data. The
text documents by using a web interface.
Oracle XML DB protocol access is useful for the following scenarios:
• Direct access from file-oriented applications by using the database like a file system
• Heterogeneous application server environments that want a uniform data access
method (for example, XML over HTTP and web services, which are now supported by
most application and data servers)
Oracle XML DB implements Hypertext Transfer Protocol (HTTP), HTTP 1.1 as defined in the
RFC2616 specification, and supports only stateless servlets. State is maintained by storing
information in HTTP cookies. Starting with Oracle Database 10g Release 2, Oracle XML DB
supports concurrent HTTP and HTTPS connections to Oracle XML DB Repository. This
enables the use of the secure HTTP protocol (HTTPS) in addition to the HTTP protocol,
thereby adding support for industry-standard security measures to the Oracle XML DB
protocol servers.

XML Fundamentals 9 - 39
WebDAV is a set of extensions to the HTTP protocol that you can use to edit or manage your
files on remote web servers. WebDAV can also be used to share and edit content by using
the Internet. Oracle XML DB supports WebDAV features such as folders (specified by
RFC2518), access control, and versioning.
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 Fundamentals 9 - 40
Oracle XML DB Versioning

• Provides basic support for the versioning of resources


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

• Manages multiple versions of resources:


– Except folders, XML schemas, and ACLs
• Supports single-branch operations:
– Check-in
– Check-out
– DML v1 a
v2 h a s
– Version history r s )
v3msฺ
– Get predecessor e i d eฺ
– Get successor v i c@ t Gu
t o ro den
s
aj s StuResource
a ฺ m hi
t i n e t
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
The Oracle i n
t XMLaDB n sversioning system provides a way to create and manage different
e
versions t r
al oofna-resource. In the past, when a resource such as a table or column was updated,
V n contents and properties were lost. Using Oracle XML DB versioning can prevent
its previous
this loss by storing a new version of the resource in the database, and retaining the old
resource contents and properties when an update is made. A version history of a resource is
maintained so that earlier versions can be accessed and loaded if required.
Oracle XML DB provides the DBMS_XDB_VERSION and DBMS_XDB PL/SQL packages for
version control management of a resource. The Oracle XML DB versioning system tracks all
the changes on version-controlled resources (VCR) and includes the following features:
• Enabling and disabling version control on a resource
• Updating a version-controlled resource. When Oracle XML DB updates a version-
controlled resource, it also creates a new version of the resource. This new version will
not be deleted from the database when you delete the version-controlled resource.
• Loading a specific version of the resource by using the object ID of the version. The
resource’s object ID can be found from the version history of the resource, or the
version-controlled resource.

XML Fundamentals 9 - 41
Lesson Agenda

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

• Features of Oracle XML DB


• XMLType
• XML Schema support in Oracle Database
• XML DB Repository
• Benefits of Oracle XML DB
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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 9 - 42
Benefits of Oracle XML DB

• XML file system


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

• Support for open XML standards


• XML and relational data integrated in the same data model
• Optimized storage of large XML documents
• Scalability and performance

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s native XML capabilities in the Oracle database include:
t of integrating
e n
The benefits
l ability r a n
V•a The o n -t to store and manage both structured and unstructured data under a standard
n XML data model
W3C
• Complete transparency and interchangeability between the XML and SQL metaphors so
that XML operations can be performed on database data, and SQL operations on XML
documents stored in the database
• Oracle XML DB Repository functionality such as creating folders, access control, FTP,
and WebDAV protocol access
• Version control of database resources
• Superior performance and scalability for XML operations
• Better management of unstructured XML data through piecewise updates, indexing,
search, multiple views on data, managing intradocument and interdocument
relationships
• Enabling data and documents from disparate systems to be accessed. For example, by
using Oracle Gateways and external tables, data can be combined into a standard data
model. These integrative capabilities reduce the complexity of developing applications
that must deal with data from different sources.

XML Fundamentals 9 - 43
XQuery Support in Oracle XML DB

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

XMLTable()
Database
XMLCast()
XMLExists()

Query result

s a
h a
s ฺrs) ฺ
@ em uide
o v ic t G
t o r den
s
aj s Stu
ฺ m
a SQL thi
XQuery
Compilation t i
n usen
into
compilation engine l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s the XQuery language through a native implementation of the
t DB asupports
Oraclen
e XML
r n
V al ofunctions,
SQL/XML n -t XMLQuery and XMLTable. Oracle XML DB supports the SQL*Plus
commandn XQUERY, which allows you to enter XQuery expressions directly. In effect, the
XQUERY command turns SQL*Plus into an XQuery command-line interpreter.
Oracle XML DB compiles XQuery expressions that are passed as arguments to the SQL
functions XMLQuery, XMLTable, XMLExists, and XMLCast. This compilation produces
SQL query blocks and operator trees that use SQL/XML functions and XPath functions. A
SQL statement that includes XMLQuery, XMLTable, XMLExists, or XMLCast is compiled
and optimized as a whole, leveraging both relational database and XQuery-specific
optimization technologies.
You can use the XQuery functions, fn:doc and fn:collection, to query the resources in
Oracle XML DB Repository. In this section, the XQuery function fn:collection is used to
query the data in database tables and views. Later in the lesson, the XQuery function
fn:collection is used to query the resources in Oracle XML DB Repository.

XML Fundamentals 9 - 44
XQuery Support in Oracle Database

Entity Reference Description


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

XMLQuery Takes an XQuery expression as a string literal, an optional context


item, and other bind variables, and returns the result of evaluating the
XQuery expression with these input values. It has two options:
RETURNING CONTENT and RETURNING SEQUENCE.
XMLQuery is typically used in a SELECT list.
XMLTable Maps the result of an XQuery evaluation into relational rows and
columns so that the user can query the XQuery result as a virtual
relational table by using SQL.
XMLTable can be used only in the FROM clause.
s a
XMLExists Checks whether a given XQuery expression returns a nonempty
) h a
XQuery sequence. If the result of evaluating the XQuery expression is
r
sฺ eฺ
a nonempty XQuery sequence, the function returns TRUE; otherwise, it
s
returns FALSE. m
e uid
c @
r o vi nt G
XMLExists()is typically used in a SQL WHERE clause.

sto Slist. ude


XMLCast Allows you to cast items from XML to the SQL scalar data type.
XMLCast is typically used ina ajSELECT t
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
l e n
• XMLQuery:
t r a n
XMLQuery takes an XQuery expression as a string literal, an optional
Va context -item, and other bind variables, and returns the result of evaluating the XQuery
non with these input values. It has two options, RETURNING CONTENT or
expression
RETURNING SEQUENCE, to indicate whether the result from the XQuery evaluation
should be converted to an XML(CONTENT) type or returned as an XML(SEQUENCE) type.
• XMLTable: XMLTable is used to map the result of an XQuery evaluation into relational
rows and columns so that the user can query the XQuery result as a virtual relational
table by using SQL. XMLTable can be used only in the FROM clause of SQL queries.
• XMLExists: XMLExists checks whether a given XQuery expression returns a
nonempty XQuery sequence. If the result of evaluating the XQuery expression is a
nonempty XQuery sequence, the function returns TRUE; otherwise, it returns FALSE.
You use XMLExists() in a SQL WHERE clause.
• XMLCast: XMLCast allows you to cast items from XML to the SQL scalar data type.

XML Fundamentals 9 - 45
Both XMLQuery and XMLTable evaluate an XQuery expression. In XQuery language, an
expression always returns a sequence of items. XMLQuery aggregates the items in this
sequence to return a single XML document or fragment. XMLTable returns a SQL table
whose rows each contain one item from the XQuery sequence.
Note: SQL/XML functions can be categorized into two types:
• Functions that you can use to query and access XML content. The XMLQuery,
XMLTable, and XMLExists functions belong to this category.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Functions that you can use to generate XML data from the result of a SQL query. These
include the XMLElement, XMLAttributes, XMLAgg, and XMLForest functions.
Oracle XML DB adds some XQuery functions to those provided in the World Wide Web
Consortium (W3C) standard. These additional functions are in the Oracle XML DB
namespace, http://xmlns.oracle.com/xdb, which uses the predefined prefix ora.
To query a relational table or view as if it were XML data, without having to first create a
SQL/XML view on top of it, use the XQuery function fn:collection within an XQuery
expression, passing as argument a URI that uses the URI-scheme name oradb together with s a
h a
the database location of the data.
s ฺrs) ฺ
search by using the contains text expression. @ em uide
XQuery Full Text search using contains text: You can use the XQuery Full Text

o v ic t G
For more information about the contains text expression,
t o r the
see
d e n XML DB
Oracle
Developer’s Guide 12c Release 1 (12.1). 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 Fundamentals 9 - 46
Using XQuery with Oracle XML DB

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

• Querying XMLType relational data


• Querying a relational table or view as if it were XML data
• Querying XML data in Oracle XML DB Repository

Oracle XML DB s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
a jsto SXQuery t ude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s and expressive language, and the SQL functions XMLQuery(),
t a veryageneral
XQuerynis
e r n
V al on-t and XMLExists() combine that power of expression and computation with
XMLTable(),
n strengths of SQL. The following are some examples where you typically use
the similar
XQuery with Oracle XML DB:
• Query XMLType relational data, possibly decomposing the resulting XML into relational
data using the XMLTable() function.
• Query a relational table or view as if it were XML data, without having to first create a
SQL/XML view on top of it, by using the XQuery function fn:collection within an
XQuery expression, passing as argument a URI that uses the URI-scheme name oradb
together with the database location of the data.
• Query XML data in Oracle XML DB Repository.
In this lesson, you see simple examples of using the XMLQuery() and XMLTable()
functions. For more information about XQuery functions in Oracle Database, see the Oracle
XML DB Developer’s Guide 12c Release 1 (12.1).

XML Fundamentals 9 - 47
XMLQuery

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

XMLQuery(<XQuery-string-literal>
[PASSING [by value] <expression-
returning-XMLType>]
RETURNING CONTENT)

Example:
s a
) h a
SELECT XMLQUERY('(1,2+3, "a")'
r s
sฺ eฺ
RETURNING CONTENT)as output m
e uid
FROM DUAL;
c @
r o vi nt G
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s is very flexible. You can provide numbers, strings, and XML nodes as
The XMLQuery
e n r a n
function
al onto-tthis function. In the slide example, the argument is an XQuery sequence of two
arguments
V n expressions and a string. In the syntax, the XQuery string literal is a
numerical
complete XQuery expression, including the prolog and so on. A prolog is a series of
declarations and imports that define the processing environment for the fragment of XQuery
code. The PASSING clause contains SQL expressions, each returning an XMLType that is
used as the context for evaluating the XQuery expression. Oracle XML DB supports only
passing “by value” and not passing “by reference,” so the BY VALUE clause is implicit and can
be omitted.
SELECT XMLQuery(‘<TEST>34</TEST>’
RETURNING CONTENT) AS xmlres
FROM DUAL;

XML Fundamentals 9 - 48
In the preceding example, the XQuery string <TEST>34</TEST>, which is an element
constructor, is invoked through the XMLQuery function. Because this XQuery string does not
require a context item or bind variables, you do not have to pass additional parameters.
XQuery returns an XQuery sequence consisting of a single element node. returning
content is supplied to force the XQuery sequence into the XML(CONTENT) type.
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 Fundamentals 9 - 49
URI Scheme oradb: Querying Table or View
Data with the XQuery fn:collection Function
• You can use the XQuery functions fn:doc and
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

fn:collection to either query resources in Oracle XML


DB Repository or query data in database tables and views.
• To do this, you pass to the fn:collection function a
URI argument that specifies the table or view to query.
• The Oracle URI scheme oradb identifies this usage.
Without it, the argument is treated as a repository location. a
s
• The table or view that is queried can be relational or of) ha
type XMLType. s ฺrs ฺ
@ em uide
• The fn:collection function replaces the ic ora:view
G
r o v n t
function, which was deprecated in sOracle
a de
j to StuDatabase 11g
Release 2. ฺm is
n t ina se th
v a le to u
v i c ( nse
t o ro © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
s
aj rable
M
n
You lcan t inatheaXQuery
use n sfe functions fn:doc and fn:collection to query resources in
V a e XML
Oracle n -
DB
tr Repository. In this lesson, the XQuery function fn:collection is used to
no in database tables and views. To do this, you pass to the fn:collection
query data
function a URI argument that specifies the table or view to query. The Oracle URI scheme
oradb identifies this usage; without it, the argument is treated as a repository location.
The table or view that is queried can be relational or of type XMLType. If relational, its data is
converted on the fly and treated as XML. The result returned by fn:collection is always
an XQuery sequence.
• For an XMLType table, the root element of each XML document that is returned by
fn:collection is the same as the root element of an XML document in the table.
• For a relational table, the root element of each XML document that is returned by
fn:collection is ROW. The children of the ROW element are elements with the same
names (uppercase) as the columns of the table. The content of a child element
corresponds to the column data. That content is an XML element if the column is of type
XMLType; otherwise (if the column is a scalar type), the content is of type xs:string.

XML Fundamentals 9 - 50
URI Scheme oradb: Querying Table or View
Data with the XQuery fn:collection Function
The format of the URI argument passed to fn:collection is
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

as follows:
• For an XMLType or relational table or view, TABLE, in the
database schema DB-SCHEMA:
– oradb:/DB_SCHEMA_NAME/TABLE_NAME/
• For an XMLType column in a relational table or view:
– oradb:/DB_SCHEMA_NAME/TABLE_NAME/ROW_PREDICATE/X-COL
s a
• Example: h a
s ฺrs) ฺ
. . . @ em uide
o v ic t G
'fn:collection("oradb:/OE/WAREHOUSES/ROW[WAREHOUSE_ID
t o r den
< 6]/WAREHOUSE_SPEC")' 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n s argument passed to fn:collection is as follows:
t of theanURI
The format
n
e r
l ann-XMLType
t
V•a For
n o or relational table or view, TABLE, in the database schema DB-
SCHEMA:
oradb:/DB-SCHEMA/TABLE/
You can use PUBLIC for DB-SCHEMA if TABLE is a public synonym or if TABLE is a
table or view that is accessible to the database user who is currently logged in.
• For an XMLType column in a relational table or view:
oradb:/DB-SCHEMA/REL-TABLE/ROWPRED/X-COL
- REL-TABLE is a relational table or view.
- PRED is an XPath predicate that does not involve any XMLType columns.
- X-COL is an XMLType column in REL-TABLE.
- PRED is optional.
- DB-SCHEMA, REL-TABLE, and X-COL are required.
The XQuery expression example in the slide represents all XML documents in the XMLType
column, warehouse_spec, of the oe.warehouses table, for rows where the column
warehouse_id has a value of less than 6.

XML Fundamentals 9 - 51
fn:collection Function: Example
-- Create and populate the emp_departments table before running
-- this example as shown in the notes section of this page.
SELECT
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

XMLQUERY('fn:collection("oradb:/HR/EMP_DEPARTMENTS")'
RETURNING CONTENT)
FROM dual;

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t in athensslide shows the use of fn:collection to query the
The example
e n
V al on-tr table. You need to create this table before running the code example in
EMP_DEPARTMENTS
n
the slide.
The EMP_DEPARTMENTS table is created by using the following commands:
CREATE TABLE EMP_DEPARTMENTS OF XMLTYPE;
INSERT INTO EMP_DEPARTMENTS VALUES (XMLType(bfilename('XML_DIR',
'department.xml'),nls_charset_id('AL32UTF8')));

XML Fundamentals 9 - 52
XMLTable

• Is used to map the results of an XQuery evaluation into


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

relational rows and columns


• Can be used only in the SQL FROM clause
Syntax:

XMLTABLE ( [XMLNAMESPACES <string>]


<XQuery-string-literal>
[BY [PASSING [BY VALUE] <expression-returning-XMLType>] s a
) h a
ฺrs ฺ
[COLUMNS [FOR ORDINALITY | datatype ]
RETURNING CONTENT) m s
@ e uide
c
vi nt G
r o
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
tuse theaXMLTable
You can
l e n t r n SQL function to map the results of an XQuery evaluation into the
a orows
relational - and columns of a new, virtual table. You can then insert the virtual table into a
V n n
pre-existing database table, or you can query it by using SQL in a join expression. You can
use XMLTable in the FROM clause of a SQL query expression.
Parameters for XMLTable
• XQuery_string_literal: Is the complete XQuery expression
• XMLNAMESPACES clause (optional): Contains namespace declarations referenced by
XQuery_string_literal and by the XPath expression in the PATH clause inside the
COLUMN clause
• Passing clause: Includes one or more SQL expressions, each returning an XMLType
instance. The result of evaluating each expression is bound to the corresponding
identifier for the evaluation of XQuery_string.
• COLUMNS clause (optional): Defines the columns of the virtual table to be created
by XMLTable in the absence of which XMLTable returns a row with a single XMLType
pseudocolumn named COLUMN_VALUE. The COLUMNS clause has the following
subclauses:
- FOR ORDINALITY: This specifies the column of generated row numbers.
- Datatype: You must specify the data type of each resulting column except the
FOR ORDINALITY column.

XML Fundamentals 9 - 53
XMLTable: Example

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

FROM
XMLTABLE('fn:collection("oradb:/OE/WAREHOUSES/")/ROW/LOC
ATION_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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t i
The XMLTable
n n n s and the output of the query are shown in the slide.
query
l e r a
Va non-t

XML Fundamentals 9 - 54
Quiz

Oracle XML DB enables you to perform:


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

• XML operations on structured data such as tables


• SQL operations on XML documents
a. True
b. False

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
t
Answer:na i n n s
l e r a
Va non-t

XML Fundamentals 9 - 55
Summary

In this lesson, you should have learned how to:


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

• Describe Oracle XML DB


• Identify the components of Oracle XML DB
• Describe the applications of Oracle XML DB
• List the benefits of Oracle XML DB
• Describe Oracle XML DB Repository
• Describe foldering in the repository s a
h a
• Use protocol server support to access data s ฺrs) ฺ
• Describe versioning @ em uide
ic G
t o rov dent
a js Stu
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t DB afunctionality
Oraclen
l e XML
t r n provides a high-performance XML storage and retrieval
a onthat
technology
V - is available with Oracle Database 12c. It fully absorbs the W3C XML data
n
model into Oracle Database and provides new standard access methods for navigating and
querying XML. With Oracle XML DB, you benefit from the combined advantages of relational
database technology and XML technology.

XML Fundamentals 9 - 56
Practice 9: Overview

This practice covers the following topics:


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

• Mapping a web folder to the XML DB repository


• Creating your student folder in the /home parent folder
• Copying and displaying an XML document in your student
folder
• Copying an XSL style sheet and an XML document that
uses the style sheet to the folder, and then s a
h a
• accessing the XML document ฺrs)
e ms 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t

XML Fundamentals 9 - 57
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ฺ

Table Descriptions

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Schema Descriptions
Overall Description
The Oracle Database sample schemas portray a sample company that operates worldwide to
fill orders for several different products. The company has two divisions:
• Human Resources (HR): Tracks information about the employees and facilities
• Order Entry (OE): Tracks product inventories and sales through various channels
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Each of these divisions is represented by a schema. In this course, you have access to the
objects in all the schemas. However, the emphasis of the examples, demonstrations, and
practices is on the HR and OE schemas.
All scripts necessary to create the sample schemas reside in the
$ORACLE_HOME/demo/schema/ 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

XML Fundamentals A - 2
Human Resources (HR)
In the HR records, each employee has an identification number, an email address, a job
identification code, salary, and manager. Some employees earn commissions in addition to
their salary.
The company also tracks information about the jobs within the organization. Each job has an
identification code, a job title, and a minimum and maximum salary range for the job. Some
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

employees have been with the company for a long time and have held different positions
within the company. When an employee resigns, the duration that the employee was working,
the job identification number, and the department are recorded.
The sample company is regionally diverse, so it tracks the locations of its warehouses and
departments. Each employee is assigned to a department, and each department is identified
by a unique department number or a short name. Each department is associated with one
location, and each location has a full address that includes the street name, postal code, city,
state or province, and the country code.
s
In places where the departments and warehouses are located, the company records details
a
h a
ฺrs) ฺ
such as the country name, currency symbol, currency name, and the geographical region
where the country is located. 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

XML Fundamentals A - 3
The HR Entity Relationship Diagram

DEPARTMENTS LOCATIONS
HR department_id location_id
department_name street_address
manager_id postal_code
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

location_id City
state_province
country_id
JOB_HISTORY
employee_id
start_date EMPLOYEES
end_date employee_id
job_id first_name COUNTRIES
department_id last_name country_id
Email country_name
s a
phone_number region_id h a
ฺrs) ฺ
hire_date
job_id s
JOBS
job_id
Salary
@ em uide
job_title
commission_pct
manager_id o v ic t G
min_salary department_id t o r denREGIONS
max_salary
s
aj s Stu region_name
region_id
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 Fundamentals A - 4
Human Resources (HR) Row Counts

SELECT COUNT(*) FROM regions;


COUNT(*)
----------
4
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

SELECT COUNT(*) FROM countries;


COUNT(*)
----------
25

SELECT COUNT(*) FROM locations;


s a
h a
ฺrs) ฺ
COUNT(*)
s
em uide
----------
23 @
ic t G
o v
r den
s t o
SELECT COUNT(*) FROM departments;
ฺ m aj s Stu
COUNT(*)
t i n a
e t hi
----------
a l en o us
27 i c (v se t
t o rov licen
a jsFROMbjobs;
le
M
SELECT COUNT(*)
a sfer a
t i n
len n-tran
COUNT(*)
a
----------
V no 19

SELECT COUNT(*) FROM employees;


COUNT(*)
----------
107

SELECT COUNT(*) FROM job_history;


COUNT(*)
----------
10

XML Fundamentals A - 5
Order Entry (OE)
The company sells several categories of products, including computer hardware and
software, music, clothing, and tools. The company maintains product information that includes
product identification numbers, the category into which the product falls, the weight group (for
shipping purposes), the warranty period if applicable, the supplier, the status of the product, a
list price, a minimum price at which the product will be sold, and a URL for manufacturer
information.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Inventory information is also recorded for all the products, including the warehouse where the
product is available and the quantity on hand. Because the products are sold worldwide, the
company maintains the names of the products and their descriptions in different languages.
The company maintains warehouses in several locations to facilitate filling customer orders.
Each warehouse has a warehouse identification number, name, and location identification
number.
Customer information is tracked in some detail. Each customer is assigned an identification
number. Customer records include name, street address, city or province, country, phone s a
numbers (up to five phone numbers for each customer), and postal code. Some customers ) h a
order through the Internet, so email addresses are also recorded. Because of language
r
sฺ eฺs
differences among customers, the company records the NLS language and territory of each
m
e uid
c @
r o vi nt G
customer. The company places a credit limit on its customers to limit the amount for which

company monitors. A customer’s phone number is also tracked. a jsto Stude


they can purchase at one time. Some customers have account managers, whom the

n a ฺm this
When a customer places an order, the company
n i
t tracks s e the date of the order, the mode of the
order, status, shipping mode, total amount e
al notoft tbbe
theu
o order, and the sales representative who
h l d place
helped l th
the order.
d Thi This may (
orv
c nse may th
the same iindividual
di id l as ththe accountt manager
v i
for a customer, or in the case
recorded. In additionjs
oro ofe an
tot the order l e over the Internet, the sales representative is not
icorder
information, the number of items ordered, the unit price, and
a
M fare b l
a tracked.
the products ordered
na e ralso
For each i
ntcountry s
ainn which it does business, the company records the country name, currency
a l e - t r
V non name, and the geographical region where the country is located. This data
symbol, currency
is useful to interact with customers living in different geographical regions around the world.

XML Fundamentals A - 6
Order Entry (OE)

OE CUSTOMERS
customer_id
cust_first_name
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

ORDER_ITEM ORDERS cust_ last_name


order_id order_id
line_item_id order_date cust_ address_typ
product_id order_mode
unit_price customer_id street_address
quantity order_status postal_code
order_total city
sales_rep_id state_province
promotion_id country_id
s a
phone_numbersh a
PRODUCT_ PRODUCT_ s ฺ r s)
nls_language
INFORMATION DESCRIPTION e i d eฺ
mnls_territory
product_id product_id
v i c@ t G credit_limit
u
cust_ email
product_name language_id
t o ro den account_mgr_id
product_description product_name
product_description
s
aj s Stu date_of_birth
category_id
a ฺ m hi marital_status
weight_class
t i
n us n e t gender
warranty_period l e
pp _id
supplier
c ( va e to income_level
product_status
r o vi ens
list_price
min_price ajs
to le licINVENTORIES WAREHOUSES
catalog_urlM r a b product_id
i na s f e warehouse_id

l e nt tran warehouse_id
quantity_on_hand
warehouse_name

Va non- location_id

XML Fundamentals A - 7
Order Entry (OE) Row Counts

SELECT COUNT(*) FROM customers;


COUNT(*)
----------
319
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

SELECT COUNT(*) FROM inventories;


COUNT(*)
----------
1112

SELECT COUNT(*) FROM orders;


COUNT(*) s a
h a
ฺrs) ฺ
----------
s
105
@ em uide
o v ic t G
SELECT COUNT(*) FROM order_items;
t o r den
COUNT(*) s
aj s Stu
a ฺ m hi
----------
t i n e t
665
a l en o us
i c (v se t
SELECT COUNT(*) FROM
t o rovproduct_descriptions;
l i c en
COUNT(*) a js ble
a M fera
----------
n t i n n s
l e r a
Va non-t
8640

SELECT COUNT(*) FROM product_information;


COUNT(*)
----------
288

SELECT COUNT(*) FROM warehouses;


COUNT(*)
----------
9

XML Fundamentals A - 8
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Additional Information About Document Type


Definitions, XML Schemas, and XSLT 2.0

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this appendix, you should be able to do the


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

following:
• Include element, attribute, entity, and notation declarations
in a DTD
• Declare attributes with the following attribute types:
– CDATA
– Enumerated
– NOTATION s a
h a
– ENTITY and ENTITIES
s ฺrs) ฺ
– NMTOKEN and NMTOKENS
@ em uide
– ID, IDREF, and IDREFS o v ic t G
t o r den
• Create an XML schema from multiple a j XML
s t u schema
documents ฺm this S
t i n a
len use
• Describe XSLT 2.0 features
va e to
c (
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t focuses s on the use of declarations in a document type definition (DTD) to
e n
This appendix
r a n
al theonvalidity
control
V -t of XML documents. Apart from revisiting basic DTD declarations for
n attributes, entities, and notations, the appendix covers additional information about
elements,
each of the attribute types listed in the slide.
Creating an XML schema from multiple XML schema documents is covered, as are the
benefits of XSLT 2.0 and examples of some of the features.

XML Fundamentals B - 2
Declaration Types in a DTD (Review)

A DTD contains declarations (using the syntax shown) for:


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

• Elements
<!ELEMENT element_name content_model>

• Attributes
<!ATTLIST element_name attribute_name attribute_type
default_value>

• Entities s a
) h a
<!ENTITY entity_name "replacement text"> r s
sฺ eฺ
m
e uid
• Notations c @
r o vi nt G
<!NOTATION notation_name SYSTEM "text">
a jsto Stude
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s
A DTDn consists ofndeclarations
a for the markup and data used in an XML document. The slide
l e t r
- syntax for declaring markup components:
a theongeneric
shows
V n
• Elements, which declare tag names and their content model. The content model for an
element includes other nested markup (elements) and/or character data.
• Attributes, which declare the attribute names for the containing element, the type of
attribute (not a data type), and optional default values
• Entities, which provide a mechanism for replacement text
• Notations, which are used in DTD attribute types to provide a way for identifying how
parsed or unparsed data is encoded, typically used by applications that can process the
data
Note: Notations are not frequently used in DTDs.
The next few pages of this appendix show examples of how to use each of these
declarations, and finally how to use DTD to validate the structure and content of an XML
document.

XML Fundamentals B - 3
Referencing the DTD (Review)

The XML document references the DTD:


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

• After the XML declaration and before the root by using:


<!DOCTYPE employees [ ... ]>

• Externally with the SYSTEM or PUBLIC keywords:


<!DOCTYPE employees SYSTEM "employees.dtd">

<!DOCTYPE employees PUBLIC "-//formal-public-ID">


s a
a
• Internally in the <!DOCTYPE root [...]> entry:ฺrs) h
<?xml version="1.0"?> e ms ideฺ
<!DOCTYPE employees [ v i c@ t Gu
<!ELEMENT employees (#PCDATA)>
t o ro den
]> s
aj s Stu
<employees>Employee Data</employees> a ฺ m hi <!DOCTYPE.
t i n e t
<!-- Use the root element
a l en o usname after

i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
The XML i n
t document n sreferences the DTD to be validated by using a specification syntax of:
e r a
al on-root-element-name
t
V
<!DOCTYPE
n
[declarations]>
The root element name must be used after the <!DOCTYPE keyword in the declaration. To
use an external DTD, replace the [declarations] part with:
• The SYSTEM keyword and a quoted string containing a URL or a relative path to a file
with a .dtd extension, such as employees.dtd in the slide, or a URL such as
http://www.myserver.com/dtd/employees.dtd
• The PUBLIC keyword and a quoted string containing a Formal Public Identifier (FPI).
For example, to reference the public XHTML identifier, use:
<!DOCTYPE xhtml PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN">
The last example in the slide uses an internal DTD with declarations placed between:
• The <!DOCTYPE root-element [ characters
• The closing ]> characters
Note: The <!DOCTYPE name [...]> specification appears after the XML declaration and
before the root element of the XML document. Public identifiers are seldom used. An XML
document can reference both an external DTD and an internal DTD. The internal DTD takes
precedence if an element is defined both externally and internally.

XML Fundamentals B - 4
Element Declarations (Review)

• Element declaration syntax:


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

<!ELEMENT element-name content-model>

• Four kinds of content models:


<!ELEMENT job EMPTY> <!-- Empty --> 1

<!–- Elements: single, ordered list, or choice -->


<!ELEMENT employees (employee)>
2 a
<!ELEMENT employee (employee_id,last_name,job_id)>
h a s
<!ELEMENT job_id (manager | worker)>
r s )
m sฺ eฺ
<!-- Mixed --> @ e uid
c
vi nt G 3
<!ELEMENT last_name (#PCDATA)>
r o
<!ELEMENT hire_date (date| (day,month,year))>
a jsto Stude
n a ฺm this
<!ELEMENT employee_id ANY> i
nt <!-- s eMixed --> 4
l e u
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s building blocks of DTDs. An element declaration contains the
t are theabasic
n
Elements
e r n
V al okeyword,
ELEMENT n -t followed by the element name, and a content model. The content model
n
defines what can be contained in the element. The four kinds of content models are as
follows:
1. Empty: This cannot contain subelements or text. However, an empty element may hold
attributes. Examples of empty elements are:
<job/> <job></job> <job id="ST_CLERK"/>
2. Child elements (subelements): These are enclosed in parentheses and can be:
- A single element name
- A sequence of element names containing an ordered, comma-separated list of
names. For example: The <employee> element must contain an
<employee_id> element that appears before <last_name>, which must appear
before <job_id>.
A choice of element names, which is a list of names separated by a vertical bar
-
(|), each representing an alternative choice. For example:
The <job_id> element may contain either a <manager> element or a <worker>
element, but not both.
- A combination of single element and a list, for example:
<!ELEMENT person (full_name | (first_name, last_name))>
XML Fundamentals B - 5
3. Mixed: The simplest form of mixed content is a text-only element that is known as
parsed character data that uses the #PCDATA keyword enclosed in parentheses.
When content is specified as #PCDATA, the element can contain a text string, but not
child elements. For example, by using the last_name element declaration, the
following XML is valid:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<last_name>Greenberg</last_name>

Another example of mixed content is shown in the hire_date element declaration,


which can contain either parsed character data or a combination of date, month, and
year elements. The following XML elements will be valid for hire_date:
<hire_date><date>May 20, 1996<date></hire_date>
Or
<hire_date>
s a
<day>17</day>
) h a
<month>August</month> r
sฺ eฺs
m
e uid
<year>2002</year>
c @
</hire_date>
r o vi nt G
a jsto Stude
4. Any: This content model means that anything
n a ฺmcanthbeiscontained within the element and
is very loose. The ANY content model i
ntallowsusany
e element declared in the DTD to be
l e
used,, in anyy order,, byy using
c vay number
g any
( e toof occurrences. That is,, the DTD does not
r o vi ens
validate the element content.
Note: You must avoid
s to using c because it entirely defeats the purpose of using a DTD
liANY
j
athe structurel e
for validating
a M f e r ab and content of an XML document.
e n tin ans
l r
Va non-t

XML Fundamentals B - 6
Specifying Cardinality of Elements (Review)

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

• Indicate the number of children permitted


• Appear as suffixes

Symbol Description
No symbol (default) Mandatory (one and only one)

? (question mark) Optional (zero or one) a


h a s
* (asterisk) Zero or more (optional)
r s )
m sฺ eฺ
+ (plus sign) One or more (mandatory)
@ e uid
c
vi parentheses)
t G
• Are placed after an element or group r o
(using
to tude n
in the content model or after the a j s
a ฺ m content h i s Smodel
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
n t
The contentinamodel
n s fe how to group subelements by using parentheses, and how many
indicates
a element is permitted, known as cardinality. Cardinality is expressed
ale on-oftreach
occurrences
V n following symbols:
using the
• Question mark (?): Indicates an optional or single-valued subelement (zero to one)
• Asterisk (*): Indicates an optional or multivalued subelement (zero to many)
• Plus sign (+): Indicates a mandatory or many-valued subelement (one to many)
The symbols can appear in the content model specification as a suffix with the individual
elements, a group, and the entire content model. By default, the absence of a symbol means
that the element, group, or content model must appear at least once; that is, it is mandatory.
Examples of using symbols are:
1. An <employees> element can contain zero or many <employee> elements:
<!ELEMENT employees (employee*)>
2. A <department> element must contain at least one or many <employee> elements:
<!ELEMENT department (employee+)>
3. An <employee> element must have one <last_name> and <salary> element, and
an optional <commission_pct> element:
<!ELEMENT employee (last_name,salary,commission_pct?)>
4. The <jobs> element must have one president, and optionally many managers or
clerks:
<!ELEMENT jobs (president , (manager | clerk)*)>
XML Fundamentals B - 7
Attribute Declarations (Review)

• The syntax for declaring an attribute is:


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

<!ATTLIST element-name attribute-name attribute_type default>

• Attribute declaration requires:


– An element name
– An attribute name
– An attribute type, which is specified as:
CDATA, enumerated, ENTITY, ENTITIES, ID, IDREF,
IDREFS, NMTOKEN, NMTOKENS, or NOTATION s a
h a
– An attribute default, which is specified as:
s ฺrs) ฺ
em ide
#IMPLIED, #REQUIRED, #FIXED, or a literal value
• Example: v i c@ t Gu
t o ro den
<!ELEMENT employee (employee_id,alast_name)> j s S tu
ฺ m
a CDATAthi#IMPLIED> s
<!ATTLIST employee manager_id
t i n e
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n
Attributes i n s
t specifyanadditional information about an element, and form a name-value pair that
l e
describes -
a oanparticular t r property of the element. Declaring an attribute requires:
V n
• An element name that identifies the element to which the attribute belongs
• An attribute name that specifies the name of the attribute
• An attribute type that is set to one of the 10 possible types of attributes listed in the
slide. These are discussed on the next page.
• An attribute default that specifies a mandatory, or optional, value by using:
- #REQUIRED indicating that the attribute must be specified (mandatory)
- #IMPLIED indicating that the attribute is optional
- #FIXED indicating that the attribute is a constant, that is, a supplied single value
- A literal value enclosed in quotation marks that is used as a default value when no
value is specified in the XML document
An example of XML by using the manager_id attribute declaration is:
<employee manager_id="100">
<employee_id>101</employee_id>
<last_name>Kochhar</last_name>
</employee>

XML Fundamentals B - 8
CDATA and Enumerated Attribute Types

• CDATA: For character data values


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

<!ELEMENT employee (employee_id, last_name)>


DTD
<!ATTLIST employee manager_id CDATA #IMPLIED>

<employee manager_id="102">
<employee_id>104</employee_id>
<last_name>Ernst</last_name> XML
</employee>

• Enumerated: For a choice from a list of values s a


h a
<!ELEMENT employee (employee_id, last_name)> s ฺrs) ฺ
<!ATTLIST employee gender (male|female) #IMPLIED>
@ em uidDTD
e
c
vi nt G
r
to tudeo
<employee gender="male">
a j s
<employee_id>104</employee_id>
a ฺ m his S
<last_name>Ernst</last_name>
e n tin se t XML
</employee>
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
CDATAn t ina aType
Attribute n sfe
e
alCDATA - tr indicates that the attribute value can be set to any character data. Most
V
The
no ntype
attributes use the CDATA type. The example in the slide declares manager_id as an attribute
of the employee element. The manager_id attribute may have any character data as its
value. The sample XML code in the slide, under the manager_id attribute declaration, shows
an example of using the attribute.
Enumerated Attribute Type
The Enumerated type provides a choice from a list of allowable values for the attribute. The
choices of values in the list:
• Are enclosed in parentheses
• Are separated by the vertical bar (|) character (spaces are optional)
• Can be case-sensitive strings (without quotation marks) or numeric values
For example, the employee element is given a gender attribute with male or female as its
possible values. In the slide, the XML code under the gender attribute declaration shows an
example of using the attribute.

XML Fundamentals B - 9
ENTITY and ENTITIES Attribute Types

• ENTITY: This indicates that the attribute value is a string


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

name that is declared as an unparsed entity in the DTD.


<!ELEMENT employee (#PCDATA)>
<!ATTLIST employee resume ENTITY #IMPLIED> DTD
<!–- Requires an unparsed entity declaration, made
by using these ENTITY & NOTATION declarations -->
<!ENTITY resume-data SYSTEM "emp.txt" NDATA text>
<!NOTATION text SYSTEM "textual data">
– In ENTITY declaration, NDATA marks it as unparsed. a
– NDATA requires a NOTATION declaration. h a s
s ฺ r s)
• In XML, the attribute value uses the entity namem e ฺ
without the ampersand (&) and semicolonic(;). @ Gue i d
v
ro dent
<employee resume="resume-data"> t
js Stu o
Bruce Ernst
ฺ m hisa XML
</employee> a
tin se t
e n
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
ENTITYn t ina anType
Attribute sfe
e
alENTITY - tr indicates that the attribute value is a string name that is declared as an
V
The
no n type
unparsed entity in the DTD. Defining a general entity is done by using the following
declaration:
<!ENTITY name "replacement text">
However, for the entity name to be used as an attribute value, it must be declared as an
external unparsed entity by using:
<!ENTITY name SYSTEM "sometext" NDATA notation-name>
The two items that mark the entity as an external unparsed entity are:
• The SYSTEM keyword that identifies the entity as an external one
• The NDATA notation-name that indicates that the data is unparsed. However,
notation-name specified after the NDATA keyword requires a corresponding DTD
notation declaration, such as:
<!NOTATION notation-name SYSTEM "sometext">
The sample XML code in the slide shows the resume-data entity name used as the value
for the resume attribute, without the ampersand (&) escape character and a semicolon (;)
terminator.

XML Fundamentals B - 10
ENTITIES Attribute Type
The ENTITIES type is an extension of the ENTITY type that:
• Allows the attribute value to be a space-separated list of external unparsed entity
names
• Is declared, as in the following example:
<!ATTLIST employee resume ENTITIES #IMPLIED>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Requires an entity declaration for each entity name used in the attribute value list of the
XML document
For example: A DTD, in a file called employee.dtd, shows the declaring of the resume
attribute by using the ENTITIES type:
<!ELEMENT employee (#PCDATA)>
<!ATTLIST employee resume ENTITIES #IMPLIED>
<!–- Required ENTITY declarations -->
s a
<!ENTITY resume-data SYSTEM "emp.txt" NDATA text>
h a
<!ENTITY resume-image SYSTEM "emp.jpg" NDATA jpeg>
s ฺrs) ฺ
<!-- Required NOTATION declarations -->
@ em uide
<!NOTATION text SYSTEM "text/plain">
o v ic t G
t o r den
<!NOTATION jpeg SYSTEM "image/jpeg">
s
aj s Stu
ฺ m
t
An example of an XML code that uses thenresume e thi can be written as:
ina sattribute
v a le to u
c ( "employee.dtd">
<?xml version="1.0"?>
<!DOCTYPE employee SYSTEM o v i
e n se
j s tor le lic resume-image">
<employee resume="resume-data
M a rab
ina ansfe
Bruce Ernst
n t
le n-tr
</employee>
a
V no resume attribute in the XML document contains a space-separated list of
Note: The
unescaped entity names, each of which must also be declared in the DTD.

XML Fundamentals B - 11
ID Attribute Type

• Is used for uniquely identifying an element instance


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

<!ELEMENT employees (employee*)>


<!ELEMENT employee (last_name)> DTD
<!ATTLIST employee employee_id ID #REQUIRED>

• Can be specified for only one attribute per element


• Must have a different value for all elements
• Must be a valid XML name (not start with a numeric) s a
) h a
<employees> r s
sฺ eฺ
<employee employee_id="_103"> m
e uid
<last_name>Hunold</last_name>
c @
</employee>
r o vi nt G
<employee employee_id="_104">
<last_name>Ernst</last_name> jst
o ude XML
a
m his S t
</employee>
a ฺ
</employees>
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t inais used
The IDntype n s fe
to uniquely identify specific elements in an XML document. This is useful
alewant
if you n ra a specific element by its ID value. Using the ID type requires the
to-tlocate
V no
following:
• An element must have only one ID attribute.
• All elements must have a different (unique) value for the ID attribute in the entire
document.
• The value must be a valid XML name. That is, it must start with an alphabetic or
underscore character, and not a numeric character.
The sample XML document in the slide uniquely identifies each employee element by using
the employee_id attribute whose values are prefixed with an underscore (_).
Note: Using the #REQUIRED attribute default is not mandatory, but it is recommended for
attributes that are declared with the ID attribute type.

XML Fundamentals B - 12
IDREF and IDREFS Attribute Types

Are used to reference one or more ID type attribute values


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

(respectively) in some element


<!ELEMENT employees (employee*)> DTD
<!ELEMENT employee (last_name)>
<!ATTLIST employee employee_id ID #REQUIRED>
<!ATTLIST employee manager_id IDREF #IMPLIED>
<!ATTLIST employee mgrs IDREFS #IMPLIED>

<employees>
s a
<employee employee_id="_102">
) h a
<last_name>De Haan</last_name> r s
sฺ eฺ
</employee>
<employee employee_id="_103" manager_id="_102"> m
e uid
c @
<last_name>Hunold</last_name>
r o vi nt G
jsto Stude
</employee>
<employee employee_id="_104" mgrs="_102 _103">
a XML
<last_name>Ernst</last_name>
a ฺ m his
</employee>
t i n e t
</employees>
a l en o us
i c (v se t
t o rov© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
i n
t andaIDREFS
The IDREF
n n s types allow other elements to reference one or more elements with a
e r
al IDonattribute.
unique -t Declare the attribute type as:
V n IDREF type to reference a single attribute value of an element with an ID attribute
• An
• An IDREFS type to reference multiple ID attribute values
The DTD in the slide declares:
• The manager_id attribute as an IDREF type
• The mgrs attribute as an IDREFS type
The XML example shows the following:
• The employee element with its ID type value _103 (in employee_id) references the
employee element with the _102 value.
• The employee element with its ID value _104 references, as a space-separated list,
the following two employee elements:
- With employee_id value _102
- With employee_id value _103

XML Fundamentals B - 13
NMTOKEN and NMTOKENS Attribute Types

Represent one or more XML name tokens without spaces


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

<!ELEMENT employees (employee*)>


<!ELEMENT employee (last_name)> DTD
<!ELEMENT last_name (#PCDATA)>
<!ATTLIST employee hire_date NMTOKEN #IMPLIED>
<!ATTLIST employee email NMTOKENS #IMPLIED>

s a
<employees>
) h a
<employee hire_date="13-JAN-1993"> r s
sฺ eฺ
<last_name>De Haan</last_name> m
e uid
</employee>
c @
<employee email="AHUNOLD Alexander.Hunold">
r o vi nt G
<last_name>Hunold</last_name>
a jsto Stude XML
</employee>
n a ฺm this
</employees> i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t Attribute sType
NMTOKEN
e n r a n
V alNMTOKEN
The o n -ttype represents a name token whose value is like an XML name; that is, it can
containncharacter values, including:
• Letters (A-Z, a-z)
• Numbers (0-9)
• Underscores (_)
• Colons (:)
• Hyphens (-)
• Periods (.)
Note: However, an NMTOKEN value cannot contain any white space characters.
NMTOKENS Attribute Type
Using the NMTOKENS type allows a space-separated list of NMTOKEN attribute values.
The example in the slide shows the hire_date attribute declared as an NMTOKEN type and
the email attribute as an NMTOKENS type.

XML Fundamentals B - 14
Entities in XML (Review)

• Entity types that are predefined by XML standards include:


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

– Built-in entities
– Character entities
• Entities in a DTD can be declared as:
– General entities <!ENTITY name "text">

– Parameter entities <!ENTITY % name "text">

• Entities reference: s a
) h a
– Built-in, character, and general entities in an XML document r s
or the DTD by using: m sฺ eฺ
@ e uid
&entity-name; &entity-name; c
vi nt G
r o
– Parameter entities in the DTD by jusing:
a sto Stude
%entity-name;
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s Wide Web Consortium (W3C) standard, the predefined entities in XML
t to theaWorld
n
According
e r n
al on-t
include:
V
• Then built-in entities, which represent the following special characters:
- The less than sign (<), represented by &lt;
- The greater than sign (>), represented by &gt;
- The ampersand (&), represented by &amp;
- The single quotation mark or apostrophe ('), represented by &apos;
- The double quotation mark ("), represented by &quot;
• The character entities, which represent any Unicode character specified in the XML
recommendation, which is based on Unicode 3.0. Character entities are formed by using
an ampersand (&), followed by a hash (#), a Unicode character number, and terminated
by a semicolon (;). For example, &#169 denotes the copyright symbol (©). The
character number can be a decimal or a hexadecimal Unicode value. Hexadecimal
values are prefixed with an x. For example, &#xA9 represents the copyright symbol (©).
An entity reference to a parameter entity is done in a DTD. However, references to built-in,
character, and general entities can be done in the XML document and a DTD, for example:
<Company>&quot;Oracle Corporation&quot;</Company> instead of
<Company>"Oracle Corporation"</Company>

XML Fundamentals B - 15
Creating an XML Schema
from Multiple XML Schema Documents
• The W3C XML Schema recommendation provides the
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

following two declarations for assembling XML schemas:


– <import>
– <include>
• Use the <import> declaration for combining XML
schemas that have different targetNamespace.
• Use the <include> declaration for combining XML a
schemas that have the same targetNamespace. h a s
r s )
m sฺ eฺ
<import> @ e uid
c
vi nt G
r o
a jsto Stude
a
<include>
n ฺm this
XSD
i
nt use XSD
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t XML Schema s recommendation provides mechanisms for combining XML schemas
The W3C
e n r a n
V alreusing
and
o n -t
definitions. By reusing existing definitions, you save time when creating the
n
documents.
You can use the <import> element to add multiple XML schemas with different
targetNamespaces to a document.
When you use the <include> element for combining XML schemas, the included files must
all reference the same targetNamespace.

XML Fundamentals B - 16
Declaring an <import>

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

xmlns:employees="http://www.hr.com/employees" employees.xsd
targetNamespace="http://www.hr.com/employees"
elementFormDefault="qualified"> . . .

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" resource.xsd
xmlns:target="http://www.hr.com/resource"
targetNamespace="http://www.hr.com/resource"
elementFormDefault="qualified"> . . .
s a
h a
<?xml version="1.0"?>
s ฺrs) ฺ
<schema xmlns="http://www.w3.org/2001/XMLSchema"
@ em uide
xmlns:employees="http://www.hr.com/employees"
o v ic t G
xmlns:resource="http://www.hr.com/resource"
t o r den
targetNamespace="http://www.hr.com/employees"
s
aj s Stu employees1.xsd
elementFormDefault="qualified">
ฺ m
a. . thi
<import namespace="http://www.hr.com/resource"
schemaLocation="resource.xsd"/> t i
n usen.
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
timport a s declarations from other XML schemas by using the <import>
You can
e n r n
global
al onThe
declaration.
V -t <import> declaration must always be a direct child of the <schema>
n
element. During the process of importing declarations from other namespaces, the XML
schema validator looks for the document based on the schemaLocation attribute specified
in the <import> declaration.
The slide example shows how to use an <import> declaration. The two XML schemas
employees.xsd and resource.xsd have different targetNamespaces.
In the employees1.xsd XML schema, the XML namespace declaration
http://www.hr.com/resource is added in the root element. The definition of
employees1.xsd shows the <import> declaration. The employees1.xsd XML schema
imports declarations from the http://www.hr.com/resource namespace, which is
located in the resource.xsd file. This declaration enables you to reuse the declarations
from the resource.xsd XML schema within the employees1.xsd XML schema.

XML Fundamentals B - 17
Declaring an <include>

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

<?xml version="1.0"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:employees="http://www.hr.com/employees"
targetNamespace="http://www.hr.com/employees"
elementFormDefault="qualified">
. . .

employees1.xsd
s a
) h a
<?xml version="1.0"?>
r s
sฺ eฺ
<schema xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:employees="http://www.hr.com/employees" m
e uid
c @
xmlns:resource="http://www.hr.com/resource"
r o vi nt G
to tude
targetNamespace="http://www.example.com/employees"
elementFormDefault="qualified">
a j s
<include schemaLocation="resource_contacts.xsd"/>
a ฺ m his S
e n tin se t
( v al to u
o v ic nse
s t o r © 2014,
Copyright l i ceOracle and/or its affiliates. All rights reserved.
M aj rable
t ina adeclaration
The <include>
n n sfe allows you to combine XML schemas that have the same
V ale on-tr or no targetNamespace. You use the schemaLocation attribute to
targetNamespace
n
specify the location of the XML schema that you are including.
The slide example shows two XML schema documents with the same targetNamespace
http://www.hr.com/employees. Observe that in employees2.xsd, there is no
namespace attribute within the <include> declaration. This is because, unlike the
<import> declaration, you use the <include> declaration only on documents with the
same targetNamespace or no targetNamespace.
When the XML schema validator processes employees2.xsd, it includes the declarations
from resource_contacts.xsd as if they were declared in one document.
Note: Consider an example involving two XML schemas: A.xsd and B.xsd. The following
may be some useful scenarios to use the <import> and <include> declarations:
• If both A.xsd and B.xsd XML schemas have a targetNamespace and the
targetNamespace of A.xsd is different from the targetNamespace of B.xsd,
A.xsd must import B.xsd.
• If both A.xsd and B.xsd XML schemas have a targetNamespace and the
targetNamespace of A.xsd is the same as the targetNamespace of B.xsd, A.xsd
must include B.xsd.

XML Fundamentals B - 18
• If both A.xsd and B.xsd XML schemas do not have a targetNamespace, A.xsd
must include B.xsd.
• If XML schema A.xsd has no targetNamespace and XML schema B.xsd has a
targetNamespace, A.xsd must import B.xsd.
• If XML schema A.xsd has a targetNamespace and XML schema B.xsd has no
targetNamespace, A.xsd can either import or include B.xsd.
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 Fundamentals B - 19
XSLT 2.0

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

• Is a W3C recommendation
• Uses the XPath 2.0 and XQuery 1.0 data model
• Supports XML Schema data types
• Provides features such as the following:
– Grouping data
– Generating multiple outputs s a
) h a
– Temporary tree r s
sฺ eฺ
– Character mapping 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t uses the s data model that is used for XPath 2.0 and for XQuery. XSLT 2.0
e n
XSLT 2.0
r a nsame
V al XPath
uses
o n -t to locate and process content within an XML document’s logical hierarchy.
2.0
To usen
XSLT 2.0, set the version attribute in your stylesheet as follows:
<? xml-stylesheet version="2.0" ... ?>
XSLT 2.0 has new elements that help with grouping tasks, which are difficult to perform in
XSLT 1.0. XSLT 2.0 allows more than one document to be output.
By using XSLT 2.0, you can transform textual data that is not well-formed XML.
XSLT 2.0 allows you to define your own custom functions. It includes the xsl:function
element. xsl:function declares the name, parameters, and implementation of a user-
defined function that is used within XPath expressions in a given stylesheet.
Note
• Both XQuery and XSLT 2.0 use XPath 2.0 as a path language. XSLT is the primary
choice for transforming XML data, whereas XQuery is for querying XML documents.
• Some of the elements, such as xsl_value-of, produce different results in XSLT 2.0.
Therefore, you must make sure to specify the version of the transformation.
The next few pages of this appendix show examples of how to use some of the XSLT 2.0
features.

XML Fundamentals B - 20
Grouping Data

Use <xsl:for-each-group> to group XML elements.


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

<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:template match="/">
<xsl:for-each-group select="/LIBRARY//BOOK" group-
by="CATEGORY">
<xsl:sort select="CATEGORY"/>
<CATEGORY name="{CATEGORY}">
<xsl:for-each-group select="current-group()" group-
s a
by="YEAR">
) h a
<YEAR year="{YEAR}">
<xsl:copy-of select="current-group()/TITLE"/> r s
sฺ eฺ
</YEAR> m
e uid
c @
</xsl:for-each-group>
</CATEGORY> r o vi nt G
</xsl:for-each-group>
a jsto Stude
</xsl:template>
n a ฺm this
</xsl:stylesheet>
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s common task when you transform XML documents using XSLT. The
t data isaanvery
e n
Grouping
r
al ofongrouping
process
V -t data is difficult in XSLT 1.0. Even though there are techniques, such as
n
the Muenchian method to perform grouping, XSLT 1.0 has no built-in support for grouping.
For example, you want to publish library information online. You want to transform the library
document into a set of books grouped by their categories. You then want to further group the
books by year.
First, you specify XPath expressions in the select attribute of <xsl:for-each-group> to
select the elements to be grouped. You then provide one of the following four attributes for the
<xsl:for-each-group> element:
• group-by: To group selected items by ignoring the order in which the items appear
• group-adjacent: To group together only adjacent items with the same value
• group-starting-with: For a pattern that matches the first node in each group
• group-ending-with: For a pattern that matches the last node in each group
In the slide example, all the selected <BOOK> elements are grouped by the content within its
child <CATEGORY> element, regardless of the selected order. Within the <xsl:for-each-
group> element, you can include an <xsl:sort> element to sort the groups. In the slide
example, <xsl:sort select=“CATEGORY”> ensures that the order of the book groups
depends on the alphabetical order of their category names.

XML Fundamentals B - 21
In XSLT 2.0, you can refer to the current group node-set by using the current-group()
function. In the slide example, the current-group() function is used to group the books in
each category by their published year.
Note: The XSLT 2.0 examples discussed in this appendix use the Library_app.xml
document that has the following code:
<LIBRARY>
<BOOK>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<TITLE>PL/SQL</TITLE>
<AUTHOR>Nancy Greenberg</AUTHOR>
<CATEGORY>LANGUAGES</CATEGORY>
<PUBLICATION>ABC</PUBLICATION>
<PRICE>38.90</PRICE>
<YEAR>2006</YEAR>
</BOOK>
<BOOK>
s a
<TITLE>XML</TITLE> h a
<AUTHOR>Chaitanya Koratamaddi</AUTHOR>
s ฺrs) ฺ
<CATEGORY>LANGUAGES</CATEGORY>
@ em uide
<PUBLICATION>XYZ</PUBLICATION>
o v ic t G
<PRICE>40.00</PRICE>
t o r den
<YEAR>2007</YEAR> s
aj s Stu
a ฺ m hi
</BOOK>
t i n e t
<BOOK>
a l en o us
i c (v se t
<TITLE>Data Warehousing</TITLE>

t o rov licen
<AUTHOR>Richard Green</AUTHOR>
js ble
<CATEGORY>APPLICATION DEVELOPMENT</CATEGORY>
a
M fera
<PUBLICATION>PQR</PUBLICATION>
a
n t i n n s
<PRICE>25.20</PRICE>
l e r a
Va non-t
<YEAR>2005</YEAR>
</BOOK>
<BOOK>
<TITLE>SOA</TITLE>
<AUTHOR>Glenn Stokol</AUTHOR>
<CATEGORY>FUSION MIDDLEWARE</CATEGORY>
<PUBLICATION>XYZ</PUBLICATION>
<PRICE>31.20</PRICE>
<YEAR>2007</YEAR>
</BOOK>
</LIBRARY>
Note: You can run XSLT 2.0 transformations using the oracle.xml.parser.v2.oraxsl
command-line utility. For more information about the oraxsl command-line utility, see the
lesson titled “Using XSL Transformations to Transform XML Documents.”

XML Fundamentals B - 22
Generating Multiple Outputs

Use <xsl:result-document> to define multiple result


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

documents.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" name="book-format"/>
<xsl:template match="/">
<xsl:for-each-group select="/LIBRARY//BOOK" group-by="CATEGORY">
<xsl:result-document
href="BOOK{position()}_{current-group()/CATEGORY}.xml"
s a
format="book-format">
) h a
<BOOK_LIST category="{current-group()/CATEGORY}">
<xsl:copy-of select="current-group()"/> r s
sฺ eฺ
</BOOK_LIST> m
e uid
c @
vi nt G
</xsl:result-document>
</xsl:for-each-group>
r o
</xsl:template>
a jsto Stude
</xsl:stylesheet>
n a ฺm this
i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
treal-world s
In many
l e n t r a napplications, you need to create multiple outputs from one XML document.
a 2.0osupports
XSLT - creating multiple XSLT outputs. This feature allows you to define multiple
V n n
XSLT transformation outputs in one XSL stylesheet.
XSLT 1.0 does not have the capability to define multiple outputs in one XSL stylesheet. This
means that you must apply separate XSL transformations for each document. You can write
one XSL stylesheet that relies on XSLT parameters to indicate which page to create. But, the
XSL stylesheet needs to execute multiple times. The process is complicated because of the
need to manage the XSLT parameters.
XSLT 2.0 introduces the <xsl:result-document> element to define multiple outputs.
Suppose, while transforming a library catalog, you want to publish the <BOOK/> element
under each category as a separate result document. If you provide the slide example as the
XSL stylesheet and Library_app.xml as the input source XML document, the oraxsl
utility generates three output documents as BOOK1_LANGUAGES.xml,
BOOK2_APPLICATION DEVELOPMENT.xml and BOOK3_FUSION MIDDLEWARE.xml.
To generate multiple output documents, perform the following steps:
1. Specify the output formats.
2. Specify the output file names.

XML Fundamentals B - 23
Each output may have a different format. Therefore, you define the output formats by using
<xsl:output> at the beginning of the XSL stylesheet. You then refer to the formats by
using their names. Each <xsl:result-document> element then generates the output
format named by its format attribute.
In the slide example, a book-format output format is defined as an XML output format with
element output indentations:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<xsl:output method="xml" indent="yes" name=“book-format"/>


The <xsl:result-document> element later uses the preceding output format to create
one XML document for the books from each category.
You can also specify expressions that define the output file names. The slide example shows
naming the output files by using the order of the current group by calling the position()
function, and the category of the current group. The category of the current group is obtained
by using the current_group()/CATEGORY expression as follows:
<xsl:result-document href="../output/BOOK{position()}_{current-
s a
group()/CATEGORY}.xml" format=“book-format">
h a
Note: The href attribute of <xsl:result-document> is used to assign the
s s) of the
ฺrURI
corresponding output document. 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

XML Fundamentals B - 24
Creating Temporary Trees

• In XSLT 2.0, the intermediate XSL transformation results


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

and XSL variables are stored as a set of document nodes,


called temporary trees.
• Temporary trees provide the benefit of modularizing XSL
processing.
<xsl:variable name="library" select="//BOOK[number(YEAR)>=2006]"/>
<xsl:template match="/">
s a
<Expensive>
) h a
<xsl:apply-templates select="$library[number(PRICE)>38]"/>
</Expensive> r s
sฺ eฺ
<Cheap> m
e uid
c @
vi nt G
<xsl:apply-templates select="$library[number(PRICE) &lt; 38]"/>
</Cheap>
r o
</xsl:template>
<xsl:template match="*"> a jsto Stude
<xsl:copy-of select="."/>
n a ฺm this
... i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t1.0, theaintermediate
In XSLT
l e n t r n XSL transformation results and XSL variables are represented
asastrings. n
V - 2.0 provides the temporary trees feature. When you use XSLT 2.0, the
XSLT
no results and XSL variables, constructed by <xsl:variable>, <xsl:param>, or
intermediate
<xsl:with-param>, are stored as a set of document nodes, called temporary trees.
The advantage with temporary trees is that, instead of employing one large template, you can
break complex XSL transformations and modularize XSL processing. This provides a lot of
flexibility when applying templates or extracting data from XSL variable or template
parameters.
In the slide example, the library variable is set to select all books published after 2006. The
example then shows that the selected nodes can be further categorized as either expensive
books or cheap books. Expensive books have a price of 38 and higher and cheap books have
a price lower than 38.
Note: Without the temporary tree feature, <xsl:apply-templates
select="$library[number(PRICE) < 38]"/> would be invalid. This is because there
is no option to access data that was previously selected when creating the $library
variable.

XML Fundamentals B - 25
The following is the complete listing of the slide example:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:variable name="library" select="//BOOK[number(YEAR)>=2006]"/>
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

<xsl:template match="/">
<Expensive>
<xsl:apply-templates select="$library[number(PRICE)>38]"/>
</Expensive>
<Cheap>
<xsl:apply-templates select="$library[number(PRICE) &lt; 38]"/>
</Cheap>
s a
</xsl:template> h a
s ฺrs) ฺ
<xsl:template match="*">
@ em uide
<xsl:copy-of select="."/>
o v ic t G
t o r den
</xsl:template> s
aj s Stu
</xsl:stylesheet> 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 Fundamentals B - 26
Character Mapping

• Character mapping allows you to generate files with


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

reserved or invalid XML characters in the XSLT outputs.


• Use <xsl:character-map> to declare mapping
characters.
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output use-character-maps="jsp"/>
<xsl:character-map name="jsp">
s a
<xsl:output-character character="&#xE001;" string="&lt;%"/>
) h a
<xsl:output-character character="&#xE002;" string="%&gt;"/>
</xsl:character-map> 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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s files with reserved or invalid XML characters in the XSLT outputs,
twant toagenerate
You may
e n r n
V al as the
such
o n -t>, and & characters. XSLT 2.0 provides the character mapping feature that is
<,
n when you want to generate documents containing markup, such as XML Schema,
very useful
XSL stylesheet, or JavaServer Pages (JSP) files.
In XSLT 1.0, you use the disable-output-escaping attribute of the <xsl:text> and
<xsl:value-of> elements to specify character escaping. But this is a complex process.
XSLT 2.0 allows you to declare mapping characters with an <xsl:character-map>
element as a top-level stylesheet element. In the <xsl:character-map> element, you
specify the mapping between the output characters and their representations in the XSL
stylesheets.
For example, you may want character representations from the Unicode private area, which
are Unicode numbers between #xE000 and #xF8FF. In this case, you associate the character
mapping definitions to each <xsl:output> by using the use-character-maps attribute.
The slide example shows a code snippet of an XSL stylesheet that defines a character map
for generating a JSP file. In the example, the character map is set to map #xE001 to <% and
#xE002 to %>. This means that in the XSLT output that is generated, all #xE001 characters
are replaced by <% and any occurrences of the #xE002 character is replaced by %>. With
character mapping in XSLT 2.0, the unescaped characters are guaranteed to persist in the
serialization output even when a text node or attribute is copied in a temporary tree.

XML Fundamentals B - 27
Summary

In this appendix, you should have learned how to:


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

• Declare elements, attributes, entities, and notations in a


DTD
• Use the additional attribute types provided by the DTD
specifications
• Reference the DTD in an XML document
• Reference entity declarations s a
) h a
• Use the <include> and <import> declarations inฺrs XML
schema documents
s
m ideฺ
e
v
• Use XSLT 2.0 features such as the following: i c@ t Gu
ro n o e
– Grouping data a jst Stu
d
ฺm this
it na
– Generating multiple outputs
n use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s
t typeadefinition
l e n
A document
t r n (DTD) establishes the grammar for a valid structure of an XML
a onWhen
document.
V - an XML document follows the rules declared in the DTD, the XML document
is said nto be “valid.” The DTD can be thought of as an agreement that will be conformed to by
applications generating or receiving the XML documents.
This appendix provided additional information about the various attribute types that are
available in <!ATTRLIST> declarations such as:
• CDATA and Enumerated type
• ID, IDREF, and IDREFS
• ENTITY and ENTITIES
• NMTOKEN and NMTOKENS
• NOTATION
This material revised and supported the basic information about DTD covered in the lessons.
This appendix discussed how to use the <import> and <include> declarations while
creating an XML schema document from multiple XML schema documents.
XSLT 2.0 features were also discussed in the appendix.

XML Fundamentals B - 28
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Managing External Tools in 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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
Objectives

After completing this appendix, you should be able to:


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

• Add external tool commands in JDeveloper


• Edit and delete external tool commands in JDeveloper
• Use the external tools added to the JDeveloper
environment

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na sare guided through the process of managing external tool commands in
e n
In this appendix,
r a n
you
al on-Adding
JDeveloper.
V t external tools enables you to invoke a variety of utilities and operating
systemncommands within JDeveloper to process files in projects.

XML Fundamentals C - 2
External Tools with JDeveloper

An external tool:
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

• Is any script or application that is external to JDeveloper


– Command-line or graphical applications
– Operating system commands or scripts
• Is added to JDeveloper Tools or context menus
• Is configured by using the External Tools window that is opened
by selecting Tools > External Tools
s a
) h a
r s
sฺ eฺ
m
e uid
c @
External r o vi nt G
tool
a jsto Stude
n a ฺm this
i
nt use
• Configuration is saved inleproduct-preferences.xml.
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s an integrated development environment (IDE). Therefore, most of the
Oraclen
e JDeveloper
r a nis
al on-tasks
development
V t that you perform must be possible through the JDeveloper interface.
n does not provide built-in tools or extensions to facilitate a particular
If JDeveloper
development task, you can configure JDeveloper to invoke an appropriate external program
or tool to perform the task.
Any application, command-line or graphical in nature, that is external to JDeveloper can be
invoked by using the JDeveloper Tools menu, context menus, or toolbar.
To add an external tool in JDeveloper:
1. Select Tools > External Tools.
2. In the External Tools window, click New.
When you click New, a wizard guides you through the steps to configure JDeveloper to invoke
the external tool. These steps to add an external tool are shown in this appendix.
Note: In Linux, JDeveloper saves the configuration of external tools in a file called product-
preferences.xml, which is created in the
/home/oracle/.jdeveloper/system11.1.1.2.36.55.36/o.jdeveloper/produc
t-preferences.xml folder.

XML Fundamentals C - 3
Managing External Tools

You can use the External Tools window to:


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

• Add new tools


• Edit or delete existing tools

s a
) h a
r s
sฺ eฺ
m
e uid
c @
r o vi nt G
Two external tools were added
a j sto Stude
in this example: Mozilla Firefox
n a ฺm this
and ShowXMLns i
nt use
l e
c ( va e to
r o v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a
t Tools swindow is displayed when you select Tools > External Tools.
e n
The External
r a n
V alslideoshows
The n -t the External Tools window with two externals tools that were added for this
course:nMozilla Firefox and ShowXMLns.
In the External Tools window, you can perform the following tasks:
1. Click New to create a new external tool.
2. Click Edit after selecting the tool name to alter the settings for the external tool.
3. Click Delete after selecting the tool name to delete the external tool from the JDeveloper
environment.
These operations are explained on the next few pages.

XML Fundamentals C - 4
Create External Tool:
Step 1 of 11 – Type
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
n t New
After clicking a ninsthe External Tools window, you are guided through the required steps
e
l then-Create
byausing t r External Tool wizard.
V n o
In Step 1 of 11: Type, you select the tool type and then click Next. The “Create External
Tool: Step 2 of 11 - External Program Options” window is displayed.
Note: The number of wizard steps that you go through depends on the options that you select
in the various windows.

XML Fundamentals C - 5
Create External Tool:
Step 2 of 11 – External Program Options
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na s to provide the path of the program executable, the directory in which to
t enablesanyou
e n
This step
l program, r
V athe
run
o n -t and the arguments to pass into it.
n
• Program Executable: Enter the complete path and file name of the external program to
be invoked, or click Browse to locate it.
• Arguments: Provide arguments that JDeveloper passes to the external program when it
is launched. You can add your own arguments, or select one from a list of macros by
clicking Insert. Make sure that you insert a space between the text you enter and the
macros you add.
• Run Directory: Enter the complete path name of the directory that the launched
external program will use as its current directory, or click Browse to locate a directory
and select it. You can add your own arguments, or select one from a list of macros by
clicking Insert. If this field is left blank, the external program will run from the parent
directory of the program executable.
• Command Sample: After you have supplied values, JDeveloper displays a sample of
the command that launches the external program for your inspection.
When all the settings are complete, click Next. The “Create External Tool: Step 9 of 11 –
Display” window is displayed.

XML Fundamentals C - 6
Create External Tool:
Step 9 of 11 – Display
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t controls s appearance of the menu items and toolbar buttons that launch the
e n
This step
r a n
the
V al program
external
o n -t from JDeveloper.
n
• Caption for Menu Items: Enter the name of the external program that will appear in
menus and on toolbars.
• ToolTip Text: Enter the tool tip label for the external program icon on the JDeveloper
IDE toolbar. If you leave this field empty, the tool tip text of the toolbar button will be the
same as “Caption for Menu Items.”
• Icon Location: If the external program is to be invoked from the toolbar, or if you want
the external program menu item to display an icon, enter the path name to an image file
or click Browse to locate and select it.
• Click Use Default to reset the icon location back to the default for the tool. For external
program tools, this is the icon of the application being launched on Windows, or a
generic external program icon on other platforms.
• Preview: This displays the menu entry that you have specified for your review.
When all the settings are complete, click Next. The “Create External Tool: Step 10 of 11 –
Integration” window is displayed.

XML Fundamentals C - 7
Create External Tool:
Step 10 of 11 – Integration
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i n a
t controls s the external program is integrated into the JDeveloper IDE.
l e n
This step
r a n
how
V•a Add o n -t to Menu: Select options to control which menus display the menu items that
Items
n
launch external programs.
• Tools Menu: Select to place the item in the main Tools menu, under External Tools.
• Navigator Context Menu: Select to place the item at the bottom of the context menu
(which appears when you right-click) for a document icon in the Navigator.
• Code Editor Context Menu: Select to place the item at the bottom of the context menu
(which appears when you right-click) for a document window.
Add Buttons to Toolbar: Select options to control where the buttons that launch external
programs will display. Select the Main Toolbar check box to place the item at the extreme
right of the main window’s toolbar.
Before tool starts: Select the Save all check box to save all open files before starting this
tool.
After Tool Exits: Select the Reload Open Files check box to ensure that JDeveloper
refreshes any open files after the external program exits.
Log Output to Message Log: Select this check box to send the output of the external tool to
the JDeveloper Message Log. When all settings are complete, click Next. The “Create
External Tool: Step 11 of 11 – Availability” window is displayed.
XML Fundamentals C - 8
Create External Tool:
Step 11 of 11 – Availability
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t controls s the external program is available from JDeveloper:
l e n
This step
r a n
when
V•a Always:
o n -tSelect this option to enable the external program to be launched at all times.
n
• When a File is Selected or Open in the Editor: Select this option to enable the
external program to be launched only when a file is selected or open in an editor.
• When Specific File Types are Selected: If you select this option, the external program
will be available only when the file types that you specified are selected. Select the
appropriate file types from Available Types and move them to Selected Types. You can
make multiple choices.
When all the settings are complete, click Finish. The External Tools window is displayed
again.

XML Fundamentals C - 9
Using the Newly Added
“gedit Text Editor” External Tool
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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
t Tools swindow now displays the newly added “gedit Text Editor” tool. Click OK
e n
The External
r
l this nwindow.a n
to a
V exit
o -t
n Tools menu to display the newly added external tool. Select the gedit Text Editor
Click the
menu option to launch the gedit text editor as shown in the slide.

XML Fundamentals C - 10
Editing an External Tool

To edit an external tool:


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

1. In the External Tools window, select the name of the tool


you want to edit.
2. Click Edit.

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
i na
tExternal s – Step 1 of 4 window is displayed. Note that the number of wizard
e n
The Edit
r a nTool
al youogo
steps
V n t
-through depends on the external tool that you select.
n
In the slide example, you want to edit the newly added “gedit Text Editor” external tool. The
window displays a graphic representation of the steps of the wizard that you can edit: External
Program Options, Display, Integration, and Availability.

XML Fundamentals C - 11
Deleting an External Tool

To delete an external tool:


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

1. In the External Tools window, select the name of the tool


to be removed.
2. Click Delete.

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na s not prompt for confirmation before the tool entry is deleted. You may
e n
Note: JDeveloper
r a n
does
V al to make
want
o n -ta copy of the product-preferences.xml file before removing or editing a
n
command.

XML Fundamentals C - 12
Summary

In this appendix, you should have learned how to:


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

• Add external tool commands in JDeveloper


• Edit and delete external tool commands in JDeveloper
• Use external tools added to the JDeveloper environment

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 v©i 2014, eOracle
nsand/or its affiliates. All rights reserved.
c
jsto ble li
Copyright
a
M fera
t i na slearned how to use the External Tools window to find, add, edit, and
e n
In this appendix,
r a n
you
V al external
remove
o n -t tools, and use the menu or toolbar options in the JDeveloper environment.
n
The configuration of each external tool is stored in a file called tools.xml, which is located
in the JDEV_HOME\jdev\system<version> directory, where JDEV_HOME is the directory
in which JDeveloper is installed and <version> is a number sequence representing the
version of JDeveloper installed.

XML Fundamentals C - 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
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Glossary

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© 2014,
Copyright l i c n and/or its affiliates. All rights reserved.
eOracle
a js ble
a M fera
n t i n n s
l e r a
Va non-t
GLOSSARY

D
Database A collection of data, usually in the form of tables or files, under the control of a
database management system. See Database management system.
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

Database administrator (DBA) A person within the information technology (or information
systems) organization, who is responsible for administering, monitoring, and maintaining the
database
Database management system (DBMS) The component of a database that controls all user
and system activities related to the core functions of the database such as security
checking, tablespace allocation, space management
Document Object Model (DOM) The Document Object Model (DOM) recommended by the
W3C is a universal API for accessing the structure of XML documents. DOM provides a a
a
language-neutral and platform-neutral object model for web pages and XML documents.
h s
s) and
DOM describes language-independent and platform-independent interfaces toฺraccess
s
operate XML components and elements. e m ideฺ
i c@ t Gofuelements
Document Type Definition (DTD) provides the definition and relationships
v
contained in an XML document. A DTD validates an XML o en
tordocument.
a js Stu d
F
n a ฺm this
File Transfer Protocol (FTP) A service that i
ntallows e to transfer files to and from other
you
s
computers on the Internet. Anyonevwho a l ehas u
access
o to the FTP server can transfer the
( t
o v ic nse
available files to his or her computer.
FLWOR The acronymtFLWOR
s o r l i ce
(pronounced “flower”) represents the XQuery clauses FOR,
a j l e
b RETURN. A FLWOR expression has at least one FOR or LET
LET, WHERE, ORDER
a M feBY, r aand
clause and
n t in ans
a return clause; single WHERE and ORDER BY clauses are optional.
HVale n - tr
no Markup Language (HTML) The language used to create HTML pages for the Web
Hypertext
using a word processor or text editor
Hypertext Transfer Protocol (HTTP) The first component, the protocol, of a URL address,
that is used widely in the Internet and intranet environment. HTTP defines how to interpret
information. Other common protocols that you may come across include FTP, news, and
gopher. Also See Uniform Resource Locator.
J
J2EE (Java 2 Platform, Enterprise Edition) A Java platform designed for the mainframe-
scale computing that is typical of large enterprises. Sun Microsystems (together with
industry partners such as IBM) designed J2EE to simplify application development in a thin
client–tiered environment. J2EE simplifies application development and decreases the need
for programming and programmer training by creating standardized, reusable modular
components and by enabling the tier to handle many aspects of programming automatically.

XML Fundamentals D - 2
J
JavaScript JavaScript is the most popular scripting language (A scripting language is a
lightweight programming language) on the Internet, and works on all major browsers such
as Internet Explorer, Firefox, Chrome, Opera, and Safari.
N
Namespace provides a mechanism to distinguish elements with the same name but different
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

definitions that are used in the same XML document.


O
Oracle XML Database (XML DB) is a high-performance XML storage and retrieval
technology that is available with the Oracle 11g server. It fully absorbs the W3C XML data
model into the Oracle server, and provides standard access methods for navigating and
querying XML. Oracle XML DB combines the advantages of relational database technology
with XML technology. a s
h a
Oracle JDeveloper is a powerful development tool for building, debugging, and deploying
)
J2EE, web services, and Internet applications. sฺrs m ideฺ
e
P
v i c@ t Gu
Parsed character data (PDATA) Textual information comprising
t o ro markup
d e n that describes the
data it contains and character data s
aj s Stu
a ฺ m hi
S t i n e t
Simple API for XML (SAX) An event-based a l en API o usreading XML documents. A program
for
v t
c ( asnsa eserialized stream of data, that is, one piece at a
using SAX reads an XML document v i
time. t o ro lice
j s
aArchitecture
b le(SOA) An architecture for the development of loosely coupled
Service-Oriented M
a sfer a
distributed
t i napplications
a len Generalized
Standard - t r an Markup Language (SGML) A markup language for organizing and
V non
tagging elements of a document, including headings, paragraphs, tables, and graphics. The
elements are marked according to their meaning and relationship to other elements rather
than to the format of their presentation.
Simple Object Access Protocol (SOAP) A lightweight, XML-based protocol for exchanging
data in a distributed environment. The definition of SOAP is in XML and places no
restriction on the format.
U
Uniform Resource Locator (URL) Text used to identify and address an item in a computer
network
Uniform Resource Identifier (URI) A string of characters that identifies an Internet Resource
Universal Description, Discovery, and Integration (UDDI) is an XML-based registry that
enables businesses to list themselves and their services on the Internet.

XML Fundamentals D - 3
W
Web Services Description Language (WSDL) uses the XML format for describing web
services. It describes what functionality a web service offers, how it communicates, and
where it is accessible.
World Wide Web Consortium (W3C) The World Wide Web Consortium (W3C) is an
international community where member organizations, a full-time staff, and the public work
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

together to develop Web standards. Led by Web inventor Tim Berners-Lee, W3C’s mission
is to lead the Web to its full potential. Contact W3C for more information.
X
Extensible Hypertext Markup Language (XHTML) The World Wide Web Consortium (W3C)
has defined Extensible Hypertext Markup Language (XHTML) as a successor to HTML.
XHTML is designed to conform with XML standards and well-formed document rules, and
provides a way to reproduce, subset, and extend HTML documents. An XHTML document a
a s
is a particular XML document instance that is intended for processing by a web browser.
h
s
XML Extensible Markup Language (XML) describes data objects called XMLฺrdocuments s)
that are composed of markup and data. em ideฺ
XML Developer’s Kit (XDK) To help application developersrtake o ic@
v advantage t
uGof XML, Oracle
n
disea standards-based set
Application Server includes XML Developer’s Kit, or jXDK.
a sto XDK t u
S and deploying XML-
of components, tools, and utilities that eases the
a ฺ m of hbuilding
task i s
enabled applications.
le ntin se t
u
XML Entity A unit of data storage(that
c va is identified
e to by a case
case-sensitive
sensitive name.
name It represents
replacement text to eliminate i s
ova lotlicofetyping
n in XML documents. An entity is referenced by
t o r
a js an ampersand
its name prefixed with
b l e (&) and terminated by a semicolon (;). The ampersand
is referred to asM
na racharacter.
an escape
fe
n ti aspecification
XMLelanguage n s defines the rules that govern XML document structure, and how
a l - t r
on must read them.
VXML processors
n
XML Parser Most browsers have a built-in XML Parser to read and manipulate XML. The
parser converts XML into a JavaScript-accessible object (the XML DOM). The XML DOM
contains methods (functions) to traverse XML trees, and access, insert, and delete nodes.
XML Schema provides a way to describe XML document structure by using data type
definitions, and uses namespace support.
XML Schema Document (XSD) XML Schema is an XML-based alternative to DTD. An
XML schema describes the structure of an XML document. The XML Schema language is
also referred to as XML Schema Definition (XSD).
XPath The XML Path Language (XPath) provides syntax for searching an XML document.
XPath expressions are used in an XSL stylesheet to match specific nodes.
XQuery is an XML query language that provides a data model for XML documents and a set
of operators to use with the data model.

XML Fundamentals D - 4
X
Extensible Stylesheet Language (XSL) XSL is implemented by XSL Transformations (XSLT)
to specify how to transform an XML document into another document. XSLT uses an XML
vocabulary for transforming or formatting XML documents.
XSL Formatting Objects (XSL-FO) An XML application that is used for describing the
precise layout of text on a page. XSLT is used to generate XSL-FO elements that are output
Unauthorized reproduction or distribution prohibitedฺ Copyright© 2017, Oracle and/or its affiliatesฺ

to another XML document. Additional processing is needed to create the final output form
such as a portable document format (PDF) document.
XSL Transformations (XSLT) An XML application that processes rules contained in an XSLT
Style Sheet. The rules in an XSLT Style Sheet can be applied to any XML document.
XSLT An XML Stylesheet Language (XSL) document is an XML document with elements
that define the transformation rules.
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 Fundamentals D - 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

You might also like