XML and COBOL

101
XML and COBOL

description

XML and COBOL. XML. XML = Extensible Markup Language Used to expose the structure and content of a document Becoming a universal means of exchanging data Tag language Charles Dickens . - PowerPoint PPT Presentation

Transcript of XML and COBOL

Page 1: XML and COBOL

XML and COBOL

Page 2: XML and COBOL

XML• XML = Extensible Markup Language• Used to expose the structure and content of a

document• Becoming a universal means of exchanging data• Tag language <author> <firstname>Charles</firstname> <lastname>Dickens</lastname> </author>

Page 3: XML and COBOL

XML

• Tags are user-defined• Every start tag has a matching stop tag <atag> …</atag>• Sometimes the tags are combined into

one start and stop tag <media type = “CD” /> • Tags can’t overlap NO: <a> <b> </a> </b>

Page 4: XML and COBOL

XML• Tags can be nested <a> <b> </b> </a>

• Documents are tree-structured <a> <b></b> <c> <d></d> </c> </a>

a

b c

d

Page 5: XML and COBOL

XML

• Text based documents• Case sensitive• Must contain a single root element• Start with an XML declaration and comments <?xml version =“1.0”?> <!– comment line - -> <a> </a>

Page 6: XML and COBOL

XML

• XML is “Well Formed” if 1) Single root element 2) Start and end tags matching for all

elements 3) Proper nesting 4) Attribute values in quotes

Page 7: XML and COBOL

XML Parsers• An XML parser is a program that can read an XML

document and provide programmatic access to the document

• Two types of parsers: 1) DOM based – Document Object Model Constructs a tree that represents the document 2) SAX based – Simple API for XML Generates events when parts of the document are encountered.• Can also be classified as “push” or “pull” parsers

Page 8: XML and COBOL

XML Characters

• Consist of carriage returns, line feeds and Unicode characters

• XML is either “markup” or “text”• Markup is enclosed in < and >• Character text is the text between a start

and end tag• Child elements are considered markup

Page 9: XML and COBOL

White Space

• Parsers consider whitespace inside text data to be significant and must pass it to an application

• An application can consider whitespace significant or insignificant.

• Normalization is the process in which whitespace is collapsed or removed

Page 10: XML and COBOL

Entities

• &, <, >, ‘ (apostrophe), and “(double quote) are special characters and may not be used in character data directly

• To use these characters we code entity references which begin with an ampersand and end with a semicolon

• &amp; &lt; &gt; &apos; &quot;

• <mytag>David&apos;s Tag</mytag>

Page 11: XML and COBOL

Unicode

• XML supports Unicode• Each Unicode character starts with an

ampersand, followed by a sharp (#), an integer, and a semicolon

• &#1583;

Page 12: XML and COBOL

Determining the Encoding Type

• Sources used to determine the encoding of an XML document when XMLPARSE(XMLSS) is in effect:– The type of data item that contains the XML

document. (We will only consider alphanumeric.)– The ENCODING phrase (if used) on the PARSE

statement– The CCSID specified in the CODEPAGE compiler

option

Page 13: XML and COBOL

Two ways to Specify the Encoding for XMLPARSE(XMLSS)

• Put the document in an alphanumeric item (PIC X)

1) Specify the encoding on the PARSE statement: PARSE MYDOC WITH ENCODING 1208 …

2) Add a CODEPAGE compiler option

Page 14: XML and COBOL

Markup• Most items have distinct begin and end tags: <name>David</name>

• Empty elements begin and end with one tag: <img src = “img.gif” />

• Tags can contain “attributes” as in the src attribute in the img tag above

• Attribute values must be quoted with single or double quotes

• Element and attribute names can be any length and may contain letters, digits, underscores, hyphens and periods. Must begin with letter or underscore.

Page 15: XML and COBOL

Comments

• Comments in XML have the same format as HTML

