Db2 V11 in z/OS - XMLELEMENT NAME population using a variable

Anandh Nagarajan

Db2 V11 in z/OS - XMLELEMENT NAME population using a variable

Hello Everybody,

I am trying to generate a XML document and i would like to populate the NAME in XMLELEMENT dynamically. I tried the following methods.

1. XMLELEMENT (NAME variable-name,element-value) - Db2 does not recognize variable-name. Based on my search i came to know that we can use EVALNAME() function in Oracle. Do we have similar function in Db2 z/OS?

2. XMLELEMENT( NAME ( CASE cond when true "ELEMENT-NAME1" else "ELEMENT-NAME2" END),ELEMENT-VALUE) - Db2 doesn't allow this either.

 

Also i tried to directly construct the XML using the below which also doesn't seem to work.

 

SELECT XMLQUERY ('element article {
attribute aid {"00010"},
attribute promoted {"NO"}
element title {"Homemade pizza"}
}' )
from sysibm.sysdummy1;

Please let me know if anybody has any idea about how to solve this.

Thanks,

Anandh Nagarajan

Michael Hannan

RE: Db2 V11 in z/OS - XMLELEMENT NAME population using a variable
(in response to Anandh Nagarajan)

In Reply to Anandh Nagarajan:

Hello Everybody,

I am trying to generate a XML document and i would like to populate the NAME in XMLELEMENT dynamically. I tried the following methods.

1. XMLELEMENT (NAME variable-name,element-value) - Db2 does not recognize variable-name. Based on my search i came to know that we can use EVALNAME() function in Oracle. Do we have similar function in Db2 z/OS?

2. XMLELEMENT( NAME ( CASE cond when true "ELEMENT-NAME1" else "ELEMENT-NAME2" END),ELEMENT-VALUE) - Db2 doesn't allow this either.

 

The Db2 z 11 manual states: "NAME element-name Specifies the name of an XML element. element-name is an SQL identifier that must be in the form of an XML qualified name, or QName."

Expression is not allowed.
You could try:

CASE WHEN eval THEN XMLELEMENT( NAME ( "ELEMENT-NAME1"),ELEMENT-VALUE)
ELSE XMLELEMENT( NAME ( "ELEMENT-NAME2"),ELEMENT-VALUE)   
END

I have not tested to see if it works, nor checked if a mixed Element Name result makes sense in the context.

Michael Hannan,
DB2 Application Performance Specialist
CPT Global Ltd

Edited By:
Michael Hannan[Organization Members] @ Apr 12, 2018 - 06:43 AM (Europe/Berlin)

Anandh Nagarajan

RE: Db2 V11 in z/OS - XMLELEMENT NAME population using a variable
(in response to Michael Hannan)

Thanks Mike.

I was able to achieve what i intended based on your reply.