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
than by looking at a particular XML document
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.
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.
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.