Search code examples
xmlxsltxsdxsd-validation

Validate an XML using XSLT/XSD


I want to "dynamically" validate an XML file that can either be based on A.xsd or B.xsd. I am thinking to use an XSLT, i.e. call the XSLT with the input XML from my program, and XSLT looks at the certain elements within the XML, and if they exist then validate against A.xsd otherwise using B.xsd for validation.

Not sure how to do it, tried looking up a solution on SO but couldn't find one that addresses my problem. Any help is much appreciated!

Thanks in Advance!


Solution

  • That's certainly possible if you use a schema-aware XSLT processor to initiate the validation; though it's complicated by the fact that a single transformation can only load one schema (this could be the union of the two schemas if they are disjoint, eg. in different namespaces; but it would be messy if they are different versions of the same schema.

    A better solution might be to implement this as an XProc pipeline. Or there are plenty of other technologies you could use depending on what you're comfortable with, for example Ant or Gradle.