dbTalk Databases Forums  

XMLSERIALIZE required on XML insert?

comp.databases.ibm-db2 comp.databases.ibm-db2


Discuss XMLSERIALIZE required on XML insert? in the comp.databases.ibm-db2 forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Frederik Engelen
 
Posts: n/a

Default XMLSERIALIZE required on XML insert? - 10-27-2011 , 05:19 AM






Hello,

I have a question about inserting XML data constructed from relational
data. I don't understand why an explicit XMLSERIALIZE of the XML-
building query seems to be necessary. This looks to be a waste of
resources as the whole XML document needs to be reparsed again on
insert.

Some sample code to recreate my 'problem':

create table srctab (a integer, b varchar(10));
create table xmltab (a integer, xmlcol xml);

insert into srctab values (1,'one'), (2,'two');

--> here I generate one xml document per record in the table

select
a,
xmlelement(
name "tag",
xmlforest(
a as "a",
b as "b"
)
)
from srctab;

--> result... returns type XML

-- A 2
-- ----------- --------------------------------
-- 1 <tag><a>1</a><b>one</b></tag>
-- 2 <tag><a>2</a><b>two</b></tag>

--> Inserting in the table...


insert into xmltab (a, xmlcol)
select
a,
xmlelement(
name "tag",
xmlforest(
a as "a",
b as "b"
)
)
from srctab;

--> returns "SQL20345N The XML value is not a well-formed document
with a single root element."
--> Weird, as both documents look well-formed to me
--> let's try serializing and re-parsing it again (in this example
implicit, explicit works as well)

insert into xmltab (a, xmlcol)
select
a,
XMLSERIALIZE(
xmlelement(
name "tag",
xmlforest(
a as "a",
b as "b"
)
) as clob(2G))
from srctab;

-->DB20000I The SQL command completed successfully.


It works this way but this seems like a waste of resources, no? The
optimized statement also shows this double effort:

SELECT Q2.A,
$INTERNAL_XMLPARSE$(Q2.$C1)
FROM (
SELECT Q1.A,
$INTERNAL_XMLSERIALIZE$

Kind regards,

Frederik

Reply With Quote
  #2  
Old   
Henrik Loeser
 
Posts: n/a

Default Re: XMLSERIALIZE required on XML insert? - 10-28-2011 , 02:46 AM






On Oct 27, 12:19*pm, Frederik Engelen <engelenfrede... (AT) gmail (DOT) com>
wrote:
Quote:
Hello,

I have a question about inserting XML data constructed from relational
data. I don't understand why an explicit XMLSERIALIZE of the XML-
building query seems to be necessary. This looks to be a waste of
resources as the whole XML document needs to be reparsed again on
insert.

Some sample code to recreate my 'problem':

create table srctab (a integer, b varchar(10));
create table xmltab (a integer, xmlcol xml);

insert into srctab values (1,'one'), (2,'two');

--> here I generate one xml document per record in the table

select
* * * * a,
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * )
from srctab;

--> result... returns type XML

-- A * * * * * 2
-- ----------- --------------------------------
-- * * * * * 1 <tag><a>1</a><b>one</b></tag
-- * * * * * 2 <tag><a>2</a><b>two</b></tag

--> Inserting in the table...

insert into xmltab (a, xmlcol)
select
* * * * a,
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * )
from srctab;

--> returns "SQL20345N *The XML value is not a well-formed document
with a single root element."
--> Weird, as both documents look well-formed to me
--> let's try serializing and re-parsing it again (in this example
implicit, explicit works as well)

insert into xmltab (a, xmlcol)
select
* * * * a,
* * * * XMLSERIALIZE(
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * ) as clob(2G))
from srctab;

-->DB20000I *The SQL command completed successfully.

It works this way but this seems like a waste of resources, no? The
optimized statement also shows this double effort:

SELECT Q2.A,
* * $INTERNAL_XMLPARSE$(Q2.$C1)
FROM (
SELECT Q1.A,
* * $INTERNAL_XMLSERIALIZE$

Kind regards,

Frederik
The XMLSERIALIZE is NOT required and it would be indeed a waste of
resources...

What is missing for your constructed XML to be a well-formed document
is the non-visible document node. Wrap an XMLDOCUMENT function call
around your construction and it should work.

Henrik

BTW: In XQuery there is a "document { }" for that purpose.

Reply With Quote
  #3  
Old   
Henrik Loeser
 
Posts: n/a

Default Re: XMLSERIALIZE required on XML insert? - 10-28-2011 , 02:51 AM



On Oct 27, 12:19*pm, Frederik Engelen <engelenfrede... (AT) gmail (DOT) com>
wrote:
Quote:
Hello,

I have a question about inserting XML data constructed from relational
data. I don't understand why an explicit XMLSERIALIZE of the XML-
building query seems to be necessary. This looks to be a waste of
resources as the whole XML document needs to be reparsed again on
insert.

Some sample code to recreate my 'problem':

create table srctab (a integer, b varchar(10));
create table xmltab (a integer, xmlcol xml);

insert into srctab values (1,'one'), (2,'two');

--> here I generate one xml document per record in the table

select
* * * * a,
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * )
from srctab;