• Start with <!—• End with -->• <!– This is a comment -->

Page 16: XML and COBOL

Processing Instructions• Example: <?xml:stylesheet type=“text/xsl” href=“usage.xsl”?>

• Delimited by <? and ?>• Passed to the parser for additional information

about the document• Contain a “PI Target”: xml:stylesheet • Contain a “PI Value”: type=“text/xsl” href=“usage.xsl”

• Allow a document author to embed application specific info in the document

Page 17: XML and COBOL

CDATA Sections

• CDATA section can contain characters, reserved characters, and white space

• Not processed by the XML parser• Sometimes used for scripting code or

embedding XML inside a document• Begin with <![CDATA[• End with ]]>• < and > must be coded as entities

Page 18: XML and COBOL

CDATA Example

<![CDATA if (x &gt; y) { x = x + y; } ]]>

Page 19: XML and COBOL

XML Namespaces

• Different authors might create the same tag names with different meanings

• Namespaces provide a means for authors to prevent collisions on tag names

• <book:title>The Idiot</book:title>• <movie:title>Avatar</movie:title>

Page 20: XML and COBOL

XML Namespaces

• Each namespace is tied to a Uniform Resource Identifier (URI)

• Authors create their own namespace prefixes

• Namespaces are created in the root tag:<library xmlns:book = “urn:woolbright:bookinfo” xmlns:movie = “urn:woolbright:movieinfo”>

• URLs are sometimes used for URIs

Page 21: XML and COBOL

Default Namespaces

• To avoid coding a prefix on every tag, code a default namespace:

<library xmlns = “urn.woolbright:default” xmlns:book = “urn:woolbright:bookinfo” xmlns:movie = “urn:woolbright:movieinfo”>

Page 22: XML and COBOL

Enterprise COBOL• Contains two event-based parsers that allows you to

read XML documents and process them with COBOL• XML documents can be retrieved from an MQ message,

CICS TD queue, or IMS message processing queue• XML documents that are read from a file must be

brought into storage as a single item. (Records can be combined using STRING or other techniques)

• If XMLPARSE(XMLSS) is in effect, you can parse an XML file by passing one record at a time

Page 23: XML and COBOL

z/OS COBOL Features for Processing XML Input

• XML PARSE – begins parsing the document and identifies the processing procedure in your document

XML PARSE MYDOCUMENT PROCESSING PROCEDURE 100-PARSE ON EXCEPTION DISPLAY ‘XML DOCUMENT ERROR’ XML-CODE STOP RUN NOT ON EXCEPTION DISPLAY ‘PARSED DOCUMENT SUCCESSFULLY’END-XML

Page 24: XML and COBOL

z/OS COBOL Features for Processing XML Input

• Processing XML involves passing control between the parser and the processing procedure you write to handle events

• Processing Procedure – receives and processes the events that are generated by the parser. This is a paragraph or section in your Cobol program

Page 25: XML and COBOL

Parser/Procedure Interaction• Parser passes control to the procedure for each XML

event• Control returns to the parser at the end of the procedure• This continues until either: 1) the parser detects an error in the document and signals an EXCEPTION event, or

2) the parser signals END-OF-INPUT event and the processing procedure returns to the parser with XML-CODE still set at 0

3) you terminate parsing deliberately by setting XML-CODE to -1 before returning to the parser

Page 26: XML and COBOL

XML Parsers

• Compiler options control the parser type• CBL XMLPARSE(XMLSS) – chooses the

z/OS XML System Services Parser. This provides enhanced features – namespace processing, validation with respect to a schema

• CBL XMLPARSE(COMPAT) – chooses the parser built into the COBOL library

Page 27: XML and COBOL

Processing Procedure

• The parser reads the document and responds to events

• Gives control to the processing procedure when events occur

• The processing procedure responds to the event and turns control back to the parser for further parsing

Page 28: XML and COBOL

Parse Syntax

