XSD中抽象类型通过abstract="true"定义,不可直接实例化,仅能被extension或restriction继承;抽象复杂类型常用于构建类型体系,抽象简单类型仅支持restriction继承且不可被union或list引用。

在XSD中定义抽象类型,只需在 <complextype></complextype> 或 <simpletype></simpletype> 元素上设置 abstract="true" 属性即可。抽象类型不能直接用于元素实例,只能被其他类型通过 extension 或 restriction 继承。
这是最常见的情况,用于构建可扩展的类型体系:
<font size="2"><xs:complexType name="Person" abstract="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
<xs:element name="age" type="xs:integer"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="Employee">
<xs:complexContent>
<xs:extension base="Person">
<xs:sequence>
<xs:element name="employeeId" type="xs:string"/>
</xs:sequence>
</xs:extension>
</xs:complexContent>
</xs:complexType></font>此时 <person></person> 元素不能直接出现在XML文档中,但 <employee></employee> 可以,并自动包含 name 和 age。
较少见但合法,适用于需要统一约束但不直接使用的值类型:
<font size="2"><xs:simpleType name="Identifier" abstract="true">
<xs:restriction base="xs:string">
<xs:pattern value="[A-Za-z][A-Za-z0-9_]*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="UserId">
<xs:restriction base="Identifier">
<xs:maxLength value="16"/>
</xs:restriction>
</xs:simpleType></font>注意:抽象简单类型不能被 xs:union 或 xs:list 直接引用,仅支持 restriction 继承。
xsi:type 使用抽象类型当元素声明使用抽象类型作为 type 时,XML实例必须通过 xsi:type 指定具体子类型:
<font size="2"><xs:element name="person" type="Person"/></font>
对应合法XML示例:
<font size="2"><person xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="Employee">
<name>Alice</name>
<age>30</age>
<employeeId>E123</employeeId>
</person></font>若省略 xsi:type 或指定为抽象类型名,校验将失败。
<element></element> 的 type 属性中,除非该元素允许运行时动态类型(即依赖 xsi:type)final 属性(如 final="#all"),限制后续继承方式block 属性,防止被某些方式(如 substitution group)替代基本上就这些。抽象类型本质是建模上的“占位符”,核心作用是规范继承结构,不是为了单独使用。
以上就是XSD怎么定义一个抽象类型 (abstract="true")的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号