简体   繁体   中英

How to restrict the values of an element according to the value of its attribute

In my homework project of university, I need to write a XSD(XML Schema) that describes which elements a school history would have. In one of the cases, I have the element "Disciplina", that has an attribute "categoria"(exemplified below):

<Disciplina categoria = "atividade academica">
  (...other elements here...)
  <situacao>AC</situacao>
</Disciplina>

Besides that, the element "situacao" has a range on some specific string values: "AP,RM,RF,ED,AB,AE,AI,TR,TD,RI,IN" and "categoria" is the same logic: "obrigatoria,optativa,livre escolha,atividade academica."

But, when the "categoria" is "atividade academica", I want to restrict the possible values of the element "situacao" only to "AC" or "NC", otherwise, the restriction values will be "AP","RM","RF","ED","AB","AE","AI","TR","TD","RI","IN". The xml below express a high level of the ideia I'm trying to represent:

<element>
  <name>situacao</name>
  <type>String</type>
  <range>AP,RM,RF,RF,ED,AB,AE,AI,TR,TD,RI,IN</range>
  <range categoria = "atividade academica">AC,NC</range>
</element>

My question is: How can I represent this change of restriction of "situacao" values, according to "categoria" value, in XML Schema?

This can't be done in XSD 1.0 (which is the version supported by many XSD processors, eg those from Microsoft). In XSD 1.1 (supported by Xerces, Saxon, and Altova) it can be done in a couple of ways:

(a) general purpose assertions, for example

<xs:assert test="(@categoria = 'atividade academia' and situacio = ('AC', 'NC')) 
  or (@categoria = 'xxxx' and situacio = ('ED', 'AB'))...."/>

(b) conditional type assignment, using xs:alternative : the idea here is to define the type of an element as a function of the values of its attributes; again this is done using XPath expressions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM