Home » SQL & PL/SQL » SQL & PL/SQL » Compare XMLType to a string (18c)
Compare XMLType to a string [message #690513] Tue, 08 September 2026 13:23 Go to next message
Darth Waiter
Messages: 104
Registered: October 2020
Senior Member
create table xm(xm xmltype);
insert into xm(xm) select xmltype('<a>a</a>') from dual;

select xmltype('<a>a</a>'), cast(xm as varchar2(10)), cast('<a>a</a>' as varchar2(10)), xmltype.getstringval(xm), xmltype.getclobval(xm) from xm; -- 1 row
select xmltype('<a>a</a>'), cast(xm as varchar2(10)), cast('<a>a</a>' as varchar2(10)) from xm where cast(xm as varchar2(10)) = '<a>a</a>'; -- no rows
select xmltype('<a>a</a>'), cast(xm as varchar2(10)), cast('<a>a</a>' as varchar2(10)) from xm where cast(xm as varchar2(10)) = cast('<a>a</a>' as varchar2(10)); -- no rows
select xmltype('<a>a</a>'), cast(xm as varchar2(10)), cast('<a>a</a>' as varchar2(10)) from xm where xmltype.getstringval(xm) = '<a>a</a>'; -- no rows
select xmltype('<a>a</a>'), cast(xm as varchar2(10)), cast('<a>a</a>' as varchar2(10)) from xm where xmltype.getstringval(xm) = cast('<a>a</a>' as varchar2(4000)); -- no rows
select xmltype('<a>a</a>'), cast(xm as varchar2(10)), cast('<a>a</a>' as varchar2(10)) from xm where xmltype.getclobval(xm) = to_clob('<a>a</a>'); -- no rows
So, like in many similar scenarios, a string representation of an XML is not equal to a string that is perfectly equal to it. Are we not supposed to compare XML to strings, not even after casting or converting?

[Updated on: Tue, 08 September 2026 13:36]

Report message to a moderator

Re: Compare XMLType to a string [message #690514 is a reply to message #690513] Wed, 09 September 2026 01:05 Go to previous messageGo to next message
Michel Cadot
Messages: 68787
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

It seems Oracle adds a line feed at the end of the XML:
SQL> select '"'||xm||'"' from xm;
'"'||XM||'"'
----------------------------------
"<a>a</a>
"
So:
SQL> select xm from xm where rtrim(xm,chr(10)) = '<a>a</a>';
XM
---------------------------------------------------------------
<a>a</a>
Re: Compare XMLType to a string [message #690515 is a reply to message #690514] Wed, 09 September 2026 08:14 Go to previous messageGo to next message
Darth Waiter
Messages: 104
Registered: October 2020
Senior Member
Wow! It's astounding. It could've never occurred to me that a DB engine may modify the data being stored by the user versus the original value:

create table x(x xml)
insert into x(x) select convert(xml, '<a>a</a>')
select x from x where convert(varchar(10), x) = '<a>a</a>' -- 1 row
So the right approach would be to add a LF character to the literal, so as not to mess up the index during the select?

[Updated on: Wed, 09 September 2026 08:14]

Report message to a moderator

Re: Compare XMLType to a string [message #690516 is a reply to message #690515] Wed, 09 September 2026 10:03 Go to previous messageGo to next message
Michel Cadot
Messages: 68787
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

This is how Oracle XMLTYPE works, it requires a new line at the end. So if it is not present in your data XMLTYPE conversion function adds it (and only in this case) instead of raising an error.
Although W3C specification don't require a new line at the end of XML it is common to assume this like must text editors put a new line at the end of your file when you read it.

Note that Oracle never thought (and many times it thinks too much) you can compare XML data as a whole but rather you use XML functions to handle them.

[Updated on: Wed, 09 September 2026 11:26]

Report message to a moderator

Re: Compare XMLType to a string [message #690517 is a reply to message #690516] Wed, 09 September 2026 12:21 Go to previous messageGo to next message
Darth Waiter
Messages: 104
Registered: October 2020
Senior Member
Whoever's motivation aside, this is spectacularly bizarre behavior as it either forces developers down the path of using non-SARGable where clauses or rigging fuzzy logic. Once this slippery slope is hit, one can never know how many LFs are out there.
Re: Compare XMLType to a string [message #690518 is a reply to message #690517] Wed, 09 September 2026 12:57 Go to previous messageGo to next message
Michel Cadot
Messages: 68787
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Quote:
one can never know how many LFs are out there.
At least one and as most as you give it. Smile

The spaces (that is (#x20 | #x9 | #xD | #xA) in XML spec) at the end of the XML data must be ignored by the XML processor (in the standard).
If you want to retrieve the XML document as a whole, you act as an XML processor and should follow its rules to be sure to properly handle it.

The only advice I can give is to always "rtrim" these characters when you retrieve the whole XML document. Wink

If you want to know more about basic XML (without namespaces...) see Extensible Markup Language (XML) 1.0 (Fifth Edition).

But if your purpose is to store and retrieve XML document, without using XML processing, you would better use a CLOB (or BLOB if you want no character set conversion) column and not an XMLTYPE one.

Re: Compare XMLType to a string [message #690520 is a reply to message #690514] Thu, 10 September 2026 06:50 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3317
Registered: January 2010
Location: Connecticut, USA
Senior Member
Michel Cadot wrote on Wed, 09 September 2026 02:05

It seems Oracle adds a line feed at the end of the XML
It seems like a bug:

SQL> select dump(cast(xm as varchar2(10))) from xm;

DUMP(CAST(XMASVARCHAR2(10)))
--------------------------------------------------------------------------------
Typ=1 Len=9: 60,97,62,97,60,47,97,62,10

SQL> select dump(cast(xmltype('<a>a</a>') as varchar2(10))) from dual;

DUMP(CAST(XMLTYPE('<A>A</A>')ASVARCHAR2(10)))
--------------------------------------------------------------------------------
Typ=1 Len=8: 60,97,62,97,60,47,97,62

SQL>
Casting XMLTYPE column to VARCHAR2 added linefeed at the end. However casting XMLTYPE of a literal does not.

SY.

[Updated on: Thu, 10 September 2026 07:28]

Report message to a moderator

Re: Compare XMLType to a string [message #690521 is a reply to message #690520] Thu, 10 September 2026 07:14 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3317
Registered: January 2010
Location: Connecticut, USA
Senior Member
On other hand, comparing CAST of XML doesn't make much sense:

SQL> select 1 from xm where cast(xm as varchar2(10)) = cast(xmltype('<a>a</a>') as varchar2(10)) || chr(10);

         1
----------
         1

SQL> select 1 from xm where cast(xm as varchar2(10)) = cast(xmltype('<a>a</a>     ') as varchar2(10)) || chr(10);

no rows selected

SQL>
Same XML documents but representation is different.

SY.

Re: Compare XMLType to a string [message #690522 is a reply to message #690521] Thu, 10 September 2026 11:02 Go to previous messageGo to next message
Michel Cadot
Messages: 68787
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

Agree, this is why I said:

Quote:
The spaces (that is (#x20 | #x9 | #xD | #xA) in XML spec) at the end of the XML data must be ignored by the XML processor (in the standard).
If you want to retrieve the XML document as a whole, you act as an XML processor and should follow its rules to be sure to properly handle it.
I still wonder why use an XMLTYPE datatype if the only purpose is to store and retrieve a string in the end (OP never mentioned he wants to treat this string as XML data).

To conclude about a bug, the documentation states (emphasis is mine, typo is Oracle):

Quote:
XMLType is a system-defined opaque type for handling XML data. It as predefined member functions on it to extract XML nodes and fragments.

which means Oracle stores the XML data as it wants and any attempt to mention a bug will return the well-known "implemented as designed" message. Smile


Re: Compare XMLType to a string [message #690523 is a reply to message #690522] Thu, 10 September 2026 14:16 Go to previous messageGo to next message
Darth Waiter
Messages: 104
Registered: October 2020
Senior Member
Michel Cadot wrote on Thu, 10 September 2026 11:02

I still wonder why use an XMLTYPE datatype if the only purpose is to store and retrieve a string in the end (OP never mentioned he wants to treat this string as XML data).
The OP has no wants.
The OP does not decide or choose to use one or another data type.
The OP has to write software that accommodates whatever data may be out there, in the world. The OP does not work with a particular DB engine instance, which the OP has control of.
The OP's software runs on whichever instances the user may wish to use it with, as they wish.
Whatever the data the users may put in whatever columns of whatever tables of whatever databases has to be selected by the OP's code, if the user so wishes.
It would have been nice if the OP could control the data, but that is not the case.
Re: Compare XMLType to a string [message #690524 is a reply to message #690523] Thu, 10 September 2026 14:43 Go to previous messageGo to next message
Michel Cadot
Messages: 68787
Registered: March 2007
Location: Saint-Maur, France, https...
Senior Member
Account Moderator

But why XMLTYPE if you just use it as a string and have problems with XMLTYPE?
If your code should work with any db engine, XMLTYPE is surely not the correct type as not all db engines have such data type.
More, it is not the correct type as you won't use Oracle XML functions which is the purpose of Oracle XMLTYPE.

Quote:
The OP's software runs on whichever instances the user may wish to use it with, as they wish.

Sorry to say that but this is impossible.
Not only because data types run differently in different db engines but also because transaction properties/levels are different.
I wrote the attached article in 2009, some things have changed since then in the compared db engines but the basics are there.


Re: Compare XMLType to a string [message #690530 is a reply to message #690524] Fri, 11 September 2026 10:40 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3317
Registered: January 2010
Location: Connecticut, USA
Senior Member
OP can use XMLDIFF to compare two XMLs.

SY.
Re: Compare XMLType to a string [message #690531 is a reply to message #690530] Fri, 11 September 2026 11:09 Go to previous messageGo to next message
Darth Waiter
Messages: 104
Registered: October 2020
Senior Member
If it SARGable and offers a better performance than the casting to varchar2, then it is a good find, but I admit my leg began to twitch when I imagined all of the plumbing necessary to compare. Probably can get away with
like '%"?>'||CHR(10)||'</%'
Re: Compare XMLType to a string [message #690532 is a reply to message #690531] Fri, 11 September 2026 15:37 Go to previous messageGo to next message
Solomon Yakobson
Messages: 3317
Registered: January 2010
Location: Connecticut, USA
Senior Member
So you, for example, consider

xmltype('<a />') different from xmltype('<a></a>')
xmldiff(xmltype('<a val="xyz" />') different from xmltype('<a val="xyz"></a>'))

SY.
Re: Compare XMLType to a string [message #690533 is a reply to message #690532] Fri, 11 September 2026 16:05 Go to previous message
Darth Waiter
Messages: 104
Registered: October 2020
Senior Member
I am only under obligation to account for the most orthodox, textbook cases, which Oracle supports except for the trailing LF. The users want to ensure that the XML that they supposedly write to their DBs are indeed there, and that's all.

Also, it looks like a combination of XMLDIFF with any extraction function is not SARGable, so the difference vs CAST to Varchar2 is going to be minimal if any, unless I am missing something that I don't know about Oracle, which is entirely possible.

Having tested more DB engines and found none that add the extra LF, I still believe that it is a bug because it is only generated by the act of our saving the value and retrieving it:

SQL> select '"'||xmltype('<a>a</a>')||'"' from dual;

'"'||XMLTYPE('<A>A</A>')||'"'
--------------------------------------------------------------------------------
"<a>a</a>"

SQL>
There is no LF in the output of XMLTYPE. It's a bug that slipped through the cracks and is now considered a norm because there is no incentive to fix it.
Previous Topic: Hints in explain plans
Next Topic: Row generator
Goto Forum:
  


Current Time: Fri Nov 06 16:13:20 CST 2026