Because I needed it recently for a unit test, today I will show you how you can validate an XML against a XSD schema in Nav.
We have the following XSD schema:
And we need to validate the below XML file against it:
First, we need to convert ‘False’ into ‘false’ and ‘,’ from decimal into ‘.’. Otherwise the XML will be considered invalid. For this I created the following function which receives as arguments the Initial file path (original file with invalid values) and the Resulted file path. The function creates the Resulted file which can be validated against the XSD schema:
Local Variables:
MyFile DotNet System.IO.File.’mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
StringBuilder DotNet System.Text.StringBuilder.’mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
RegEx DotNet System.Text.RegularExpressions.Regex.’System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
ReplaceDecimalsWithCommaToBeWithPoint(InitialXMLFilePath : Text;ResultXMLFilePath : Text) //convert 'False' into 'false' StringBuilder := StringBuilder.StringBuilder(MyFile.ReadAllText(InitialXMLFilePath)); StringBuilder.Replace('False','false'); //convert decimals with comma to be with point RegEx := RegEx.Regex('(?<=\d)[.,](?=\d)'); StringBuilder := StringBuilder.StringBuilder(RegEx.Replace(StringBuilder.ToString(),'.')); //save the modified xml MyFile.WriteAllText(ResultXMLFilePath,StringBuilder.ToString());
The Resulted XML file (“test.xml”) generated by the above function looks like this:
Next I created the function that validates a XML file against a XSD Schema:
Local Variables:
XMLDoc DotNet System.Xml.XmlDocument.’System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
Schemas DotNet System.Xml.Schema.XmlSchemaSet.’System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
Null DotNet System.Object.’mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
ValidateXMLwithXSD(XMLPath : Text;XSDPath : Text) XMLDoc := XMLDoc.XmlDocument(); XMLDoc.Load(XMLPath); Schemas := Schemas.XmlSchemaSet(); Schemas.Add('',XSDPath); XMLDoc.Schemas := Schemas; IF NOT TryValidate(XMLDoc) THEN ERROR(IncorrectXMLErr,XMLPath); MESSAGE(CorrectXMLErr,XMLPath);
And the helper Try function which helps us to give a custom error in case the Validation of the XML fails:
Local Variable:
Validation DotNet System.Xml.Schema.ValidationEventHandler.’System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089′
LOCAL [TryFunction] TryValidate(XMLDoc : DotNet "System.Xml.XmlDocument") XMLDoc.Validate(Validation);
I wrapped these functions into Codeunit 50008 “Validate XML Schema” and I call them from a sample codeunit 50009 “Update and Check XML File” in order to transform the Initial XML file into a valid XML and then Validate it against the schema:
OnRun() //Convert decimals with Comma to be with Point in XML file ValidateXMLSchema.ReplaceDecimalsWithCommaToBeWithPoint('C:\temp\test_initial.xml','C:\temp\test.xml'); //validate xml with XSD ValidateXMLSchema.ValidateXMLwithXSD('C:\temp\test.xml','C:\temp\test.xsd'); //Validate the Initial XML file--> error expected ValidateXMLSchema.ValidateXMLwithXSD('C:\temp\test_initial.xml','C:\temp\test.xsd');
In case of Validation of the Converted file we get a message that the file is Valid:
And in case of Validation of the Initial file we get an error becausse the file is Invalid:
You can download and use Sample file from THIS DOWNLOAD LINK. PS: don’t forget before you run Codeunit 50009 to change the file paths or to copy the 2 files (“test_initial.xml” and “test.xsd”) into C:\temp