Skip to Content

Using XML Schema

I fell in love with http://en.wikipedia.org/wiki/XML_schemaXML Schema while working on SAP integration projects.

Whoever developed XML Schema sure had humans (not computers) in mind.

 

Once you passed that XML Schema learning curve (check out w3schools website) it is much easier to understand the XML document by looking at its graphical schema representation

image

than by looking at a particular XML document

image

For example, by looking at the above XML schema one can see that sub1 is a required element and there can be a sub3 element, which is not even present in the XML document.

Looking for XML validator in ABAP

ABAP if_ixml_parser interface “does” (interfaces don’t do anything) DTD validation. There is no XML Schema (XSD) validation at the moment.

I posted a Validating XML with XML Schema (XSD) on SDN forums but at the time of writing this blog it was not answered.

Developing my own validator

Java

I quickly developed the XML validator in Java.  In this case Java serves me well as it can run on Windows and Unix platforms meaning I can use the same validator for the frontend and backend validation.

The Java validator takes an XML file and an XML Schema file and prints results of the validation on the standard output (System.out)

Script

To run the Java validator from ABAP I developed operating system specific scripts.
  For Windows it is xmlvldt.bat and for Unix it is xmlvldt.sh

The Windows batch file looks like this

:| %1 is the XMLvalidator jar file
:| %2 is the XML file to validate
:| %3 is the XML Schema file to validate with
:| %4 is the validation result file

@call java.exe -jar %1 %2 %3 >%4 2>&1

ABAP

The validator classes are shown on the following UML class diagram.

image

ZCL_EVP_XML_VALIDATOR_FE class implements the ABAP validator on the frontend

The frontend validation uses CL_GUI_FRONTEND_SERVICES class to execute the validator script file

ZCL_EVP_XML_VALIDATOR_BE class implements the ABAP validator on the backend

The backend validation uses a custom command ZXSDVALIDATOR defined in transaction SM69 as follows.

image

There is a known limitation on the length of the additinal parameters field which is up to 255 chars

A test program

The program ZSLQA_UTL_VALIDTE_XML_WITH_XSD shows how both frontend and backend validators can be used.

There is a piece of code in the program that defines what type of validator will be used

...
" Fronend or backend validation
if p_fe = 'X'.
  lv_valdator_type = 'ZCL_EVP_XML_VALIDATOR_FE'.
else.
  lv_valdator_type = 'ZCL_EVP_XML_VALIDATOR_BE'.
endif.

create object lo_validator type (lv_valdator_type).
...

Once the validator object is created the rest of the code is the same.

Where everything is

You can download all of the files described in this blog in one zip file here.
ABAP source code is in the SAPlink NUGG file NUGG_XML_XSD_VALIDATOR.nugg

The live source code is located on Google project as follows.

To report this post you need to login first.

3 Comments

You must be Logged on to comment or reply to a post.

  1. Alejandro Bindi
    This may really come in handy!
    I think the iXML library parser should already provide a method for this as well. But till then…this will certainly do!
    Thanks for sharing
    (0) 
  2. Otto Gold
    Maybe somebody should tell the SAP guys to implement this for us. Your work looks pretty useful, but for that money… shouldn´t they develop it for us in the SAP standard?
    Keep going (if you would make a SAP guy to comment on this and maybe implement this for us, that would be a great job!!).
    Thank you, Otto
    (0) 
  3. Alejo Ecoca Ecube PĂ©rez
    I haven’t implemented the sap class, but the java it works.
    We have problems with the standard directory of SM69’s commands. Then we have added instructions to change the directory to the bat. file.
    It doesn’t work with java 1.4.2. We have changed the xmlvldt.bat file with
    @call (directory where java 1.6 is)/java.exe -jar %1 %2 %3 >%4 2>&1
    Thank you!
    (0) 

Leave a Reply