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  |
 |
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 #690515 is a reply to message #690514] |
Wed, 09 September 2026 08:14   |
 |
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   |
 |
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 #690520 is a reply to message #690514] |
Thu, 10 September 2026 06:50   |
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   |
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 #690533 is a reply to message #690532] |
Fri, 11 September 2026 16:05  |
 |
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.
|
|
|
|
Goto Forum:
Current Time: Fri Nov 06 16:13:20 CST 2026
|