Here is the syntax for the XML PARSE statement

Page 29: XML and COBOL

XML PARSE

• XML PARSE begins parsing, identifies the source document, and the processing procedure

• Specify the ENCODING option to describe the document’s encoding

• Specify the VALIDATING option to identify an XML schema against which the document will be validated

Page 30: XML and COBOL

COBOL Features for Processing XML Input

• Special Registers– XML-CODE - to determine the status of XML

parsing. PIC S9(9) BINARY– XML-EVENT - to receive the name of the event.

PIC X(30) – XML-NTEXT – to receive XML document fragments

that are returned as national character data (Unicode). Variable-length alphanumeric item

– XML-TEXT – to receive XML document fragments returned as aphanumeric data. Variable-length alphanumeric item

Page 31: XML and COBOL

COBOL Features for Processing XML Input

• Special Registers– XML-NAMESPACE - to receive a namespace

identifier event, or for an element name or attribute name that is in the namespace. Variable-length alphanumeric item

– XML-NNAMESPACE – national namespace. Variable-length alphanumeric item

– XML-NAMESPACE-PREFIX – to receive a namespace prefix. Variable-length alphanumeric item

– XML-NNAMESPACE-PREFIX – to receive a national namespace prefix. Variable-length alphanumeric item

Page 32: XML and COBOL

Prior to Parsing

• In order to process an XML document, the entire document must be in memory

• Common sources of XML:– WebSphere MQ message– CICS Transient Queue– CICS Communications area– IMS message processing queue– Reading a file of records

Page 33: XML and COBOL

Reading XML Off A File• The entire XML file must be placed in a COBOL

data item• You will need:

– A FILE-CONTROL entry to define the file– An OPEN statement to open the file– READ statements to read all the records into a data

item in WORKING-STORAGE– Optionally, a STRING command to string all the

separate records together into one continuous stream, removing extraneous blanks, and to handle variable length records

Page 34: XML and COBOL

Parsing with XMLPARSE(XMLSS) XML PARSE document PROCESSING PROCEDURE event-handler-name ON EXCEPTION … NOT ON EXCEPTION … END-XML• Parsing continues until 1) an END-DOCUMENT event occurs

2) the parser signals EXCEPTION and the procedure returns to the parser with the XML-CODE register still set to 0 which indicates that no further XML data will be provided to the parser

3) you terminate processing by moving -1 to XML-CODE

Page 35: XML and COBOL

Parsing

• If XMLPARSE(XMLSS) is in effect, you can also use any of these optional phrases of the XML PARSE statement:– ENCODING, to specify the CCSID of the document– RETURNING NATIONAL to cause the parser to

automatically convert UTF-8 or single byte characters to national characters for return to the processing procedure

– VALIDATING, to cause the parser to validate the document against an XML schema

Page 36: XML and COBOL

Events• For each event that occurs during parsing, the parser sets the

associated event name in the XML-EVENT register and passes this to the processing procedure.

• Depending on the event, other registers can also be set• Typically, XML-TEXT is set with the data that caused the event• Some typical events: START-OF-DOCUMENT START-OF-ELEMENT ATTRIBUTE-NAME END-OF-ELEMENT CONTENT-CHARACTERS START-OF-CDATA-SECTION END-OF-DOCUMENT

Page 37: XML and COBOL

Processing Flow

Page 38: XML and COBOL

Parsing• The parser checks XML documents for most

aspects of well formedness. • Documents can be parsed with or without

validation• Validation insures that the document adheres to

the content and structure described in the schema.

• Validation can insure that there are no unexpected elements, no required elements are missing, and that element and attribute values are legal

Page 40: XML and COBOL

Transforming XML Text to Cobol Data Items

• For alphanumeric items decide if the XML data should be at the left or right end. For right justification define a field as JUSTIFIED RIGHT

• You might be able to move a numeric field that is decorated by moving it to a numeric-edited Cobol field. Then move it (de-edit) to a numeric field.

