Uniface User Forum

 View Only
Expand all | Collapse all

XML read and validation with xmltostruct

  • 1.  XML read and validation with xmltostruct

    Posted 09-20-2024 03:15

    Hi,

    in our application I need to implement reading xml from the folder and then I need to check if the xml is correct. I found the xmlToStruct command. Do you have any example how to use it? First of all what is the best way to read xml and then how the xmltostruct should be used with the xsd schema which is local file in my folder. 

    My xml files will be some where between 1MB to 30MB.

    Are there any other better (more efficient) ways how to do it? 

    Is it possible to use 3rd party libraries (C, C++, C#, Java) for this goal? If so do you have experience which one is ok and how it can be used in Uniface?

    BR,

    Jozef



    ------------------------------
    Jozef Prokopovic
    Uniface Community Edition Shared Account
    US
    ------------------------------


  • 2.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 06:00

    Hi Jozef

    What about UXMLREADER,UXMLWRITER from UnifAce?

    About XmlToStruct:
       xmlToStruct/full/validate/schema vy_STRUCT, vx_XML,v_UF_SCHEMA_LST
    Where
      vy_STRUCT is the to be created structure
      vx_XML the XML which should be convertet
      v_UF_SCHEMA_LST is a schema list as an uniface item list

    Ingo



    ------------------------------
    Ingo Stiller
    Aareon Deutschland GmbH
    ------------------------------



  • 3.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 06:53
    Edited by Jozef Prokopovic 09-20-2024 07:25

    Yes, I would like to but do you have any example how to use it?

    I have tried this part of code but it looks like the xsd is not recognized I am getting -1504 error:

       v_data_tmp is string
       lfileload "path_to_file\file_name.xml", v_data_tmp                  (I see data loaded to v_data_tmp)
       xmlToStruct /validate v_struct, v_data_tmp, "file.xsd"             (-1504 error : Unknown element 'PRODUCTIE')
    But I do have the element PRODUCTIE defined in the xsd. Where should I store the xsd file if it is only one file and I do not want to use the reference. In the help it is saying that I can place it into working folder. Is that enough? Is it maybe some issue in my xsd file?
      <xs:element name="PRODUCTIE">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="PAKKETCODE" type="PakketCode"/>
            <xs:element name="KLANTNUMMER" type="KlantNummer"/>



    ------------------------------
    Jozef Prokopovic
    Uniface Community Edition Shared Account
    US
    ------------------------------



  • 4.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 07:48

    Hi Jozef

    The last parameter is not a XSD-File/text but a schema list :-)

    Here is my NameSpaceInfo itemlist which a helper compent returns me:


    ALIAS=appl·;

    NAMESPACE=urn:applname_xyz:xsd:gen.gen.01.20160501·;

    LOCATION=~gen.gen.01.20160501.p11868.xsd·;

    REF_NS_LST=!APPL*TYPES*ALL*01*20160501=ALIAS=applt·!·!·;
        NAMESPACE=urn:applname_xyz:xsd:types.all.01.20160501·!·!·;
        LOCATION=~types.all.01.20160501.p11868.xsd·;

    ALIAS_LST=APPL=urn:applname_xyz:xsd:gen.gen.01.20160501·!·;
      applt=urn:applname_xyz:xsd:types.all.01.20160501·;

    UF_SCHEMA_LST=NAMESPACE=urn:applname_xyz:xsd:gen.gen.01.20160501·!·!·;
        LOCATION=~gen.gen.01.20160501.p11868.xsd·!·;
      NAMESPACE=urn:applname_xyz:xsd:types.all.01.20160501·!·!·;
        LOCATION=~types.all.01.20160501.p11868.xsd·;

    XSI_SCHEMALOCATION=urn:applname_xyz:xsd:gen.gen.01.20160501 ~gen.gen.01.20160501.p11868.xsd urn:applname_xyz:xsd:types.all.01.20160501 ~types.all.01.20160501.p11868.xsd

    As we do have our own SOAP-interface with XML-payload in different versions and with nested namespaces, it loolk on first sight a litte bit scary.
    UnifAce is not able do handle XSD inline/in memory, so the XSD-Files has to be writte to some place to the hard drive.
    Our XSD-File do have the name as the schema plus a tilde in front (like othe temp files)

    The UF_SCHEMA_LST is then to be taken as third parameter of 
     xmlToStruct/full/validate/schema vy_STRUCT, vx_XML,v_UF_SCHEMA_LST
    Uniface interprets the XML and reads the namespace alias<->namespace assigment.
    Then it loads the XSD by the location-string and does the validation

    Ingo



    ------------------------------
    Ingo Stiller
    Aareon Deutschland GmbH
    ------------------------------



  • 5.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 07:53

    BTW: this is the header of such XSD
    Note the IMPORT instruction (if you have one), the filename must also include tilde here

    <xs:schema elementFormDefault="qualified"
        targetNamespace="urn:applname_xyz:xsd:gen.gen.01.20160501"
        xmlns:appl="urn:applname_xyz:xsd:gen.gen.01.20160501"
        xmlns:applt="urn:applname_xyz:xsd:types.all.01.20160501"
        xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xs:import namespace="urn:applname_xyz:xsd:types.all.01.20160501"
            schemaLocation="~types.all.01.20160501.p11868.xsd" />
    ...

    .



    ------------------------------
    Ingo Stiller
    Aareon Deutschland GmbH
    ------------------------------



  • 6.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 08:04

    And a hint:
    Namespaces and alias are no more than 'sound and smoke' 
    There is no technical reason to use a real URN, you can use "Hugo" and "Elisabeth" as names. you should also not rely on the fact that the aliases (<alias>+": "+<element_name>) are always provided as you think they are. If you find an alias, look in the schema definition, get the namespace name there and then use it in lookup tables or in the location statement to find the corresponding XSD description.
    But be careful when writing with aliases. Not all recipients can handle the official way of arbitrary aliases and require very special aliases.
    This is actually wrong, but what can you do if there is an authority or a large customer on the other side :-)

    Ingo



    ------------------------------
    Ingo Stiller
    Aareon Deutschland GmbH
    ------------------------------



  • 7.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 08:44

    to be honest I am lost :) I found this text in help: 

    • SchemaList-namespace and location of one or more schemas. For a single schema, SchemaList can be a file name, URL, or a Uniface index list. For multiple schemas, it must be a Uniface list of name-value pairs in the format:

    so I was thinking that it is enough to enter the schema name and place it in the right location



    ------------------------------
    Jozef Prokopovic
    Uniface Community Edition Shared Account
    US
    ------------------------------



  • 8.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 09:13

    Just
    a) write the XSD-File into the working-directory (on a harddrive)

    b) check if the in the XML-File on top there is a line with your namespace

    <xs:schema targetNamespace="name_of_the_namespace"  xmlns:my_alias="name_of_the_namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" >

    c) make a UIL (UnifAce-Item-List)
    $1 =""
    putitem/id $1,"NAMESPACE" ,"name_of_the_namespace"
    putitem/id $1,"LOCATION"  ,"name_of_the_XSD_file_on_a_drive.xsd"

    d) call XmlToStruct:

       xmlToStruct/full/validate/schema vy_STRUCT, vx_XML,$1



    ------------------------------
    Ingo Stiller
    Aareon Deutschland GmbH
    ------------------------------



  • 9.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 09:37

    a) write the XSD-File into the working-directory (on a harddrive)

           which one is the working directory? the one where I start the application from?

    b) check if the in the XML-File on top there is a line with your namespace

    <xs:schema targetNamespace="name_of_the_namespace"  xmlns:my_alias="name_of_the_namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" >

    so the xml needs to contain this line? What is the Namespace? Can I just add random name for namespace? How the uniface will know that this namespace is the one where is the xsd file located?

    the c and d section is clear.



    ------------------------------
    Jozef Prokopovic
    Uniface Community Edition Shared Account
    US
    ------------------------------



  • 10.  RE: XML read and validation with xmltostruct

    Posted 09-20-2024 09:50

    ad a) check out the article in the docu about workdir
    If you are on windows and start you application by shortcut,  the second line in the shorctcut properties is the workdir (I do have onyl a german environment, so I'm not sure how it is labeled :-) ) 
    You can set also the workind directory by /dir=

    ad b)
    If you don't have specific namespaces just use 'targetnamespace'
    <xs:schema targetNamespace="name_of_the_namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    The name is any thing you like, but it must match the name in the UF-SchemaList

    Here is an example with the alias "h" for a namespace

    <h:table>
      <h:tr>
        <h:td>Apples</h:td>
        <h:td>Bananas</h:td>
      </h:tr>
    </h:table>

    <f:table>
      <f:name>African Coffee Table</f:name>
      <f:width>80</f:width>
      <f:length>120</f:length>
    </f:table>


    ------------------------------
    Ingo Stiller
    Aareon Deutschland GmbH
    ------------------------------



  • 11.  RE: XML read and validation with xmltostruct

    Posted 09-24-2024 02:20
    Edited by Jozef Prokopovic 09-24-2024 02:42

    >ad a) check out the article in the docu about workdir
    >If you are on windows and start you application by shortcut,  the second line in the shorctcut properties is the workdir (I do have onyl a german environment, >so I'm not sure how it is labeled :-) ) 
    >You can set also the workind directory by /dir=

            Is it possible to place the xsd somewhere else then to workdir? For example in the uar file with folder XSD? Do I have then to add redirection in the asn file?

    >If you don't have specific namespaces just use 'targetnamespace'
    ><xs:schema targetNamespace="name_of_the_namespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    >The name is any thing you like, but it must match the name in the UF-SchemaList

        

        The namespace is just a name as far as I understand, but I still do not understand how the Uniface will pair the right xsd to validated xml when the NAMESPACE is just a name and LOCATION is only a name of the file and not the path.

    putitem/id $1,"LOCATION"  ,"name_of_the_XSD_file_on_a_drive.xsd" ; or is it the path to the file?

    I also understand that the XML-File needs to contain the line on the top with the namespace. 

    Let's say I have this xml:

    <?xml version="1.0" encoding="ISO8859-1"?>
    <PRODUCTIE>
     <CODE>1234</CODE>
    </PRODUCTIE>

    so I will add the line 

    <?xml version="1.0" encoding="ISO8859-1"?>

    <xs:schema targetNamespace="MyApp"  xmlns:my_alias="MyApp" xmlns:xs="http://www.w3.org/2001/XMLSchema" >

    <PRODUCTIE>
     <CODE>1234</CODE>
    </PRODUCTIE>

    MySchema.xsd contains this on the top:

    <?xml version="1.0" encoding="ISO8859-1"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">

    Do I have to update also the xsd then?

    And the next step would be to place it in the xsd folder of the uar zip file.

    next step:

    $1 =""
    putitem/id $1,"NAMESPACE" ,"MyApp"
    putitem/id $1,"LOCATION"  ,"MySchema.xsd"

    xmlToStruct/full/validate/schema vy_STRUCT, vx_XML,$1

    is that correct?
    ------------------------------
    Jozef Prokopovic
    Uniface Community Edition Shared Account
    US
    ------------------------------



  • 12.  RE: XML read and validation with xmltostruct

    Posted 09-24-2024 08:55

    Hi Jozef

    For sure you can use fulliy qualified file names :-)
    We do have a LOGICAL-variable in the ASN-File
    SOAP_XSD=X:\path_to_xsd\ 
    If this "variable"is set, all/the XSD are/is placed into this directory (beforeXML2STRUCT) .
    In the UF-schema-list, the LOCATION points the to a specific XSD-file in this directory
      NAMESPACE=Hugo
      LOCATION=X:\path_to_xsd\MySchema.XSD

    In front of all elements (like  "<elementname/>) you place thislime
    <xs:schema targetNamespace="Hugo"  xmlns:xs="http://www.w3.org/2001/XMLSchema" >

    The definition of "xmlns:xs" has to be in this, as "xs:schema" do have a namespace(alias) "xs"

    So your XML couldlooklike

    <?xml version="1.0" encoding="ISO8859-1"?>
    <xs:schema targetNamespace="Hugo"  xmlns:xs="http://www.w3.org/2001/XMLSchema" >
    <PRODUCTIE>
     <CODE>1234</CODE>
    </PRODUCTIE>

    or direct in an elemet

    <?xml version="1.0" encoding="ISO8859-1"?>
    <Hugo:PRODUCTIExmlns:hugo="Hugo" xmlns:xs="http://www.w3.org/2001/XMLSchema" >
     <Hugo:CODE>1234</CODE>
    </Hugo:PRODUCTIE>

    BTW

    1)  elements starting with "<!"  or  "<?" are not normal XML-elements but comments resp. directives

    2) "http://www.w3.org/2001/XMLSchema"  is a "offical" defined namespaces and holds elements/attributs which make it possible to have nicer XML
          it is the rar moments where actual a officaland public web side exists under this name which holds the XSD.
         Just open the URL and you will see it :-)
         Most browsers and XML-tools do have this XSD internal so it is no need to have a connect to the internet

    Ingo



    ------------------------------
    Ingo Stiller
    Aareon Deutschland GmbH
    ------------------------------