XSD Import NameSpaces

2019-05-24 17:12发布

问题:

I have a two xsd files "first.xsd" and "second.xsd".

Inside second.xsd, I want to refer to an element defined in first.xsd (with different namespace).
My second.xsd looks like:

<xs:schema ... xmlns:schema1="http://www.schema.org/schema1" targetNamespace "http://www.schema.org/schema2   ...  >
<xs:import namespace = "http://www.schema.org/schema1" schemaLocation = "first.xsd" />

<xs:element name = "complex1">
<xs:complexType>
.........
<xs:element ref="schema1:name2"/>
.........
</xs:complexType>
</xs:element>

Now I want to include and import second.xsd inside first.xsd So I do the following in first.xsd:

<xs:schema ... xmlns:schema2="http://www.schema.org/schema2" targetNamespace "http://www.schema.org/schema1  ... >
<xs:import namespace="http://www.schema.org/schema2" schemaLocation="second.xsd">
<xs:element name = "name1"/>
<xs:element name = "name2"/>
<xs:element name = "name3"/>

Is this the right way to do since I not able to successfully validate the schema.
Is it a problem because of some cyclic namespace inclusion??
I'm using oxygen xml developer to validate the schema files and my first.xsd when validated says:
E[Xerces] src-resolve: Cannot resolve the name to a(n) type definition component. (second.xsd)

But if I validate the second.xsd file separately it says schema valid.

In my actual case I have a very complex scenario so I've boiled it down to a simple example as shown above.
Could someone please help.

回答1:

It is perfectly valid (albeit debatable) to have these kind of circular dependencies; the reason why your schema set doesn't validate can't be it; you'll have to inspect the errors your schema processor generates and try to understand each one of them. It is generaly accepted that the best is to start with the first error message.

Your snippets are invalid since you're referencing an element2 (in second.xsd) that you didn't list in schema1. This is the kind of thing that won't pass validation, so focus on them.

If you need help with understanding what the real issue is, start by updating your post with the actual error message.