简体   繁体   中英

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?

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.

I want to point out there is another method to achieve this. However, I should first state that I believe Notepad++'s schema validation tool is... buggy, and so this method I'm providing won't work with Notepad++'s XMLTools plugin. Normally, you should be able to declare multiple namespaces and their corresponding schema files in the XML file's root element, like so:

<?xml version="1.0" encoding="utf-8"?>
<ROOT xmlns="https://www.A.org"
xmlns:r="https://www.B.org"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://www.A.org A.xsd https://www.B.org B.xsd" >

The above example is similar to the one provided on page 454 of O'Reilly's XML In a Nutshell.

In such a case, when you run validation in Notepad++, you don't have to select the target XSD file for validation because Notepad++ detects your schemaLocation declaration and runs the validation using that. However, it seems that Notepad++ will only check the last declared schema file in the schemaLocation attribute. So in my example, Notepad++ will validate your XML file only against B.xsd and inevitably throw errors. The solution is the one Petru mentions: to import A.xsd into B.xsd via the "import" declaration.

If you are using a more robust XML tool, you might not run into this problem. For example, OxygenXML handles this just fine, as long as you let it detect the XSD files on its own and not force it to validate against a specific XSD.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM