Академический Документы
Профессиональный Документы
Культура Документы
Introduction to XML
for
iSeries Developers
Presented By:
Sharon L. Hoffman
shoffman@techreflections.com
Agenda
XML Overview
definition and function
architecture
XML Overview
XML Defined
eXtensible Markup Language
Looks similar to HTML
<tag> </tag> syntax
both based on SGML (Standard Generalized Markup Language)
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
XML Architecture
XML document
contains tags and data
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
XML Syntax
10
case-sensitive
no spaces
names cannot start with xml
start names with a letter or _
after the first character, names can also contain
numbers
.
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
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
15
16
17
order
required or optional
number of entries (zero or more, one, one or more)
attributes
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:
10
DTD Coding
ATTLIST entry specifies the element name plus:
Multiple entries for: attribute name, attribute type, attribute default
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 <)
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
</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
13
Schema Basics
Standard data types (approximately 40)
examples: string, integer, date
27
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
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)
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
37
XLink
Similar to hyperlinks in HTML
Allows the actual links to be maintained in a
centralized document
reduces maintenance for broken links
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
40
20
XML Editors
May be simple text editors or XML specific
An XML-aware editor will handle tasks such as:
41
42
21
43
44
22
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
46
23
47
24
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 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.