Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.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 XSD-取决于属性值的子元素_Xml_Xsd - Fatal编程技术网

Xml XSD-取决于属性值的子元素

Xml XSD-取决于属性值的子元素,xml,xsd,Xml,Xsd,我正在尝试为以下XML创建XSD架构: <root> <!-- The actual file must contain one of the following constraints --> <constraint type="interval"> <min>100</min> <max>200</max> </constraint> <constraint

我正在尝试为以下XML创建XSD架构:

<root>
  <!-- The actual file must contain one of the following constraints -->
  <constraint type="interval">
    <min>100</min>
    <max>200</max>
  </constraint>

  <constraint type="equals">
    <value>EOF</value>
  </constraint>
</root>

100
200
EOF
约束元素的子元素取决于type属性的值

我已经使用一个定义type属性的抽象类型和两个定义子元素的扩展类型成功地验证了XML。这需要我用xsi:type属性来修饰XML,命名实际的扩展类型:

  <constraint type="interval" xsi:type="intervalConstraintType">
    <min>100</min>
    <max>200</max>
  </constraint>

100
200
遗憾的是,我无法控制XML结构,很难引入新的属性


这在XSD中可行吗?有更合适的替代方案吗?

我认为这应该是可能的,但我目前不知道自己该怎么做。作为一种解决方法,您可以动态重写xml以包含扩展

编辑:嗯,看起来这是不可能的,至少在XSD1.0中是不可能的

约束元素的子元素取决于type属性的值

我认为在XSD1.1中使用断言是可能的。 您的模式可能看起来像这样(未测试)



您将如何动态重写xml?我假设您正在用某种编程语言处理它,在这种情况下,您将创建一个从另一个源加载的xml的本地副本,并对本地副本进行修改。
<!-- ... -->
<xs:element name="constraint"> 
  <xs:complexType> 
     <xs:sequence> 
         <xs:element name="min" type="xs:decimal" minOccurs="0" maxOccurs="1" /> 
         <xs:element name="max" type="xs:decimal" minOccurs="0" maxOccurs="1" />
         <xs:element name="value" type="xs:string"minOccurs="0" maxOccurs="1" />
     </xs:sequence> 
     <xs:attribute name="type" type="contraintType" />

     <xs:assert test="

        if (@type eq 'equals')
        then (exist(value) and empty(min, max))
        else (exist(min) and exist(max) and empty(value))

     "/>

</xs:complexType> 
</xs:element>

<xs:simpleType name="contraintType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="interval"/>
        <xs:enumeration value="equals"/>
    </xs:restriction>
</xs:simpleType>