Introduction to XML in Db2 for z/OS – Part 8 – pureXML enhancements in Db2 12

By Jan Marek posted Mar 08, 2018 06:10 AM


It has been some time since the first part of this short series came out. The previous post reviewed SQL/XML functions and concluded our short series of basic introductions to XML support in Db2 for z/OS. Since that time, besides Db2 losing its capital B, a new version of Db2 12 came out that introduced a couple of interesting enhancements to the SQL/XML functions. We are going to discuss them in this last post of the whole series.


This function is used to modify XML documents. Aside the fact it can be used only in multiple versioned tablespaces, the biggest limitation of this function is that you can specify only a single basic updating expression (insert, replace, delete node). So, for example, if you need to modify the price for every item in your order list, you will need an XMLMODIFY call for each item.

This limitation has been removed in Db2 12. XMLMODIFY function accepts more than a basic updating expression. Updating expressions can contain:

  • A basic updating expression
  • A sequence expression
  • A FLWOR that returns an updating expression
  • A conditional expression that contains an updating expression in its then/else clause

The following examples illustrate the new possibilities that are available to updating expressions (a basic updating expression has been covered in the previous post). For the last two examples, we will use a generic item with price.

A sequence expression:

// Change the blog url and add old_url as a new item
          'replace node /blog/url with $newnode1,
           insert node $newnode2 after /blog/url',
           XMLPARSE('') as "newnode1",
           XMLPARSE('') as "newnode2"); 

A FLWOR that returns an updating expression:

// Apply the sale 
UPDATE ORDERS SET XMLDATA = xmlmodify(          
  'for $item in /order/item                     
   let $p := $item/price
     replace value of node $p with $p - 100 

A conditional expression that contains updating expression in its then/else clause:

// Appply the sale conditionally
UPDATE ORDERS SET XMLDATA = xmlmodify(          
  'for $item in /order/item                     
   let $p := $item/price
   let $type := $item/type
   where $p > 10000
     (if ($type = 'X') then
         replace value of node $p with $p – 100
         replace value of node $p with $p – 200)'



This function allows you to create a relational Db2 table from the XML data. The last post discussed the various uses of the XMLTABLE function. Db2 12 focused on one use case, which has not been discussed, and significantly impoved performance. Let’s take a look at the use case and the performance improvements made to XMLTABLE in Db2 12.

Asume you have an XML document structure where you represent attributes of an object as name-value tuples as ilustrated below:


You want to decompose these attributes as relational data, so you use the XMLTABLE function:  

SELECT X.* FROM BLOGS B,                                        
XMLTABLE('$b/blogs/item' passing B.XML_DATA as "b"  
      COLUMNS "ID"     INT          PATH '../blog[@name="id"]/@value',
              "TITLE"  VARCHAR(128) PATH '../blog[@name="title"]/@value',
              "ARTIST" VARCHAR(128) PATH '../blog[@name="author"]/@value',

All the XPath expressions in the above example share the same pattern, but differ in predicate value. In previous versions of Db2, the XPath was evaluated for each column definition. Now in Db2 12, an XPath expression is evaluated only once and this evaluation is then used in all the subsequent column definitions. This improvement is a significant performance benefit for this use case. 


The XSLTRANFORM function is used to transform XML documents into other formats such as HTML, plain text, different XML schemas, and so on. XSL stands for eXtensible Stylesheet Language and it is a styling language for XML. For more information about XSL, see Db2 supports the V1.0 Recommendation.

You can convert part of an XML document or the whole XML document. You can also select or rearrange the data with the XPath query language and the built-in functions of XSL. The XSLTRANSFORM function requires Java. 

XSLTRANSFORM has three parameters:


The following example will illustrate the transformation of an XML document into HTML using the XSLTRANSFORM function.

The XML document (the one used in most of the examples in this series):


The XSLT code to transform the XML document:

 The transformed document in HTML:




  • Best Chicken Tikka Masala

  • Chocolate Ganache

  • How to Make Besan Ladoo




This brief overview of the new enhancements to D2b 12 concludes our introduction to XML support in Db2 11 for z/OS. This introduction included the basics of XML support, from XML document manipulation through internals of the pureXML storage, to the rudiments of the XPath and XQuery expressions used in SQL/XML functions. If you have further interest in this topic, refer to the references below for more advanced information.



1 view