Need help in using Validation.LAX
Added by chandra vudumula about 9 years ago
My goal is to validate xml file thats produced through transformer.transfor(), this validation should be done on the fly i.e., without generating xml file and loading back again.
for this purpose i am using Validation.LAX, here my issue is generated output not validating against "v2p1.xsd".
MyErrorListener errListner = new MyErrorListener();
System.setProperty("javax.xml.transform.TransformerFactory", "com.saxonica.config.EnterpriseTransformerFactory");
//TransformerFactory factory = TransformerFactory.newInstance();
TransformerFactory factory = new EnterpriseTransformerFactory();
factory.setAttribute(FeatureKeys.XSLT_SCHEMA_AWARE, true);
System.err.println("TransformerFactory class: " + factory.getClass().getName());
factory.setAttribute(FeatureKeys.SCHEMA_VALIDATION, new Integer(Validation.LAX));
factory.setAttribute(FeatureKeys.VALIDATION_WARNINGS, Boolean.TRUE);
factory.setAttribute(FeatureKeys.GENERATE_BYTE_CODE, false);
StreamSource schema = new StreamSource(TestUtil.getResourceFile("v2p1.xsd"));
TransformerFactoryImpl factoryImpl = (TransformerFactoryImpl) factory; Configuration config = factoryImpl.getConfiguration(); config.addSchemaSource(schema,errListner);
Transformer trans = factory.newTransformer(new StreamSource(new StringReader("XML_ReportExtractLayout.xsl")));
for( final Map.Entry<String, Object> parameter : _record.getParameters().entrySet() ) { trans.setParameter(parameter.getKey(), parameter.getValue()); }
trans.transform(new StreamSource(new StringReader(xml)), new StreamResult(System.out));
Thank in Advance Chandar
Replies (1)
RE: Need help in using Validation.LAX - Added by Michael Kay about 9 years ago
When you say it is "not validating", do you mean that no validation is taking place, or that validation is reporting errors?
You suggest that you want to validate the OUTPUT of the transformation. This approach isn't going to achieve that: it will validate the input, but not the output. To validate the output, the simplest way is to use
<xsl:template match="/"> <xsl:document validation="strict"> xsl:apply-templates/ (or whatever) </xsl:document> </xsl:template>
in the stylesheet.
The alternative would be to pipe the output of the transformer into a validator - you can do that very conveniently with the s9api API, where a SchemaValidator is a Destination that can be supplied as the output destination for a transformation. But controlling the validation from XSLT is better, because it will often tell you exactly which line in the stylesheet is generating the invalid output.
Please register to reply