Previous Topic Next topic Print topic


NAMESPACE Clause

XML documents often contain namespaces. The NAMESPACE clause enables you to set or identify those namespaces in your COBOL. Specify the NAMESPACE clause in a data description entry. The following example shows how to use the NAMESPACE clause:

Using NAMESPACE

0010  xd book-file.
0020  78  book-ns value "http://xml.microfocus.com/book.xsd".
0030  01  book                identified by "book"
0040                          namespace is book-ns.
0050      05  publisher pic x(80)
0060              identified by "name"
0070              namespace is
0080              "http://xml.microfocus.com/publisher.xsd".
0090 
0100      05  author identified by "name"
0110                 namespace is
0120                 "http://xml.microfocus.com/author.xsd".
0130          10  author-first identified by "firstname"
0140                           pic x(80).
0150          10  author-last  identified by "lastname"
0160                           pic x(80).
0170      05  title            identified by "title"
0180                           pic x(80).

Line 0020:

0020  78  book-ns value "http://xml.microfocus.com/book.xsd".

Defines a namespace and its value.

Lines 0030 - 0040:

0030  01  book                identified by "book"
0040                          namespace is book-ns.

Defines a data record with a namespace equivalent to the value in book-ns.

Lines 0050 - 0080:

0050      05  publisher pic x(80)
0060              identified by "name"
0070              namespace is
0080              "http://xml.microfocus.com/publisher.xsd".

Sets a namespace to a fixed URI (Uniform Resource Identifier), which is a specific point of contact on the World Wide Web - in this case an XML schema file.

Lines 0100 - 0120:

0100      05  author identified by "name"
0110                 namespace is
0120                 "http://xml.microfocus.com/author.xsd".

Sets a namespace to a fixed URI.

Lines 0130 - 0160:

0130          10  author-first identified by "firstname"
0140                           pic x(80).
0150          10  author-last  identified by "lastname"
0160                           pic x(80).

Because these data items are in the author group, they take on the same namespace as author.

Lines 0170 - 0180:

0170      05  title            identified by "title"
0180                           pic x(80).

Title inherits the same namespace as book because it is in the book group but does not specify a namespace.

On input, both tags and namespaces must match those in the XML stream. On output, XML tags are qualified by the indicated namespace. The code in the example Using Namespace would yield an XML stream similar to:

<?xml version="1.0" encoding="utf-8" ?>
<book xmlns="http://xml.microfocus.com/book.xsd">
   <name xmlns="http://xml.microfocus.com/publisher.xsd">
      Just a publisher
   </name>
   <name xmlns="http://xml.microfocus.com/author.xsd">
      <firstname>AuthorFirstname</firstname>
      <lastname>AuthorFirstname</lastname>
   </name>
   <title>
      This is the title
   </title>
</book>

You can replace a specific NAMESPACE value with a data-name. This facilitates dynamic discovery of a namespace in XML. In this case, namespace matching is not performed on input. Instead, the namespace of the matching tag is populated into the data item specified by NAMESPACE. On output, the value of the namespace referenced by the data-name is used to qualify the output tag. The following record:

     xd generic-file.
     01  generic-tag identified by generic-tag-name
                     namespace is generic-tag-namespace.
         05  generic-tag-name           pic x(80).
         05  generic-tag-namespace      pic x(80).
         05  generic-tag-value          pic x(80).

and XML stream input:

   <name xmlns="http://xml.microfocus.com/publisher.xsd">
      Just a publisher
   </name>

yields the value of "name" for generic-tag-name, the value of "http://xml.microfocus.com/publisher.xsd" for generic-tag-namespace and the value of "Just a publisher" for generic-tag-value.

Previous Topic Next topic Print topic