完整订单XSD:三种设计与相同约束
保留内联、全局ref、命名类型三方案,并明确新增六位订单号规则的差异。
学完这一章,你能够
- 保留内联、全局ref、命名类型三方案,并明确新增六位订单号规则的差异。
- 保留内联、全局ref、命名类型三方案,并明确新增六位订单号规则的差异。
订单结构与有效输入
shiporder含必需orderid,顺序为orderperson、shipto和至少一条item;shipto有name/address/city/country,item有title、可选note、positiveInteger quantity和decimal price。
本包使用蓝色天空网自有模拟订单和两条商品,所有路径/字段/价格可核对。缺id、零数量、错顺序、缺地址和未知项分别失败,不因只出现两条示例而把unbounded误写最多两条。
三种设计与演进
内联类型适合局部阅读,全局元素ref复用声明,命名类型复用类型。三份设计基本约束分别验证同一输入。第三种若额外增加六位数字orderid模式,必须说明是收紧,不再声称所有字符串id仍等价。
xsi提示不主动执行验证,属性声明在内容模型之后按XSD语法组织。模型拆分为文件和全局ref是不同层次,不把多声明误称已经完成外部include。
声明、用途与适用条件
| 项目 | 语法/类型 | 含义及约束 | 条件/边界 |
|---|---|---|---|
| 三方案 | 内联/全局ref/命名类型 | 结构复用方式不同 | 都编译验证 |
| orderid | 必需字符串;第三方案6位数字 | 明确收紧差异 | 前两letters允许、第三拒绝 |
| shipto | name/address/city/country | 固定顺序 | 缺地址失败 |
| item | 至少1,maxOccurs=unbounded | title/note?/quantity/price | note可选,quantity>0 |
| QName引用 | type/ref | 目标声明/类型 | 不等同外部include |
| 实例提示 | noNamespaceSchemaLocation | 位置提示 | 不自动触发验证 |
本章逐项运行分支(完整输入见核心SCENARIOS)
| 编号 | 模型文件 | 具体教学目标 | 明确预期 |
|---|---|---|---|
| 1 | order-inline.xsd | 两条商品可选note | 应通过 |
| 2 | order-inline.xsd | 缺orderid | 应拒绝:实例验证 |
| 3 | order-inline.xsd | 数量0 | 应拒绝:实例验证 |
| 4 | order-inline.xsd | 缺地址 | 应拒绝:实例验证 |
| 5 | order-inline.xsd | 六位模式仅命名方案增加 | 应通过 |
| 6 | order-ref.xsd | 两条商品可选note | 应通过 |
| 7 | order-ref.xsd | 缺orderid | 应拒绝:实例验证 |
| 8 | order-ref.xsd | 数量0 | 应拒绝:实例验证 |
| 9 | order-ref.xsd | 缺地址 | 应拒绝:实例验证 |
| 10 | order-ref.xsd | 六位模式仅命名方案增加 | 应通过 |
| 11 | order-named.xsd | 两条商品可选note | 应通过 |
| 12 | order-named.xsd | 缺orderid | 应拒绝:实例验证 |
| 13 | order-named.xsd | 数量0 | 应拒绝:实例验证 |
| 14 | order-named.xsd | 缺地址 | 应拒绝:实例验证 |
| 15 | order-named.xsd | 六位模式仅命名方案增加 | 应拒绝:实例验证 |
本章独立核心源码与验证步骤
from schema_lab import run_cases
# 蓝色天空网 mbsky.com:独立输入与逐项预期。
SCENARIOS = [{'id': '1',
'schema': 'schemas/order-inline.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': True,
'goal': '两条商品可选note'},
{'id': '2',
'schema': 'schemas/order-inline.xsd',
'input': '<shiporder '
'><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '缺orderid'},
{'id': '3',
'schema': 'schemas/order-inline.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>0</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '数量0'},
{'id': '4',
'schema': 'schemas/order-inline.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '缺地址'},
{'id': '5',
'schema': 'schemas/order-inline.xsd',
'input': '<shiporder '
'orderid="letters"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': True,
'goal': '六位模式仅命名方案增加'},
{'id': '6',
'schema': 'schemas/order-ref.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': True,
'goal': '两条商品可选note'},
{'id': '7',
'schema': 'schemas/order-ref.xsd',
'input': '<shiporder '
'><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '缺orderid'},
{'id': '8',
'schema': 'schemas/order-ref.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>0</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '数量0'},
{'id': '9',
'schema': 'schemas/order-ref.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '缺地址'},
{'id': '10',
'schema': 'schemas/order-ref.xsd',
'input': '<shiporder '
'orderid="letters"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': True,
'goal': '六位模式仅命名方案增加'},
{'id': '11',
'schema': 'schemas/order-named.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': True,
'goal': '两条商品可选note'},
{'id': '12',
'schema': 'schemas/order-named.xsd',
'input': '<shiporder '
'><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '缺orderid'},
{'id': '13',
'schema': 'schemas/order-named.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>0</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '数量0'},
{'id': '14',
'schema': 'schemas/order-named.xsd',
'input': '<shiporder '
'orderid="123456"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '缺地址'},
{'id': '15',
'schema': 'schemas/order-named.xsd',
'input': '<shiporder '
'orderid="letters"><orderperson>蓝色天空网</orderperson><shipto><name>学习者</name><address>模拟1</address><city>模拟城</city><country>模拟</country></shipto><item><title>HTML课程</title><note>自有模拟商品</note><quantity>1</quantity><price>10.90</price></item><item><title>CSS课程</title><quantity>2</quantity><price>9.90</price></item></shiporder>',
'expected': False,
'goal': '六位模式仅命名方案增加'}]
if __name__ == "__main__":
run_cases('schema-example', SCENARIOS)
所需环境:Windows / Python3.12 / xmlschema4.3.2 XMLSchema10 / elementpath5.1.4;lxml6.1.3+libxml2.11.9交叉观察
当前源码 SHA256:abc65584e05baf8e47f5fc3edc831086ef0b4caffe52c2f9f0e2686e58b3cfba
- 本章15项主验证结果与逐项预期一致。
- 具体分支:两条商品可选note;缺orderid;数量0;缺地址;六位模式仅命名方案增加;两条商品可选note;缺orderid;其余分支逐项列于清单与源码。
- lxml观察可能不同,所有差异保留为处理器限制/后续复核,不能据此升级完整语义或其它处理器。
完整XSD模型:order-inline.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ><xs:element name="shiporder"><xs:complexType><xs:sequence><xs:element name="orderperson" type="xs:string"/><xs:element name="shipto"><xs:complexType><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string"/></xs:sequence></xs:complexType></xs:element><xs:element name="item" maxOccurs="unbounded"><xs:complexType><xs:sequence><xs:element name="title" type="xs:string"/><xs:element name="note" type="xs:string" minOccurs="0"/><xs:element name="quantity" type="xs:positiveInteger"/><xs:element name="price" type="xs:decimal"/></xs:sequence></xs:complexType></xs:element></xs:sequence><xs:attribute name="orderid" type="xs:string" use="required"/></xs:complexType></xs:element></xs:schema>所需环境:Windows / Python3.12 / xmlschema4.3.2 XMLSchema10 / elementpath5.1.4;lxml6.1.3+libxml2.11.9交叉观察
当前源码 SHA256:a445ea1ed84245cd82082d89b961b5e6af7f0dcc5e7aeb29efe9541cdfdfb943
- 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。
完整XSD模型:order-ref.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ><xs:element name="orderperson" type="xs:string"/><xs:element name="name" type="xs:string"/><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string"/><xs:element name="title" type="xs:string"/><xs:element name="note" type="xs:string"/><xs:element name="quantity" type="xs:positiveInteger"/><xs:element name="price" type="xs:decimal"/><xs:attribute name="orderid" type="xs:string"/><xs:element name="shipto"><xs:complexType><xs:sequence><xs:element ref="name"/><xs:element ref="address"/><xs:element ref="city"/><xs:element ref="country"/></xs:sequence></xs:complexType></xs:element><xs:element name="item"><xs:complexType><xs:sequence><xs:element ref="title" /><xs:element ref="note" minOccurs="0"/><xs:element ref="quantity" /><xs:element ref="price" /></xs:sequence></xs:complexType></xs:element><xs:element name="shiporder"><xs:complexType><xs:sequence><xs:element ref="orderperson"/><xs:element ref="shipto"/><xs:element ref="item" maxOccurs="unbounded"/></xs:sequence><xs:attribute ref="orderid" use="required"/></xs:complexType></xs:element></xs:schema>所需环境:Windows / Python3.12 / xmlschema4.3.2 XMLSchema10 / elementpath5.1.4;lxml6.1.3+libxml2.11.9交叉观察
当前源码 SHA256:8bfe284e493dd221d56d09856df2098ecc5f18144771ed717506d335c89d28aa
- 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。
完整XSD模型:order-named.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" ><xs:simpleType name="orderidType"><xs:restriction base="xs:string"><xs:pattern value="[0-9]{6}"/></xs:restriction></xs:simpleType><xs:complexType name="shiptoType"><xs:sequence><xs:element name="name" type="xs:string"/><xs:element name="address" type="xs:string"/><xs:element name="city" type="xs:string"/><xs:element name="country" type="xs:string"/></xs:sequence></xs:complexType><xs:complexType name="itemType"><xs:sequence><xs:element name="title" type="xs:string"/><xs:element name="note" type="xs:string" minOccurs="0"/><xs:element name="quantity" type="xs:positiveInteger"/><xs:element name="price" type="xs:decimal"/></xs:sequence></xs:complexType><xs:complexType name="orderType"><xs:sequence><xs:element name="orderperson" type="xs:string"/><xs:element name="shipto" type="shiptoType"/><xs:element name="item" type="itemType" maxOccurs="unbounded"/></xs:sequence><xs:attribute name="orderid" type="orderidType" use="required"/></xs:complexType><xs:element name="shiporder" type="orderType"/></xs:schema>所需环境:Windows / Python3.12 / xmlschema4.3.2 XMLSchema10 / elementpath5.1.4;lxml6.1.3+libxml2.11.9交叉观察
当前源码 SHA256:d435461e16849342649c9ce3be5f4ae14f030866b245143aac3a00b4d395abee
- 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。
常见错误与边界
- 把良构、Schema编译、实例有效和业务事实混为一层。
- 把旧处理器接受非法Base64/悬空IDREF或拒大数等实现结果误写成规范要求。
- 默认值、通配符strict/lax/skip、命名空间和替换block忽略适用条件。
练习与解答
选择本章一个明确输入只改一个值或结构,先写预期与适用条件,再运行模型和两处理器观察。
展开参考解答
按指定XSD1.0规则和章节清单核对,不更改预期或删失败用例来掩盖处理器差异;原资料全文逐项复核仍待完成。