如何在 XSD 中添加条件验证: [英] How to add conditional validation in XSD :

查看:32
本文介绍了如何在 XSD 中添加条件验证:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现有的 XSD 代码段:

Existing XSD Snippet:

<xs:element name="searchcriteria">
   <xs:complexType>
     <xs:sequence>  
       <xs:element ref="filter" 
                   minOccurs="0" 
                   maxOccurs="unbounded" />
     </xs:sequence>
     <xs:attribute name="Request" 
                   type="RequestType" />
   </xs:complexType>
 </xs:element>  


<xs:element name="filter">
   <xs:complexType>
     <xs:sequence>
       <xs:element ref="filter" 
                   minOccurs="0" 
                   maxOccurs="unbounded" />
     </xs:sequence>
     <xs:attribute name="FieldName" 
                   type="FieldNameType" />
    </xs:complexType>
 </xs:element>

...

  • RequestType : 2 个值 R1 和 R2 的枚举

  • RequestType : Enumeration of 2 values R1 and R2

FieldNameType :枚举 2 个值 F1 和 F2

FieldNameType : Enumeration of 2 values F1, and F2

现在我想修改这个 XSD 以提供一个验证:

Now i want to modify this XSD to provide a validation that :

  1. 当 RequestType = R1 时,则允许的字段名称为 F1 和 F2
  2. 当请求类型 = R2 时,则允许的字段名称为 F1、F3 和 F4.(可能需要一个新的枚举)

如何添加此类验证?谢谢.

How can i add such validations ? Thanks.

推荐答案

XSD(和大多数其他 XML 模式语言)的基本思想是将验证行为绑定到元素类型.如果您希望一组实例以一种方式验证而另一组实例以另一种方式验证(这个想法是这样的),初步证据表明它们实际上是两种不同类型的元素,应该有两个不同的名称.

The basic idea of XSD (and most other XML schema languages) is to bind validation behavior to element types. If you want one set of instances validated one way and another set of instances validated another way (goes the idea), it's prima facie evidence that they are actually two distinct types of elements, and should have two distinct names.

因此,使 XSD 验证您的请求的最简单方法是将 searchcriteria 元素替换为一对名为 R1R2.用不同的类型声明它们,如你所愿:要么它们都有一个名为 filter 的元素,带有一个携带适当枚举类型的属性,或者 R1 被声明为具有 filtercode>F1F2 作为孩子,并且 R2 被声明为可以选择 F1, F3F4.

So the simplest way to make XSD validate your requests as you describe is to replace your searchcriteria element with a pair of elements named R1 and R2. Declare them with different types, as you wish: either they each have an element named filter with an attribute carrying an appropriate enumerated type, or R1 is declared as having either an F1 or an F2 as a child, and R2 is declared as having a choice of F1, F3, or F4.

如果您不能或不会这样做,您可以使用 XSD 1.1 及其条件类型分配功能为 searchcriteria 声明两种类型(否则您将在 searchcriteria 中使用这两种类型code>R1 和 R2) 并根据 Request 属性的值分配适当的类型.您还可以在 searchcriteria 上使用断言来检查共同约束.

If you can't or won't do this, you can use XSD 1.1 and its conditional type assignment feature to declare two types for searchcriteria (the two types you would otherwise have used for R1 and R2) and assign the appropriate type based on the value of the Request attribute. You can also use assertions on searchcriteria to check the co-constraint.

如果您无权访问 XSD 1.1 验证器,请告诉 XSD 1.0 验证器的供应商您想要升级,并探索使用 Schematron 断言来检查约束.

If you don't have access to an XSD 1.1 validator, tell the vendor of your XSD 1.0 validator that you'd like an upgrade, and explore the use of Schematron assertions to check the constraint.

另见这个本质上类似的问题以获得更具体的信息讨论选项,并附有示例.

See also this essentially similar question for more concrete discussion of the options, with examples.

这篇关于如何在 XSD 中添加条件验证:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