XML usage in DB2

50
XML for z/OS COBOL Developers Troy Coleman CA Technologies Session Code: E11 Wednesday, 10 N ovember 2010 13:00 14:00 Platform: z/OS 1

Transcript of XML usage in DB2

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 1/50

XML for z/OS COBOL

Developers

Troy Coleman

CA Technologies

Session Code: E11

Wednesday, 10 November 2010 13:00 – 14:00Platform: z/OS

1

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 2/50

 Agenda

• Basic XML terminology

• XML Benefits and use on z/OS

• Enterprise COBOL XML Support

• Native XML Parsing and Publishing in COBOL

• Native XML Parsing and Publishing in COBOL using DB29 for z/OS

• Future XML improvements with DB2 10

2

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 3/50

XML Terminology

• XML – Extensible Markup Language• Universal means of exchanging data

• Expose structure and content of documents

• Textual Data Format

• Tag Language<Name>

<FirstName> Troy </FirstName>

<LastName>Coleman</LastName>

</Name>

The eXtensible Markup Language (XML) is seen by some people as the universal format for sharing data between applications.

Any application running on any platform using any database or file system can process data as long as that application understands how to process XML.

The XML document is self describing. That is you know what eachelement and repeating group of elements are based on tags imbedded within the document. Each element has a begin-tag and end-tag. The structure or order of the datais inherent through the hierarchical order of tags.

3

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 4/50

XML Terminology

• Well Formed Document• Contains a single root element

• Start and End tags matching on all elements

• Proper Nesting

• Attribute values in quotes

Well Formed:

<Name><First>Troy</First><Last>Coleman</Last></Name>

Note well formed:<Name><First>Troy<Last>Coleman</First></Last></Name>

The XML document is considered well-formed when each element has a start-tag “<>” and a corresponding end-tag “</>”.

The last example is not well formed because the <First> tag overlaps with the <Last> ta g.

4

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 5/50

XML Terminology

• Document Validation

• DTD – Document Type Definition (oldest schema language)

• Schema XSD – XML Schema Definition

• Validation Support

• XML System Services in z/OS R10 supports optional XML Validation

• COBOL XML PARSE validation added in V4.2

• DB2 V9

• No DTD support• XML Schema registered in XML Schema Repository

• Must use explicit function DSN_XMLVALIDATE

The well formed XML language does not enforce the structure of the document. The DTD language was developed to validate and enforce the document structure.

The DTD was an important step toward the development of defininga family of documents that will be shared between producers and consumers of the data.

The XML Schema is a W3C-recommended language that improves upon the limited capabilitiesof DTD. The language supports governing the order of elements,Boolean predicates along with data types used to govern the content ofelements, and specialized rules to specify uniqueness and referential integrity (RI) constraints.

5

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 6/50

XML Terminology

• Parser – A program that can read an XML document andprovide programmatic access to the document.

• SAX – Simple API for XML

• XML “EVENTS” generate a Callback to your program for processing.

• DOM – Document Object Model

• The program can traverse the document which is represented in atree structure.

The parser processes the markup tags in the XLM document and passes structured information back to an application.

Some parsers process the XML document through the SAX specification and others process using the DOM specification.

6

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 7/50

XML Benefi ts and use on z/OS

• XML – Universal Format for Data

• Why z/OS?

• Why COBOL?

The next few slides will go int o details on the benefits of using XML along with the benefits of using XML specifically on z/OS with COBOL.

7

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 8/50

XML – Universal Format for Data

• Data is platform-independent

• B2B – Industry Standard Schema

• Self Documenting Data Structure

• Change content and structure does not require programchanges

•Benefits:

• With XML, applications can more easily read information from a variety of 

 platforms. The data is platform-independent, so now the sharing of data between

you and your customers can be simplified.

• B2B - Businesses are developing DTDs and schemas for their industry. The ability to parse

standardized XML documents gives business products an opportunity to be

exploited in the B2B environment.

• Self Doc Data: Applications understand how to read a schema which describes the data in the XML document.

•Changes to content and structure is easier in XML. The data is tagged so you can add and remove elements without impacting existing

elements. You will be able to change the data without having to change the application.

8

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 9/50

Why z/OS?

