229

What's the difference between xsd:include and xsd:import? When would you use one instead of the other, and when might it not matter?

kjhughes
  • 106,133
  • 27
  • 181
  • 240
Pops
  • 30,199
  • 37
  • 136
  • 151
  • See also answer at https://stackoverflow.com/questions/4998063/one-xml-namespace-equals-one-and-only-one-schema-file/4998182#4998182 – Nashev Jun 21 '18 at 12:38

6 Answers6

227

The fundamental difference between include and import is that you must use import to refer to declarations or definitions that are in a different target namespace and you must use include to refer to declarations or definitions that are (or will be) in the same target namespace.

Source: https://web.archive.org/web/20070804031046/http://xsd.stylusstudio.com/2002Jun/post08016.htm

tony19
  • 125,647
  • 18
  • 229
  • 307
Sergiy Belozorov
  • 5,856
  • 7
  • 40
  • 73
  • 1
    The stylusstudio post that @Pops refers to is here: https://web.archive.org/web/20140831005739/http://xsd.stylusstudio.com/2005Mar/post05007.htm – tony19 Aug 28 '16 at 00:27
61

Use xsd:include to bring in an XSD from the same or no namespace.

Use xsd:import to bring in an XSD from a different namespace.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
23

Another difference is that <import> allows importing by referring to another namespace with the @namespace attribute, and/or the schemaLocation with the @schemaLocation attribute.

However, <include> only allows importing by referring to a URI of intended include schema with the @schemaLocation attribute, <include> does not allow the @namespace attribute.

The ability to use the @namespace attribute is definitely another difference than inter-intra namespace importing.

For example, the xml schema validator may already know the locations of all schemas by namespace. Especially considering that referring to XML namespaces by their schema URL location may be problematic on different systems (i.e. classpath:// means nothing, or where http:// isn't allowed, or where the URL doesn't point to the same thing as it does on another system.)

Code sample of valid and invalid imports and includes:

Valid:

<xsd:import namespace="some/name/space"/>
<xsd:import schemaLocation="classpath://mine.xsd"/>

<xsd:include schemaLocation="classpath://mine.xsd"/>

Invalid:

<xsd:include namespace="some/name/space"/>
Nate Anderson
  • 18,334
  • 18
  • 100
  • 135
Zombies
  • 25,039
  • 43
  • 140
  • 225
  • 1
    I think you mean _referring to XML namespaces by location URLs_ rather than _by URI_. Namespace URIs (i.e. the `namespace` attribute of ``) are always OK, and considered as an **identifier** (just text, not interpreted), while the `schemaLocation` (a URL not a URI) must typically be processed, to be resolved. And there indeed, the Java-specific `classpath:` URL _scheme_ may mean nothing. But beside this neat-pick, this is a valuable comment, thanks. – ddevienne Nov 12 '19 at 15:57
7

I'm interested in this as well. The only explanation I've found is that xsd:include is used for intra-namespace inclusions, while xsd:import is for inter-namespace inclusion.

Matt Luongo
  • 14,371
  • 6
  • 53
  • 64
1

Direct quote from MSDN: <xsd:import> Element, Remarks section

The difference between the include element and the import element is that import element allows references to schema components from schema documents with different target namespaces and the include element adds the schema components from other schema documents that have the same target namespace (or no specified target namespace) to the containing schema. In short, the import element allows you to use schema components from any schema; the include element allows you to add all the components of an included schema to the containing schema.

S Meaden
  • 8,050
  • 3
  • 34
  • 65
  • 1
    So, what I get from that description is that import will bring in stuff you refer to only, whereas include means those who refer your XSD will be implicitly getting all the stuff from the XSD you imported – George Birbilis Oct 20 '20 at 11:37
-2

Use xsd:include brings all declarations and definitions of an external schema document into the current schema.

Use xsd:import to bring in an XSD from a different namespace and used to build a new schema by extending existing schema documents..

Shailej Shimpi
  • 145
  • 2
  • 3