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

Applications Systems Group

September 18, 2002

Introduction to XML
for
iSeries Developers
Presented By:
Sharon L. Hoffman
shoffman@techreflections.com

Agenda
XML Overview
definition and function
architecture

Basic XML syntax


Integrating XML with other technologies such as
HTML and Java
Translating and transforming XML for relational
database storage and presentation
iSeries XML support and tools

XML Overview

XML Defined
eXtensible Markup Language
Looks similar to HTML
<tag> </tag> syntax
both based on SGML (Standard Generalized Markup Language)

Can be used to define data elements


An XML document contains tags and data
Some browsers can display an XML document but
data is normally filtered using a stylesheet for more
useful display

XML Value Proposition


Create your own tags for data elements
Data interchange
industry-specific standards

Context sensitive searching

<?xml version ="1.0"?>


<policies>
<policy>
<type>auto</type>
<policy_number>12345</policy_number>
<customer_name>Sharon L. Hoffman</customer_name>
</policy>
<policy>
<type>auto</type>
<policy_number>76656</policy_number>
<customer_name>John Doe</customer_name>
</policy>
</policies>

XML Variations
specific variations can be created to handle specific
tasks
example: XSLT is an XML variant used to translate
an XML document into another format
use to translate between two XML formats
use to format data for display
requires an XSLT processor

example: PCML is an XML variant used to call


iSeries programs from Java applications
included in IBM Toolbox for Java

XML Architecture
XML document
contains tags and data

DTD - Document Type Definition


optional
defines the structure of an XML document
used to validate an XML document

Schema
newer standard for defining and validating XML documents
multiple schemas can be combined

XML Architecture
Use a DTD or a schema -- not both
A parser or an an editor can validate the XML
document using the DTD or schema
Why use DTDs and schemas?
share information about the document structure
ensure that all required elements are present
ensure that values pass basic data type and value constraints
additional, application-specific validation is often
required

critical when documents are received from many different sources

XML Syntax

10

General XML Syntax

case-sensitive
no spaces
names cannot start with xml
start names with a letter or _
after the first character, names can also contain
numbers
.

Use entity references for reserved characters


&gt; &lt; &quot; &apos; &amp;
11

XML Elements
Elements
a tag and its data
<name>Sharon L. Hoffman</name>

Sub elements
break elements into parts
<name>
<first_name>Sharon</first_name>
<middle_initial>L</middle_initial>
<last_name>Hoffman</last_name>
</name>

Root element
one per XML document

12

XML Attributes

specify things about an element


name/value pairs
quotes (single or double) are required for values
must be unique within an element
use for things with limited options
e.g., policy type
<policy type=auto> </policy>

In most cases, a sub-element can be substituted for


an attribute
13

Additional XML Document


Components
empty elements

contain only one tag


usually contain attributes
example: <region assignment=Northeast/>
example: </br>

Comments
same format as HTML comments
<!-- this is a comment -->

CDATA
everything is treated as character data
<![CDATA[Treat this information as text ]]>
14

Document Prolog
Precedes the root element
Document declaration
Specifies the XML version
Specifies encoding

Specify the DTD or schema (if any)


Can contain optional processing instructions
example: stylesheet that applies to this document

15

XML Document Structure


XML documents must be well-formed
Can optionally be validated (checked against a DTD
or schema)
start and end tags are required except for empty
elements
start and end tag name must match exactly
No overlapping elements

16

Document Type Definition


(DTD)

17

Document Type Definition


(DTD)
Defines the elements for an XML document
For each element specifies

order
required or optional
number of entries (zero or more, one, one or more)
attributes

Usually a separate document, but can be embedded


within an XML document

18

DTD Coding
Linking an XML document to a DTD
Include a <!DOCTYPE tag in the document prolog
SYSTEM keyword for external (to the XML document) DTD
Link to a URL or a directory structure

In the DTD
Specify each element with a <!ELEMENT tag
Specify attributes for an element with a <!ATTLIST tag

19