• No platform can compete with enterprise scale workloads• Reliability

• Scalability

• Security

• Availability

• Manage the world’s business data and transactions.• Finance Industry

• Transportation Industry

• Health Industry

• Government

9

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 10/50

Why process XML in COBOL?

• Exploit your existing assets/skills

• Keeps development controls in one place/style

• Modernize existing applications as web services

You have lots of experience mainframe developers who develop in COBOL.

Retain that asset and skills by having them bridge the new XML Web UI with the people and systems that have been running your system.

Using IBM’s SOA architecture you can modernize existing COBOL applications to web services.

10

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 11/50

Enterprise COBOL XML Support

• Enterprise COBOL for z/OS version 4.2 takes advantageof the robust z/OS XML System Services• Released August 2009

• http://www-01.ibm.com/software/awdtools/cobol/zos/

• Adds Validation against SCHEMA (no DTD support)

• Performance for non-validating parsing improved

• Java 5 and 6 interoperability

Each new release of Enterprise COBOL for z/OS continues to improve on XML features as well as performance processing XML.

The latest release V4.2 takes advantage of th e new z/OS XML System Services which provides the ability to valid a document through a Schema.

11

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 12/50

COBOL XML Parse Special Registers

• XMLPARSE(COMPAT) compiler option

• XML-CODE - XML event set code then pass control to procedure

• XML-EVENT – Set event name then pass control to procedure

• XML-NTEXT – Set with document fragment that are returned as national character data(UNICODE)

• XML-TEXT – Set with document fragment that are returned as alphanumeric data

• New COBOL V4: XMLPARSE(XMLSS) additional registers

• XML-NAMESPACE or XML-NNAMESPACE

• XML-NAMESPACE-PREFIX or XML-NNAMESPACE-

PREFIX

To take advantage of the new XML services you will need to use the compiler option XMLPARSE(XMLSS).

A few new special registers are included when you use this option. The XML Namespace and Namespace prefix along with national encoded Namespace and Namespace prefix.

12

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 13/50

COBOL XML PARSE Processing

XML PARSE xml-document

PROCESSING PROCEDURE xml-handler 

ON EXCEPTION

display ‘XML document error ‘ XML-CODE

NOT ON EXCEPTION

display ‘XML document successfully parsed’

END-XML

The XML PARSE statement takes a document string as input ‘xml-document’.

It sends this document to the parser and a fter each XML event callback is made t o your program to the xml-handler pa ragraph.A set of special registers are set of which one includes the XML structure which caused the event.

You program can then inspect and process the data before controlis handed back to the parser.

13

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 14/50

COBOL Parsing Events

<?xml version="1.0"?><msg type="short">Hello, World!</msg>

XML-EVENT XML-TEXT

START-OF-DOCUMENT

VERSION-INFORMATION 1.0

START-OF-ELEMENT msg

ATTRIBUTE-NAME type

ATTRIBUTE-CHARACTERS short

CONTENT-CHARACTERS Hello, World!

END-OF-ELEMENT msg

END-OF-DOCUMENT

This is a simple sample of what the parser will return for each event.

14

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 15/50

COBOL Parsing Events Catagories

• XML text nodes

• XML element nodes

• XML processing instructions

• XML comments

The previous example showed specific events. In events are triggered on either a text node, element node, processing instruction or comment.

15

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 16/50

Sample COBOL Programs

• First Program shows XMLPARSE

• Compiler Options & Identification Division

• Working Storage Variables

• Procedure Division – Parse Statement

• XML Handler Paragraph

• XML Output

• Second program for XMLGENERATE

