Home » Developer & Programmer » JDeveloper, Java & XML » XML namespace extraction (Oracle 9i)
XML namespace extraction [message #289408] Fri, 21 December 2007 06:03 Go to next message
sugathanck
Messages: 2
Registered: May 2005
Junior Member
Hi All

I have a XML like this
<MGGCDR xmlns="<Some_Link>" xmlns:xsi="Some_Schema_Instance" xsi:schemaLocation="Some_Schema_Location">
------
------
-------
</MGGCDR>

I want to store the value of xmlns, xmlns:xsi and xsi:schemaLocation.

I am fine to read this as one string also. How can I do that?

Very urgently required

Thanks, CK

Re: XML namespace extraction [message #293890 is a reply to message #289408] Tue, 15 January 2008 08:43 Go to previous message
mchadder
Messages: 224
Registered: May 2005
Location: UK
Senior Member
Hello,

This isn't as easy as you might think, since the SQL functions expect you to know what namespace you're trying to query by. However, it is possible (given a few caveats) to extract this information, using the local-name() xpath function, which basically ignores namespaces :
SQL> SELECT 
  2    EXTRACTVALUE(VALUE(t), '/*[local-name()="MGGCDR"]/@*[local-name()="xmlns"]') xmlns_val,
  3    EXTRACTVALUE(VALUE(t), '/*[local-name()="MGGCDR"]/@*[local-name()="xsi"]') xsi_val,
  4    EXTRACTVALUE(VALUE(t), '/*[local-name()="MGGCDR"]/@*[local-name()="schemaLocation"]') 
  5      schemalocation_val  
  6  FROM 
  7    TABLE(XMLSEQUENCE(EXTRACT(
  8      XMLTYPE('<MGGCDR xmlns="xyz" 
  9                       xmlns:xsi="http://xsi/"
 10                       xsi:schemaLocation="http://schemaLocation" />'), 
 11      '/*[local-name()="MGGCDR"]'))) t
 12  /

XMLNS_VAL
----------------------------------------------------------------------------------------------------
XSI_VAL
----------------------------------------------------------------------------------------------------
SCHEMALOCATION_VAL
----------------------------------------------------------------------------------------------------
xyz
http://xsi/
http://schemaLocation

However, be aware of the caveats. If the XML contains more than one attribute called "xsi" or "schemaLocation", then the EXTRACTVALUE will fail, i.e.
SQL> SELECT 
  2    EXTRACTVALUE(VALUE(t), '/*[local-name()="MGGCDR"]/@*[local-name()="xmlns"]') xmlns_val,
  3    EXTRACTVALUE(VALUE(t), '/*[local-name()="MGGCDR"]/@*[local-name()="xsi"]') xsi_val,
  4    EXTRACTVALUE(VALUE(t), '/*[local-name()="MGGCDR"]/@*[local-name()="schemaLocation"]') 
  5      schemalocation_val  
  6  FROM 
  7    TABLE(XMLSEQUENCE(EXTRACT(
  8      XMLTYPE('<MGGCDR xmlns="xyz" 
  9                       xmlns:xsi="http://xsi/"
 10                       xsi="http://xsi2/"
 11                       xsi:schemaLocation="http://schemaLocation" />'), 
 12      '/*[local-name()="MGGCDR"]'))) t
 13  /
SELECT
*
ERROR at line 1:
ORA-19025: EXTRACTVALUE returns value of only one node

Regards
Previous Topic: XMLCDATA in Oracle 9i (or how to write extended chars in a XML file)
Next Topic: PL/SQL Cursor output as XML
Goto Forum:
  


Current Time: Sun Dec 04 06:44:21 CST 2016

Total time taken to generate the page: 0.11351 seconds