{"id":1772,"date":"2010-08-26T10:52:01","date_gmt":"2010-08-26T15:52:01","guid":{"rendered":"http:\/\/jianmingli.com\/wp\/?p=1772"},"modified":"2010-09-23T12:55:56","modified_gmt":"2010-09-23T17:55:56","slug":"generate-xml-schema-from-oracle-tables","status":"publish","type":"post","link":"https:\/\/jianmingli.com\/wp\/?p=1772","title":{"rendered":"Generate XML Schema from oracle tables"},"content":{"rendered":"<p><a href=\"http:\/\/forums.oracle.com\/forums\/thread.jspa?messageID=1515908\">Here <\/a>is a nice thread describing how to generate XML schema from Oracle tables. I tried the following function and it worked great.<\/p>\n<p>* Compile the following PL\/SQL function:<\/p>\n<pre lang=\"sql\">\r\nCREATE OR REPLACE function GEN_XML_SCHEMA(target_table varchar2) return xmltype\r\nas\r\n  xmlSchema XMLTYPE;\r\n  currUser VARCHAR2(128);\r\nbegin\r\n  select sys_context('USERENV','CURRENT_USER') \r\n  into currUser\r\n  from dual;\r\n  \r\n  select\r\n    -- Begin schema\r\n    xmlElement(\r\n      \"xsd:schema\",\r\n      xmlAttributes(\r\n        'http:\/\/www.w3.org\/2001\/XMLSchema' as \"xmlns:xsd\",\r\n        'http:\/\/xmlns.oracle.com\/xdb' as \"xmlns:xdb\",\r\n        'http:\/\/' || target_table || '\/xsd' as \"targetNamespace\",\r\n        'http:\/\/' || target_table || '\/xsd' as \"xmlns:tns\",\r\n        'qualified' as \"elementFormDefault\"\r\n      ),\r\n      -- Base type\r\n      xmlElement(\r\n        \"xsd:complexType\",\r\n        xmlAttributes(\r\n          currUser || '.Base_T' as \"name\"\r\n        )\r\n      ),\r\n      -- Element\r\n      xmlElement(\r\n        \"xsd:element\",\r\n        xmlAttributes(\r\n          currUser || '.' || target_table || 'S' as \"name\",\r\n          'tns:' || currUser || '.' || target_table || 'S_T' as \"type\"\r\n        )\r\n      ),\r\n      -- Rows type to hold multiple rows\r\n      xmlElement(\r\n        \"xsd:complexType\",\r\n        xmlAttributes (\r\n          currUser || '.' || target_table || 'S_T' as \"name\"\r\n        ),\r\n        -- Multipe rows\r\n        xmlElement (\r\n          \"xsd:sequence\",\r\n          xmlElement (\r\n            \"xsd:element\",\r\n            xmlAttributes (\r\n              currUser || '.' || target_table as \"name\",\r\n              'tns:' || currUser || '.' || target_table || '_T' as \"type\",\r\n              'unbounded' as \"maxOccurs\"\r\n            )\r\n          )\r\n        )\r\n      ),\r\n      -- Row type to hold single row\r\n      xmlElement (\r\n        \"xsd:complexType\",\r\n        xmlAttributes (\r\n          currUser || '.' || target_table || '_T' as \"name\"\r\n        ),\r\n        xmlElement(\r\n            \"xsd:complexContent\",\r\n            xmlElement(\r\n                \"xsd:extension\",\r\n                xmlAttributes (\r\n                  'tns:' || currUser || '.Base_T' as \"base\"\r\n                ),\r\n                -- Individual columns\r\n                xmlElement (\r\n                  \"xsd:sequence\",\r\n                  (\r\n                    xmlAgg(ELEMENT)\r\n                  )\r\n                )\r\n            )\r\n        )\r\n      )\r\n    )\r\n  into xmlSchema\r\n  from (\r\n    select INTERNAL_COLUMN_ID,\r\n      case\r\n        when DATA_TYPE in ('VARCHAR2', 'CHAR') then\r\n          xmlElement (\r\n            \"xsd:element\",\r\n            xmlattributes (\r\n              column_name as \"name\",\r\n              decode(NULLABLE, 'Y', 0, 1) as \"minOccurs\",\r\n              column_name as \"xdb:SQLName\",\r\n              DATA_TYPE as \"xdb:SQLTYPE\"\r\n            ),\r\n            xmlElement (\r\n              \"xsd:simpleType\",\r\n              xmlElement (\r\n                \"xsd:restriction\",\r\n                xmlAttributes (\r\n                  'xsd:string' as \"base\"\r\n                ),\r\n                xmlElement (\r\n                  \"xsd:maxLength\",\r\n                  xmlAttributes (\r\n                  DATA_LENGTH as \"value\"\r\n                  )\r\n                )\r\n              )\r\n            )\r\n          )\r\n        when DATA_TYPE = 'DATE' then\r\n          xmlElement (\r\n            \"xsd:element\",\r\n            xmlattributes (\r\n              column_name as \"name\",\r\n              --'xsd:dateTime' as \"type\",\r\n              'xsd:date' as \"type\",\r\n              decode(NULLABLE, 'Y', 0, 1) as \"minOccurs\",\r\n              column_name as \"xdb:SQLName\",\r\n              DATA_TYPE as \"xdb:SQLTYPE\"\r\n            )\r\n          )\r\n        when DATA_TYPE = 'NUMBER' then\r\n          xmlElement (\r\n            \"xsd:element\",\r\n            xmlattributes (\r\n              column_name as \"name\",\r\n              decode(DATA_SCALE, 0, 'xsd:integer', 'xsd:double') as \"type\",\r\n              decode(NULLABLE, 'Y', 0, 1) as \"minOccurs\",\r\n              column_name as \"xdb:SQLName\",\r\n              DATA_TYPE as \"xdb:SQLTYPE\"\r\n            )\r\n          )\r\n        else\r\n          xmlElement (\r\n            \"xsd:element\",\r\n            xmlattributes (\r\n              column_name as \"name\",\r\n              'xsd:anySimpleType' as \"type\",\r\n              decode(NULLABLE, 'Y', 0, 1) as \"minOccurs\",\r\n              column_name as \"xdb:SQLName\",\r\n              DATA_TYPE as \"xdb:SQLTYPE\"\r\n            )\r\n          )\r\n      end ELEMENT\r\n    from user_tab_cols c\r\n    where TABLE_NAME = target_table\r\n    order by internal_column_id\r\n  );\r\n  \r\n  return xmlSchema;\r\nend;\r\n\/<\/pre>\n<p>* Run the function:<\/p>\n<pre lang=\"sql\">\r\n-- Complete schema\r\nselect gen_xml_schema('MY_TABLE').extract('\/*') from dual;\r\n\r\n-- Select element and complexType\r\nSELECT gen_xml_schema('RF_TAG_TXN').extract('\/schema\/element | \/schema\/complexType', \r\n'xmlns=\"http:\/\/www.w3.org\/2001\/XMLSchema\"') FROM dual;\r\n<\/pre>\n<p>* Generate schema from multiple tables:<\/p>\n<pre lang=\"sql\">\r\nCREATE or REPLACE DIRECTORY dir_temp AS \r\n    'C:\\TEMP';\r\n\r\nDECLARE\r\n  TYPE tableNameArray IS varray(100) of VARCHAR2(64);\r\n  tableNames tableNameArray;\r\n  \r\n  outputFileName VARCHAR2(100) := 'MY_SCHEMA.xsd';\r\n  outputFileHandler UTL_FILE.file_type;\r\n  \r\n  tableXmlType xmlType;\r\n  xmlRootElementBegin VARCHAR2(500) := \r\n    '<xsd:schema xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\" \r\n    xmlns:xdb=\"http:\/\/xmlns.oracle.com\/xdb\"\r\n    targetNamespace=\"http:\/\/my.db\/xsd\"\r\n    xmlns:tns=\"http:\/\/my.db\/xsd\" \r\n    elementFormDefault=\"qualified\">';\r\n  xmlRootElementEnd VARCHAR2(500) := '<\/xsd:schema>';\r\nBEGIN\r\n  -- Database tables\r\n  tableNames := tableNameArray(\r\n    'TABLE1',\r\n    'TABLE2');\r\n\r\n  -- Open schema file to write\r\n  outputFileHandler := UTL_FILE.fopen('DIR_TEMP', outputFileName, 'W');\r\n  -- Write root element begin tag\r\n  UTL_FILE.put_line(outputFileHandler, xmlRootElementBegin);\r\n\r\n  -- Loop through all tables\r\n  FOR tblName IN 1 .. tableNames.count loop\r\n    DBMS_OUTPUT.put_line('Processing table: ' || tableNames(tblName));\r\n    \r\n    -- Generate xml schema from each table\r\n    SELECT gen_xml_schema(tableNames(tblName)).extract(\r\n        '\/schema\/element|\/schema\/complexType',\r\n        'xmlns=\"http:\/\/www.w3.org\/2001\/XMLSchema\"') \r\n    INTO tableXmlType\r\n    FROM dual;\r\n    \r\n    -- Write xml schema\r\n    IF tableXmlType IS NOT NULL THEN\r\n      UTL_FILE.put_line(outputFileHandler, tableXmlType.getStringVal());\r\n    END IF;\r\n  END loop;\r\n\r\n  -- Write root element end tag\r\n  UTL_FILE.put_line(outputFileHandler, xmlRootElementEnd);\r\n  -- Close file\r\n  UTL_FILE.fclose(outputFileHandler);\r\nEXCEPTION\r\n  WHEN OTHERS THEN\r\n    DBMS_OUTPUT.put_line (DBMS_UTILITY.FORMAT_ERROR_STACK);\r\nEND;\r\n\/<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Here is a nice thread describing how to generate XML schema from Oracle tables. I tried the following function and it worked great. * Compile the following PL\/SQL function: CREATE OR REPLACE function GEN_XML_SCHEMA(target_table varchar2) return xmltype as xmlSchema XMLTYPE; &hellip; <a href=\"https:\/\/jianmingli.com\/wp\/?p=1772\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_exactmetrics_skip_tracking":false,"_exactmetrics_sitenote_active":false,"_exactmetrics_sitenote_note":"","_exactmetrics_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[35],"tags":[],"class_list":["post-1772","post","type-post","status-publish","format-standard","hentry","category-oracle"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p8cRUO-sA","_links":{"self":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1772","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1772"}],"version-history":[{"count":9,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1772\/revisions"}],"predecessor-version":[{"id":1813,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=\/wp\/v2\/posts\/1772\/revisions\/1813"}],"wp:attachment":[{"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1772"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1772"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jianmingli.com\/wp\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1772"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}