简体   繁体   English

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

[英]ignoring elements order in xml validation against xsd

Ia processing an email and saving some header inside a xml document. 即处理电子邮件并在xml文档中保存一些标头。 I also need to validate the document against a xml schema. 我还需要针对xml模式验证文档。

As the subject suggest, I need to validate ignoring the elements order but, as far as I read this seems to be impossible. 正如主题所暗示的那样,我需要验证忽略元素顺序,但是据我看来,这似乎是不可能的。 Am I correct? 我对么?

If I put the headers in a <xsd:sequence> , the order obviously matter. 如果将标题放在<xsd:sequence> ,顺序显然很重要。 If I us <xsd:all> the order is ignored but for some strange reason this imply that the elements must occur at least once. 如果我使用<xsd:all>该顺序将被忽略,但是由于某些奇怪的原因,这意味着这些元素必须至少出现一次。

My xml is something like this: 我的xml是这样的:

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

but I think the final document is valid even if subject and recipient elements are swapped. 但是我认为即使交换主题和收件人元素,最终文档也是有效的。

There is really nothing to do? 真的有事可做吗?

Yes, it is possible. 对的,这是可能的。 Just create a choice (in some type or element content model, of course) with maxOccurs set to unbounded. 只需创建一个选择(当然是在某种类型或元素内容模型中),并将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>

First, some requirement guessing: 首先,一些需求猜测:

  • "subject" is mandatory “主题”是强制性的
  • At least one "recipient" is mandatory 至少有一个“收件人”是强制性的

Since you have only two different elements it is very easy to accomplish it: 由于您只有两个不同的元素,因此很容易实现:

<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