XML schema attribute dependancies

岁酱吖の 提交于 2019-12-25 02:42:00

问题


I'd like to ask if it's possible in XML xsd schema declare dependant attributes...

Example:

<xsd:simpleType name="packCategories">
 <xsd:restriction base="xsd:byte">
  <xsd:minInclusive value="0"/>
  <xsd:maxInclusive value="4"/>
 </xsd:restriction>
</xsd:simpleType>

<xs:element name="pack">
 <xs:complexType>
   <!-- elements go here -->
   <xs:attribute type="packCategories" name="category" use="required"/>
   <xs:attribute type="xs:string" name="explanation" use="optional"/>
 </xs:complexType>
</xs:element>

Everything seems fine here, HOWEVER, i want the explanation attribute to be MANDATORY if category attribute is equal to 4. Is that possible? Maybe with elements then?


回答1:


If you're using XML Schema 1.0, you cannot express such a constraint in the schema, but you could use Schematron or check it at the application level.

If you're using XML Schema 1.1, you can specify co-occurrence constraints via XPath 2.0 using xs:assert like this:

  <xs:element name="pack">
    <xs:complexType>
      <!-- elements go here -->
      <xs:attribute type="packCategories" name="category" use="required"/>
      <xs:attribute type="xs:string" name="explanation" use="optional"/>
      <xs:assert test="@explanation or @packCategories != 4"/>
    </xs:complexType>
  </xs:element>


来源:https://stackoverflow.com/questions/20701626/xml-schema-attribute-dependancies

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!