Issue
Pega Cloud does not support reading XML schema documents directly from file repository locations when validating XML with Parse XML rules. Schema documents must be read from the local file system, which is ephemeral on Pega Cloud and generally not advised.
Symptoms and Impact
XML schema validation fails when the schema document is configured using a file repository specification in the Schema URL field on the Parse XML rule form. In this case, validation fails with an error similar to the one shown below.
Error
Error Found: schema_reference.4: Failed to read schema document 'file://pegacloudrepository:/ImportClaimsCases/XSD/XSD_Lower/FNOL_XSD.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Root cause
During XML parser validation, if the schema file is stored in a file repository and the Schema URL value is similar to file://pegacloudrepository:/<directory path>, then Pega is unable to resolve it and the validation fails.
Solution
The following configuration steps allow a Parse XML to validate the XML being parsed against an XML Schema document. This approach leverages the local file system and has been confirmed to work in all the Pega Cloud environments.
1. Create a Text File record to hold the XML Schema document content
Select appropriate values for the Identifier and App Name key values. Set the File Type key value to xml. Use the Upload file button to upload the XML Schema file, and then save the record.
2. Create an Application Setting record for the XML Schema file URL
Select appropriate values for the Identifier and Owning Ruleset key values.
Category: <<blank>>
Value type: String/text
Value per production level: <<use the same file URL value for all production levels>>
Example for file URL
file:///usr/local/tomcat/xsd/AccountInquiry.xsd
3. Create a Function rule called FileExistsOnDisk
This function must be evaluated prior to applying the validating parse rule to ensure that the XML Schema file exists in the file location indicated by the Schema URL value on the Parse XML rule being applied. Add the function to an existing library or create a new one for this purpose.
Input parameters
Name |
Java type |
filePath |
String |
Output
Java data type: boolean
Classification
Usage type: Condition Evaluation
Java
if (filePath.startsWith("file://")) {
filePath = filePath.substring(7);
}
File file = new File(filePath);
return file.exists();
4. Create an Activity rule called WriteTextFileToDisk
This activity must be called prior to applying the validating parse rule if the FileExistsOnDisk function evaluates to False for the XML Schema file being used for validation.
Set the Applies To class for the activity to a class from which all of your Parse XML rules inherit, possibly @baseclass or Int-.
Parameters
Name |
Data type |
Required |
In/Out |
ApplicationName |
String |
Yes |
In |
FileName |
String |
Yes |
In |
FileType |
String |
Yes |
In |
FilePath |
String |
Yes |
In |
Pages & Classes
Page name |
Class |
TextFilePage |
Rule-File-Text |
Steps
- Method: Obj-Open, Step page: TextFilePage
Method Parameters
Name |
Value |
Open class |
Rule-File-Text |
Lock |
<<unchecked>> |
ReleaseOnCommit |
<<unchecked>> |
LockInfoPage |
<<blank>> |
PropertyName |
PropertyValue |
.pyApplicationName |
param.ApplicationName |
.pyFileName |
param.FileName |
.pyFileType |
param.FileType |
Use a Jump to exit the activity if step status is not good.
2. Method: Java, Step page: TextFilePage
String filePath = tools.getParamValue("FilePath");
String fileSource = myStepPage.getString("pyFileSource");
byte[] fileBytes = fileSource.getBytes();
if (filePath.startsWith("file://")) {
filePath = filePath.substring(7);
}
if (oLog.isDebugEnabled()) {
oLog.debug("Writing " + fileBytes.length + " byte file '" + filePath + "' to disk");
}
try {
java.nio.file.Path path = java.nio.file.Paths.get(filePath);
java.nio.file.Files.createDirectories(path.getParent());
java.nio.file.Files.write(path, fileBytes);
}
catch (Exception e) {
throw new PRRuntimeException("Caught exception", e);
}
3. Method: Page-Remove, Step page: TextFilePage
5. Invoke WriteTextFileToDisk immediately before invoking Apply-Parse-XML
Add a step that conditionally writes the XML Schema file to disk before applying the parse rule.
Step details:
When
Enable conditions before this action: <<checked>>
Use @FileExistsOnDisk to check whether the XML Schema document needs to be written to disk.
Example
@FileExistsOnDisk(D_pxGetApplicationSettingValue[OwningRuleset:"XMLSchema",Purpose:" AccountInquiryXSDFilePath"].pySettingValue)
If true: Skip Step
If false: Continue Whens
Method
Call WriteTextFileToDisk
Pass current parameter page: <<unchecked>>
Pass the keys of the Text rule that holds the XML Schema document as method parameters and use the Application Setting for the local file path.
Example
Method Parameters
Name |
Value |
ApplicationName |
XSDValidation |
FileName |
AccountInquiryXSD |
FileType |
xml |
FilePath |
D_pxGetApplicationSettingValue[OwningRuleset:"XMLSchema",Purpose:" AccountInquiryXSDFilePath"].pySettingValue) |
Step page
Same as the Apply-Parse-XML step
6. Modify the Parse XML rule Schema URL value
Set the Schema URL value on the XML tab to resolve the XML Schema file from the local file system. Use GRS syntax to look up the Application Setting that holds the file URL value.
Example
=D_pxGetApplicationSettingValue[OwningRuleset:"XMLSchema",Purpose:"AccountInquiryXSDFilePath"].pySettingValue