1 2 Previous Next 18 Replies Latest reply: Mar 5, 2014 7:20 AM by Elad RSS

    Multi attribute in xml schema  - in the root element

    Elad

      Hi

      I have several attribute in xml file. all of them  - in the root element.

      I know that I have to put the declaration of the attribute in the  bottom of the schema.

      When I put only one attribute in the schema - everything is OK.

      but I have more than one attribute (I have 8 attributes...), and the I get this error:

      ORA-01460: UNIMPLEMENTED OR UNREASONABLE CONVERSION


      xml file:

      <MyRootElement Myattribute1="MyValue1"

                                   MyAttribute2="MyValue2"

                                   MyAttribute3="MyValue3"

                                   xmlns="http://www.oracle.com/xml/test.xsd"

                                   xmlns="http://www.w3.org/2001/xmlSchema-instance in the beginning of this row it's should be written: "xmlns:xsi" , but this is erased....

                                   xsi:schemaLocation=http://www.oracle.com/xml/test.xsd    my_schema_file.xsd">

       

      xsd file:

      <schema targetNameSpace="http://www.oracle.com/xml/test.xsd"

                     xmlns="http://www.w3.org/2001/xmlSchema"

                     xmlns : xdb = "http://xmlns.oracle.com/xdb"

                     vesion="1.0"

                     elementFormDefault="qualified">

      <element name="MyRootElement">

           <complexType>

                     <sequnce>

                               ----------------

                               ---the rest of the xsd file -------------

                              ---------------------------------------------

                    </sequnce>

                          <attribute  name="MyAttribute1"  type="string"  use="required"

                          <attribute  name="MyAttribute2"  type="string"  use="required"

                   </complexType>

      </element>

      </schema>

       

      Anyone knows what the problem is?

       

      Thanks in advanced,

      Elad

        • 1. Re: Multi attribute in xml schema  - in the root element
          odie_63

          Hi,

           

          In what situation do you get that error?

          What are you trying to do?

          • 2. Re: Multi attribute in xml schema  - in the root element
            Elad

            Hi odie

            Thank-you for you quick replay

            I have a pl/sql code that validate the xml file againest the xsd file.

            • 3. Re: Multi attribute in xml schema  - in the root element
              odie_63

              May I see this code?

               

              What's your database version? (SELECT * FROM v$version)

              • 4. Re: Multi attribute in xml schema  - in the root element
                Elad

                create or replace function schema_xml_333(p_tested_xml varchar2)

                return varchar2

                as

                     l_file_in        utl_file.file_type;

                     l_single_record  varchar2(20000);

                     l_string        varchar2(20000);

                     l_file_schema        utl_file.file_type;

                     l_single_record_schema  varchar2(20000);

                     xml_schema_text  varchar2(20000) ;

                     l_xml_is_valid  number;

                  begin

                   l_file_in := utl_file.fopen('/home/myPath' , p_tested_xml , 'R');

                     loop

                      begin 

                     utl_file.get_line(l_file_in , l_single_record);

                    exception 

                            when no_data_found then

                              utl_file.fclose(l_file_in);

                           exit;

                           end;

                     l_string := l_string || l_single_record ;

                    end loop;

                    l_file_schema := utl_file.fopen('/home/myPath' , 'xml_schema.xsd' , 'R');

                    loop

                      begin

                      utl_file.get_line(l_file_schema , l_single_record_schema);

                    exception

                              when no_data_found then 

                            utl_file.fclose(l_file_schema);

                           exit; 

                          end;

                    xml_schema_text := xml_schema_text || l_single_record_schema ;

                    end loop;

                     begin

                  dbms_xmlschema.registerschema(schemaurl => 'xml_schema.xsd'

                                              , schemadoc => xml_schema_text

                                               , local    => true 

                                             , genTypes  => false 

                                             , genbean  => false

                                              , gentables => false);

                  exception

                    when others then

                      null;

                  end;   

                  select xmltype.createxml(l_string,'xml_schema.xsd').createschemabasedxml('xml_schema.xsd').isschemavalid('xml_schema.xsd') is_valid   into l_xml_is_valid   from dual;

                     if l_xml_is_valid = 1 then

                    return ('Schema is Valid');

                   else

                     return ('Schema is NOT Valid');

                  end if;

                  end schema_xml_333;

                 

                I use this version: Oracle Database 11g Enterprise Edition Release 11.2.0.1.0

                • 5. Re: Multi attribute in xml schema  - in the root element
                  odie_63

                  Thanks.

                   

                  Ok... where to begin? Almost everything is wrong or at least very awkward

                   

                  1) Don't use physical file path with UTL_FILE, this has been obsolete for several years now. Use directory objects instead.

                  And actually, you don't even need UTL_FILE here.

                   

                  2) The XML schema should be registered once, in a separate script.

                  Or do you expect it to change so often that you have to include the registration part in the procedure?

                   

                  3) If your input XML document has an xsi:schemaLocation attribute, make sure you register the schema using the same URL.

                  In your example, there's a mismatch : "my_schema_file.xsd" vs. "xml_schema.xsd" in the register command.

                   

                   

                  So, to summarize :

                   

                  Create a directory :

                  create or replace directory xml_dir as '/home/myPath';

                   

                  Register the schema (once) :

                  dbms_xmlschema.registerschema(

                    schemaurl => 'my_schema_file.xsd'

                  , schemadoc => xmltype(

                                   bfilename('XML_DIR','<schema filename here>')

                                 , nls_charset_id('AL32UTF8') -- use the real encoding of the file here

                                 )

                  , local     => true

                  , genTypes  => false

                  , gentables => false

                  , enableHierarchy =>  dbms_xmlschema.enable_hierarchy_none

                  );

                   

                  Then, here's how I would rewrite the function :

                  create or replace function schema_xml_333( p_tested_xml in varchar2 )

                  return varchar2

                  is

                   

                    doc  xmltype := xmltype(bfilename('XML_DIR', p_tested_xml), nls_charset_id('AL32UTF8'));

                   

                  begin

                   

                    return case when doc.isSchemaValid() = 1

                                then 'XML is valid'

                                else 'XML is not valid'

                           end;

                  end;

                  /

                  Again, this will work if the XML contains an xsi:schemaLocation attribute whose schema URL part matches the URL used for registration.

                  In this case, you don't need the createSchemaBasedXML method.

                  • 6. Re: Multi attribute in xml schema  - in the root element
                    Elad

                    Thanks, Odie !!!

                    I will try your solution, but what about the attributes?

                    Do I need to use attributeGroup?

                    If so , what the location in the schema for this?

                     

                    Elad

                    • 7. Re: Multi attribute in xml schema  - in the root element
                      odie_63

                      I will try your solution, but what about the attributes?

                      My guess is that the error you're receiving has nothing to do with attributes, but more with the size of the document and how you were reading it.

                       

                      Which line of your code raised the error?

                       

                      Anyway, try the new version and let me know, thanks.

                      • 8. Re: Multi attribute in xml schema  - in the root element
                        Elad

                        Hi odie

                        I have several question on your solution:

                        1) general question - why I can't do copy/paste here? Is this a private problem, only at me...?

                        2)schemurl => do I need to put here the xsd name? like "xml_schema.xsd"? but I put it also in schemadoc=>

                          (where you write <'schema filename here'>).

                        3) if I write the registerschema in separate procedure, I need to call it from the function and I need to ask if the schema is already exist in the DB. how to write it?

                        I don't find a function like IsExist function refer to xsd...

                         

                        Thanks,

                        Elad

                        • 9. Re: Multi attribute in xml schema  - in the root element
                          odie_63

                          Hi Elad,

                           

                          1) general question - why I can't do copy/paste here? Is this a private problem, only at me...?

                          Dunno.

                          There are still some pending issues related to forum migration last year.

                          You can report them here : List of forum editor issues and bugs

                           

                          2)schemurl => do I need to put here the xsd name? like "xml_schema.xsd"? but I put it also in schemadoc=>

                            (where you write <'schema filename here'>).

                          schemaUrl parameter must contain the URL you want to use to identify the schema in the database.

                          In particular, if your input XML instance document possesses a xsi:schemaLocation attribute, e.g.

                          xsi:schemaLocation="http://www.oracle.com/xml/test.xsd my_schema_file.xsd"

                          then you have to register the schema using the same URL part : schemaUrl => 'my_schema_file.xsd'

                          Important : the URL has nothing to do with the schema's filename (if you actually register it from the filesystem, see below).

                           

                          schemaDoc parameter is the content of the schema itself, it could be VARCHAR2, CLOB, BLOB, BFILE.

                          In my example, I used the BFILE overload :

                          xmltype(

                            bfilename('XML_DIR','<schema filename here>')

                          , nls_charset_id('AL32UTF8') -- use the real encoding of the file here

                          )

                          where '<schema filename here>' is the name of the schema file in the filesystem (in the directory XML_DIR).

                           

                           

                          3) if I write the registerschema in separate procedure, I need to call it from the function and I need to ask if the schema is already exist in the DB. how to write it?

                          I don't find a function like IsExist function refer to xsd...

                          No, you register the schema once, in a PL/SQL block (no need of a procedure), I thought I already explain that...

                          Schema registration is like DDL, it creates a persistent object (or a set of objects, depends on the options), just like you would create a table in some deployment script.

                          So you don't have to recreate the schema every single time you want to use it, unless you want to evolve it because the expected XML structure has changed - that was one of my previous questions which you didn't care to answer :

                          Or do you expect it to change so often that you have to include the registration part in the procedure?

                          • 10. Re: Multi attribute in xml schema  - in the root element
                            Elad

                            Hi Odie,

                            You are right. I don't need the schema more than once.

                            1) in the first time, I wrote mistake in the xml file. It should be:    xsi:schemaLocation="http://www.oracle.com/xml/test1.xsd  xml_scema.xsd

                            and not my_scema_file.xsd  . it's ok to write xml_schema.xsd either in schemaurl and in schemadoc?

                            2)in my xml file I don't have in the top of the file: <?xml version="1.0" encoding="utf-8"?>  if I do write it, I get an error.

                            so what I should to write in: nls_charset_id() ?

                            3) another question: I have an element in my schema which get only true or false value.

                                if it's "true" the xml file has certain stracture with a particular element, and if it's "false", the file has diffrent structure, with another element.

                                is there IF condition in xsd (or something similar) to solve this problem?

                             

                            thanks,

                            Elad

                            • 11. Re: Multi attribute in xml schema  - in the root element
                              odie_63

                              1) in the first time, I wrote mistake in the xml file. It should be:    xsi:schemaLocation="http://www.oracle.com/xml/test1.xsdxml_scema.xsd

                              and not my_scema_file.xsd  . it's ok to write xml_schema.xsd either in schemaurl and in schemadoc?

                              Use schemaUrl => 'xml_schema.xsd'.

                              If the XSD file in the directory happens to be named the same (but it doesn't have to), then yes, reference that name as well in the schemaDoc parameter.

                               

                               

                              2)in my xml file I don't have in the top of the file: <?xml version="1.0" encoding="utf-8"?>  if I do write it, I get an error.

                              so what I should to write in: nls_charset_id() ?

                              You're still talking about the XSD file, right? What error do you get?

                              What is the encoding of the XSD file? UTF-8, ISO-8859-1, ... ?

                              Once you've found the real encoding used for the file, pass it to the NLS_CHARSET_ID function.

                              For example :

                              UTF-8 encoding --> nls_charset_id('AL32UTF8')

                              ISO-8859-1 --> nls_charset_id('WE8ISO8859P1')

                               

                              3) another question: I have an element in my schema which get only true or false value.

                                  if it's "true" the xml file has certain stracture with a particular element, and if it's "false", the file has diffrent structure, with another element.

                                  is there IF condition in xsd (or something similar) to solve this problem?

                              No, XML Schema doesn't support that (yet).

                              You have to define two different complexTypes (one for true, one for false) and use xsd:choice.

                              Post an example of what you're trying to achieve, I'll try to show you how to do it in XSD.

                              • 12. Re: Multi attribute in xml schema  - in the root element
                                Elad

                                Thanks Odie !!!

                                here is an example to my problem:

                                I have an element APPROVED

                                case 1: APPROVED=TRUE

                                <INFO>

                                     <TYPE>0</TYPE> -- this element have to be shown and the value have to be zero.

                                </INFO>

                                <RESULR>

                                     <APPROVED>true</APPROVED>

                                            <AUTH>88</AUTH> -- this element have to be shown

                                            <AMOUNT>58.64</AMOUNT>  --  this element have to be shown and the value have to be differnent from zero

                                </RESULT>

                                 

                                case 2: APPROVED=FALSE

                                <INFO>

                                      <TYPE>3</TYPE>  -- this element have to be shown and the value have to be different from zero.

                                <INFO>

                                <RESULT>

                                     <APPROVED>false</APPROVED>

                                            <REJECT>777</REJECT> --this element haveto be shown

                                           <AMOUNT>0.00</AMOUNT>  -- this element have to be show and the value have to be zero.

                                </RESULT>

                                • 13. Re: Multi attribute in xml schema  - in the root element
                                  odie_63

                                  Are those two fragments supposed to be wrapped in the same element?

                                  e.g.

                                  <CONTENT>

                                    <INFO> ... </INFO>

                                    <RESULT> ... </RESULT>

                                  </CONTENT>

                                   

                                  Do you have any latitude in the XML structure or is it fixed?

                                  • 14. Re: Multi attribute in xml schema  - in the root element
                                    Elad

                                    my mistake: INFO element is wrap also the REJECT element (REJECT is under INFO)

                                    2) Do you mean latitude on order and location?  NO, I don't have this latitude. The elements needs to be in a particular order.

                                    1 2 Previous Next