• Use intrinsic function NUMVAL to extract and decode simple numeric values

• Use intrinsic function NUMVAL-C to extract and decode XML data that represents monetary values

Page 41: XML and COBOL

Exercise #1• Use the file BCST.SICCC01.PDSLIB(XMLDATA2)• The file structure is similar to the one below:

<?xml version=”1.0” encoding=”ibm-1140” standalone=”yes”?><batch> <trans> <name>Joe Smith</name> <amt>12.32</amt> <amt>5.42</amt> </trans> <trans <name>Tina Louise</name> <amt>8.99</amt> </trans> …</batch

Page 42: XML and COBOL

Exercise #1

• Write an XML Cobol program that reads the file and copies it to memory.

• Print out a report that lists each customer name and a total for each customer.

• Print a grand total for the entire file Name AmountJoe Smith 17.74Tina Louise 8.99Grand Total 26.73

Page 43: XML and COBOL

Parsing XML in Segments

• Read a segment (record) from the file• Pass the record to the parser using XML PARSE

to start the parser• Control flows between the parser and the

processing procedure until the end of the record• At record end, the parser returns control to the

processing procedure after setting XML-EVENT to END-OF-INPUT and setting XML-CODE to 0.

Page 44: XML and COBOL

Parsing XML in Segments• If the processing procedure reads the next

record successfully, it sets XML-CODE to 1 to signal more input, and returns to the parser to continue parsing.

• This process continues until EOF when the processing procedure returns to the parser after leaving XML-CODE set to 0.

• CSU.PUBLIC.XML(XMLSEG) is a good example program to use

Page 45: XML and COBOL

Exercise #2• Convert(XMLDATA2)• The file structure is similar to the one below:

<?xml version=”1.0” encoding=”ibm-1140” standalone=”yes”?><batch> <trans> <name>Joe Smith</name> <amt>12.32</amt> <amt>5.42</amt> </trans> <trans <name>Tina Louise</name> <amt>8.99</amt> </trans> …</batch

Page 46: XML and COBOL

Exercise #2• For this exercise, rework the code you

wrote in Exercise #1

• Use the same input file BCST.SICCC01.PDSLIB(XMLDATA2)

• Instead of reading all the XML into a single area of memory, read and process the XML file one record at a time

Page 47: XML and COBOL

Exception Processing

• Document errors cause the parser to set an exception code in XML-CODE and to signal an XML exception event.

• The exception event can be handled by the ON EXCEPTION or NOT ON EXCEPTION clause of the PARSE statement

Page 48: XML and COBOL

Exception Processing

• XML-CODE contains a four-byte field that is the concatenation of two two-byte fields:

Return Code

2 Bytes

Reason Code

2 Bytes

XML-CODE

2 Bytes

Page 49: XML and COBOL

Exception Processing

• Cobol definition of the return code and reason code fields:

1 XML-DECODE. 2 RTN COMP PIC 9(2). 2 RSN COMP-5 PIC 9(4). • The two values combine to describe the

error. Consult the IBM XML documentation for codes:

http://publib.boulder.ibm.com/cgi-bin/bookmgr/BOOKS/gxlza120/CCONTENTS

Page 50: XML and COBOL

Printing the XML-CODE1 XML-DECODE. 2 RTN COMP PIC 9(2). 2 RSN COMP-5 PIC 9(4).

1 HV PIC X(16) VALUE '0123456789ABCDEF'.

DISPLAY ' RC=' RTN ',REASON=X ''' HV(FUNCTION MOD(RSN / 4096 16) + 1:1)

