ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

XSD validation

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • XSD validation

    I have an XML document in an ILERPG variable. How can I validate this document against an XSD located in the IFS. Is there an SQL function that can do this?

  • #2
    The XMLVALIDATE function returns a copy of the input XML value augmented with information obtained from XML schema validation, including default values and type annotations.

    Comment


    • #3
      Thanks. After a bit of trial and error I have now been able to validate my xml document against the schema.
      However, I have only been able to invoke the XMLVALIDATE function by using it in conjunction with an insert statement and then test the sql diagnostics after executing the statement. Is there any way to use the XMLVALIDATE function without having to insert the document into a table?

      Comment


      • #4
        I'm not aware of a way to run an sql scalar function in SQLRPGLE without assigning the result somewhere. But it doesn't have to be assigned to a table, it can be assigned to an RPGLE variable using an SQL Set statement. e.g.
        Code:
        exec sql set :rpgLowerStringVar = lower(:rpgUpperStringVar);
        xmlValidate() returns the input xml, modified with additional data from the schema. So you can just define a second xml data type, and assign the result to it, and then ignore it:
        Code:
        exec sql set :validatedXmlDoc = xmlValidate(DOCUMENT :xmlDoc ACCORDING TO XMLSCHEMA ID foo.bar);
        Or if you do not want to store a whole useless second copy, you can translate the result to a different data type using a case statement. This example translates to -1 if the result is null, else to 1, and stores the result in an int variable. Not much value in knowing if the output was or was not null, but it's a conveniently easy conversion
        Code:
        dcl-s xmlStatus  int(5) inz(0);​
        
        exec sql set :xmlStatus = case
          when xmlValidate(DOCUMENT :xml ACCORDING TO XMLSCHEMA ID foo.bar)
            is null then -1
            else 1
            end;​
        ​​

        Comment


        • Vectorspace
          Vectorspace commented
          Editing a comment
          P.S. this works for any of the many SQL scalar functions: https://www.ibm.com/docs/en/ssw_ibm_...rbafzscale.htm
          There are plenty of functions that do not have an equivalent RPGLE BIF, like using VARCHAR_FORMAT() and TIMESTAMP_FORMAT() for date/time <==> String conversions. It's a useful tool for your RPGLE toolbox

      • #5
        Thank you for your brilliant suggestions. It now works like I want it to. Except for one thing.
        At design time I don't know the name of the SCHEMA ID.
        I have tried to pass the ID as a host variable with no luck.
        I tried to create a prepared statement and using a place holder (?) for the SCHEMA ID also no luck.
        Any idea how to solve my remaining conundrum?

        Comment

        Working...
        X