蓝色天空网mbsky.com · 知识 · 实践 · 创造XML Schema · 本地阅读预览
教程 / XML / Web Service / XML Schema

完整订单XSD:三种设计与相同约束

保留内联、全局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允许、第三拒绝
shiptoname/address/city/country固定顺序缺地址失败
item至少1,maxOccurs=unboundedtitle/note?/quantity/pricenote可选,quantity>0
QName引用type/ref目标声明/类型不等同外部include
实例提示noNamespaceSchemaLocation位置提示不自动触发验证

本章逐项运行分支(完整输入见核心SCENARIOS)

编号模型文件具体教学目标明确预期
1order-inline.xsd两条商品可选note应通过
2order-inline.xsd缺orderid应拒绝:实例验证
3order-inline.xsd数量0应拒绝:实例验证
4order-inline.xsd缺地址应拒绝:实例验证
5order-inline.xsd六位模式仅命名方案增加应通过
6order-ref.xsd两条商品可选note应通过
7order-ref.xsd缺orderid应拒绝:实例验证
8order-ref.xsd数量0应拒绝:实例验证
9order-ref.xsd缺地址应拒绝:实例验证
10order-ref.xsd六位模式仅命名方案增加应通过
11order-named.xsd两条商品可选note应通过
12order-named.xsd缺orderid应拒绝:实例验证
13order-named.xsd数量0应拒绝:实例验证
14order-named.xsd缺地址应拒绝:实例验证
15order-named.xsd六位模式仅命名方案增加应拒绝:实例验证

本章独立核心源码与验证步骤

python · 阅读源码
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)
完整订单XSD:三种设计与相同约束:逐项真实XSD1.0验证

所需环境: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

xml · 阅读源码
<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>
完整XSD模型:order-inline.xsd

所需环境:Windows / Python3.12 / xmlschema4.3.2 XMLSchema10 / elementpath5.1.4;lxml6.1.3+libxml2.11.9交叉观察

当前源码 SHA256:a445ea1ed84245cd82082d89b961b5e6af7f0dcc5e7aeb29efe9541cdfdfb943

下载本站示例源码
预期结果 · 待实际运行核验
  • 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。

完整XSD模型:order-ref.xsd

xml · 阅读源码
<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>
完整XSD模型:order-ref.xsd

所需环境:Windows / Python3.12 / xmlschema4.3.2 XMLSchema10 / elementpath5.1.4;lxml6.1.3+libxml2.11.9交叉观察

当前源码 SHA256:8bfe284e493dd221d56d09856df2098ecc5f18144771ed717506d335c89d28aa

下载本站示例源码
预期结果 · 待实际运行核验
  • 此模型在指定主处理器真实编译,相关输入的通过/拒绝按本章SCENARIOS核对。

完整XSD模型:order-named.xsd

xml · 阅读源码
<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>
完整XSD模型:order-named.xsd

所需环境: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规则和章节清单核对,不更改预期或删失败用例来掩盖处理器差异;原资料全文逐项复核仍待完成。

规范与官方依据