Hi Pavel

On 14.01.25 09:14, Pavel Stehule wrote:
> I did some research and the design of this document is different
>
> 1. Oracle doesn't support this
> 2. DB2 has different implementations for z/OS (variadic) and for unix
> (nonvariadic)
> 3. looks so db2 allows some concatenation of xml content when xmlexpr
> is not the document already (not tested)
> 4. Your implementation just raise an exception

I'm not entirely sure I follow. XMLDOCUMENT is designed to produce a 
well-formed XML document, and according to the XML specification, a well-formed 
document must have precisely one root element.

SELECT
  xmlserialize(DOCUMENT
  xmldocument(
    xmlelement(NAME root,
      xmlattributes(42 AS att),
      xmlcomment('comment'),
      xmlelement(NAME foo,'<foo&bar>'),
      xmlelement(NAME bar, 
        xmlconcat('va', 'lue')),
      xmlpi(name pi),
      xmlelement(NAME txt, xmltext('<"&>'))
    )) AS text INDENT) ;
           xmlserialize           
----------------------------------
 <root att="42">                 +
   <!--comment-->                +
   <foo>&lt;foo&amp;bar&gt;</foo>+
   <bar>value</bar>              +
   <?pi?>                        +
   <txt>&lt;"&amp;&gt;</txt>     +
 </root>
(1 row)


Could you provide an example of this feature you're missing?

Malformed CONTENT xml strings will indeed raise an exception.

SELECT xmldocument('foo'::xml);
ERROR:  invalid XML document
DETAIL:  line 1: Start tag expected, '<' not found
foo
^

>
> I didn't find a free downloadable SQL/XML standard with description of
> XMLDOCUMENT so I read just the DB2 doc, but it isn't fully consistent
> and it is different from your implementation.


The main idea is to ensure that an xml string is a valid document (even
in CONTENT mode)

postgres=# SET xmloption TO DOCUMENT;
SET
postgres=# SELECT 'foo'::xml;
ERROR:  invalid XML document
LINE 1: SELECT 'foo'::xml;
               ^
DETAIL:  line 1: Start tag expected, '<' not found
foo
^
postgres=# SET xmloption TO CONTENT;
SET
postgres=# SELECT 'foo'::xml;
 xml
-----
 foo
(1 row)

postgres=# SELECT xmldocument('foo'::xml);
ERROR:  invalid XML document
DETAIL:  line 1: Start tag expected, '<' not found
foo
^

> So the argument of better compatibility for this patch doesn't look
> too strong. But I found that the usage of XMLDOCUMENT is required for
> storing XML, so it can  be a frequently used function. Unfortunately,
> I do not have any knowledge about db2. It is hard to understand the
> usage of this function, because the sense is probably different than
> in DB2, and the documentation doesn't explain well an usage and
> motivation for this function. If it does a check, then it is not
> described in doc.
>
Perhaps changing the documentation like this would make things clearer?

"The xmldocument function encapsulates the XML expression within a valid XML 
document structure. The expression passed as the argument must be a valid, 
single-rooted XML fragment. If the XML expression is NULL, the result will also 
be NULL."

Many thanks for the review!

Best, Jim



Reply via email to