Altova MapForce 2024 Enterprise Edition

Example: Extracting Data from IBM DB2 XML Type Columns

Home Prev Top Next

This example illustrates how to extract data from IBM DB2 database columns of XML type and write it to a target CSV file. It also illustrates how to use XQuery statements embedded into SQL in order to retrieve XML content conditionally. The example requires access to an IBM DB2 database where you have permission to create and populate tables.

 

First, let's prepare the database so that it actually contains XML data. This can be done either in a database administration tool specific to your database, or directly in MapForce. To do this directly in MapForce, follow the steps below:

 

1.Create a new mapping and click the DBQuery tab.

2.Click Quick Connect ( quickconnect ) and follow the wizard steps to create a new database connection (see also Database Connection Examples).

3.Paste the following text into the SQL Editor. This SQL query creates a database table called ARTICLES and populates it with data.

-- Create the table
CREATE TABLE
   ARTICLES (
       id INTEGER NOT NULL,
       article XML ) ;
-- Populate the table
INSERT INTO ARTICLES VALUES
  (1, '<Article>
     <Number>1</Number>
     <Name>T-Shirt</Name>
     <SinglePrice>25</SinglePrice>
  </Article>'),
(2, '<Article>
     <Number>2</Number>
     <Name>Socks</Name>
     <SinglePrice>230</SinglePrice>
  </Article>'),
(3, '<Article>
     <Number>3</Number>
     <Name>Pants</Name>
     <SinglePrice>34</SinglePrice>
  </Article>'),
(4, '<Article>
     <Number>4</Number>
     <Name>Jacket</Name>
     <SinglePrice>5750</SinglePrice>
  </Article>');

4.Click the Execute ( ic_execute-sql ) button. The query execution result is displayed in the Query Results window. If the query is executed successfully, four rows are added to the newly created table.

 

Next, we will create a mapping which retrieves XML data from the ARTICLES table created above conditionally. The goal is to retrieve from the ARTICLES column only articles with a price greater than 100.

 

Step 1: Add the database

1.Click the Mapping tab to switch back to the mapping pane.

2.On the Insert menu, click Database, and follow the wizard steps to connect to the database.

3.When prompted to select the database objects, select the ARTICLES table created previously.

mf_db2_xmltype1

 

Step 2: Assign the schema to the XML type field

1.Right-click the ARTICLE item of the component, and select Assign XML Schema to field from the context menu.

mf_db2_xmltype2

2.Select File, and browse for the following schema: <Documents>\Altova\MapForce2024\MapForceExamples\Tutorial\DB2xsd.xsd.

 

Step 3: Add the SQL WHERE/ORDER component

1.On the Insert menu, click SQL WHERE/ORDER.

2.Connect the ARTICLE XML type column to the input of the SQL WHERE/ORDER.

mf_db2_xmltype4

3.In the SQL-WHERE/ORDER Properties dialog box, enter the following text:

XMLEXISTS('\$a/Article[SinglePrice>100]' PASSING ARTICLE as "a")

mf_db2_xmltype5

The text above represents the "WHERE" part of the SQL query. At mapping runtime, it will be combined with the "SELECT" part displayed on the dialog box. This statement uses the XMLEXISTS function and syntax specific to IBM DB2 databases.

 

Step 4: Add the target CSV file

1.On the Insert menu, click Text File.

2.When prompted, select Use simple processing for standard CSV... , and click Continue.

3.Click Append Field three times to add three fields which will store the article number, name, and price, respectively. Leave all other settings as is.

4.Draw the mapping connections as shown below.

mf_db2_xmltype6

You can now preview the mapping result, by clicking the Output pane. As expected, only articles with price greater than 100 are shown in the output.

mf_db2_xmltype7

© 2017-2023 Altova GmbH