provide-free-support Ujjwal's Blog: Generate XSD of Oracle Tables

Tuesday, July 3, 2012

Generate XSD of Oracle Tables

Hi,

Recently i was finding a way across generating XSD from Oracle Tables. I found a way as below which fulfills my requirement.

Create a plsql function in oracle as below, i used oracle 10G

create or replace function GENERATE_XSD(target_table varchar2) return xmltype
as
xmlSchema XMLTYPE;
begin
select
xmlElement(
"xsd:schema",
xmlAttributes(
'http://www.w3.org/2001/XMLSchema' as "xmlns:xsd",
'http://xmlns.oracle.com/xdb' as "xmlns:xdb"
),
xmlElement(
"xsd:element",
xmlAttributes(
'ROWSET' as "name",
'rowset' as "type"
)
),
xmlElement(
"xsd:complexType",
xmlAttributes
(
'rowset' as "name"
),
xmlElement
(
"xsd:sequence",
xmlElement
(
"xsd:element",
xmlAttributes
(
'ROW' as "name",
table_name || '_T' as "type",
'unbounded' as "maxOccurs"
)
)
)
),
xmlElement
(
"xsd:complexType",
xmlAttributes
(
table_name || '_T' as "name"
),
xmlElement
(
"xsd:sequence",
(
xmlAgg(ELEMENT)
)
)
)
)
into xmlSchema
from (
select TABLE_NAME, INTERNAL_COLUMN_ID,
case
when DATA_TYPE in ('VARCHAR2', 'CHAR') then
xmlElement
(
"xsd:element",
xmlattributes
(
column_name as "name",
decode(NULLABLE, 'Y', 0, 1) as "minOccurs",
column_name as "xdb:SQLName",
DATA_TYPE as "xdb:SQLTYPE"
),
xmlElement
(
"xsd:simpleType",
xmlElement
(
"xsd:restriction",
xmlAttributes
(
'xsd:string' as "base"
),
xmlElement
(
"xsd:maxLength",
xmlAttributes
(
DATA_LENGTH as "value"
)
)
)
)
)
when DATA_TYPE = 'DATE' then
xmlElement
(
"xsd:element",
xmlattributes
(
column_name as "name",
--'xsd:dateTime' as "type",
'xsd:date' as "type",
decode(NULLABLE, 'Y', 0, 1) as "minOccurs",
column_name as "xdb:SQLName",
DATA_TYPE as "xdb:SQLTYPE"
)
)
when DATA_TYPE = 'NUMBER' then
xmlElement
(
"xsd:element",
xmlattributes
(
column_name as "name",
decode(DATA_SCALE, 0, 'xsd:integer', 'xsd:double') as "type",
decode(NULLABLE, 'Y', 0, 1) as "minOccurs",
column_name as "xdb:SQLName",
DATA_TYPE as "xdb:SQLTYPE"
)
)
else
xmlElement
(
"xsd:element",
xmlattributes
(
column_name as "name",
'xsd:anySimpleType' as "type",
decode(NULLABLE, 'Y', 0, 1) as "minOccurs",
column_name as "xdb:SQLName",
DATA_TYPE as "xdb:SQLTYPE"
)
)
end ELEMENT
from user_tab_cols c
where TABLE_NAME = target_table
order by internal_column_id
)
group by TABLE_NAME;

return xmlSchema;
end;


Run it as below ::

select GENERATE_XSD('EMPLOYEE_SALARY_TABLE').extract('/*') from dual;

Dont forget to put '.extract('/*')' before from dual.



Cheers,

Ujjwal Soni

5 comments:

Unknown said...

Hi...thanks for the post..I just need to do the opposite. I've a XML schema(.xsd) file and need to create Oracle tables from to match the XML schema. How can I do that?

Thanks

Unknown said...

Send me ur email address, i can send you a document for that.

Anshuman said...

Hi, i have an xsd and an xml. can i associate the xsd with the xml So tht i can view the values in the xml arranged in columns. my mail id is anshisansh@gmail.com . Please mail me the procedure

Unknown said...

I've a XML schema(.xsd) file and need to create Oracle tables from to match the XML schema. How can I do that?

Unknown said...

OK.. I need exactly what this function i saying it produces, but when I run this function in SQL Developer, I get an error "ORA-22275: invalid LOB locator specified". This table does not have any LOBs, so I am guessing it is talking about the output. Any ideas?

best-it-exam-    | for-our-work-    | hottst-on-sale-    | it-sale-    | tast-dumps-us-    | test-king-number-    | pass-do-it-    | just-do-it-    | pass-with-us-    | passresults-everything-    | passtutor-our-dumps-    | realtests-us-exam-    | latest-update-source-for-    | cbtnuggets-sale-exam    | experts-revised-exam    | certguide-sale-exam    | test4-sale-exam    | get-well-prepared-    | certkiller-sale-exam    | buy-discount-dumps    | how-to-get-prepared-for-the    | in-an-easy-way    | brain-dumps-sale    | with-pass-exam-guarantee    | accurate-study-material    | at-first-try    | 100%-successful-rate    | get-certification-easily    | material-provider-exam    | real-exam-practice    | with-pass-score-guarantee    | certification-material-provider    | for-certification-professionals    | get-your-certification-successfully    | 100%-Pass-Rate    | in-pdf-file    | practice-exam-for    | it-study-guides    | study-material-sku    | study-guide-pdf    | prep-guide-demo    | certification-material-id    | actual-tests-demo    | brain-demos-test    | best-pdf-download    | our-certification-material    | best-practice-test    | leading-provider-on    | this-course-is-about    | the-most-reliable    | high-pass-rate-of    | high-pass-rate-demo    | recenty-updated-key    | only-for-students-free-download    | courseware-plus-kit-for    | accurate-answers-of    | the-most-reliable-id    | provide-training-for    | welcome-to-buy    | material-for-success-pass    | provide-free-support    | best-book-for-pass    | accuracy-of-the-answers    | pass-guarantee-id    | pass-exam-key    | pass-sku-id    | study-for-exid    | pdf-sku-exid    | sku-pdf-id    | pdf-demo-key    | exam-demo-ske    | pass-it-dump    |
http://www.menuiserie-dauvergne.com/    | http://www.menuiserie-dauvergne.com/    |