-->

How to specify “any element” in a sequence

2019-02-25 13:06发布

问题:

Could someone tell me how to specify in an XSD, that an element can contain any number of arbitrary elements? For exmaple, if I have

<person>
    <name>Foo</name>
</person>

I would like to allow any element after the "<name>" element.

<person>
    <name>Foo</name>
    <gender>male</gender>
</person>

<person>
    <name>Foo</name>
    <address>west of here</address>
</person>

<person>
    <name>Foo</name>
    <address>west of here</address>
    <spooge>something else</spooge>
</person>

That is, the name element is required, but after that, you can add any arbitrary element with any type. So if this is the XSD element to describe the <person> element, what would go after the "<xs:element name='name' .../>"

<xs:element name="person">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="name" type="xs:string"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

回答1:

You can use xsd:any:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           version="1.0">
  <xs:element name="person">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="name" type="xs:string"/>
        <xs:any processContents="skip" namespace="##any"
                minOccurs="0" maxOccurs="unbounded"/>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Pay particular attention to the possible values for processContent:

  • skip: Allow any element, and ignore any definition for the element.
  • lax: Allow any element, but validate the element if any definition is found for it.
  • strict: Allow any element, but require that it have a definition and be valid per that definition.


回答2:

The solution for me was :

<xs:complexType name="dataType">
    <xs:simpleContent>
        <xs:extension base="xs:string">
            <xs:anyAttribute processContents="skip"/>
        </xs:extension>
    </xs:simpleContent>
</xs:complexType>

My IDE (PHPStorm) run error with xs:any but xs:anyAttribute is supported