HV(FUNCTION MOD(RSN / 256 16) + 1:1) HV(FUNCTION MOD(RSN / 16 16) + 1:1) HV(FUNCTION MOD(RSN / 1 16) + 1:1) ''''

Page 51: XML and COBOL

Exception Processing

• Parsing does not continue after an exception is raised (for XMLPARSE(XMLSS))

• When processing returns to the parser, control is passed to the statement in the ON EXCEPTION phrase

• If no exceptions occur during parsing, control is passed to the statement in the NOT ON EXCEPTION phrase (if it exists) or to the end of the XML PARSE statement

Page 52: XML and COBOL

Exercise #3

• Use program XMLONEXC unchanged to read file XMLDATA4 which is an XML file with “flaws”

• Repair the flaws in the XML by looking up the return and reason codes

Page 53: XML and COBOL

Generating XML• XML Generate statement is used to produce

XML output• In the XML Generate statement, you identify the

source and output data items• Optionally, you can identify:

– A field to receive a count of XML characters that are generated

– A code page in which the generated XML document is to be encoded

– A namespace for the document– A namespace prefix– A statement to receive control if an exception occurs

Page 54: XML and COBOL

XML GENERATE

• Use XML-CODE to determine the status of XML generation

• COBOL programs that contain XML GENERATE must be link-edited with AMODE 31

Page 55: XML and COBOL

ExampleXML GENERATE XML XML-OUTPUT FROM SOURCE-REC COUNT IN XML-CHAR-COUNT ON EXCEPTION DISPLAY ‘XML GENERATION ERROR ‘ XML-CODE STOP RUN NOT ON EXCEPTION DISPLAY ‘SUCCESS GENERATING DOCUMENT’END-XML

Page 56: XML and COBOL

Example• XML-OUTPUT must be defined to be large

enough (usually 5 to 10 times as large as the source)

• Define the receiving field as alphanumeric or national group or elementary item

• Some Cobol items are not transformed – Items that specifiy REDEFINES or are subordinated

to a REDEFINES– Items that specify a RENAMES

Page 57: XML and COBOL

Source Items

• Elementary FILLER or unnamed items are not generated

• Slack bytes inserted for SYNCHRONIZED data items are not generated

• No extra white space is inserted to make the generated XML more readable

Page 58: XML and COBOL

Generate Example01 doc pic x(512).01 docSize pic 9(9) binary.01 G.

05 A pic x(3) value “aaa”.05 B.

10 C pic x(3) value “ccc”.

10 D pic x(3) value “ddd”.05 E pic x(3) value “eee”

…XML GENERATE doc from G

Generated XML(displayed after inserting

spacing)<G> <A>aaa</A> <B> <C>ccc</C> <D>ddd</D> </B> <E>eee</E></G>

Page 59: XML and COBOL

Generate With Attributes01 doc pic x(512).01 docSize pic 9(9) binary.01 G.

05 A pic x(3) value “aaa”.05 B.10 C pic x(3) value “ccc”.10 D pic x(3) value “ddd”.05 E pic x(3) value “eee”

…XML GENERATE doc from G

with attributes

Generated XML(displayed after inserting

spacing)<G A=“aaa” E=“eee”> <B C=“ccc”

D=“ddd”>

</B></G>

Page 60: XML and COBOL

Generate With Declaration01 Greeting.

05 msg pic x(80) value ‘Hello’.…

XML GENERATE Doc from Greetingwith Encoding 1208with XML-declaration

End-XML

Generates:<? Xml version=“1.0” encoding=“UTF-8”?> <Greeting> <msg>Hello</msg> </Greeting>

Page 61: XML and COBOL

Generate With Namespace01 Greeting.

05 msg pic x(80) value ‘Hello’.01 NS pic x(20) value

‘http://example”…

XML GENERATE Doc from Greeting namespace is NS

End-XML

Generates: <pre:Greeting xmlns:pre=“http://example”> <pre:msg>Hello</pre:msg> </pre:Greeting>

Page 62: XML and COBOL

Generate With Exception01 Greeting.

05 msg pic x(80) value ‘Hello’.…

XML GENERATE Doc from Greeting ON EXCEPTION DISPLAY “ERROR CODE: “ XML-CODE NOT ON EXCEPTION DISPLAY “SUCCESSFUL PARSE”

END-XML

Page 63: XML and COBOL

Handling XML Generate Exceptions

• If you code ON EXCEPTION, control is passed to the following sentence . XML-CODE contains the error code

• If you do not code ON EXCEPTION control is passed to the end of the XML GENERATE statement

• If you coded a COUNT IN field, that field contains the count of characters that were generated before an error occurred. Use reference modification to reference the data

XML-OUTPUT(1:XML-CHAR-COUNT)

Page 64: XML and COBOL

Exercise 4

• Read file CSU.PUBLIC.DATA(XMLDAT1)• Generate XML that represents a batch of

transactions

Page 65: XML and COBOL

Exercise 5• Read file CSU.PUBLIC.DATA(XMLDAT2)• The data looks like this:<PHONEBOOK> <PERSON> <NAME>JOE SMITH</NAME> <AGE>30</AGE> <PHONE>7065653524</PHONE> </PERSON> … <PERSON> <NAME>FRED FUNK</NAME> <AGE>49</AGE> <PHONE>7062237448</PHONE> </PERSON> </PHONEBOOK>

Page 66: XML and COBOL

Exercise 5

• I want the output to look like this:<?xml version="1.0" encoding="IBM1047"?> <PHONEBOOK> <PHONEREC> <NAME>JOE SMITH</Name> <PHONE>7063231234</PHONE> </PHONEREC> … <PHONEREC> <NAME>JILL IRELAND</NAME> <PHONE>7065697878</PHONE></PHONEREC> </PHONEREC> </PHONEBOOK>

Page 67: XML and COBOL

Exercise 5

• Use XMLHELLO and XMLSAMP1 and XMLONEXC (if needed) to guide the work

Page 68: XML and COBOL

Parsing XML in Z/OS COBOL

Page 69: XML and COBOL

XML in CICS

Page 70: XML and COBOL

XML Schemas

• IBM uses the W3C XML Schema specification

• W3C maintains excellent tutorials covering XML technologies

• Here is the XML Schema tutorial from which I am freely borrowing:

• http://www.w3schools.com/schema/default.asp

Page 71: XML and COBOL

Schema Example<?xml version="1.0"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"><xs:element name="note">  <xs:complexType>    <xs:sequence>      <xs:element name="to" type="xs:string"/>      <xs:element name="from" type="xs:string"/>      <xs:element name="heading" type="xs:string"/>      <xs:element name="body" type="xs:string"/>    </xs:sequence>  </xs:complexType></xs:element>

</xs:schema> ======================================================================<?xml version="1.0"?>

<note>  <to>Tove</to>  <from>Jani</from>  <heading>Reminder</heading>  <body>Don't forget me this weekend!</body></note>

Page 72: XML and COBOL

What do Schemas Do?

An XML Schema:• defines elements that can appear in a document • defines attributes that can appear in a document • defines which elements are child elements • defines the order of child elements • defines the number of child elements • defines whether an element is empty or can include text • defines data types for elements and attributes • defines default and fixed values for elements and

attributes

Page 73: XML and COBOL

XML Schemas are the Successors of DTDs

Here are some reasons:• XML Schemas are extensible to future

additions • XML Schemas are richer and more

powerful than DTDs • XML Schemas are written in XML • XML Schemas support data types • XML Schemas support namespaces

Page 74: XML and COBOL

Root Element

<?xml version="1.0"?>

<xs:schema>......</xs:schema>

Page 75: XML and COBOL

Simple ElementsA simple element is an XML element that can

contain only text. It cannot contain any other elements or attributes.

• The text can be of many different types. It can be one of the types included in the XML Schema definition (boolean, string, date, etc.), or it can be a custom type that you can define yourself.

• You can also add restrictions (facets) to a data type in order to limit its content, or you can require the data to match a specific pattern.

Page 76: XML and COBOL

Simple Element Syntax

The syntax for defining a simple element is: • <xs:element name="xxx" type="yyy"/>

Page 77: XML and COBOL

Some Simple Data Types

XML Schema has a lot of built-in data types, including:

• xs:string • xs:decimal • xs:integer • xs:boolean • xs:date • xs:time

Page 78: XML and COBOL

Simple Elements

• Here are some XML elements:• <lastname>Refsnes</lastname><age>36</age><dateborn>1970-03-27</dateborn>

• And here are the corresponding simple element definitions:

• <xs:element name="lastname" type="xs:string"/><xs:element name="age" type="xs:integer"/><xs:element name="dateborn" type="xs:date"/>

Page 79: XML and COBOL

Default and Fixed Values for Simple Elements

• Simple elements may have a default value OR a fixed value specified.

• A default value is automatically assigned to the element when no other value is specified.

• In the following example the default value is "red":• <xs:element name="color" type="xs:string" default="red"/>

• A fixed value is also automatically assigned to the element, and you cannot specify another value.

• In the following example the fixed value is "red":• <xs:element name="color" type="xs:string" fixed="red"/>

Page 80: XML and COBOL

Attributes

• The syntax for defining an attribute is:• <xs:attribute name="xxx" type="yyy"/>

Page 81: XML and COBOL

Attribute Example

Here is an XML element with an attribute:• <lastname lang="EN">Smith</lastname>

• And here is the corresponding attribute definition:

• <xs:attribute name="lang" type="xs:string"/>

Page 82: XML and COBOL

Optional and Required Attributes

• Attributes are optional by default. To specify that the attribute is required, use the "use" attribute:

• <xs:attribute name="lang" type="xs:string" use="required"/>

Page 83: XML and COBOL

Restrictions

<xs:element name="age">  <xs:simpleType>    <xs:restriction base="xs:integer">      <xs:minInclusive value="0"/>      <xs:maxInclusive value="120"/>    </xs:restriction>  </xs:simpleType></xs:element>

Page 84: XML and COBOL

Complex Elements

• Contains other elements and/or attributes.• There are four kinds of complex elements:• empty elements • elements that contain only other elements • elements that contain only text • elements that contain both other elements

and text

Page 85: XML and COBOL

Empty Complex Element

• <product pid="1345"/>

Page 86: XML and COBOL

Complex Element Containing Other Elements

• <employee>  <firstname>John</firstname>  <lastname>Smith</lastname></employee>

Page 87: XML and COBOL

Complex Element Containing Only Text

• <food type="dessert">Ice cream</food>

Page 88: XML and COBOL

Complex Element with Elements and Text

• <description>It happened on

<date lang="norwegian">03.03.99</date> ....</description>

Page 89: XML and COBOL

Complex Sequence<xs:element name="employee">

  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>  </xs:complexType>

</xs:element> =========================================<employee>

  <firstname>John</firstname>  <lastname>Smith</lastname>

</employee>

Page 90: XML and COBOL

Building a Complex Type<xs:element name="employee" type="personinfo"/>

<xs:element name="student" type="personinfo"/><xs:element name="member" type="personinfo"/>

<xs:complexType name="personinfo">  <xs:sequence>    <xs:element name="firstname" type="xs:string"/>    <xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType>

Page 91: XML and COBOL

Empty Elements

• <product prodid="1345" /> ================================<xs:element name="product">

  <xs:complexType>    <xs:complexContent>      <xs:restriction base="xs:integer">        <xs:attribute name="prodid" type="xs:positiveInteger"/>      </xs:restriction>    </xs:complexContent>  </xs:complexType></xs:element>

Does not introduce any element content.

Page 92: XML and COBOL

Complex Elements with Only Elements

• <xs:element name="person">  <xs:complexType>    <xs:sequence>      <xs:element name="firstname" type="xs:string"/>      <xs:element name="lastname" type="xs:string"/>    </xs:sequence>  </xs:complexType></xs:element>

Or<xs:element name="person" type="persontype"/>

<xs:complexType name="persontype">  <xs:sequence>    <xs:element name="firstname" type="xs:string"/>    <xs:element name="lastname" type="xs:string"/>  </xs:sequence></xs:complexType>

Page 93: XML and COBOL

Complex Text Only• <xs:element name="shoesize">

  <xs:complexType>    <xs:simpleContent>      <xs:extension base="xs:integer">        <xs:attribute name="country" type="xs:string" />      </xs:extension>    </xs:simpleContent>  </xs:complexType></xs:element>

• ============================================

<shoesize country="france">35</shoesize>

Page 94: XML and COBOL

Content with Mixed Type<xs:element name="letter">

  <xs:complexType mixed="true">    <xs:sequence>      <xs:element name="name" type="xs:string"/>      <xs:element name="orderid" type="xs:positiveInteger"/>      <xs:element name="shipdate" type="xs:date"/>    </xs:sequence>  </xs:complexType></xs:element>

============================================================<letter>

  Dear Mr.<name>John Smith</name>.  Your order <orderid>1032</orderid>  will be shipped on <shipdate>2001-07-13</shipdate>.

</letter>

To enable character data to appear between the child-elements of "letter", the mixed attribute must be set to "true".

Page 95: XML and COBOL

Seven IndicatorsOrder indicators:• All • Choice • Sequence Occurrence indicators:• maxOccurs • minOccurs Group indicators:• Group name • attributeGroup name

Page 96: XML and COBOL

Family XML<?xml version="1.0" encoding="ISO-8859-1"?>

<persons xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:noNamespaceSchemaLocation="family.xsd">

<person>  <full_name>Hege Refsnes</full_name>  <child_name>Cecilie</child_name></person>

<person>  <full_name>Tove Refsnes</full_name>  <child_name>Hege</child_name>  <child_name>Stale</child_name>  <child_name>Jim</child_name>  <child_name>Borge</child_name></person>

<person>  <full_name>Stale Refsnes</full_name></person>

</persons>

Page 97: XML and COBOL

Family Schema<?xml version="1.0" encoding="ISO-8859-1"?>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"elementFormDefault="qualified">

<xs:element name="persons">  <xs:complexType>    <xs:sequence>      <xs:element name="person" maxOccurs="unbounded">        <xs:complexType>          <xs:sequence>            <xs:element name="full_name" type="xs:string"/>            <xs:element name="child_name" type="xs:string"            minOccurs="0" maxOccurs="5"/>          </xs:sequence>        </xs:complexType>      </xs:element>    </xs:sequence>  </xs:complexType></xs:element>

</xs:schema>

Page 98: XML and COBOL

Schema Must Be Compiled in z/OS

Go to a UNIX prompt with TSO OMVSType:xsdosrg -v -o /u/HLQ/xml/item.osr /u/HLQ/xml/item.xsd

Page 99: XML and COBOL

Modifying XML Parse

XML PARSE myDocument VALIDATING WITH mySchema PROCESSING PROCEDURE myProc ON EXCEPTION DISPLAY “DOCUMENT ERROR” NOT ON EXCEPTION DISPLAY “VALID DOCUMENT”END-XML

Page 100: XML and COBOL

Schema Specification

ENVIRONMENT DIVISION.CONFIGURATION SECTION.SPECIAL-NAMES. XML-SCHEMA mySchema IS ‘DDSCHEMA’

JCL://DDSCHEMA DD PATH=‘u/xml/item.osr’

Page 101: XML and COBOL

Exercise 6

• Try writing a schema for XMLDATA2 in CSU.PUBLIC.DATA

• Modify XMLHELLO to parse the document with your schema

• Modify the data to have bad values and run your program against the schema