schema验证包含布尔值的xml

时间:2022-10-26 17:02:23

Hi I have a xml like this:

嗨我有这样的xml:

<xml><fullname>
<name attrib="true"/>
<lastname1 attrib="false"/>
<lastname2 attrib="false"/></fullname></xml>

I need to create a schema to validate it in such a way that it will allow only 1 attrib with value "true" (and the rest of them must be false [the attrib attribute is defined to be xs:boolean]), so i added a unique check in the element fullname like this:

我需要创建一个模式来验证它,它只允许1个attrib值为“true”(其余的必须为false [attrib属性定义为xs:boolean]),所以我在元素fullname中添加了一个唯一的检查,如下所示:

   <xs:unique name="attribcheck">
<xs:selector xpath="name|lastname1|lastname2"/>
<xs:field xpath="@attrib"/>

Of course, it will detect that there is a duplicated "true", but also, it will detect a duplicated "false". Does anyone know if there is a way to set a restriction on which value to apply the unique constraint? meaning that i can ensure that only one of them is "true" at any given time

当然,它会检测到存在重复的“true”,但是,它也会检测到重复的“false”。有没有人知道是否有办法对应用唯一约束的值设置限制?这意味着我可以确保在任何给定时间只有其中一个是“真实的”

1 个解决方案

#1


2  

XML Schema 1.0 does not support co-occurrence constraints - you cannot express a condition on one element based on the value of another.

XML Schema 1.0不支持共现约束 - 您不能基于另一个元素的值在一个元素上表达条件。

This will change in XML Schema 1.1, but the latter is only in draft stage and is still changing, and will not be widely implemented for some time.

这将在XML Schema 1.1中发生变化,但后者仅处于草案阶段并且仍在变化,并且在一段时间内不会被广泛实施。

However, if you use Saxon 9.2, it has preliminary support for XML Schema 1.1 now. See the feature grid to determine which version is needed for schema validation.

但是,如果您使用Saxon 9.2,它现在已经初步支持XML Schema 1.1。请参阅功能网格以确定架构验证所需的版本。

#1


2  

XML Schema 1.0 does not support co-occurrence constraints - you cannot express a condition on one element based on the value of another.

XML Schema 1.0不支持共现约束 - 您不能基于另一个元素的值在一个元素上表达条件。

This will change in XML Schema 1.1, but the latter is only in draft stage and is still changing, and will not be widely implemented for some time.

这将在XML Schema 1.1中发生变化,但后者仅处于草案阶段并且仍在变化,并且在一段时间内不会被广泛实施。

However, if you use Saxon 9.2, it has preliminary support for XML Schema 1.1 now. See the feature grid to determine which version is needed for schema validation.

但是,如果您使用Saxon 9.2,它现在已经初步支持XML Schema 1.1。请参阅功能网格以确定架构验证所需的版本。