以文本方式查看主题

-  计算机科学论坛  (http://bbs.xml.org.cn/index.asp)
--  『 DTD/XML Schema 』  (http://bbs.xml.org.cn/list.asp?boardid=23)
----  问一个关于XSD的问题  (http://bbs.xml.org.cn/dispbbs.asp?boardid=23&rootid=&id=47122)


--  作者:bornayuan
--  发布时间:5/18/2007 10:51:00 PM

--  问一个关于XSD的问题
有一个这样的需求:
父节点的某个属性值为指定值的时候,子节点的属性也必须为某个指定值。
例子如下:
<root>
  <a type="string">
    <b type="int"></b>
  </a>
</root>
当a的type为string的话,b的type必须为int。

针对如上的情况,请问如何书写XSD文件?谢谢各位朋友的帮忙。


--  作者:gemingke
--  发布时间:5/23/2007 11:27:00 AM

--  
首先请认识到,XML不是编程语言,所以请尽量少的出现条件判断

类似的问题建议你多多活用choice,举例如下:

<xs:complexType name="abc">
  <xs:choice>
   <xs:sequence>
    <xs:element ref="a"/>
   </xs:sequence>
   <xs:sequence>
    <xs:choice>
     <xs:element name="x"/>
    </xs:choice>
   </xs:sequence>
  </xs:choice>
 </xs:complexType>
 <xs:element name="a">
  <xs:complexType>
   <xs:sequence>
    <xs:element name="b">
     <xs:complexType>
      <xs:attribute name="type" type="xs:string" use="required" fixed="int"/>
     </xs:complexType>
    </xs:element>
   </xs:sequence>
   <xs:attribute name="type" type="xs:string" use="required" fixed="string"/>
  </xs:complexType>
 </xs:element>


W 3 C h i n a ( since 2003 ) 旗 下 站 点
苏ICP备05006046号《全国人大常委会关于维护互联网安全的决定》《计算机信息网络国际联网安全保护管理办法》
50.781ms