U. Busto
U. Busto

Reputation: 194

How to refer to two schemas from a single XML file

I am starting with XML and XSD and I want to build an XML file that must match two different schemas I got in my hard drive.

What should I type in targetNamespace to reference to files schema1.xsd and schema2.xsd?

Upvotes: 3

Views: 2142

Answers (1)

kjhughes
kjhughes

Reputation: 111541

In an XSD, xs:schema/@targetNamespace defines the single namespace that an XSD governs:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           xmlns:ns1="http://example.com/1" 
           targetNamespace="http://example.com/1">
     ...

In an XML document, you can hint about multiple XSDs, one for each namespace used in your XML document instance via `xs:schemaLocation':

<ns1:root xmlns:ns1="http://example.com/1"
          xmlns:ns2="http://example.com/2"
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xsi:schemaLocation="http://example.com/1 ns1.xsd
                              http://example.com/2 ns2.xsd">
    <ns2:a/>
</ns1:root>

In an XML document, you cannot hint that a single XML document instance must simultaneously adhere to multiple XSDs (other than for separate namespaces). You could, however, validate sequentially once per XSD you wish to apply to the XML.

Upvotes: 3

Related Questions