xsd 中的元素名称相同,但验证逻辑不同 [英] Same element name in xsd, but different validation logic

查看:26
本文介绍了xsd 中的元素名称相同,但验证逻辑不同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个看起来像这样的 xml 文件

I have xml file that seems like that

<doc>
 <field name="simple_meta">book</field>
 <field name="complex_meta">journal</field>
 <field name="text_date">some text</field>
</doc>

我想根据属性name"中的值按模式验证元素文本.也就是说,如果属性的值为simple_meta",我想进行simpleRestriction"验证,以防complex_meta"进行complexRestriction"验证.问题是我不能在同一个节点下定义同名元素.有人可以帮我解决这个问题吗?

I would like to validate element text by pattern based on value in attribute "name". that is, if the value of attribute is "simple_meta" I want to make the "simpleRestriction" validation, in case of the "complex_meta" to make the "complexRestriction" validation. The problem is that I cannot define element with same name under the same node. Can someone to help me to resolve this problem?

<xs:schema ......>
   <xs:simpleType name="simpleRestriction">
      <xs:restriction base="xs:string">
         <xs:maxLength value="20"/>
      </xs:restriction>
   </xs:simpleType>
   <xs:simpleType name="complexRestriction">
      <xs:restriction base="xs:string">
         <xs:maxLength value="10"/>
         <xs:pattern value="([\w])*"/>
      </xs:restriction>
   </xs:simpleType>

   <xs:element name="str">
      <xs:complexType>
         <xs:simpleContent>
            <xs:extention base="simpleRestriction">
               <xs:attribute name="name">
                  <xs:simpleType>   
                     <xs:restriction base="xs:string">  
                        <xs:pattern value="simple_meta"/>
                     </xs:restriction>
                  </xs:simpleType>   
               </xs:attribute>
            </xs:extention>
         </xs:simpleContent>
      </xs:ComplexType>
   </xs:element>
   <xs:element name="str">
      <xs:complexType>
         <xs:simpleContent>
            <xs:extention base="complexRestriction">
               <xs:attribute name="name">
                  <xs:simpleType>   
                     <xs:restriction base="xs:string">  
                        <xs:pattern value="complex_meta"/>
                     </xs:restriction>
                  </xs:simpleType>   
               </xs:attribute>
            </xs:extention>
         </xs:simpleContent>
      </xs:ComplexType>
   </xs:element>
</xs:schema>

推荐答案

简短的回答是:不要那样做.

The short answer is: don't do that.

XSD 旨在主要基于(在简单情况下仅)基于元素名称验证元素.如果您有三种不同的验证逻辑,那么最好告诉 XSD 验证器存在三种类型的元素(可能命名为 simple_meta、complex_meta 和 test_date),而不是难以置信地声称只有一种类型的元素.使用公共基类型来明确三个元素类型之间的关系,或者使用公共替换组来关联三个元素类型本身.

XSD is designed to validate elements based primarily (and in the simple case exclusively) on the element's name. If you have three different validation logics, then you will do better to tell the XSD validator that there are three types of elements (named, perhaps, simple_meta, complex_meta, and test_date) instead of claiming implausibly that there is just one type of element. Use a common base type to make the relation among the types of the three elements explicit, or a common substitution group to relate the three element types themselves.

如果您真的必须这样做,或者如果您真的知道自己在做什么并且想要这样做(我不确定这种组合是否可行,但我会尽量做到心胸开阔),您的基本选项是:

If you really have to do that, or if you really know what you're doing and want to do that (I'm not sure that combination is possible, but I'll try to be broad-minded here), your basic options are:

  • 在实例中使用 xsi:type 来指定实例中每个元素的类型(大致类似于您的 name 属性,但命名了声明的类型在架构中)

  • use xsi:type in the instance to specify the type of each element in the instance (roughly similar to your name attribute, but names a type declared in the schema)

使用 XSD 1.1 和条件类型赋值

use XSD 1.1 and conditional type assignment

使用 XSD 1.1 和断言

use XSD 1.1 and assertions

或者留下 XSD:

  • 使用 Schematron 和断言

  • use Schematron and assertions

使用RelaxNG,将属性的不同值写入内容模型

use RelaxNG and write the different values of the attribute into the content model

这篇关于xsd 中的元素名称相同,但验证逻辑不同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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