如何为给定XML编写XSD并解决schema命名空间报错问题
直接报错原因
你当前的XSD代码中存在命名空间拼写错误:<schema>节点的属性写为了xlmns,正确拼写应为xmlns,字母顺序为x-m-l-n-s,你误将m和l顺序写反,导致解析器无法识别到正确的XSD命名空间,触发该报错。
其他需要修正的逻辑问题
修正拼写后还需要调整以下内容,否则XML实例验证仍然会失败:
- XSD规范要求
<complexType>下的属性声明必须放在<sequence>等结构声明之后,你当前将Category属性写在了<sequence>前面,不符合规范 - 你的XML实例中
name节点下要么只有firstname、要么只有lastname,但你现有XSD要求两个节点必须同时存在,需要将name下的<sequence>改为<choice>,适配二选一的场景 adhaarno、age、doorno、pincode都是数字内容,建议显式指定类型为int或long更严谨,Category属性是可选属性,建议显式加use="optional"声明
修正后的完整XSD代码
<?xml version="1.0" encoding="utf-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema"> <element name="PersonList"> <complexType> <sequence> <element name="Person" maxOccurs="unbounded"> <complexType> <sequence> <element name="adhaarno" type="long"/> <element name="name"> <complexType> <choice> <element name="firstname" type="string"/> <element name="lastname" type="string"/> </choice> </complexType> </element> <element name="age" type="int"/> <element name="address"> <complexType> <sequence> <element name="doorno" type="int"/> <element name="street" type="string"/> <element name="city" type="string"/> <element name="pincode" type="int"/> </sequence> </complexType> </element> </sequence> <attribute name="Category" type="string" use="optional"/> </complexType> </element> </sequence> </complexType> </element> </schema>
内容的提问来源于stack exchange,提问作者Shield77
相关产品推荐
相关产品推荐