In this example I ran into an error due to the bracket “[“ used with the CDATA parameter. The default codepage was causing an error.

I had to specify the compiler option codepage(1047) to correct the problem.

16

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 17/50

COBOL Compiler Options (codepage)

cbl codepage(1047)

I DENTI FI CATI ON DI VI SI ON.

PROGRAM- I D. XMLPARS1

DATA DI VI SI ON.

WORKI NG- STORAGE SECTI ON.

• Codepage(1047) needed to support bracket “[“<![CDATA[We should add a <picture> element in the future!]]>

In this example I ran into an error due to the bracket “[“ used with the CDATA parameter. The default codepage was causing an error.

I had to specify the compiler option codepage(1047) to correct the problem.

17

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 18/50

Inline Sample Document01 xml - document.

05 pi c x( 39) val ue ' <?xml versi on="1. 0" encodi ng="i bm- 1140”’ .

05 PI C X( 39) val ue ' ?><!- - Empl oyee HR I nf ormat i on - - >' .

05 PI C X(39) val ue ' <emp><empno>123456</ empno><name> ' .

05 pi c x(39) val ue ' <f i r st nme>Troy </ f i r st nme> ' .

05 pi c x( 39) val ue ' <l ast name>Col eman </ l ast name> ' .

05 pi c x( 39) val ue ' </ name><workdept>D01</ workdept> ' .

05 pi c x( 39) val ue ' <phoneno>555-123- 4459</ phoneno> ' .

05 pi c x(39) val ue ' <hi r edate>03/ 30/ 2001</ hi r edate> ' .

05 pi c x( 39) val ue ' <j ob>MANAGER </ j ob> ' .

05 pi c x( 39) val ue ' <edl evel >16</ edl evel ><sex>M</ sex> ' .

05 pi c x(39) val ue ' <bi r t hdate>01/13/1963</ bi r t hdate> ' .

05 pi c x(39) val ue ' <sal ary>000078250. 00</ sal ary> ' .

05 pi c x( 39) val ue ' <bonus>000000500. 00</ bonus> ' .05 pi c x( 39) val ue ' <comm>000000000. 00</ comm> ' .

05 pi c x( 39) val ue ' <! [ CDATA[ We shoul d add a <pi ct ure> el e' .

05 pi c x(39) val ue ' ment i n t he f ut ure!] ]> ' .

05 pi c x( 39) val ue ' <?Si gn Cont r act? ></ emp> ' .

To jump start working with XML I didn’t want to take the time to read a file or interface with CICS or IMS.

I decided to create an in working storage document to practice with.

This is a sample of an XML document I used to learn about the different events and parsing features.

18

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 19/50

Sample Program Working Variables

01 xml - t ext - l en comput at i onal pi c 999999999.

01 cur r ent - el ement pi c x(30) .

01 ws- compensat i on comp pi c s9(9) v99 val ue zero.

01 ws- di s- sal ary pi c $$$, $$$, $$9. 99.

01 ws- sal ary comp pi c s9( 9) v99 val ue zero.

01 ws- bonus comp pi c s9(9) v99 val ue zero.

01 ws- comm comp pi c s9(9) v99 val ue zero.

01 ws- empno pi c x( 06) val ue spaces.

77 WS- START- OF- DOC pi c X.

88 WS- START- DOC VALUE ' N' .

88 WS- NOT- START- DOC VALUE ' Y' .

This is a simple set of working storage variables used t o process the XML document and do some computations for the fina l output.

19

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 20/50

Procedure Division – parse programProcedure Division.

mainline section.

DISPLAY 'XML Event XML Text'.

SET WS-NOT-START-DOC TO TRUE.

XML PARSE xml-document PROCESSING PROCEDURE xml-handler

ON EXCEPTION

display 'XML document error ' XML-CODE

NOT ON EXCEPTION

display 'XML document successfully parsed'

END-XML.DISPLAY ' '.

DISPLAY '-----+++++***** Using information from XML '*****+++++-----'.

DISPLAY ' '.

DISPLAY ' Employee Number: ' ws-empno.

COMPUTE ws-compensation = ws-salary + ws-bonus + ws-comm.

move ws-compensation to ws-dis-salary.

DISPLAY ' Total Compensation: ' ws-dis-salary.

GOBACK.

To keep things simple I’m setting the start of processing at the beginning of the program.

I’m then invoking the XML PARSE statement passing the working storage xml-document.

When the xml event is t riggered control will be passed back to paragraph xml-handler.

Once XML processing is complete the rest of mainline will be processed.

20

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 21/50

XML-Handlerxml-handler section.

if ws-start-doc then

compute xml-text-len = function length(xml-text)

* display 'doc length:' xml-text-len

end-if.

evaluate XML-EVENT

* ==> Order XML events most frequent first

when 'START-OF-ELEMENT'

display 'Start element tag: <' XML-TEXT '>'

move XML-TEXT to current-element

when 'CONTENT-CHARACTERS'

display 'Content characters: <' XML-TEXT '>'

* ==> Transform XML content to operational COBOL dataitem...

evaluate current-element

when 'salary'

xml-handler will look for the event type then perform some task.

In my example at start of document processing I compute the length of the entire document just as a debugging aid.

 Now I display all the different XML Events along with the data that triggered the event. Another learning aid.

I also want to do some processing with some element so I’m looking for the element name “salary”, “bonus” and “Comm”.

Continue on the next slide

21

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 22/50

XML-Handler* ==> Using function NUMVAL-C...

compute ws-salary = function numval-c(XML-TEXT)

when 'bonus'

compute ws-bonus = function numval-c(XML-TEXT)

when 'comm'

compute ws-comm = function numval-c(XML-TEXT)

when 'empno'

move xml-text to ws-empno

end-evaluate

when 'END-OF-ELEMENT'

display 'End element tag: <' XML-TEXT '>'

move spaces to current-element

when 'START-OF-DOCUMENT'display 'Start of document processing'

SET WS-START-DOC TO TRUE

when 'END-OF-DOCUMENT'

display 'End of document.'

This is a continuation of the previous slide.

22

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 23/50

XML-Handlerwhen 'VERSION-INFORMATION'

display 'Version: <' XML-TEXT '>'

when 'ENCODING-DECLARATION'

display 'Encoding: <' XML-TEXT '>'when 'STANDALONE-DECLARATION'

display 'Standalone: <' XML-TEXT '>'

when 'ATTRIBUTE-NAME'

display 'Attribute name: <' XML-TEXT '>'

when 'ATTRIBUTE-CHARACTERS'

display 'Attribute value characters: <' XML-TEXT '>'

when 'ATTRIBUTE-CHARACTER'

display 'Attribute value character: <' XML-TEXT '>'

when 'START-OF-CDATA-SECTION'

display 'Start of CData: <' XML-TEXT '>'

when 'END-OF-CDATA-SECTION'

display 'End of CData: <' XML-TEXT '>'

when 'CONTENT-CHARACTER'

display 'Content character: <' XML-TEXT '>'

Continue showing the different event types and associated data

23

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 24/50

XML-Handlerwhen 'PROCESSING-INSTRUCTION-TARGET'

display 'PI target: <' XML-TEXT '>'

when 'PROCESSING-INSTRUCTION-DATA'

display 'PI data: <' XML-TEXT '>'

when 'COMMENT'

display 'Comment: <' XML-TEXT '>'

when 'EXCEPTION'

display 'Exception' XML-CODE

display 'text:' xml-text

display 'len:' xml-text-len

when 'other'

display 'Unexpected XML event:' XML-EVENT '.'end-evaluate .

End program XMLPARS1.

Continue showing the different event types and associated data

24

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 25/50

Parsed OutputXML Event XML Text

Start of document processing

Version: <1.0>

Encoding: <ibm-1140>

Comment: < Employee HR Information >

Start element tag: <emp>

Start element tag: <empno>

Content characters: <123456>

End element tag: <empno>

Start element tag: <name>

Content characters: < >

Start element tag: <firstnme>

Content characters: <Troy >

End element tag: <firstnme>

Page 1

*** Cut a bunch of lines from this sample output ****

Start of CData: <>

Content characters: <We should add a <picture> elementin the future!>

End of CData: <>

Content characters: < >

PI target: <Sign>

PI data: <Contract>

End element tag: <emp>

End of document.

XML document successfully parsed

-----+++++***** Using information from XML *****+++++---

Employee Number: 123456

Total Compensation: $78,750.00

Page 2

This is page 1 and 2 of the sample output generated by parsing the working storage xml-document

25

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 26/50

Publish XML (Generate)

XML GENERATE xml-document

FROM cobol-data-item

ON EXCEPTION

display ‘XML document error ‘ XML-CODE

NOT ON EXCEPTION

display ‘XML document successfully published’

END-XML

The XML GENERATE command is used to build an XML document.

The input or source data from the cobol-data-item will be formatt ed into an XML document and stored in xml-document.

26

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 27/50

COBOL-Data-Item01 EMP.

10 EMPNO PI C X( 6) val ue ‘ 123456’ .

10 NAME.

15 FI RSTNME pi c x( 12) val ue ‘ Tr oy ‘ .

15 LASTNAME pi c x( 15) val ue ‘ Col eman ‘ .

10 WORKDEPT PI C X( 3) val ue ‘ D01’ .

10 PHONENO PI C X( 4) val ue ‘ 4459’ .

10 HI REDATE PI C X(10) val ue ‘ 03/ 30/ 2001’ .

10 J OB PI C X( 8) val ue ‘ MANAGER ‘ .

10 EDLEVEL PI C S9(4) USAGE COMP val ue 16.

10 SEX PI C X( 1) val ue ‘ M’ .

10 BI RTHDATE PI C X( 10) val ue ‘ 01/ 13/ 1963’ .10 SALARY PI C 9(7)V9(2) USAGE di spl ay val ue 78250. 00.

10 BONUS PI C 9(7) V9(2) USAGE di spl ay val ue 500. 00.

10 COMM PI C 9( 7) V9( 2) USAGE di spl ay val ue zer o.

In my sample program I’m using a static working storage area for the source of

input. You of course will read this in from CICS, IMS, DB2, or files.

Remember that the XML tag names are taken from the COBOL variable

names.

Is this case the document will be “EMP” and will have a tag for EMPNO and NAME and ….. COMM

27

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 28/50

Sample COBOL code to publ ish XML

MOVE SPACES TO XML- DOC.

XML GENERATE xml - doc

FROM EMP

WI TH XML- decl ar at i on

ON EXCEPTI ON

di spl ay ‘ XML document err or ‘ XML- CODE

NOT ON EXCEPTI ON

di spl ay ‘ XML document successf ul l y generated’

END- XML

The procedure division sample code.

First clear out the xml document before calling XML GENERATE.

 Now generate an XML document xml-doc from the COBOL field EMP.

The with “XML-DECLARATION” tells the generator to add the XML version

at the beginning of the doc.

28

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 29/50

XML document generated from EMP data item<?xml versi on=”1. 0” encodi ng=”I BM- 1047”?>

<EMP>

<EMPNO>123456</ EMPNO>

<NAME>

<FI RSTNME>Tr oy</ FI RSTNME>

<LASTNAME>Col eman</ LASTNAME>

</ NAME>

<WORKDEPT>D01</ WORKDEPT>

<PHONENO>4459</ PHONENO>

<HI REDATE>03/ 30/ 2001</ HI REDATE>

<J OB>MANAGER</ J OB>

<EDLEVEL>16</ EDLEVEL>

<SEX>M</ SEX>

<BI RTHDATE>01/ 13/ 1963</ BI RTHDATE><SALARY>78250. 00</ SALARY>

<BONUS>500. 00</ BONUS>

<COMM>. 00</ COMM>

</ EMP>

As you can see the first line of text has the XML DECLARATION then the

EMP document.

The names are taken from the COBOL variable names.

29

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 30/50

DB2 Scalar XML Publishing Functions

• XMLDOCUMENT (XML Expression)

The XMLDOCUMENT function returns an XML value with asingle document node and zero or more nodes as itschildren.

REMEMBER: the first thing you should have in the SQL SELECT is the

function XMLDOCUMENT followed by XML expressions. This will

ensure you have returned a well formed XML document.

 Now I would like to talk about publishing XML Documents from DB2.

The first XML function needed as you are building the document is

XMLDOCUMENT.

Otherwise you will not have a well formed document and insert into an XML

column will fail.

30

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 31/50

DB2 Scalar XML Publishing Functions

• XMLELEMENT(NAME element name, xmlnamespace declaration, xmlattributes-function, element content expression

OPTION empty on null | null on null )

• Can be one or more expressions• <cust omer xml ns: i nf o="ht t p: / / t r oyl col eman. com” ci d="1">

<i nf o: name>Tr oy Col eman</ i nf o: name></ cust omer >

The XMLELEMENT function is used to build the element which includes it’s

name, optional namespace, attribute, and content.

In this example I’ve built the element “CUSTOMER” which is made up of

namespace, attribute “CID” and element “NAME”

31

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 32/50

DB2 Scalar XML Publishing Functions

• XMLNAMESPACES( namespace uri as namespace prefix)

This function returns a namespace declaration whichprovides a unique context to the element.

XMLNAMESPACES('http://troylcoleman.com' as "info")

Note: The name is case sensitive. If you define the name inuppercase “INFO” but then reference in lowercase “info”,

you will receive SQL error -20275 reason code 2

I used the XMLNAMESPACE function to provide a unique prefix on the

element names.

32

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 33/50

DB2 Scalar XML Publishing Functions

• XMLATTRIBUTES (attribute value expression AS attribute-name)

The attribute function provides additional informationabout the element. In the DB2 world this is usually anidentifying key value.

In this example the primary key customerid will bereturned as CID.

XMLATTRIBUTES(c.customerid as “CID”)

The XMLATTRIBUTES function is used when you need to provide what I

think of as the PRIMARY KEY for the element.

This is the identifying key value used for searching.

33

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 34/50

DB2 Scalar XML Publishing Functions

• XMLCOMMENT (string expression)

• You may find at times you have a need to put commentsinto the XML document. The comment function returns asingle comment node from a string expression.

XMLCOMMENT('CID is the Customer ID')

<! - - CI D i s t he Cust omer I D- - >

You may find that you need to add a comment in the XML document. The

XMLCOMMENT function will insert that comment for you.

34

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 35/50

More DB2 Scalar XML Publishing Funct ions

• XMLCONCAT

• XMLFOREST

• XMPARSE

• XMLPI

• XMLQUERY

• XMLSERIALIZE

• XMLTEXT

There are many more XML publish functions that can be used for more

complex xml documents.

This is a list of these functions.

35

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 36/50

Publishing XML from DB2 Relational DataSELECT BI GI NT( C. CUSTOMERI D)

, XMLDOCUMENT(

XMLELEMENT( NAME "c ust omer "

, XMLNAMESPACES(' htt p:/ / t r oyl col eman.com'

as "i nfo")

, XMLATTRI BUTES(C. CUSTOMERI D AS "ci d")

, XMLCOMMENT(' CI D i s t he Customer I D' )

, XMLELEMENT( NAME "i nf o: name"

, XMLELEMENT( NAME "i nfo: f i r st name", C.FI RSTNAME)

, XMLELEMENT( NAME "i nf o: l ast name", C. LASTNAME)

)

, XMLELEMENT( NAME "i nf o: addr"

, XMLATTRI BUTES( ' USA' AS "countr y")

, XMLELEMENT( NAME "i nf o: st r eet" , C. STREET_ADDRESS)

, XMLELEMENT(NAME " i nfo: ci ty" , C.CI TY)

, XMLELEMENT( NAME " i nfo: st ate" , C.STATE)

, XMLELEMENT(NAME "i nfo: zi p", C. ZIP) )

, XMLELEMENT( NAME "i nf o: agegroupi d", C. AGEGROUPI D)

, XMLELEMENT( NAME "i nf o: i ncome_r angei d", C. I NCOME_RANGEI D)

, XMLELEMENT(NAME " i nfo: mari t al _st atus" , C.MARI TAL_STATUS)

, XMLELEMENT( NAME "i nf o: gender ", C. GENDER)

, XMLELEMENT(NAME "i nfo: ethni cgroupi d", C.ETHNI CGROUPI D)

)

)

FROM CUSTOMERS C

This is a sample SQL statement used to publish relational data into an XML

Document.

 Notice I start with the XMLDOCUMENT function followed by an

XMLELEMENT function that is made up of a list of other XMLELEMENT

functions.

36

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 37/50

XML Document f rom DB2 Relational Data<?xml versi on="1. 0" encodi ng=" I BM1047"?>

<cust omer xml ns:i nfo="ht t p:/ / t r oyl col eman. com" ci d="1">

<! - - CI D i s t he Cust omer I D- - >

<i nf o: name>

<FI RSTNAME>J OHN</ FI RSTNAME>

<LASTNAME>DOE</ LASTNAME>

</ i nfo: name>

<i nf o: addr ><STREET_ ADDRESS>4356 SARATOGA AVE</ STREET_ ADDRESS>

<CI TY>SAN J OSE</ CI TY>

<STATE>CALI FORNI A</ STATE>

<ZI P>94534-0567</ ZI P>

</ i nf o: addr>

<AGEGROUPI D>1</ AGEGROUPI D>

<I NCOME_RANGEI D>1</ I NCOME_RANGEI D><MARI TAL_STATUS>U</ MARI TAL_STATUS>

<GENDER>M</ GENDER>

<ETHNI CGROUPI D>1</ ETHNI CGROUPI D>

</ cust omer>

This is the result of the previous SQL statement using the XML publishing

functions.

37

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 38/50

Convert XML Document into Relational Table

• XMLTABLE(xmlnamespace-declaration,row-xquery-expression-constant

PASSING row-xquery-argumentCOLUMNS xml-table-regular-column-definition )

You may find that you have some XML data and would like to publish this

data as a relational table to be used in join processing with other tables.

The XMLTABLE function will do this for you.

In my example on the next slide I did not use XMLNAMESPACE.

I did use $CUST as the row-xquery-expression-constant which is used by the

 passing row-xquery-argument.

The COLUMNS function looks like what you would see in a CREATE table

statement for each column being defined.

38

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 39/50

XMLTABLE Statementsel ect cx. cust _i d, cx. f i rst name, cx. l ast name, cx. st reet

, cx. ci t y, cx. state, cx. count ry

f r om dsn8910. customer c

, xml t abl e( ' $cust / cust omer' passi ng i nf o as "cust "

col umns

cust _i d i nt eger pat h ' @ci d'

, f i r st name var char ( 10) path ' name/ f i r st name'

, l astname var char( 10) path ' name/ l astname'

, st r eet varchar ( 20) pat h ' addr / st r eet '

, ci t y varchar ( 11) pat h ' addr / ci t y'

, st at e varchar ( 10) pat h ' addr / st at e', count r y var char ( 03) pat h ' addr / @count r y'

) as cx

order by cx. l ast name asc

In this SQL statement I’m taking the XML column “INFO” found in the

customer table and parsing it with the XMLTABLE function to return what

looks like a table. The table returned is CX. Notice I’m doing an order by on

CX.LASTNAME.

39

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 40/50

Insert sample XML DocumentINSERT INTO COLTR05.CUSTOMER

SELECT BIGINT(1000)

, XMLDOCUMENT(

XMLELEMENT(NAME "customer"

,XMLNAMESPACES('http://troylcoleman.com' as "info")

,XMLATTRIBUTES(1000 AS "cid")

,XMLCOMMENT('CID is the Customer ID')

,XMLELEMENT(NAME "name"

,XMLELEMENT(NAME "firstname" ,'TROY')

,XMLELEMENT(NAME "lastname" ,'COLEMAN')

)

,XMLELEMENT(NAME "addr"

,XMLATTRIBUTES('USA' AS "country")

,XMLELEMENT(NAME "street" ,'123 Main Street')

,XMLELEMENT(NAME "city" , 'Palatine')

,XMLELEMENT(NAME "state" ,'IL'),XMLELEMENT(NAME "zip" ,'60067')

)

)

)

FROM SYSIBM.SYSDUMMY1

When I’m testing my SQL I usually prototype the statement and verify that it

is a valid XML document.

To do this I publish an XML document using the SELECT publishing

functions and once it looks good I prefix the SELECT statement with an

INSERT statement.

In this case I have two columns. CID and INFO. The CID is set to 1000 and

INFO will contain the published document.

40

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 41/50

XMLTABLE results in relational dataCUST_I D FI RSTNAME LASTNAME STREET CI TY STATE

- - - - - +- - - - - - - - - +- - - - - - - - - +- - - - - - - - - +- - - - - - - - - +- - - - - - - - - +- - - - - - - - - +- - - - - - - - - +

1000 TROY COLEMAN 123 Mai n St r eet Pal at i ne I L

• The result of XMLTABLE is a relational table that can be joined and processed just like any other DB2 table.

Using the XMLTABLE function against the row I just inserted into INFO

would product the following relational table which can be joined with other

tables.

41

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 42/50

Future XML Improvements wi th DB2 10

• pureXML Enhancements• Schema Validation in Engine

• Update of Subparts of XML Document

• Multi-Versioning

• Binary XML Exchange Format

• XML Data Type in Routines

• XML Index matching with Date/Timestamp

• XML CHECK Utility

Many new enhancements have been delivered in DB2 10 pureXML to improve

 performance and functionality.

A few key enhancements are Schema validation, Multi-Versioning, and

Update of subparts of XML document

42

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 43/50

DB2 10 – Schema Validation in Engine

INSERT INTO customer CID, INFO

VALUES (1000, XMLVALIDATE(:xml-document,‘SYSXSR.CUSTSCHEMA’));

Warning: Avoid high overhead of XML validation inproduction. Use INSERT trigger in development to verifyand make sure the XML documents are valid.

DB2 now supports validation of the XML document in the engine. In this case

the CUSTSCHEMA was registered before processing this statement.

XML Validation is very CPU intensive. You will want to avoid this in a

 production environment.

43

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 44/50

DB2 10 – Update of Subparts of XML Document

UPDATE MYCUSTOMER

SET INFO = XMLMODIFY(

'declare default element namespace "http://posample.org";

replace value of node /customerinfo/addr/street

with "42 Rosedale"')

WHERE CID=1000#

It’s been a long time requirement to have the ability to update a portion of the

XML document and not take the performance hit with updating the entire

document.

The XMLMODIFY provides the ability to update a portion of the XML

Document.

44

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 45/50

DB2 10 – Multi-Versioning

• XML Column supporting multiple versions• Universal Table Space

• DB2 10 NFM

• INSERT – Version number assigned

• UPDATE – Improved Concurrency and Memory Usage• Entire document – new version

• Partial document – new version

Multiple versions of an XML document can coexist in an XML table. The

existence

of multiple versions of an XML document can lead to improved concurrency

through lock avoidance. In addition, multiple versions can save real storage by

avoiding a copy of the old values in the document into memory in some cases.

45

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 46/50

DB2 10 – Binary XML Exchange Format

• JDBC or SQLJ Application• JDBC 4.0 using java.sql.SQLXML data type

• ODBC using SQL_C_BINARYXML data type

• LOAD & UNLOAD utility using field specifications• CHAR BLOBF template-name BINARYXML

• VARCHAR BLOBF template-name BINARYXML

• XML BINARYXML

The Binary data type is not supported at this point in time by COBOL.

The current API format is for JDBC, SQLJ, ODBC, or UNLOAD utility

46

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 47/50

DB2 10 – XML Data Type in Routines

• Native XML Data Type for Parameters• Native SQL Procedures

• SQL Scalar Functions

• SQL Table Functions

• Native XML Data Type for Variables• Native SQL Procedures

• SQL Scalar Functions

 Native SQL routines support parameters and variables with the XML data

type.

XML parameters can be used in SQL statements in the same way as variables

of any other data type.

In addition, variables with the XML data type can be passed as parameters toXQuery expressions in XMLEXISTS, XMLQUERY and XMLTABLE

expressions.

47

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 48/50

DB2 10 – XML Index matching Date/Timestamp

• Data, Time, and Timestamp data type support• xs:dateTime

• xs:time

• xs:date

• xs:duration

• xs:yearMonthDuration

• xs:datTimeDuration

Version 10 includes date and time support for XML data types and functions.

This includes matching index support using comparison operators on duration,

date, and time values

48

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 49/50

DB2 10 – XML CHECK Utility

• Verify all nodes in document are structurally intact

• Node ID index is consistent with XML Table Space

• Schema Validation - XML Type Modifier • Validate all documents with at least one XML Schema with an

XML type modifier 

Version 10 adds functionality to the CHECK DATA utility, so that you can

use this utility to verify the consistency of XML documents that are stored in a

separate XML table space.

The CHECK DATA utility verifies that all nodes in that XML document are

structurally intact and that the node ID index is consistent with the content that

is

in the XML table space.

In addition, this utility verifies that all of the XML documents of an XML

column are valid against at least one XML schema that is

specified in the XML type modifier 

49

8/10/2019 XML usage in DB2

http://slidepdf.com/reader/full/xml-usage-in-db2 50/50

Troy ColemanCA [email protected]

E11XML for z/OS COBOL Developers