繁体   English   中英

忽略针对xsd的xml验证中的元素顺序

[英]ignoring elements order in xml validation against xsd

即处理电子邮件并在xml文档中保存一些标头。 我还需要针对xml模式验证文档。

正如主题所暗示的那样,我需要验证忽略元素顺序,但是据我看来,这似乎是不可能的。 我对么?

如果将标题放在<xsd:sequence> ,顺序显然很重要。 如果我使用<xsd:all>该顺序将被忽略,但是由于某些奇怪的原因,这意味着这些元素必须至少出现一次。

我的xml是这样的:

<headers>
  <subject>bla bla bla</subject>
  <recipient>rcp01@domain.com</recipient>
  <recipient>rcp02domain.com</recipient>
  <recipient>rcp...@domain.com</recipient>
</headers>

但是我认为即使交换主题和收件人元素,最终文档也是有效的。

真的有事可做吗?

对的,这是可能的。 只需创建一个选择(当然是在某种类型或元素内容模型中),并将maxOccurs设置为无界。

<xs:element name="headers">
    <xs:complexType>
        <xs:choice maxOccurs="unbounded">
            <xs:element name="subject" type="xs:string"/>
            <xs:element name="recipient" type="xs:string"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

首先,一些需求猜测:

  • “主题”是强制性的
  • 至少有一个“收件人”是强制性的

由于您只有两个不同的元素,因此很容易实现:

<xs:element name="headers">
<xs:complexType>
 <xs:choice>
   <xs:sequence><!-- The recipient MUST be after the subject -->         
     <xs:element name="subject" type="xs:string" />
     <xs:element name="recipient" minOccurs="1" maxOccurs="unbound" type="xs:string" />
   </xs:sequence>
   <xs:sequence><!-- The recipient is before the subject -->          
     <xs:element name="recipient" minOccurs="1" maxOccurs="unbound" type="xs:string" />
     <xs:element name="subject" type="xs:string" />
     <xs:element name="recipient" minOccurs="0" maxOccurs="unbound" type="xs:string" />
   </xs:sequence>
 </xs:choice>
</xs:complexType>
</xs:element>

暂无
暂无

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

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