XSDVALID

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


H DFTACTGRP(*NO) BNDDIR('RXSBND') ACTGRP(*CALLER)
 /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)
D i               S             10I 0
 /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;

  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