通过存储过程从oracle表生成XSD文件

时间:2014-05-05 11:59:20

标签: xml xml-parsing xsd plsqldeveloper xsd.exe

我想从存储过程生成oracle表的XSD文件,我已经为它创建了一个函数,但它没有显示正确的表列顺序。

create or replace 
function GEN_XML_SCHEMA1(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;

我创建了上面的函数,但它没有给出正确的架构

示例 - desc AGNCY 名称空类型


AGENCYID NOT NULL CHAR(4 CHAR)
姓名CHAR(60 CHAR) MARKDELETED VARCHAR2(1)

当我运行函数时,它给出了结果

此处表格列顺序已更改。 我已经检查了许多工具或其他解决方案来生成XSD文件,但我需要从存储过程生成。

我也创建了一个存储过程,但是当表有超过40列时它会显示错误 请检查商店程序

create or replace 
PROCEDURE Export_In_XXS1D 
(
   V_TABLE_NAME1 IN varchar2,
   v_FLAG OUT NUMBER
  )
AS
BEGIN
declare
  filehandle1 utl_file.file_type;
  v_FILENAME varchar2(50);
  V_TABLE_NAME varchar2(50);
   result clob;
BEGIN

     V_TABLE_NAME :=trim(UPPER(V_TABLE_NAME1));   

  filehandle1 := utl_file.fopen ('MYXML', V_TABLE_NAME ||'.xsd', 'w');
    select xmlElement
             (
               "xsd:schema",
               xmlAttributes
              (
                'http://www.w3.org/2001/XMLSchema' as "xmlns:xs",
                'unqualified' as "attributeFormDefault",
                'qualified' as "elementFormDefault"
              ),
              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 as "type",
                       'unbounded' as "maxOccurs"
                     )
                   )
                 )
              ),
              xmlElement
              (
                "xsd:complexType",
                xmlAttributes
                (
                  table_name as "type"
                ),
                xmlElement
                (
                  "xsd:sequence",
                  (
                    xmlAgg(ELEMENT)
                  )
                )
              )
            ).getStringVal() AS result
       into result
       from (select TABLE_NAME,COLUMN_ID,
                    case
                      when DATA_TYPE = 'VARCHAR2' then
                        xmlElement
                        (
                          "xsd:element",
                          xmlattributes
                          (
                            column_name as "name",
                            column_name as "xsd:SQLName",
                            DATA_TYPE as "xsd: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",
                           'xs:dateTime' as "type",
                           column_name as "xsd:SQLName",
                           DATA_TYPE as "xsd:SQLTYPE"
                         )
                       )
                     when DATA_TYPE = 'NUMBER' then
                       xmlElement
                       (
                         "xsd:element",
                         xmlattributes
                         (
                           column_name as "name",
                           'xs:integer' as "type",
                           column_name as "xsd:SQLName",
                           DATA_TYPE as "xsd:SQLTYPE"
                         )
                       )
                     else
                       xmlElement
                       (
                         "xsd:element",
                         xmlattributes
                         (
                           column_name as "name",
                           'xs:anySimpleType' as "type",
                           column_name as "xsd:SQLName",
                           DATA_TYPE as "xsd:SQLTYPE"
                         )
                       )
                   end ELEMENT
              from user_tab_cols c
             where c.TABLE_NAME = V_TABLE_NAME and c.column_id is not null
               order by c.column_id
            )
      group by TABLE_NAME ;
           dbms_xslprocessor.clob2file( result, 'MYXML', ''||V_TABLE_NAME||'.xsd');
            v_FLAG := 1;                           
           UTL_FILE.FCLOSE(filehandle1);


          utl_file.fclose(filehandle1);
       /*  EXCEPTION
               WHEN OTHERS THEN
               DBMS_OUTPUT.PUT_LINE(SQLERRM);
               UTL_FILE.FCLOSE(filehandle1);
               v_FLAG := 0;   */


          END;
end Export_In_XXS1D;

并且错误是

Connecting to the database D1.
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at "SYS.XMLTYPE", line 169
ORA-06512: at "CTIPS.EXPORT_IN_XXS1D", line 18
ORA-06512: at line 7
Process exited.
Disconnecting from the database D1.

请帮助我如何从脚本创建表的xsd文件?

0 个答案:

没有答案