[英]How to restrict the values of an element according to the value of its attribute
在大学的家庭作业项目中,我需要编写一个XSD(XML Schema)来描述学校历史中的哪些元素。 在其中一个案例中,我有“Disciplina”元素,它具有属性“categoria”(如下所示):
<Disciplina categoria = "atividade academica">
(...other elements here...)
<situacao>AC</situacao>
</Disciplina>
除此之外,元素“situacao”在一些特定的字符串值上有一个范围:“AP,RM,RF,ED,AB,AE,AI,TR,TD,RI,IN”和“categoria”是相同的逻辑:“ obrigatoria,optativa,livre escolha,atividade academica。“
但是,当“categoria”是“atividade academica”时,我想将元素“situacao”的可能值仅限制为“AC”或“NC”,否则,限制值将为“AP”,“RM” , “RF”, “ED”, “AB”, “AE”, “AI”, “TR”, “TD”, “RI”, “IN”。 下面的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>
我的问题是: 如何根据XML Schema中的“categoria”值来表示“situacao”值的限制变化?
这不能在XSD 1.0中完成(这是许多XSD处理器支持的版本,例如来自Microsoft的版本)。 在XSD 1.1中(由Xerces,Saxon和Altova支持),它可以通过以下几种方式完成:
(a)例如通用断言
<xs:assert test="(@categoria = 'atividade academia' and situacio = ('AC', 'NC'))
or (@categoria = 'xxxx' and situacio = ('ED', 'AB'))...."/>
(b)条件类型赋值,使用xs:alternative
:这里的想法是将元素的类型定义为其属性值的函数; 再次使用XPath表达式完成此操作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.