Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xml 如何根据父标记对同一复杂类型应用不同的限制_Xml_Xsd - Fatal编程技术网

Xml 如何根据父标记对同一复杂类型应用不同的限制

Xml 如何根据父标记对同一复杂类型应用不同的限制,xml,xsd,Xml,Xsd,我希望达到下面所示的场景,其中我有相同的元素(在本例中是组件)和不同的父元素。我只想在第一级的应用程序中添加一个限制。例如,我想添加一个限制,即颜色(在“组件”下)只能是黄色或绿色,但在“组件”下的颜色可以是任何颜色 <components> <component color="yellow"/> <component color="green"/> <component-set> <component c

我希望达到下面所示的场景,其中我有相同的元素(在本例中是组件)和不同的父元素。我只想在第一级的应用程序中添加一个限制。例如,我想添加一个限制,即颜色(在“组件”下)只能是黄色或绿色,但在“组件”下的颜色可以是任何颜色

<components>
    <component color="yellow"/>
    <component color="green"/>
    <component-set>
      <component color="black"/>  
    </component-set>
  </xs:complexType>
</components>

这是我当前的xsd,我不应该编辑复杂的类型或元素名

<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema" targetNamespace="http://../custom.xsd" xmlns="http://../custom.xsd">

 <xs:complexType name="componentType">
    <xs:simpleContent>
      <xs:extension base="xs:string">
        <xs:attribute type="xs:string" name="color" use="required"/>
      </xs:extension>
    </xs:simpleContent>
  </xs:complexType>

  <xs:complexType name="component-setType" mixed="true">
    <xs:sequence>
      <xs:element type="componentType" name="component" maxOccurs="unbounded" minOccurs="1"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="componentsType">
    <xs:choice maxOccurs="unbounded" minOccurs="0">
      <xs:element type="componentType" name="component"/>
      <xs:element type="component-setType" name="component-set"/>
    </xs:choice>
  </xs:complexType>
</xs:schema>


您已经在使用本地元素声明(
xs:element
,在复杂类型的内容模型中显示
名称
属性)。出现在不同内容模型中的两个局部元素声明可以具有不同的类型;仅仅因为这两个元素都被称为
“component”
,并不意味着它们都需要类型
“componentType”

,是否可以有相同的类型,并且只对一个元素应用这些限制?不,如果有不同的限制,那么根据定义,它们是不同的类型。