简单内容复合类型:文本与属性
正确使用simpleContent extension/restriction,分别验证值类型和属性。
学完这一章,你能够
- 正确使用simpleContent extension/restriction,分别验证值类型和属性。
- 正确使用simpleContent extension/restriction,分别验证值类型和属性。
文本不是子元素模型
例如鞋码值35配country属性,内容是integer而元素为复杂类型。simpleContent/extension可基于简单类型增加属性,restriction的合法基类型和派生条件需要匹配,不能机械替换关键字。
本章有效扩展接受35、拒绝35.5/子元素;命名带属性基类型再限制数值范围,比较边界和必需属性。
词法、类型与单位
country只是字符串元数据,现实鞋码还需尺码体系/单位约定,不因字段叫country就自动换算。
简单值与混合文本不相同,schema无法靠自动猜测知道35是否某地正确尺码。
声明、用途与适用条件
| 项目 | 语法/类型 | 含义及约束 | 条件/边界 |
| 空内容 | complexType+attribute | 只带允许属性 | 不能complexContent限制xs:integer |
| 元素内容 | sequence/all/choice | 子元素约束 | 允许缩进不等于mixed任意文本 |
| 简单内容 | simpleContent extension/restriction | 简单值+属性 | 基类型/派生规则匹配 |
| 混合内容 | mixed="true" | 文字+受限子元素 | 顺序/日期/类型仍限制 |
| 匿名/命名 | 内联/type引用 | 不同复用方式 | QName和命名空间准确 |
| extension | complexContent扩展复杂基类 | 保留原基础字段再追加 | 缺失基础内容仍拒绝 |
本章逐项运行分支(完整输入见核心SCENARIOS)
| 编号 | 模型文件 | 具体教学目标 | 明确预期 |
| 1 | simplecontent-extension.xsd | 整数简单内容+必需属性 | 应通过 |
| 2 | simplecontent-extension.xsd | 整数简单内容+必需属性 | 应拒绝:实例验证 |
| 3 | simplecontent-extension.xsd | 整数简单内容+必需属性 | 应拒绝:实例验证 |
| 4 | simplecontent-extension.xsd | 整数简单内容+必需属性 | 应拒绝:实例验证 |
| 5 | simplecontent-restriction.xsd | 简单内容restriction边界 | 应通过 |
| 6 | simplecontent-restriction.xsd | 简单内容restriction边界 | 应通过 |
| 7 | simplecontent-restriction.xsd | 简单内容restriction边界 | 应拒绝:实例验证 |
| 8 | simplecontent-restriction.xsd | 简单内容restriction边界 | 应拒绝:实例验证 |
本章独立核心源码与验证步骤
展开查看收起
python
from schema_lab import run_cases
# 蓝色天空网 mbsky.com:独立输入与逐项预期。
SCENARIOS = [{'id': '1',
'schema': 'schemas/simplecontent-extension.xsd',
'input': '<shoe country="模拟">35</shoe>',
'expected': True,
'goal': '整数简单内容+必需属性'},
{'id': '2',
'schema': 'schemas/simplecontent-extension.xsd',
'input': '<shoe country="模拟">35.5</shoe>',
'expected': False,
'goal': '整数简单内容+必需属性'},
{'id': '3',
'schema': 'schemas/simplecontent-extension.xsd',
'input': '<shoe>35</shoe>',
'expected': False,
'goal': '整数简单内容+必需属性'},
{'id': '4',
'schema': 'schemas/simplecontent-extension.xsd',
'input': '<shoe country="模拟"><value>35</value></shoe>',
'expected': False,
'goal': '整数简单内容+必需属性'},
{'id': '5',
'schema': 'schemas/simplecontent-restriction.xsd',
'input': '<shoe country="模拟">30</shoe>',
'expected': True,
'goal': '简单内容restriction边界'},
{'id': '6',
'schema': 'schemas/simplecontent-restriction.xsd',
'input': '<shoe country="模拟">45</shoe>',
'expected': True,
'goal': '简单内容restriction边界'},
{'id': '7',
'schema': 'schemas/simplecontent-restriction.xsd',
'input': '<shoe country="模拟">29</shoe>',
'expected': False,
'goal': '简单内容restriction边界'},
{'id': '8',
'schema': 'schemas/simplecontent-restriction.xsd',
'input': '<shoe country="模拟">46</shoe>',
'expected': False,
'goal': '简单内容restriction边界'}]
if __name__ == "__main__":
run_cases('schema-complex-text', SCENARIOS)
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 本章8项主验证结果与逐项预期一致。
- 具体分支:整数简单内容+必需属性;整数简单内容+必需属性;整数简单内容+必需属性;整数简单内容+必需属性;简单内容restriction边界;简单内容restriction边界;简单内容restriction边界;其余分支逐项列于清单与源码。
- lxml观察可能不同,所有差异保留为处理器限制/后续复核,不能据此升级完整语义或其它处理器。
完整XSD模型:simplecontent-extension.xsd
展开查看收起
xml
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ><xs:complexType name="shoeType"><xs:simpleContent><xs:extension base="xs:integer"><xs:attribute name="country" type="xs:string" use="required"/></xs:extension></xs:simpleContent></xs:complexType><xs:element name="shoe" type="shoeType"/></xs:schema>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。
完整XSD模型:simplecontent-restriction.xsd
展开查看收起
xml
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ><xs:complexType name="shoeType"><xs:simpleContent><xs:extension base="xs:integer"><xs:attribute name="country" type="xs:string" use="required"/></xs:extension></xs:simpleContent></xs:complexType><xs:complexType name="limited"><xs:simpleContent><xs:restriction base="shoeType"><xs:minInclusive value="30"/><xs:maxInclusive value="45"/><xs:attribute name="country" type="xs:string" use="required"/></xs:restriction></xs:simpleContent></xs:complexType><xs:element name="shoe" type="limited"/></xs:schema>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。
本章附件与实例包
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202611a529.zip
常见错误与边界检查
- 把良构、Schema编译、实例有效和业务事实混为一层。
- 把旧处理器接受非法Base64/悬空IDREF或拒大数等实现结果误写成规范要求。
- 默认值、通配符strict/lax/skip、命名空间和替换block忽略适用条件。
技术规范与官方资料
练习与参考解答
查看参考解答
按指定XSD1.0规则和章节清单核对,不更改预期或删失败用例来掩盖处理器差异;原资料全文逐项复核仍待完成。