2010-06-03 43 views
1

Ia处理一封电子邮件并在xml文档中保存一些标题。我还需要根据xml模式验证文档。忽略元素顺序xml中的xml验证

由于主题提示,我需要验证忽略元素顺序,但据我所知,这似乎是不可能的。我对么?

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

我的XML是这样的:

<headers> 
    <subject>bla bla bla</subject> 
    <recipient>[email protected]</recipient> 
    <recipient>rcp02domain.com</recipient> 
    <recipient>[email protected]</recipient> 
</headers> 

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

有没有什么可做的?

回答

4

是的,这是可能的。只要创建一个选项(当然在某些类型或元素内容模型中),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> 
+0

@segolas,我可以看到此解决方案符合您的要求,请展示礼貌接受解决方案。 – 2010-06-07 07:17:28

+0

我正在研究Netbeans,并且使用这个解决方案给了我一些我想弄明白的错误。所以,现在我不知道这个答案是否有效! – Segolas 2010-06-07 10:45:42

+1

嗯...我认为它的工作,但现在jaxb生成一个方法getSubjectOrRecipient()而不是getSubject()和getRecipient()。 根据http://www.w3schools.com/Schema/el_choice.asp“XML Schema选择元素只允许包含在声明中的元素中的一个出现在包含元素中。” 所以,似乎我只能在标题元素内有主题或收件人......我是对吗? – Segolas 2010-06-07 14:00:39

0

首先,一些要求猜测:

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

因为你只有两种不同的元素是很容易实现它:

<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> 
+0

不幸的是我只发布了一个我的xml的例子,实际上我有很多元素。我理解你的解决方案,但我认为会使我的模式不易读,并且难以维护... – Segolas 2010-06-07 13:39:53