2013-03-14 62 views
1

如何描述架构文件中的“content”元素,以便知道子元素的顺序,但在“元数据”之后和“content-section”之前,可能会放置任何元素?我如何用一些任何子元素描述一个元素,并且一些已知?

基本上我想要做下面的例子中描述的内容。这是粗糙的,将不起作用。我会将1个或多个元素放置在当前位置。

<xs:element name="content"> 
    <xs:complexType> 
     <xs:sequence> 
     <xs:element name="metadata" minOccurs="0" maxOccurs="unbounded"></<xs:element> 
     <xs:any> 
     <xs:element name="content-section" minOccurs="0" maxOccurs="unbounded">/<xs:element> 
     <xs:element name="content-footer" minOccurs="0" maxOccurs="unbounded"> 
    </<xs:element>  
</xs:element> 

XML:

<content> 
    <metadata></metadata> 
    <h1>Not in schema</h1> 
    <p>also not in schema</p> 
    <content-section></content-section> 
    <content-footer></content-footer> 
    </content> 

回答

0

您可能遇到的问题之一称为唯一粒子归因。这一切都取决于你想要的元素可能包含哪些命名空间。

最糟糕的情况是任何命名空间。因此,接受您的要求(与InfantPro'Aravind'不同,后者通过将content-section设置为必填项,并且将您的任何元素的maxOccurs限制为任意数字来解决问题),这是我们从哪里开始(不良错误,违反UPA)。首先,解决任何的maxOccurs的允许1 or more elements to be placed in the location

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="content"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="metadata" minOccurs="0" maxOccurs="unbounded"/> 
       <xsd:any maxOccurs="unbounded"/> 
       <xsd:element name="content-section" minOccurs="0" maxOccurs="unbounded"/> 
       <xsd:element name="content-footer" minOccurs="0" maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

错误是:

Error occurred while loading [how-can-i-describe-an-element-with-some-any-child-element-and-some-known.xsd], line 8 position 6. 
Wildcard '##any' allows element 'metadata', and causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence. 

现在你需要解决这个问题。这是你的选择:

1)限制任何别的东西(例如##等)的命名空间:

<xsd:any maxOccurs="unbounded" namespace="##other"/> 

如果你不能限制你的命名空间,这是你的选择:

2)包住any内容到另一个元件(称为扩展):

<?xml version="1.0" encoding="utf-8" ?> 
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) --> 
<xsd:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <xsd:element name="content"> 
     <xsd:complexType> 
      <xsd:sequence> 
       <xsd:element name="metadata" minOccurs="0" maxOccurs="unbounded"/> 
       <xsd:element name="extension"/> 
       <xsd:element name="content-section" minOccurs="0" maxOccurs="unbounded"/> 
       <xsd:element name="content-footer" minOccurs="0" maxOccurs="unbounded"/> 
      </xsd:sequence> 
     </xsd:complexType> 
    </xsd:element> 
</xsd:schema> 

所有其它涉及移动底部的any内容。

3)将您的any右移到底部,并在其前面加上一个必需元素。

如果你想要这种类型的全球性,并允许它不是最终的,那么你进入其他模式。

+0

很好解释..我想我应该给出类似的解释,而不是显示过度使用'any':P – 2013-03-14 13:03:47

+0

太棒了!我最终添加了另一个元素。选项2.感谢您的答案! – Nick 2013-03-14 13:05:53

1

Any用自己的方式processContents="skip"作品用法..

我捡了一个更好的例子:

<?xml version="1.0" encoding="utf-8"?> 
<content> 
    <metadata></metadata> 
    <h1>Not in schema</h1> 
    <p>also not in schema</p> 
    <content-section></content-section> 
    <content-footer>Do not validate this as well</content-footer> 
</content> 

及相关XSD为此:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema"> 
    <xs:element name="content" type="content"/> 
    <xs:complexType name="content"> 
    <xs:sequence> 
     <xs:element name="metadata" type="xs:string"/> 
     <xs:any processContents="skip" minOccurs="2" maxOccurs="2"/> 
     <xs:element name="content-section" type="xs:string"/> 
     <xs:any processContents="skip" minOccurs="1" maxOccurs="1"/> 
    </xs:sequence> 
    </xs:complexType> 
</xs:schema> 
相关问题