· Dec 11, 2017

Validating XML


I want to be able to import XML (say, using %XML.Reader) to Caché objects permissively (ignoring invalid/unexpected tags/attributes), but also to validate the same XML and list any/all invalid tags/attributes that it contains.

Possible path to a solution:

XML-enabled classes (see documentation) inherit XMLIGNOREINVALIDTAG and XMLIGNOREINVALIDATTRIBUTE parameters that can customize the treatment of unexpected tags/attributes in XML import. One heavy-handed option would be to have parallel sets of classes representing the same XML document, one with both parameters set to 0 and one with both parameter set to 1. (And different XML namespaces, of course.) But this seems like a lot of work, and I strongly suspect that %XML.Reader would give up at the first error, which doesn't help the user much if there are lots of problems in the XML document. Ideally there would be some runtime option to make a list of the times XMLIGNOREINVALIDTAG=1/XMLIGNOREINVALIDATTRIBUTE=1 made a difference.

Any thoughts/recommendations? Has anyone done this before in Caché?

Discussion (3)1
Log in or sign up to continue

Hi Tim,

I'm highly interested to see this result.
I tried to do something similar recently to to be able to feed back  to the sender what  was wrong if something was wrong.
I failed to get the combinations of params for XERCES parser right. XMLIGNORE* worked OK but was not so useful.

You might have a shorter link to contact Marvin Tenner then me.  wink  yes

I did not test this approach, but something along these lines can help. The general idea is that SAX parser can validate against the XML schema, and we can use that.

1. Generate schema from your XML enabled classes.

2. Call %XML.Reader:Open method, and provide it with your xml data and schema:

#include %occSAX
set reader = ##class(%XML.Reader).%New()
set reader.SAXSchemaSpec = "/path/to/schema.xsd" // "maybe "file:///path/to/schema.xsd"
set reader.SAXFlags = $$$SAXFULLDEFAULT
set sc = reader.OpenString("<xml/>")
w $System.Status.GetErrorText(sc)

3. sc should contain errors, $$$SAXFULLDEFAULT is defined in %occSAX and contains several possible values:

#; ------------------------------------------------------------------------
#; Bit flags for %XML.SAX.Parser feature selection (flags argument)
#; ------------------------------------------------------------------------
#; Specify this value if you want to accept the SAX defaults (see below)
#define SAXDEFAULTS 27
#; Specify this value if you want the SAX defaults plus namespaces prefixes/
#; Specify this bit if you want the parser to perform validation
#; On: Report all validation errors. (default) 
#; Off: Do not report validation errors. 
#; Specify this bit if you want the parser to recognize namespaces
#; On: Perform Namespace processing (default) 
#; Off: Optionally do not perform Namespace processing 
#; Specify this bit if you want the parser to process namespace prefixes
#; On: Report the original prefixed names and attributes used for Namespace declarations 
#; Off: Do not report attributes used for Namespace declarations, and optionally do not report original prefixed names (default)
#; Specify this bit if you want the parser to perform validation dynamically
#; On: The parser will validate the document only if a grammar is specified. ( must be true) (default)
#; Off: Validation is determined by the state of the feature
#; Specify this bit if you want the parser to recognize schemas
#; On: Enable the parser's schema support. (default) 
#; Off: Disable the parser's schema support. 
#; Specify this bit if you want the parser to perform full schema checking
#; On: Enable full schema constraint checking, including checking which may be time-consuming or memory intensive. Currently, particle unique attribution constraint checking and particle derivation resriction checking are controlled by this option
#; Off: Disable full schema constraint checking (default). 
#; On: Cache the grammar in the pool for re-use in subsequent parses
#; Off: Do not cache the grammar in the pool (default)
#; If set to true, the is also set to true automatically.
#; Flags to force SAX not to validate but DO recognize namespaces and prefixes


Maybe try change the flags , but defaults seems to do what you need.

If you actually try this approach can you please post if it works or not?