DTD Coding
Elements can contain text (#PCDATA) or other
elements
Order of elements is controlled by order specified in
DTD
Number of entries:

no qualifier -- exactly one


* -- zero or more
+ -- one or more
? -- zero or one

Separate element choices with |


Example: must contain either a VIN# or a description
20

10

DTD Coding
ATTLIST entry specifies the element name plus:
Multiple entries for: attribute name, attribute type, attribute default

Common attribute types


CDATA -- any valid XML character data
ID -- Unique within the document
Enumerated -- pick from specified values separated by | (no
keyword)

Attribute Defaults

#Required
#Implied (optional)
#Fixed (constant)
literal (default value may be overridden in document)
21

DTD Entities
Create a shorthand for repeated information in the
XML document or in the DTD itself
Use a <!ENTITY tag
Define the information to be included in the XML
document in the DTD
Specify SYSTEM to link to an external file or URL
Reference with &entity_name; (similar to built-in
entity references such as &lt;)

22

11

Sample DTD
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT policies (policy+)>
<!ELEMENT policy (type, policy_number,
customer_name)>
<!ELEMENT customer_name (#PCDATA)>
<!ELEMENT policy_number (#PCDATA)>
<!ELEMENT type (#PCDATA)>

23

Document Referencing DTD


<?xml version="1.0"?>
<!DOCTYPE policies SYSTEM "D:\My
Documents\XML\test.dtd">
<policies>

</policies>
24

12

Schema

25

Schema Basics
Like a DTD a schema provides a definition for the
structure of an XML document
A schema can be used to validate XML
An XML document itself
Reference the W3.org schema definition as a
namespace
by convention this is associated with the xsd or xs prefix

Reference the schema in your XML document so that


it can be used for validation by parsers and editors
26

13

Schema Basics
Standard data types (approximately 40)
examples: string, integer, date

Data restriction attributes (e.g., minOccurs)


Complex elements
sequence defines order for sub-elements
all allows sub-elements in any order
choice offers a selection of mutually exclusive sub-elements

A schema attribute element is only allowed within an


element element
for each attribute specify name, type, use, value (default value)

27

User-defined Data Types


use a restriction element to limit values (can apply
to standard schema data types)
use an extension element to expand an existing
element definition by adding additional sub-elements
any element can become the basis for other element
definitions using the syntax: base =
defines the starting point for restriction or extension
an XML schema standard data type (e.g., xsd:string) or an element
youve defined elsewhere

28

14

Sample Schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSch
ema" elementFormDefault="qualified">
<xs:element name="customer_name"
type="xs:string"/>

</xs:schema>
29

Document Referencing
a Schema
<?xml version="1.0"?>
<policies
xmlns:xsi="http://www.w3.org/2001/XMLSc
hema-instance"
xsi:noNamespaceSchemaLocation="D:\My
Documents\XML\test.xsd">

</policies>
30

15

Namespaces

31

Namespaces
Purpose
Resolve conflicting tags (e.g., two elements called description)
Allows an application to only process elements that belong to a
certain namespace

Define a prefix for element names within an XML


document
Can be linked to schemas

32

16

Namespace Coding
Reserved attribute: xmlns
A namespace is specified in URL format, but the URL
does not have to actually exist
Scope
a namespace applies to the element where its defined and all
descendants of that element

Namespace prefix
required on each sub-element and attribute
required on both starting and ending tags

33

Special Namespaces
Default namespace
No prefix specified
All elements within the scope of the default namespace belong to
the default namespace, unless they are prefixed
Reduces coding
Retains ability for applications to selectively process elements
associated with the namespace

W3C namespaces
XML grammars
Example: XHTML - basically a well-formed version of HTML tags

34

17

Related Standards

35

XSLT
Written in XML
Used for simple mapping between two different XML
documents (i.e., different structures)
Used for mapping to HTML
sometimes in conjunction with Cascading Style Sheets (CSS)

Defines a template for matching against the input


document
Requires an XSLT processor
usually part of a web application server

36

18

XPATH
Usually (but not always) associated with XSLT
Used to navigate relative or absolute hierarchical
structure within XML
Use an @ to match on attributes

Basic programming syntax


Comparisons and expressions
Functions
examples: count, concat, substring, position, sum

37

XLink
Similar to hyperlinks in HTML
Allows the actual links to be maintained in a
centralized document
reduces maintenance for broken links

Standard is still evolving


Legal challenges
Most browsers do not currently support XLINK
XPointer: a related standard used to link to portions
of an XML document
38

19

XML Tools

39

Parsers
Compose and decompose XML documents under
application control
Two general models
Document Object Model (DOM)
Parse the entire document
Convert from one structure to another

Simple API for XML (SAX)


Scan for a specific element

May be invoked by an editor or by an application


written in Java, RPG, etc.

40

20

XML Editors
May be simple text editors or XML specific
An XML-aware editor will handle tasks such as:

Recognize XML syntax


Check for well-formed documents
Generate matching tags
Show document structure
Validate documents against DTDs and schemas

41

iSeries XML Tools


XML Extenders LPP
XML support in WebSphere Studio
XML Toolkit for iSeries

42

21

XML Extender LPP


New with V5R1 for iSeries (available for other DB2
implementations)
Two extenders in one LPP
DB2 Text Extender
DB2 XML Extender

XML columns versus XML collections


XML columns store an entire XML document within a database
record
XML collections compose and decompose XML documents using
data in DB2 relational database files

43

XML in WebSphere Studio


WebSphere Studio Application Developer (WSAD) is
the primary tool for working with XML within this
product family
For web designers with a background in HTML and
related tools consider WebSphere Studio Site
Developer (WSSD)
WSAD XML support
authoring tools
transforming tools
database mapping tools

44

22

WSAD XML Tools


Create XML documents, DTDs, and schema
Text view and hierarchical view
context sensitive selections

Debugging tools
Mapping and document generation
Generate schema or DTD
Generate XML documents from SQL
Generate other associated components such as Java beans and
HTML

45

WSAD XML Tools


Map database files to a DTD
Map between XML documents
visual mapping
generate XSLT
add XPATH expressions, conversion functions, etc.

46

23

XML Toolkit for iSeries

Announced April 9, 2002


Announcement letter 202-072
Provides packaging for parser updates
Currently includes XML4C and XMLPR parsers

47

24

Sample XML Document


<?xml version="1.0"?>
<policies>
<policy>
<type>auto</type>
<policy_number>12345</policy_number>
<customer_name>Sharon L. Hoffman</customer_name>
</policy>
<policy>
<type>auto</type>
<policy_number>76656</policy_number>
<customer_name>John Doe</customer_name>
</policy>
</policies>
Sample DTD
<?xml version="1.0" encoding="UTF-8"?>
<!ELEMENT policies (policy+)>
<!ELEMENT policy (type, policy_number, customer_name)>
<!ELEMENT customer_name (#PCDATA)>
<!ELEMENT policy_number (#PCDATA)>
<!ELEMENT type (#PCDATA)>

XML Document Referencing DTD


<?xml version="1.0"?>
<!DOCTYPE policies SYSTEM "D:\My Documents\XML\test.dtd">
<policies>
<policy>
<type>auto</type>
<policy_number>12345</policy_number>
<customer_name>Sharon L. Hoffman</customer_name>
</policy>
<policy>
<type>auto</type>
<policy_number>76656</policy_number>
<customer_name>John Doe</customer_name>
</policy>
</policies>

Sample Schema
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified">
<xs:element name="customer_name" type="xs:string"/>
<xs:element name="policies">
<xs:complexType>
<xs:sequence>
<xs:element ref="policy"
maxOccurs="unbounded"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="policy">
<xs:complexType>
<xs:sequence>
<xs:element ref="type"/>
<xs:element ref="policy_number"/>
<xs:element ref="customer_name"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="policy_number">
<xs:simpleType>
<xs:restriction base="xs:int">
<xs:enumeration value="12345"/>
<xs:enumeration value="76656"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="type" type="xs:string"/>
</xs:schema>

XML Document Referencing Schema


<?xml version="1.0"?>
<policies xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance" xsi:noNamespaceSchemaLocation="D:\My
Documents\XML\test.xsd">
<policy>
<type>auto</type>
<policy_number>12345</policy_number>
<customer_name>Sharon L. Hoffman</customer_name>
</policy>
<policy>
<type>auto</type>
<policy_number>76656</policy_number>
<customer_name>John Doe</customer_name>
</policy>
</policies>

XML Resources
There are tons of web-based XML resources as well as many excellent books on XML.
I've listed a few that I use frequently to get you started, but I encourage you to explore
additional options. One caveat, when working with XML, you need to be very aware of
publication dates. While a book published in 2000 may contain good, basic XML
information, it will not contain the latest best practices and newer standards such as
schemas.
Web sites:
IBM PartnerWorld for Developers iSeries XML information:
http://www.as400.ibm.com/developer/java/xml/index.html
I particularly recommend the whitepapers available at the iSeries PartnerWorld for
Developers site which cover the specifics of using the IBM XML parsers from RPG and
Java applications. Here's a direct link to the list of whitepapers:
http://www.as400.ibm.com/developer/java/xml/resources.html#tech
The XML section of the World Wide Web Consortium site:
http://www.w3.org/XML/
IBM Developerworks XML site:
http://www-106.ibm.com/developerworks/xml
XML FAQs, articles, discussion groups and more:
http://www.devx.com/xml
OReilly, one of the premier technical book publishers, maintains this XML information
site:
http://www.xml.com/
A list of XML books with comments by Charles F. Goldfarb, one of the original
developers of the markup languages upon which both XML and HTML are based.
http://www.xmlbooks.com
Books:
Beginning XML
by David Hunter, et al.
ISBN: 1-861005-59-8
A comprehensive (nearly 800 pages) book that looks at XML from a programmers
perspective.

CodeNotes for XML


edited by Gregory Brill
ISBN: 0-8129-9191-5
A quick guide to XML syntax and architecture.
Mastering XML
by Ann Navarro et al.
ISBN: 0-7821-2266-3
An A-to-Z XML primer of more than 800 pages. This version, which I still use, was
published in 2000, a newer version with a slightly different title and a different group of
authors is now available:
Mastering XML Premium Edition
by Chuck White et al.
ISBN: 0782128475
XML: A Managers Guide
by Kevin Dick
ISBN: 0-201-43335-4
Details are somewhat dated in this book which was published in 2000, but the overview
of what XML is and how it can be put to work is still very useful.
XML by Example (Second Edition)
by Benoit Marchal
ISBN: 0-7897-2504-5
One of the best introductory books on XML I've come across. All of the basic concepts
are clearly explained with lots of code examples.
XML in a Nutshell (Second Edition)
by Elliotte R. Harold and W. Scott Means
ISBN: 0596002920
A comprehensive introduction to XML

Вам также может понравиться