目前设计的XML格式如下:
<?xml version="1.0" encoding="utf-8"?>
<root>
<div name ="项目信息" type="TabPage">
<control name ="项目名称" type="TextBox" defaultvalue=""/>
<control name="项目类型" type="DropDown">
<item index="1" name ="A" value ="A"> </item>
<item index="2" name ="B" value ="B"> </item>
<item index="3" name ="C" value ="C"> </item>
</control>
</div>
<div name ="项目基本信息" type="TabPage">
<control name ="项目名称" type="TextBox" defaultvalue=""/>
<control name="项目类型" type="DropDown">
<item index="1" name ="A" value ="A"> </item>
<item index="2" name ="B" value ="B"> </item>
<item index="3" name ="C" value ="C"> </item>
</control>
</div>
</root>
同样的control包含不同的类型,TextBox没有子项,DropDown有下拉列表子项.尝试写XSD来限制,但是遇到如下报错:
XSD代码如下:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- 简易元素的定义 -->
<!-- 属性的定义 -->
<!-- 简单类型的定义(通用) -->
<xs:simpleType name="StringNotEmpty">
<xs:restriction base="xs:string">
<xs:pattern value=".+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="IntStartFrom1">
<xs:restriction base="xs:int">
<xs:minInclusive value="1"></xs:minInclusive>
</xs:restriction>
</xs:simpleType>
<!-- 简单类型的定义(指定类型) -->
<xs:simpleType name="DivTypeTabPage">
<xs:restriction base="xs:string">
<xs:pattern value="TabPage"></xs:pattern>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="DivTypeExcel">
<xs:restriction base="xs:string">
<xs:pattern value="Excel"></xs:pattern>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="DivTypeLogic">
<xs:restriction base="xs:string">
<xs:pattern value="logic"></xs:pattern>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="DivTypeBackUp">
<xs:restriction base="xs:string">
<xs:enumeration value="TabPage"/>
<xs:enumeration value="Excel"/>
<xs:enumeration value="logic"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ControlTypeTextBox">
<xs:restriction base="xs:string">
<xs:pattern value="TextBox"></xs:pattern>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="ControlTypeDropDown">
<xs:restriction base="xs:string">
<xs:pattern value="DropDown"></xs:pattern>
</xs:restriction>
</xs:simpleType>
<!-- 属性组 -->
<xs:attributeGroup name="AGDivTabPage">
<xs:attribute name="name" type="StringNotEmpty"></xs:attribute>
<xs:attribute name="type" type="DivTypeTabPage"></xs:attribute>
</xs:attributeGroup>
<xs:attributeGroup name="AGControlTextBox">
<xs:attribute name="name" type="StringNotEmpty" ></xs:attribute>
<xs:attribute name="type" type="ControlTypeTextBox"></xs:attribute>
<xs:attribute name="defaultvalue" type="xs:string" ></xs:attribute>
<xs:attribute name="propertytype" type="xs:string" ></xs:attribute>
<xs:attribute name="propertyid" type="xs:int" ></xs:attribute>
<xs:attribute name="propertyname" type="xs:string" ></xs:attribute>
<xs:attribute name="propertyindex" type="xs:int" default="0"></xs:attribute>
</xs:attributeGroup>
<xs:attributeGroup name="AGControlDropDown">
<xs:attribute name="name" type="StringNotEmpty"></xs:attribute>
<xs:attribute name="type" type="ControlTypeDropDown"></xs:attribute>
<xs:attribute name="defaultvalue" type="xs:string" ></xs:attribute>
</xs:attributeGroup>
<xs:attributeGroup name="AGControlDropDownItem">
<xs:attribute name="index" type="IntStartFrom1"></xs:attribute>
<xs:attribute name="name" type="xs:string"></xs:attribute>
<!--检查代码是否需要唯一性-->
<xs:attribute name="value" type="xs:string" ></xs:attribute>
</xs:attributeGroup>
<!-- 元素组 -->
<xs:group name="EGControlTextBox">
<xs:sequence>
<xs:element name="control" >
<xs:complexType>
<xs:attributeGroup ref="AGControlTextBox"></xs:attributeGroup>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:group>
<xs:group name="EGControlDropDown">
<xs:sequence>
<xs:element name="control">
<xs:complexType>
<xs:group ref="EGControlDropDownItem" minOccurs="1" maxOccurs="unbounded"></xs:group>
<xs:attributeGroup ref="AGControlDropDown"></xs:attributeGroup>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:group>
<xs:group name="EGControlDropDownItem">
<xs:sequence>
<xs:element name="item">
<xs:complexType>
<xs:group ref="EGControlDropDown" minOccurs="0" maxOccurs="1"></xs:group>
<xs:attributeGroup ref="AGControlDropDownItem"></xs:attributeGroup>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:group>
<xs:group name="EGDivTabPage">
<xs:sequence>
<xs:element name="div" maxOccurs="unbounded">
<xs:complexType>
<xs:choice>
<xs:group ref="EGControlTextBox" maxOccurs="unbounded" minOccurs="0"></xs:group>
<xs:group ref="EGControlDropDown" maxOccurs="unbounded" minOccurs="0"></xs:group>
</xs:choice>
<xs:attributeGroup ref="AGDivTabPage"></xs:attributeGroup>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:group>
<!-- 复合元素的定义 -->
<!-- 根元素的定义-->
<xs:element name="root">
<xs:complexType>
<xs:choice>
<xs:group ref="EGDivTabPage" maxOccurs="unbounded" minOccurs="1"></xs:group>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
[code=xml]
[/code]
报错原因是98行和88行定义了相同的control元素,目的是通过不同的元素组来区分不同的control.
但是出现如图片报错,还请各位帮忙看看!