Xml Schema:根据另一个属性的类型限制一个属性的值类型 [英] Xml Schema: Restricting the value type of an attirbute based on the type of another attribute

查看:46
本文介绍了Xml Schema:根据另一个属性的类型限制一个属性的值类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下架构定义了一个具有两个属性的book"元素,即DEPARTMENT"和BOOK_NAME".我想改进架构,以便在编写相应的 XML 文件时,首先出现DEPARTMENT"属性,选择它的值(PHOTOGRAPHY/COMPUTER_SCIENCE/MEDICINE),然后根据DEPARTMENT"属性的值,BOOK_NAME"' 从相应的列表中选择.因此,如果 DEPARTMENT="PHOTOGRAPHY",XML 文件的作者应该无法从 COMPUTER_SCIENCE 或 MEDICINE 部门选择一本书.

The following schema defines a 'book' element with two attributes i.e., 'DEPARTMENT' and 'BOOK_NAME'. I want to improve the schema so that while writing a corresponding XML file, the 'DEPARTMENT' attribute appears first, it's value be selected (PHOTOGRAPHY/COMPUTER_SCIENCE/MEDICINE) and then based on the value of the 'DEPARTMENT' attribute, a 'BOOK_NAME' is selected from the corresponding list. So, if DEPARTMENT="PHOTOGRAPHY", the author of the XML file shouldn't be able to select a book from COMPUTER_SCIENCE or MEDICINE department.

请不要建议拆分 book 元素并将这些属性作为子元素,我正在寻找一种解决方案,根据为另一个属性选择的类型来限制一个属性的类型.

Please don't suggest to split the book element and have these attributes as child elements, I'm looking for a solution that restricts the type of one attribute based on the type selected for another attribute.

谢谢

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
  <xs:element name="books">
    <xs:complexType>
      <xs:sequence>
        <xs:element ref="book"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
  <xs:element name="book">
    <xs:complexType>
      <xs:attribute name="DEPARTMENT" type="departmentName" use="required"/>
      <xs:attribute name="BOOK_NAME" type="should be photographyBooks or computerScienceBooks or medicineBooks depending on the selected department" use="required"/>
    </xs:complexType>
  </xs:element>

  <xs:simpleType name="departmentName">
    <xs:restriction base="xs:string">
      <xs:enumeration value="PHOTOGRAPHY"/>
      <xs:enumeration value="COMPUTER_SCIENCE"/>
      <xs:enumeration value="MEDICINE"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="photographyBooks">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Adobe PhotoShop in a nutshell"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="computerScienceBooks">
    <xs:restriction base="xs:string">
      <xs:enumeration value="An Intruduction to Computer Programming using C"/>
      <xs:enumeration value="Best Practices in Java"/>
      <xs:enumeration value="Guide to Perl Scripting"/>
    </xs:restriction>
  </xs:simpleType>

  <xs:simpleType name="medicineBooks">
    <xs:restriction base="xs:string">
      <xs:enumeration value="Principles an Practices of Medicine"/>
      <xs:enumeration value="Mortality in relation to Smoking"/>
      <xs:enumeration value="Heart Protection Study"/>
    </xs:restriction>
  </xs:simpleType>
</xs:schema>

推荐答案

XSD 1.0 并非旨在表达此类约束.

XSD 1.0 is not designed to express constraints of this kind.

如果您想使用 XSD 1.0,您不希望这种设计用于您的 XML.如果您想要这种 XML 结构,您就不想使用 XSD 1.0.替代方案包括 XSD 1.1(使用断言或条件类型分配)或 Relax NG 或 Schematron.

If you want to work with XSD 1.0, you don't want this design for your XML. If you want this XML structure, you don't want to work with XSD 1.0. Alternatives include XSD 1.1 (using assertions or conditional type assignment) or Relax NG or Schematron.

这篇关于Xml Schema:根据另一个属性的类型限制一个属性的值类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