To use xsi:type
in your XML document:
-
Declare the
xsi
namespace prefix, usually on the root element:xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
Declare the
xs
(orxsd
) namespace prefix, usually on the root element:xmlns:xs="http://www.w3.org/2001/XMLSchema"
-
Add any XSD type to the element you wish to constrain:
xsi:type="xs:boolean"
These changes are made directly in your XML document. You do not have to make any changes to your XSD. However, the chosen type must be validly derived from the type provided by the XSD for the given element.
Relevant W3C References
From XML Schema Part 1: Structures Second Edition…
The Simple Type Definition (§2.2.1.2) or Complex Type Definition
(§2.2.1.3) used in ·validation· of an element is usually
determined by reference to the appropriate schema components. An
element information item in an instance may, however, explicitly
assert its type using the attributexsi:type
. The value of this
attribute is a ·QName·; see QName Interpretation (§3.15.3)
for the means by which the ·QName· is associated with a type
definition.
[…]
Validation Rule: Element Locally Valid (Element)
4.3 The ·local type definition· must be validly derived from the {type definition} given the union of the {disallowed
substitutions} and the {type definition}‘s {prohibited
substitutions}, as defined in Type Derivation OK (Complex)
(§3.4.6) (if it is a complex type definition), or given
{disallowed substitutions} as defined in Type Derivation OK (Simple)
(§3.14.6) (if it is a simple type definition).