Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate XML against multiple XSD files?

I need to validate multiple XML files against given XSD files. The problem is that XSD is made out of two files. They are nested, though I know which one is the first ("parent" of others). In the XML file I use namespaces to define in which XSD the childnode is defined (<CBIBdySDDReq xmlns="urn:CBI:xsd:CBIBdySDDReq.00.00.06" xmlns:msg="urn:CBI:xsd:CBISDDReqLogMsg.00.00.06">). I am using XMLTools plugin for Notepad++ for the other tests, but it is not able to find and validate against these XSD files, cause I can only give one XSD to validate against as parameter.

Is there a tool that is able to test my generated XML files against more than one XSD files?

like image 231
Robert P Avatar asked Jan 16 '14 09:01

Robert P


People also ask

Can we validate XML documents against so schema?

You can validate your XML documents against XML schemas only; validation against DTDs is not supported. However, although you cannot validate against DTDs, you can insert documents that contain a DOCTYPE or that refer to DTDs.

Can XML have multiple schemas?

Schemas can be composed of one or more XML documents. These schema documents can be explicitly joined together using the include and import elements.


1 Answers

You should consider yourself lucky that you didn't get heavily downvoted... if only because tools that are to do things (recommendations) are outside the scope here...

Nonetheless, if this is about how to achieve what you need using the tool that you're already using... and for which I've seen lots of issues reported on SO... then one approach which would work anytime someone has the limitation of being able to provide a single XSD file... is to create such an XSD file which underneath would import all the other XSD files that you need referenced.

This is an example of a stub XSD that would work in your case:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <xsd:import namespace="(a)" schemaLocation="(b)"/>
</xsd:schema>

where:

(a) is the value you see in the targetNamespace attribute of the xsd:schema you wish referenced; if there's no targetNamespace, then remove the namespace attribute (and add a targetNamespace containing some dummy value to the stub XSD)

(b) the location of the file containing the XSD you want to reference. Start by using the full path here; as you learn more about XSD or your tool, you may reach the conclusion that it is better, if supported by your tool, to provide the relative URI, that is between the location of your "stub" XSD (it really is an "aggregator") and the other referenced XSD. Relative URIs are friendlier in terms of moving your stuff around (as a whole unit, as in zipping all your files and send to someone else). An example... if the files are in the same folder, you simply have to put here the name of the file.

Add one xsd:import line for each of the XSDs that are not "reachable" through all the other XSDs, or that your tool seems to complain about as non-reachable.

like image 193
Petru Gardea Avatar answered Sep 24 '22 01:09

Petru Gardea