W3C home > Mailing lists > Public > xmlschema-dev@w3.org > May 2001

Re: Question about combination of xsi:type and import

From: Eddie Robertsson <eddie@allette.com.au>
Date: Fri, 18 May 2001 13:58:50 +1000
Message-ID: <3B049DF9.E81874DE@allette.com.au>
To: O.Geisser@ceyoniq.com
CC: xmlschema-dev@w3.org
> My question: Is XML Spy right on this ?

Yes, because there is an error in your instance. See below.

> The short version of the question is:
> Can I reference types with "xsi:type" that are not in the schema for the
> namespace of the element the "xsi:type" attribute is applied to, and also
> not in a schema imported by this namespace.
>
> I've tried to understand the spec about this, but I do not quite get it.
> http://www.w3.org/TR/2001/REC-xmlschema-1-20010502/#xsi_type

You can use xsi:type to identify which of the derived types of the declared
base type that should be used in your instance document. So, in your schema
you declare the element to be of the basetype and then in the instance you can
use xsi:type to identify any derived type that uses the basetype as base for
the derivation. The primer says [1]:

"XML Schema allows us to define the billTo and shipTo elements as Address
types (see ipo.xsd) but to use instances of international addresses in place
of instances of Address. In other words, an instance document whose content
conforms to the UKAddress type will be valid if that content appears within
the document at a location where an Address is expected (assuming the
UKAddress content itself is valid). To make this feature of XML Schema work,
and to identify exactly which derived type is intended, the derived type must
be identified in the instance document. The type is identified using the
xsi:type attribute which is part of the XML Schema instance namespace. In the
example, ipo.xml, use of the UKAddress and USAddress derived types is
identified through the values assigned to the xsi:type attributes."

> treeWithExtendedLeaf.xml:
> <?xml version="1.0" encoding="UTF-8"?>
> <tree:tree xmlns:tree="urn:tree" xmlns:ext="urn:extLeaf"
> xmlns:xsi="http://www.w3.org/2000/10/XMLSchema-instance"
> xsi:schemaLocation="urn:tree tree.xsd">
>         <tree:leaf xsi:schemaLocation="urn:extLeaf extLeaf.xsd"
> xsi:type="ext:extendedLeafType">
>                 <tree:name>eins</tree:name>
>                 <ext:info>Test</ext:info>
>         </tree:leaf>
> </tree:tree>

This is where your problem is. In your instance you should only identify the
urn:extLeaf namespace in your schemaLocation attribute. Since the schema that
defines this namespace imports the namespace urn:tree all these declarations
will be imported along with it. So your instance should be:

<?xml version="1.0" encoding="UTF-8"?>
<tree:tree xmlns:tree="urn:tree" xmlns:ext="urn:extLeaf"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="urn:extLeaf extLeaf.xsd">
        <tree:leaf xsi:type="ext:extendedLeafType">
                <tree:name>eins</tree:name>
                <ext:info>Test</ext:info>
        </tree:leaf>
</tree:tree>

Isn't this what you want?

Cheers,
/Eddie

[1] http://www.w3.org/TR/xmlschema-0/#UseDerivInInstDocs
Received on Thursday, 17 May 2001 23:59:52 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Tuesday, 11 January 2011 00:14:20 GMT