类型

指定元素的类型。 该属性将一个元素标记为特定的类型,即使在架构中可能没有将该元素绑定到该类型的元素声明。

<xsi:type="QName">

特性

  • QName
    数据类型名称,该名称将被替换为元素的声明数据类型。

备注

type 属性还将在以下情况下使用:当在实例文档中使用派生的复杂类型,而非预期的基类型时。

示例

以下示例显示如何使用 type 属性。 本示例使用架构文档 person.xsd 和实例文档 person.xml。 该架构文档包含基类型 Person、派生类型 Employee 以及元素声明 person。 实例文档演示如何使用 xsi:type 特性将 urn:contoso-com:People 命名空间中的 person 元素的类型指定为同一命名空间中的 Employee 类型。

<xs:schema xmlns:xs= "http://www.w3.org/2001/XMLSchema"
   targetNamespace="urn:contoso-com:People"
   xmlns:ns="urn:contoso-com:People">
   <xs:element name="person" type="ns:Person"/>
      <xs:complexType name="Person">
         <xs:sequence>
            <xs:element name= "name" type="xs:string"/>
            <xs:element name= "height" type="xs:double" />
         </xs:sequence>
      </xs:complexType>
      <xs:complexType name="Employee">
         <xs:complexContent>
            <xs:extension base="ns:Person">
               <xs:sequence>
                  <xs:element name="jobDescription" type="xs:string" />
               </xs:sequence>
             </xs:extension>
         </xs:complexContent>
       </xs:complexType>
</xs:schema>
<p:Person
   xmlns:p="urn:contoso-com:People"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:type="p:Employee">
   <name>John</name>
   <height>59</height>
   <jobDescription>manager</jobDescription>
</p:Person>

请参见

参考

XML 架构 (XSD) 参考

XML 架构元素

XML 架构实例特性