Skip to content

[BUG] Node types are not wrapped correctly by the REST API #5909

@adamretter

Description

@adamretter

When executing an XQuery via the REST API that has wrap="yes" set, atomic values are wrapped but Element Node types are not wrapped. There is an incorrect asymmetry here. Elements should also be wrapped, otherwise it is impossible for the caller to know their exact type in the result set.

POST'ing this:

<query xmlns="http://exist.sourceforge.net/NS/exist" xmlns:sx="http://exist-db.org/xquery/types/serialized" wrap="yes" typed="yes">
	<text><![CDATA[
<hello>{current-dateTime()}</hello>
	]]></text>
</query>

Incorrectly returns:

<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist" exist:hits="1" exist:start="1" exist:count="1" exist:compilation-time="61" exist:execution-time="61">
    <hello>world</hello>
</exist:result>

The expected result would be the following or similar:

<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist" exist:hits="1" exist:start="1" exist:count="1" exist:compilation-time="61" exist:execution-time="61">
    <exist:value exist:type="element()">
        <hello>world</hello>
    </exist:value>
</exist:result>

A further example which shows additional problems with Comment and Processing Instruction Nodes that are also not wrapped correctly:

POST'ing this:

<query xmlns="http://exist.sourceforge.net/NS/exist" xmlns:sx="http://exist-db.org/xquery/types/serialized" wrap="yes" typed="yes">
        <text><![CDATA[
<hello>world</hello>,
xs:string("hello"),
xs:integer("123"),
document {
   <other>things</other>
},
attribute stuff { "and things" },
comment { "just thinking" },
processing-instruction some-pi { "of this" },
text { "some words" },
current-date(),
<another>element</another>
        ]]></text>
</query>

Incorrectly returns:

<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist" exist:hits="10" exist:start="1" exist:count="10" exist:compilation-time="2" exist:execution-time="2">
    <hello>world</hello>
    <exist:value exist:type="xs:string">hello</exist:value>
    <exist:value exist:type="xs:integer">123</exist:value>
    <exist:document exist:uri="/db">
        <other>things</other>
    </exist:document>
    <exist:attribute exist:local="stuff">and things</exist:attribute><!--just thinking--><?some-pi of this?>
    <exist:text>some words</exist:text>
    <exist:value exist:type="xs:date">2025-10-13Z</exist:value>
    <another>element</another>
</exist:result>

The expected result should perhaps be the following:

<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist" exist:hits="10" exist:start="1" exist:count="10" exist:compilation-time="2" exist:execution-time="2">
    <exist:element><hello>world</hello></exist:element>
    <exist:value exist:type="xs:string">hello</exist:value>
    <exist:value exist:type="xs:integer">123</exist:value>
    <exist:document exist:uri="/db">
        <other>things</other>
    </exist:document>
    <exist:attribute exist:local="stuff">and things</exist:attribute>
    <exist:comment><!--just thinking--></exist:comment>
    <exist:processing-instruction><?some-pi of this?></exist:processing-instruction>
    <exist:text>some words</exist:text>
    <exist:value exist:type="xs:date">2025-10-13Z</exist:value>
    <exist:element><another>element</another></exist:element>
</exist:result>

The use of different wrappers for atomic values and node types complicates things unnecessarily for the consumer, a much better and simpler result format would be:

<exist:result xmlns:exist="http://exist.sourceforge.net/NS/exist" hits="10" start="1" count="10" compilation-time="2" execution-time="2">
    <exist:item type="element()"><hello>world</hello></exist:item>
    <exist:item type="xs:string">hello</exist:item>
    <exist:item type="xs:integer">123</exist:item>
    <exist:item type="document()" uri="/db">
        <other>things</other>
    </exist:item>
    <exist:item type="attribute()" name="stuff">and things</exist:item>
    <exist:item type="comment()"><!--just thinking--></exist:item>
    <exist:item type="processing-instruction()"><?some-pi of this?></exist:item>
    <exist:item type="text()">some words</exist:item>
    <exist:item type="xs:date">2025-10-13Z</exist:item>
    <exist:item type="element()"><another>element</another></exist:item>
</exist:result>

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions