Example 6

This example program demonstrates the basic structure of an RPG program utilizing RXS to validate sample XML against an XSD. Several methods are demonstrated, with both pass and fail outcomes.

**FREE
Ctl-Opt ActGrp(*Caller) BndDir('RXSBND') Option(*NoDebugIO) ExtBinInt(*Yes) DecEdit('0.')
  Text('Ex. 6 - XML XSD Validate');

/COPY QRPGLECPY,RXSCB

Dcl-Ds TransmitDS LikeDS(RXS_TransmitDS_t);
Dcl-Ds XsdValidateDS LikeDS(RXS_XsdValidationDS_t);
Dcl-Ds PutStmfDS LikeDS(RXS_PutStmfDS_t);
Dcl-Ds DeleteStmfDS LikeDS(RXS_DeleteStmfDS_t);

Dcl-S request Like(RXS_Var64Kv_t);
Dcl-S goodXml Like(RXS_Var64Kv_t);
Dcl-S badXml Like(RXS_Var64Kv_t);
Dcl-S xsd Like(RXS_Var64Kv_t);

exsr Setup;

// Test validation against URI (Expected Success)
RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
XsdValidateDS.XsdLocation
  = 'http://files.rpg-xml.com/example/xsdvalidate/validate.xsd';
if RXS_Validate( goodXml : XsdValidateDS );
  RXS_JobLog( 'XML 1 Validated successfully');
else;
  RXS_JobLog( 'XML 1 Validation failed');
  RXS_JobLog( 'Validation Error #%s: %s'
            : %Char( XsdValidateDS.ValidationErrorCode )
            : XsdValidateDS.ValidationErrorText );
endif;

// Test validation against XSD in a field (Expected Success)
RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
XsdValidateDS.XsdPointer = %Addr( xsd : *DATA );
XsdValidateDS.XsdLength = %Len( xsd );
if RXS_Validate( goodXml : XsdValidateDS );
  RXS_JobLog( 'XML 2 Validated successfully');
else;
  RXS_JobLog( 'XML 2 Validation failed');
  RXS_JobLog( 'Validation Error #%s: %s'
            : %Char( XsdValidateDS.ValidationErrorCode )
            : XsdValidateDS.ValidationErrorText );
endif;

// Test validation against XSD in a file (Expected Success)
RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
XsdValidateDS.XsdLocation = PutStmfDS.Stmf;
if RXS_Validate( goodXml : XsdValidateDS );
  RXS_JobLog( 'XML 3 Validated successfully');
else;
  RXS_JobLog( 'XML 3 Validation failed');
  RXS_JobLog( 'Validation Error #%s: %s'
            : %Char( XsdValidateDS.ValidationErrorCode )
            : XsdValidateDS.ValidationErrorText );
endif;

// Test validation against URI (Expected Failure)
RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
XsdValidateDS.XsdLocation
  = 'http://files.rpg-xml.com/example/xsdvalidate/validate.xsd';
if RXS_Validate( badXml : XsdValidateDS );
  RXS_JobLog( 'XML 4 Validated successfully');
else;
  RXS_JobLog( 'XML 4 Validation failed');
  RXS_JobLog( 'Validation Error #%s: %s'
            : %Char( XsdValidateDS.ValidationErrorCode )
            : XsdValidateDS.ValidationErrorText );
endif;

// Test validation against XSD in a field (Expected Failure)
RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
XsdValidateDS.XsdPointer = %Addr( xsd : *DATA );
XsdValidateDS.XsdLength = %Len( xsd );
if RXS_Validate( badXml : XsdValidateDS );
  RXS_JobLog( 'XML 5 Validated successfully');
else;
  RXS_JobLog( 'XML 5 Validation failed');
  RXS_JobLog( 'Validation Error #%s: %s'
            : %Char( XsdValidateDS.ValidationErrorCode )
            : XsdValidateDS.ValidationErrorText );
endif;

// Test validation against XSD in a file (Expected Failure)
RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
XsdValidateDS.XsdLocation = PutStmfDS.Stmf;
if RXS_Validate( badXml : XsdValidateDS );
  RXS_JobLog( 'XML 6 Validated successfully');
else;
  RXS_JobLog( 'XML 6 Validation failed');
  RXS_JobLog( 'Validation Error #%s: %s'
            : %Char( XsdValidateDS.ValidationErrorCode )
            : XsdValidateDS.ValidationErrorText );
endif;

exsr Cleanup;

*INLR = *On;
return;

begsr Setup;
  // Get good XML to test validation
  RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
  TransmitDS.Uri = 'http://files.rpg-xml.com/example/xsdvalidate/success.xml';
  TransmitDS.HTTPMethod = RXS_HTTP_METHOD_GET;
  request = *Blanks;
  goodXml = RXS_Transmit( request : TransmitDS );

  // Get bad XML to test validation
  RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
  TransmitDS.Uri = 'http://files.rpg-xml.com/example/xsdvalidate/failure.xml';
  TransmitDS.HTTPMethod = RXS_HTTP_METHOD_GET;
  request = *Blanks;
  badXml = RXS_Transmit( request : TransmitDS );

  // Get XSD from remote URI
  RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
  TransmitDS.Uri = 'http://files.rpg-xml.com/example/xsdvalidate/validate.xsd';
  TransmitDS.HTTPMethod = RXS_HTTP_METHOD_GET;
  request = *Blanks;
  xsd = RXS_Transmit( request : TransmitDS );

  // Put the XSD in an IFS file
  RXS_ResetDS( PutStmfDS : RXS_DS_TYPE_PUTSTMF );
  PutStmfDS.Stmf = '/tmp/rxs_xsd_validate.xsd';
  PutStmfDS.StmfCcsid = RXS_CCSID_ISO88591;
  RXS_PutStmf( xsd : PutStmfDS );
endsr;

begsr Cleanup;
  RXS_ResetDS( DeleteStmfDS : RXS_DS_TYPE_DELETESTMF );
  DeleteStmfDS.Stmf = PutStmfDS.Stmf;
  RXS_ProcessStmf( DeleteStmfDS );
endsr;
 
     H DFTACTGRP(*NO) ACTGRP(*CALLER) BNDDIR('RXSBND') OPTION(*NODEBUGIO)
     H EXTBININT(*YES) DECEDIT('0.')
     H TEXT('Fixed Format Ex. 6 - XML XSD Validate')

      /COPY QRPGLECPY,RXSCB

     D TransmitDS      DS                  LikeDS(RXS_TransmitDS_t)
     D XsdValidateDS   DS                  LikeDS(RXS_XsdValidationDS_t)
     D PutStmfDS       DS                  LikeDS(RXS_PutStmfDS_t)
     D DeleteStmfDS    DS                  LikeDS(RXS_DeleteStmfDS_t)

     D request         S                   Like(RXS_Var64Kv_t)
     D goodXml         S                   Like(RXS_Var64Kv_t)
     D badXml          S                   Like(RXS_Var64Kv_t)
     D xsd             S                   Like(RXS_Var64Kv_t)

      /FREE

       exsr Setup;

       // Test validation against URI (Expected Success)
       RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
       XsdValidateDS.XsdLocation =
         'http://files.rpg-xml.com/example/xsdvalidate/validate.xsd';
       if RXS_Validate( goodXml : XsdValidateDS );
         RXS_JobLog( 'XML 1 Validated successfully');
       else;
         RXS_JobLog( 'XML 1 Validation failed');
         RXS_JobLog( 'Validation Error #%s: %s'
                   : %Char( XsdValidateDS.ValidationErrorCode )
                   : XsdValidateDS.ValidationErrorText );
       endif;

       // Test validation against XSD in a field (Expected Success)
       RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
       XsdValidateDS.XsdPointer = %Addr( xsd : *DATA );
       XsdValidateDS.XsdLength = %Len( xsd );
       if RXS_Validate( goodXml : XsdValidateDS );
         RXS_JobLog( 'XML 2 Validated successfully');
       else;
         RXS_JobLog( 'XML 2 Validation failed');
         RXS_JobLog( 'Validation Error #%s: %s'
                   : %Char( XsdValidateDS.ValidationErrorCode )
                   : XsdValidateDS.ValidationErrorText );
       endif;

       // Test validation against XSD in a file (Expected Success)
       RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
       XsdValidateDS.XsdLocation = PutStmfDS.Stmf;
       if RXS_Validate( goodXml : XsdValidateDS );
         RXS_JobLog( 'XML 3 Validated successfully');
       else;
         RXS_JobLog( 'XML 3 Validation failed');
         RXS_JobLog( 'Validation Error #%s: %s'
                   : %Char( XsdValidateDS.ValidationErrorCode )
                   : XsdValidateDS.ValidationErrorText );
       endif;

       // Test validation against URI (Expected Failure)
       RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
       XsdValidateDS.XsdLocation =
         'http://files.rpg-xml.com/example/xsdvalidate/validate.xsd';
       if RXS_Validate( badXml : XsdValidateDS );
         RXS_JobLog( 'XML 4 Validated successfully');
       else;
         RXS_JobLog( 'XML 4 Validation failed');
         RXS_JobLog( 'Validation Error #%s: %s'
                   : %Char( XsdValidateDS.ValidationErrorCode )
                   : XsdValidateDS.ValidationErrorText );
       endif;

       // Test validation against XSD in a field (Expected Failure)
       RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
       XsdValidateDS.XsdPointer = %Addr( xsd : *DATA );
       XsdValidateDS.XsdLength = %Len( xsd );
       if RXS_Validate( badXml : XsdValidateDS );
         RXS_JobLog( 'XML 5 Validated successfully');
       else;
         RXS_JobLog( 'XML 5 Validation failed');
         RXS_JobLog( 'Validation Error #%s: %s'
                   : %Char( XsdValidateDS.ValidationErrorCode )
                   : XsdValidateDS.ValidationErrorText );
       endif;

       // Test validation against XSD in a file (Expected Failure)
       RXS_ResetDS( XsdValidateDS : RXS_DS_TYPE_XSDVALIDATION );
       XsdValidateDS.XsdLocation = PutStmfDS.Stmf;
       if RXS_Validate( badXml : XsdValidateDS );
         RXS_JobLog( 'XML 6 Validated successfully');
       else;
         RXS_JobLog( 'XML 6 Validation failed');
         RXS_JobLog( 'Validation Error #%s: %s'
                   : %Char( XsdValidateDS.ValidationErrorCode )
                   : XsdValidateDS.ValidationErrorText );
       endif;

       exsr Cleanup;

       *INLR = *On;
       return;


       begsr Setup;
         // Get good XML to test validation
         RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
         TransmitDS.Uri =
           'http://files.rpg-xml.com/example/xsdvalidate/success.xml';
         TransmitDS.HTTPMethod = RXS_HTTP_METHOD_GET;
         request = *Blanks;
         goodXml = RXS_Transmit( request : TransmitDS );

         // Get bad XML to test validation
         RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
         TransmitDS.Uri =
           'http://files.rpg-xml.com/example/xsdvalidate/failure.xml';
         TransmitDS.HTTPMethod = RXS_HTTP_METHOD_GET;
         request = *Blanks;
         badXml = RXS_Transmit( request : TransmitDS );

         // Get XSD from remote URI
         RXS_ResetDS( TransmitDS : RXS_DS_TYPE_TRANSMIT );
         TransmitDS.Uri =
           'http://files.rpg-xml.com/example/xsdvalidate/validate.xsd';
         TransmitDS.HTTPMethod = RXS_HTTP_METHOD_GET;
         request = *Blanks;
         xsd = RXS_Transmit( request : TransmitDS );

         // Put the XSD in an IFS file
         RXS_ResetDS( PutStmfDS : RXS_DS_TYPE_PUTSTMF );
         PutStmfDS.Stmf = '/tmp/rxs_xsd_validate.xsd';
         PutStmfDS.StmfCcsid = RXS_CCSID_ISO88591;
         RXS_PutStmf( xsd : PutStmfDS );
       endsr;

       begsr Cleanup;
         RXS_ResetDS( DeleteStmfDS : RXS_DS_TYPE_DELETESTMF );
         DeleteStmfDS.Stmf = PutStmfDS.Stmf;
         RXS_ProcessStmf( DeleteStmfDS );
       endsr;

      /END-FREE