简体   繁体   English

如何根据元素的值限制元素的值

[英]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. 在大学的家庭作业项目中,我需要编写一个XSD(XML Schema)来描述学校历史中的哪些元素。 In one of the cases, I have the element "Disciplina", that has an attribute "categoria"(exemplified below): 在其中一个案例中,我有“Disciplina”元素,它具有属性“categoria”(如下所示):

<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." 除此之外,元素“situacao”在一些特定的字符串值上有一个范围:“AP,RM,RF,ED,AB,AE,AI,TR,TD,RI,IN”和“categoria”是相同的逻辑:“ 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". 但是,当“categoria”是“atividade academica”时,我想将元素“situacao”的可能值仅限制为“AC”或“NC”,否则,限制值将为“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: 下面的xml表达了我想要表达的高水平的意识形态:

<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? 我的问题是: 如何根据XML Schema中的“categoria”值来表示“situacao”值的限制变化?

This can't be done in XSD 1.0 (which is the version supported by many XSD processors, eg those from Microsoft). 这不能在XSD 1.0中完成(这是许多XSD处理器支持的版本,例如来自Microsoft的版本)。 In XSD 1.1 (supported by Xerces, Saxon, and Altova) it can be done in a couple of ways: 在XSD 1.1中(由Xerces,Saxon和Altova支持),它可以通过以下几种方式完成:

(a) general purpose assertions, for example (a)例如通用断言

<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; (b)条件类型赋值,使用xs:alternative :这里的想法是将元素的类型定义为其属性值的函数; again this is done using XPath expressions. 再次使用XPath表达式完成此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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