3

I need to create a XSD 1.0 that validates a XML file.

The validation will use lxml.etree from python, and this tool is supporting just XML Schema 1.0 (lxml with schema 1.1)

The structure that I need to use is of type:

    item
    | owners*
    | config+
    | | config_id
    | | tests*
    | | picked?
    | | capability*
    | | | name
    | | | value

Used notation are:

  • * The element can occur zero or more times.
  • + The element can occur one or more times.
  • ? The element is optional.

Elements in config tag can be in any order, this means that I can't use <sequence> indicator. <all> indicator is giving me the random order, but in this case the maxOccurs is 1. <choice> indicator with maxOccurs="unbounded" will give me the random order and the multiple number of elements, but there will be no bottom limit for elements.

My XSD file looks something like:

<?xml version="1.0" encoding="ISO-8859-1" ?>

<!--Schema version: 1.0, date: 29-02-2016-->
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <!-- definition of complex types -->    
  <xs:complexType name="capability_type">
    <xs:sequence>
      <xs:element name="name" type="xs:string" />
      <xs:element name="value" type="xs:string" />
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="config_type">
    <xs:all>
      <xs:element name="config_id" type="xs:string" />
      <xs:element name="tests" type="xs:string" minOccurs="0" 
                          maxOccurs="unbounded" />
      <xs:element name="picked" type="xs:string" minOccurs="0" />
      <xs:element name="capability" type="capability_type" 
                          minOccurs="0" maxOccurs="unbounded" />
    </xs:all>
  </xs:complexType>

  <xs:complexType name="item_type">
    <xs:sequence>
      <xs:element name="owners" type="xs:string" minOccurs="0" 
                          maxOccurs="unbounded" />
      <xs:element name="config" type="config_type" minOccurs="1" 
                          maxOccurs="unbounded" />
    </xs:sequence>
  </xs:complexType>

  <!-- definition of schema -->
  <xs:element name="item" type="item_type" />

</xs:schema>

Using this schema I receive error:

element element: Schemas parser error : Element '{http://www.w3.org/2001/XMLSchema}element': Invalid value for maxOccurs (must be 0 or 1).

Is there any alternatives for my problem ?

Community
  • 1
  • 1
Iulian Stana
  • 1,632
  • 1
  • 14
  • 17
  • Alternative approach: when making schemas I almost always use a schema generator, which I feed in my proposed final XML. I use a simple tool I wrote in .NET but they are also floating around online, like here: https://xml.mherman.org ... The output absolutely must be reviewed carefully. Also those online tools are sometimes total garbage. – DAG Nov 16 '21 at 16:17

1 Answers1

1

Yes, there are alternatives:

  1. Completely abandon the random order requirement. It tends to be more trouble than it's worth.
  2. Partially abandon the random order requirement by migrating config_id and picked outside of the xs:all and migrating maxOccurs="unbounded" from the children of xs:all to xs:all itself.
  3. Keep the random order requirement, migrate maxOccurs="unbounded" to xs:all, and use XSD 1.1 assertions to enforce the other occurrence constraints.
kjhughes
  • 106,133
  • 27
  • 181
  • 240