如何设计属性与内容模型具有排斥性约束的Schema?

carylin 2010-01-15 10:35:07
如何设计属性与内容模型具有排斥性约束的Schema文档?
举个例子,假设这是两份份满足Schema语法的XML示例文档:

<school>
<!-- global -->
<student id="1">
<name>Li Xiao Ming</name>
<age>7</age>
</student>
<monitor>
<!-- localReference -->
<student ref="1" />
</monitor>
</school>



<school>
<monitor>
<!-- local -->
<student>
<name>Wang Gang</name>
<age>7</age>
</student>
</monitor>
</school>

这里的student元素有三种格式:分别是全局的、局部的和局部引用的(分别标注上了global、local和localReference)。
monitor元素的内容模型既可以是local student元素,也可以是localReference student元素。student具有一个id属性,一个ref属性,一个有name与age组成的内容模型。而id属性与ref属性以及内容模型都有排斥性,ref属性又与内容模型具有排斥性。我没该如何设计Schema文档以满足这里的约束?

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/test"
xmlns:tns="http://www.example.org/test" elementFormDefault="qualified">
<xsd:element name="school">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" type="tns:GlobalStudentType" minOccurs="0" maxOccurs="unbounded"/>
<xsd:element name="monitor">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="student" type="tns:InnerStudentType" />
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType>
</xsd:element>

<xsd:complexType name="StudentType" abstract="true">
<xsd:sequence minOccurs="0">
<xsd:element name="name" type="xsd:string" />
<xsd:element name="age" type="xsd:unsignedShort" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" />
<xsd:attribute name="ref" type="xsd:IDREF" />
</xsd:complexType>

<xsd:complexType name="GlobalStudentType">
<xsd:complexContent>
<xsd:restriction base="tns:StudentType">
<xsd:sequence minOccurs="1">
<xsd:element name="name" type="xsd:string" />
<xsd:element name="age" type="xsd:unsignedShort" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="required" />
<xsd:attribute name="ref" type="xsd:IDREF" use="prohibited" />
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="LocalStudentType">
<xsd:complexContent>
<xsd:restriction base="tns:StudentType">
<xsd:sequence minOccurs="1">
<xsd:element name="name" type="xsd:string" />
<xsd:element name="age" type="xsd:unsignedShort" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="prohibited" />
<xsd:attribute name="ref" type="xsd:IDREF" use="prohibited" />
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="LocalRefStudentType">
<xsd:complexContent>
<xsd:restriction base="tns:StudentType">
<xsd:sequence minOccurs="0">
<xsd:element name="name" type="xsd:string" />
<xsd:element name="age" type="xsd:unsignedShort" />
</xsd:sequence>
<xsd:attribute name="id" type="xsd:ID" use="prohibited" />
<xsd:attribute name="ref" type="xsd:IDREF" use="required" />
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="InnerStudentType">
<!-- How to designed this? Should LocalStudentType and LocalRefStudentType be used? -->
</xsd:complexType>

</xsd:schema>


如何通过LocalStudentType与LocalRefStudentType设计InnerStudentType?或是这里的方向不对?
...全文
132 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liuahuilele 2010-01-18
  • 打赏
  • 举报
回复
不懂 顶了
carylin 2010-01-18
  • 打赏
  • 举报
回复
顶上去
carylin 2010-01-15
  • 打赏
  • 举报
回复
不是吧,都一天了,怎么还是没人解答?CSDN真是没落了。
carylin 2010-01-15
  • 打赏
  • 举报
回复
楼上的我也试过了,是不行的。定义在choice内的元素不能重名的。不过还是感谢。
reandyner 2010-01-15
  • 打赏
  • 举报
回复
<xsd:complexType name="InnerStudentType">
<xsd:complexContent>
<xsd:restriction base="tns:StudentType">
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xsd:element name="student" type="tns:LocalStudentType" />
<xsd:element name="student" type="tns:LocalRefStudentType" />
</xs:choice>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
哒哒路 2010-01-15
  • 打赏
  • 举报
回复
看不懂 帮顶

81,122

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