urac 2019-05-25 20:32 采纳率: 0%
浏览 439
已结题

XSD 元素名称相同 但是要分不同类别如何设置

目前设计的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.

但是出现如图片报错,还请各位帮忙看看!

  • 写回答

1条回答 默认 最新

  • threenewbee 2019-05-26 08:30
    关注

    html也是一种xml,当你遇到问题的时候,你可以借鉴html的定义的思路
    在html里,和你类似的场景就是
    一个form元素下面有很多input
    而input通过type区分出textbox textarea hidden file checkbox ...
    而input元素的属性,是所有各种类型的属性的超集

    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题