问题
I have a XML Schema that contains multiple imports which in turn contain imports. I need to generate semantically equal schema where all imports are inlined. I want to replace these:
<xs:import namespace="http://some.name/" schemaLocation="./path/to/it.xsd"/>
with the contents of referenced schemata. And I need to get output as a string, not as some internal representation.
I tried Apache Xerces but did not find a way to write XSModel to a string. Is there?
I tried Apache XmlSchema 2 but when it writes XML Schema it does not replace import declarations with schema.
Is there any library that can do it? Any suggestions?
Thanks.
回答1:
xsd:include can usually be inlined, but xsd:import can't. That's because xsd:import is used to reference a schema document for a different target namespace, and you can't have components with different target namespaces in the same schema document.
回答2:
As pointed out here and here, XSD's limitation of having one target namespaces per file makes your "semantically equivalent" request impossible to solve. This is true, and also typical, in all scenarios where the namespace itself is used to define boundaries of (or refine) semantic sets.
For one time or design time refactoring, where you don't have to programatically deal with such a thing in a recurrent way or dynamically, you may also try to take a look here; Maybe the issue in your case is not that the imports are not supported (which I would find strange) but rather that the complexity of the include/imports makes the graph too complicated for your tooling. As shown in the latter post, by collapsing the includes, with a net effect of reducing the number of required imports, the problem was solved.
Alternatively, if somehow your "semantic equivalence" doesn't involve namespaces (for e.g. I've seen people that were rather interested in developing a relational model from XSD), it may be possible, through refactoring, to bring all namespaces into one (or none i.e. no target namespace) and then feed it to your tool. The only catch here, from an automatic refactoring perspective, is to ensure that there are no duplicate named XSD components across different namespaces; e.g. can't have same name for an element, or type, or attribute, or group, etc. in different namespaces.
来源:https://stackoverflow.com/questions/9159964/xml-schema-replacing-imports-with-corresponding-schema