0

I have the below query:

select XMLType('<root>
  <TPL>
    <fld>f7</fld><val>v1v2</val>
  </TPL>
</root>') from dual;

When i execute the above query,it executes perfectly fine and i get the following output:

<root>
  <TPL>
    <fld>f7</fld><val>v1v2</val>
  </TPL>
</root>

But when i execute the following(which is my requirement):

select XMLType('<root>
  <TPL>
    <fld>f7</fld><val>v1&v2</val>
  </TPL>
</root>') from dual;

I am being asked to enter the value for '&' in a popup screen.

I have tried the following two ways:

select XMLType('<root>
  <TPL>
    <fld>f7</fld><val>v1&amp;v2</val>
  </TPL>
</root>') from dual;


select XMLType('<root>
  <TPL>
    <fld>f7</fld><val>v1''&''v2</val>
  </TPL>
</root>') from dual;

But still unable to get the OUTPUT correctly which should be:

<root>
  <TPL>
    <fld>f7</fld><val>v1&v2</val>
  </TPL>
</root>

How can i achieve this?

vikky
  • 171
  • 1
  • 1
  • 16
  • You didn't say what client you're using, but these options should work for most of them: https://stackoverflow.com/questions/118190/how-do-i-ignore-ampersands-in-a-sql-script-running-from-sql-plus – kfinity Mar 19 '19 at 13:36
  • I am using Toad For Oracle – vikky Mar 19 '19 at 13:37

3 Answers3

1

There's two problems here - & is a special character in both Oracle SQL and XML, and it has to be escaped in both. cdb_dba gave some examples of how to escape it in SQL (CHR(38) or & at the end of a string), and mkuligowski gave examples of how to escape it in XML (&amp; or CDATA), but the two kinds of escaping need to be combined. Here's a couple of examples:

-- with CHR() + CDATA
select XMLType('<root>
  <TPL>
    <fld>f7</fld><val><![CDATA[v1'|| chr(38) || 'v2]]></val>
  </TPL>
</root>') from dual;

-- with ampersand at the end of a string + "amp" character reference 
select XMLType('<root>
  <TPL>
    <fld>f7</fld><val>v1&' || 'amp;v2</val>
  </TPL>
</root>') from dual;
kfinity
  • 8,581
  • 1
  • 13
  • 20
  • Thank you for the clarification! I don't typically work with XMLType data, so I will add that to my list of things to watch out for if I ever happen to find myself in that area. :) – 1991DBA Mar 19 '19 at 15:22
0

Instead of using v1''&''v2 or v1&amp;v2, try using v1'|| chr(38) || 'v2 or 'v1 &'||'v2'

chr(38) is ASCII for the amperstand, and the second option is just a different method of concatenating. Hope this helps!

1991DBA
  • 805
  • 1
  • 9
  • 18
0

I bet that you are using SQLDeveloper when querying. Try run SET DEFINE OFF; and you won't be prompted for the value .

Moreover, when you creating XMLType object then you should escape illegal characters, so following will work:

SET DEFINE OFF;
select XMLType('<root>
 <TPL>
  <fld>f7</fld><val>v1&amp;v2</val>
 </TPL>
 </root>') from dual;

On the other hand, you can always wrap illegal character in CDATA:

select XMLType('<root>
 <TPL>
  <fld>f7</fld><val><![CDATA[v1&v2]]></val>
 </TPL>
</root>') from dual;
mkuligowski
  • 1,544
  • 1
  • 17
  • 27