Link to home
Start Free TrialLog in
Avatar of Mr_Oz
Mr_Oz

asked on

Problem with Xquery XMLType

I have an XMLType column containing XML Data.

one of the tag names is <ns4:ContactFullName>.
I would like to extract the value from that element.  First I need to select the row which contains the xmltype data that I want to extract from.

My query looks like this:

  SELECT EXTRACTVALUE(VALUE(x) , '\ns4:ContactFullName')
  FROM table_name, XMLTABLE(XMLSEQUENCE(EXTRACT(xmltype( SELECT xmltype_column from table_name where COLUMN1 = 10 and  COLUMN2= 'somestring' and COLUMN3 ='N'), '\\ns4:ContactFullName'))) x

I am getting this error:
An exception was thrown: ORA-19102: XQuery string literal expected

any help would be appreciated.
SOLUTION
Avatar of Sean Stuber
Sean Stuber

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of slightwv (䄆 Netminder)
slightwv (䄆 Netminder)

I'm not sure why you are doing the XMLTABLE.  This works in 10.2.0.3.
drop table tab1 purge;
create table tab1(col1 xmltype);

insert into tab1 values(xmltype('<ns4:ContactFullName xmlns:ns4="myNamespace">Fred</ns4:ContactFullName>'));
commit;

select extractvalue(col1,'/ns4:ContactFullName','xmlns:ns4="myNamespace"') from tab1 t1;

Open in new window

if there is only one instance of that node for each row  then use extractvalue as slightwv has indicated.

if there could be more than one instance of that node for each row then use xmltable.
>>if there could be more than one instance

Good point.  Didn't think of that.
the question doesn't specify either way,  so having an example of single node is just as useful as an example of multi-node.

good catch on your part too!
Avatar of Mr_Oz

ASKER

Hmm ok I got this error.

An exception was thrown: ORA-19228: XP0008 - undeclared identifier: prefix 'ns4' local-name 'ns4:ContactFullName'

if I take out the namespace I get this error

An exception was thrown: ORA-06553: PLS-306: wrong number or types of arguments in call to 'OGC_X'

also this tag is nested do I need to give the full path from the root node?

Thanks
Avatar of Mr_Oz

ASKER

In answer to your question there is a possibility that this tag can occur more than once.
to fix the OGC_X problem change the "x" alias to "xx"

for the namespace error
you can either remove the namespace, or declare the namespace with the xmlnamespaces function

no, you do not need to specify the full path, that's what // is for
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Oz

ASKER

OK well for now I took out the namespace just to see if I can get it working without it.  I think that is going to be a problem because no namespace is declared when it is inserted, since the xmltype data that goes into that column can have various namespaces.

But to deal with the first problem I replaced the x's with xx and now I get this error.

An exception was thrown: ORA-00904: "XX": invalid identifier
Avatar of Mr_Oz

ASKER

OK let me try the namespace solution quick.
Avatar of Mr_Oz

ASKER

OK yes I think what you did fixed the name space error but running your code I am still getting An exception was thrown: ORA-00904: "XX": invalid identifier.

Thanks
did you replace it in both places?
Avatar of Mr_Oz

ASKER

Yes I actually copy and pasted from yours.
I stripped out the namespace and this works fine...

SELECT EXTRACTVALUE(VALUE(xx), 'ContactFullName')
FROM table_name, XMLTABLE(
                     '//ContactFullName'
                     PASSING xmltype_column
                 ) xx
WHERE column1 = 10 AND column2 = 'somestring' AND column3 = 'N'


if this doesn't work.  Can you post your xml and namespace to work from?
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Mr_Oz

ASKER

unforutnately I cannot post either, due to confidentialtiy reasons.  But I can say that the namespace appears to not be an issue whether it is there or not I get the same issue.
Avatar of Mr_Oz

ASKER

I can try to set up a dummy xml and table that I can reproduce the issue on.  Give me a few min.
Avatar of Mr_Oz

ASKER

OK here it is
  create table tab1(col1 xmltype, col2 number(3));
 
  insert into tab1 values(xmltype('<Root>
      <Category1><Address><CityName>Rochester</CityName></Address><ContactList><Contact><ContactFirstName>Jane</ContactFirstName>            <ContactFullName>Jane Jones</ContactFullName><ContactLastName>Jones</ContactLastName></Contact><Phone><PhoneExtensionNumber>6666</PhoneExtensionNumber>                        <PhoneNumber>4004445555</PhoneNumber></Phone><Sub><Contact><ContactFullName>Fred Smith</ContactFullName></Contact><Phone>      <PhoneNumber>3123213213</PhoneNumber></Phone></Sub></ContactList></Category1></Root>'),10);

select extractvalue(value(xx),'/ContactFullName')
from tab1,xmltable('//ContactFullName' passing Col1)xx
where col2=10


Error given
An exception was thrown: ORA-00904: "XX": invalid identifier       


Formatted xml

<Root>
      <Category1>
            <Address>
                  <CityName>Rochester</CityName>
            </Address>
            <ContactList>
                  <Contact>
                        <ContactFirstName>Jane</ContactFirstName>
                        <ContactFullName>Jane Jones</ContactFullName>
                        <ContactLastName>Jones</ContactLastName>
                  </Contact>
                  <Phone>
                        <PhoneExtensionNumber>6666</PhoneExtensionNumber>
                        <PhoneNumber>4004445555</PhoneNumber>
                  </Phone>
                  <Sub>
                        <Contact>
                              <ContactFullName>Fred Smith</ContactFullName>
                        </Contact>
                        <Phone>
                              <PhoneNumber>3123213213</PhoneNumber>
                        </Phone>
                  </Sub>
            </ContactList>
      </Category1>
</Root>
eeSample.txt
Avatar of Mr_Oz

ASKER

slightwv:

You latest post does work but does not account for the fact that I need to select a specific row before querying the column.


I am also not really clear on what this does:
(column_value,'/*/text()')

Thanks
Avatar of Mr_Oz

ASKER

OK never mind on the last part of my comment I understand (column_value,'/*/text()')
I just was not thinking straight while looking at it.
>>I need to select a specific row before querying the column.

I don't understand what this means.  If you are referring to the where clause in your original post, just add the where clause to my example.
Avatar of Mr_Oz

ASKER

Yes nevermind sorry I forgot to post back.  slightwv your solution worked for me thanks.  I gave a few points to sdstuber for all of his assistance as well.
Avatar of Mr_Oz

ASKER

Thanks alot guys
When I try to use the command

update from mytable
set my_column_xmldatatye = xmltype(:data_xml)
where
  id_mytable = xxxyyy

the oracle don't accpet, this error message is displayed

ORA-01461: can bind a LONG value only for insert into a LONG column.