简体   繁体   English

如何参考给定的XSD在ORACLE 11g中生成XML?

[英]How to generate XML in ORACLE 11g with reference to given XSD?

I am learning to generate XML in Oracle 11g PL/SQL language. 我正在学习以Oracle 11g PL / SQL语言生成XML。

I would like to generate XML depending on the data I get from the db tables, but at same time it should follow the structure and validate the XML with reference to the XSD I am provided with. 我想根据从db表中获得的数据生成XML,但同时它应遵循结构并参照提供的XSD验证XML。

I have registered the XSD schema through dbms_xmlschema.registerSchema and generated the XML through dbms_xmlquery.getXml using dynamic script and then validated it by using XMLTYPE.schemaValidate . 我已经注册过的XSD架构dbms_xmlschema.registerSchema并产生通过XML dbms_xmlquery.getXml使用动态脚本,然后通过验证它XMLTYPE.schemaValidate

But in my process the flow is creating XML first and then validating it with XSD, so due to this I may not be able to get correct structure and need to work on structure while generating XML. 但是在我的过程中,流程是先创建XML,然后使用XSD对其进行验证,因此,由于这个原因,我可能无法获得正确的结构,并且需要在生成XML时对结构进行研究。

Is there any way I can generate the XML by referencing the XSD I stored so that I don't miss out on structure and the XML only gets created if the result of referencing is valid? 有什么方法可以通过引用存储的XSD来生成XML,以便我不会错过结构,并且仅在引用结果有效的情况下才能创建XML?

You can't really generate xml based on a schema. 您不能真正基于模式生成xml。 The best is probably to generate the xml into an xmltype variable and then validate this against the schema. 最好的办法可能是将xml生成为xmltype变量,然后根据模式进行验证。

You can use the following plsql block (to be run as a script) as a starting point. 您可以使用以下plsql块(作为脚本运行)作为起点。 Alter the query to use xml functions (XMLFOREST, XMLELEMENT, XMLATTRIBUTES) to generate the xmltype variable. 更改查询以使用xml函数(XMLFOREST,XMLELEMENT,XMLATTRIBUTES)来生成xmltype变量。

Once you have this inside l_xml you should be able to validate it against your schema. 一旦在l_xml中拥有了此名称,您就应该能够根据您的模式对其进行验证。 (Then I presume report the error if you need to or continue and process the xml as required) (然后,我假设您需要报告该错误,或​​者根据需要继续处理xml)

My sample just pretty prints the xml and puts it in dbms output. 我的示例仅打印了xml并将其放入dbms输出中。

set serveroutput on;
declare
  l_error varchar2(4094);
  --
  l_xml xmltype;
  l_tmp varchar2(4096);  
begin
  dbms_output.put_line('Start');

  --generate variable from query
  select xmlelement("this:root"
      ,xmlattributes(
        'http://www.w3.org/2001/XMLSchema-instance' as "xmlns:xsi",
        'http://ic.ac.uk.relationshipvisualiser.app.data.InputData' as "xmlns:this"
      )
      ,xmlelement("this:subelement"
        ,'subelementValue'
      )
      ,xmlelement("this:subelement2"
        ,'subelementValue2'
      )
    )
  into l_xml
  from dual;  

  --todo Write code to validate l_xml against xsd schema

  --now output xml
  select XMLSERIALIZE(Document l_xml as CLOB INDENT SIZE = 2)
  into l_tmp
  from dual;
  dbms_output.put_line(l_tmp);


  dbms_output.put_line('End');
EXCEPTION WHEN OTHERS THEN
  l_error := NVL(SUBSTR(SQLERRM,0,990),'NULL');
  dbms_output.put_line(l_error || ':-' || NVL(SUBSTR(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE,0,3000),'NULL')); 
end;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM