XSD属性:简单类型、use和默认值
验证可选、必需、禁止、default/fixed及属性类型,不混淆元素赋值。
学完这一章,你能够
- 验证可选、必需、禁止、default/fixed及属性类型,不混淆元素赋值。
- 验证可选、必需、禁止、default/fixed及属性类型,不混淆元素赋值。
属性声明与使用条件
xs:attribute的类型是简单类型,带属性的元素本身使用复杂类型。name/type分别表示名字与类型,use默认optional,可required或prohibited;属性缺失和显式空串不同。
本例lang可选、id必需、旧属性禁止、lang默认zh、mode固定draft分别检查。required与default不能随意组合,default/fixed也不能同时设置成冲突规则。
默认信息与验证输出
属性值按类型验证,声明xs:date不接受普通问候字符串。固定值限制显式值;可选固定属性缺失不必等同验证失败。
本例分别展示默认验证和attribute_defaults=True的填充观察,不把lxml树更新行为泛化为所有处理器。数据约束作用于元素和属性的简单值,而非只能约束元素。
声明、用途与适用条件
| 项目 | 语法/类型 | 含义及约束 | 条件/边界 |
| attribute | name/type | 简单类型属性 | 元素本身可simpleContent复杂类型 |
| use | optional默认/required/prohibited | 使用条件 | 缺失与空串不同 |
| default | 缺失时默认语义 | 本例lang=zh | 不能随意与required并用 |
| fixed | mode=draft | 其它值拒绝 | 可选缺失不一定失败 |
| attribute_defaults | 显式lxml选项 | 观察默认属性填入 | 不泛化所有处理器和文本归一化 |
本章逐项运行分支(完整输入见核心SCENARIOS)
| 编号 | 模型文件 | 具体教学目标 | 明确预期 |
| 1 | attributes.xsd | 默认/固定可选属性缺失 | 应通过 |
| 2 | attributes.xsd | id必需 | 应拒绝:实例验证 |
| 3 | attributes.xsd | 违反fixed | 应拒绝:实例验证 |
| 4 | attributes.xsd | 禁止属性 | 应拒绝:实例验证 |
| 5 | attributes.xsd | 覆盖default | 应通过 |
| 6 | attributes.xsd | 明确请求填入默认属性 | 应通过 |
| 7 | bad-required-default.xsd | required+default模型非法 | 应拒绝:schema_compile_rejection |
本章独立核心源码与验证步骤
展开查看收起
python
from schema_lab import run_cases
# 蓝色天空网 mbsky.com:独立输入与逐项预期。
SCENARIOS = [{'id': '1',
'schema': 'schemas/attributes.xsd',
'input': '<v id="a"/>',
'expected': True,
'goal': '默认/固定可选属性缺失'},
{'id': '2', 'schema': 'schemas/attributes.xsd', 'input': '<v/>', 'expected': False, 'goal': 'id必需'},
{'id': '3',
'schema': 'schemas/attributes.xsd',
'input': '<v id="a" mode="published"/>',
'expected': False,
'goal': '违反fixed'},
{'id': '4',
'schema': 'schemas/attributes.xsd',
'input': '<v id="a" old="x"/>',
'expected': False,
'goal': '禁止属性'},
{'id': '5',
'schema': 'schemas/attributes.xsd',
'input': '<v id="a" lang="en"/>',
'expected': True,
'goal': '覆盖default'},
{'id': '6',
'schema': 'schemas/attributes.xsd',
'input': '<v id="a"/>',
'expected': True,
'goal': '明确请求填入默认属性',
'attribute_defaults': True,
'expected_attributes': {'lang': 'zh', 'mode': 'draft'}},
{'id': '7',
'schema': 'schemas/bad-required-default.xsd',
'input': '',
'expected': False,
'goal': 'required+default模型非法',
'kind': 'schema_compile_rejection'}]
if __name__ == "__main__":
run_cases('schema-simple-attributes', SCENARIOS)
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 本章7项主验证结果与逐项预期一致。
- 具体分支:默认/固定可选属性缺失;id必需;违反fixed;禁止属性;覆盖default;明确请求填入默认属性;required+default模型非法。
- lxml观察可能不同,所有差异保留为处理器限制/后续复核,不能据此升级完整语义或其它处理器。
完整XSD模型:attributes.xsd
展开查看收起
xml
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ><xs:element name="v"><xs:complexType><xs:attribute name="id" type="xs:string" use="required"/><xs:attribute name="lang" type="xs:string" default="zh"/><xs:attribute name="mode" type="xs:string" fixed="draft"/><xs:attribute name="old" use="prohibited"/></xs:complexType></xs:element></xs:schema>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。
故意错误模型(应编译拒绝):bad-required-default.xsd
展开查看收起
xml
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ><xs:element name="v"><xs:complexType><xs:attribute name="x" type="xs:string" use="required" default="x"/></xs:complexType></xs:element></xs:schema>
查看独立实例 ↗ 在独立页面查看源码、运行环境与工程包
- 应编译拒绝并给诊断。
本章附件与实例包
课程关联工程包(登记关联24章) · 含当前章节或实例 · mbskycom202611a529.zip
常见错误与边界检查
- 把良构、Schema编译、实例有效和业务事实混为一层。
- 把旧处理器接受非法Base64/悬空IDREF或拒大数等实现结果误写成规范要求。
- 默认值、通配符strict/lax/skip、命名空间和替换block忽略适用条件。
技术规范与官方资料
练习与参考解答
查看参考解答
按指定XSD1.0规则和章节清单核对,不更改预期或删失败用例来掩盖处理器差异;原资料全文逐项复核仍待完成。