--> result... returns type XML

-- A * * * * * 2
-- ----------- --------------------------------
-- * * * * * 1 <tag><a>1</a><b>one</b></tag
-- * * * * * 2 <tag><a>2</a><b>two</b></tag

--> Inserting in the table...

insert into xmltab (a, xmlcol)
select
* * * * a,
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * )
from srctab;

--> returns "SQL20345N *The XML value is not a well-formed document
with a single root element."
--> Weird, as both documents look well-formed to me
--> let's try serializing and re-parsing it again (in this example
implicit, explicit works as well)

insert into xmltab (a, xmlcol)
select
* * * * a,
* * * * XMLSERIALIZE(
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * ) as clob(2G))
from srctab;

-->DB20000I *The SQL command completed successfully.

It works this way but this seems like a waste of resources, no? The
optimized statement also shows this double effort:

SELECT Q2.A,
* * $INTERNAL_XMLPARSE$(Q2.$C1)
FROM (
SELECT Q1.A,
* * $INTERNAL_XMLSERIALIZE$

Kind regards,

Frederik
[Not sure if my first reply came through]

Using XMLSERIALIZE is indeed a waste of resources and it is not
required. Just wrap the XMLDOCUMENT function around your construction.
It adds the missing non-visible document node to the top of your
document. With that it is well-formed...

Henrik

Reply With Quote
  #4  
Old   
Henrik Loeser
 
Posts: n/a

Default Re: XMLSERIALIZE required on XML insert? - 10-28-2011 , 02:58 AM



[it seems that my earlier replies didn't came through]
Hi Frederik,

using XMLSERIALIZE is indeed a waste of resources. Just wrap a call to XMLDOCUMENT around your constructed XML and it will add the non-visible document node and make it well-formed. XQuery has the "document { }" expression for that purpose.

Henrik

Reply With Quote
  #5  
Old   
Frederik Engelen
 
Posts: n/a

Default Re: XMLSERIALIZE required on XML insert? - 10-28-2011 , 03:12 AM



On 28 okt, 09:46, Henrik Loeser <henrik.loe... (AT) gmail (DOT) com> wrote:
Quote:
On Oct 27, 12:19*pm, Frederik Engelen <engelenfrede... (AT) gmail (DOT) com
wrote:





Hello,

I have a question about inserting XML data constructed from relational
data. I don't understand why an explicit XMLSERIALIZE of the XML-
building query seems to be necessary. This looks to be a waste of
resources as the whole XML document needs to be reparsed again on
insert.

Some sample code to recreate my 'problem':

create table srctab (a integer, b varchar(10));
create table xmltab (a integer, xmlcol xml);

insert into srctab values (1,'one'), (2,'two');

--> here I generate one xml document per record in the table

select
* * * * a,
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * )
from srctab;

--> result... returns type XML

-- A * * * * * 2
-- ----------- --------------------------------
-- * * * * * 1 <tag><a>1</a><b>one</b></tag
-- * * * * * 2 <tag><a>2</a><b>two</b></tag

--> Inserting in the table...

insert into xmltab (a, xmlcol)
select
* * * * a,
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * )
from srctab;

--> returns "SQL20345N *The XML value is not a well-formed document
with a single root element."
--> Weird, as both documents look well-formed to me
--> let's try serializing and re-parsing it again (in this example
implicit, explicit works as well)

insert into xmltab (a, xmlcol)
select
* * * * a,
* * * * XMLSERIALIZE(
* * * * xmlelement(
* * * * * * * * name "tag",
* * * * * * * * xmlforest(
* * * * * * * * * * * * a as "a",
* * * * * * * * * * * * b as "b"
* * * * * * * * )
* * * * ) as clob(2G))
from srctab;

-->DB20000I *The SQL command completed successfully.

It works this way but this seems like a waste of resources, no? The
optimized statement also shows this double effort:

SELECT Q2.A,
* * $INTERNAL_XMLPARSE$(Q2.$C1)
FROM (
SELECT Q1.A,
* * $INTERNAL_XMLSERIALIZE$

Kind regards,

Frederik

The XMLSERIALIZE is NOT required and it would be indeed a waste of
resources...

What is missing for your constructed XML to be a well-formed document
is the non-visible document node. Wrap an XMLDOCUMENT function call
around your construction and it should work.

Henrik

BTW: In XQuery there is a "document { }" for that purpose.- Tekst uit oorspronkelijk bericht niet weergeven -

- Tekst uit oorspronkelijk bericht weergeven -
Thanks Henrik, I might've asked it directly to you as well ;-)

--
Frederik

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.