如何描述具有某个ANY子元素的元素和一些已知的元素?

时间:2013-03-14 05:11:20

标签: xml xsd

如何描述模式文件中的“content”元素,以便了解子元素的顺序,但在“元数据”之后和“内容部分”之前可以放置任何元素?

基本上我想做下面例子中描述的内容。粗糙的这个不起作用。我将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>

2 个答案:

答案 0 :(得分:1)

AnyprocessContents="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>

答案 1 :(得分:0)

您可能遇到的一个问题称为“唯一粒子”归因。这一切都取决于您想要的any元素的名称空间。

最糟糕的情况是任何命名空间。所以,接受你的要求(不同于InfantPro'Aravind',它通过规定内容部分是必需的,你希望它是可选的,并将你的任何元素的maxOccurs限制为任意数字),这是我们从( BAD架构,违反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)将any的名称空间限制为其他名称(例如## other):

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

如果您无法限制命名空间,可以选择以下选项:

2)将any内容包装到另一个元素(称为Extension)中:

<?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向右移动到底部,并在其前面加上必填元素。

如果你希望这种类型是全局的并且允许它不是最终的,那么你会进入其他模式。

相关问题