Skip to content

Failed validation: XsdFieldSelector(path='@trigger|@attime') field selects multiple values #418

@aardalath

Description

@aardalath

Hi,

I have an XML with events defined that can occur when a certain trigger from a third system arrives or at a given fixed time. For that, we wanted to have a single event element type definition with several attributes, two of them, trigger or attime, mutually exclusive. In order to implement that I found this solution:

. . .
      <xs:element type="eventType" name="event" maxOccurs="unbounded" minOccurs="0">
        <xs:key name="attributeKey">
          <xs:selector xpath="."/>
          <xs:field xpath="@trigger|@attime"/>
        </xs:key>
      </xs:element>
. . .

So, I created a file compliant with this schema, with event nodes having either trigger or attime attribute, but not both, and another one with a failing instance of event, with trigger and attime at the same time.

The problem is that both failed, and XMLSchema.validate() raises in both cases an exception for a valid event instance (with only the trigger attribute), as follows:

Traceback (most recent call last):
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/elements.py", line 911, in collect_key_fields
    xsd_fields = identity.get_fields(xsd_element.xpath_node)
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/identities.py", line 331, in get_fields
    raise XMLSchemaValueError(msg % field)
xmlschema.exceptions.XMLSchemaValueError: XsdFieldSelector(path='@trigger|@attime') field selects multiple values!

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/user/trigev/etc/trigev/schema/./validate.py", line 12, in <module>
    raise(ee)
  File "/home/user/trigev/etc/trigev/schema/./validate.py", line 9, in <module>
    isvalid = schema.validate(sys.argv[2])
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/schemas.py", line 1698, in validate
    for error in self.iter_errors(source, path, schema_path, use_defaults,
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/schemas.py", line 1829, in iter_errors
    for result in xsd_element.iter_decode(elem, **kwargs):
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/elements.py", line 739, in iter_decode
    for result in content_decoder.iter_decode(obj, validation, **kwargs):
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/groups.py", line 1050, in iter_decode
    for result in xsd_element.iter_decode(child, validation, **kwargs):
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/elements.py", line 739, in iter_decode
    for result in content_decoder.iter_decode(obj, validation, **kwargs):
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/groups.py", line 1050, in iter_decode
    for result in xsd_element.iter_decode(child, validation, **kwargs):
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/elements.py", line 836, in iter_decode
    yield from self.collect_key_fields(obj, xsd_type, validation, nilled, **kwargs)
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/elements.py", line 922, in collect_key_fields
    yield self.validation_error(validation, err, obj, **kwargs)
  File "/home/user/venv/lib/python3.10/site-packages/xmlschema/validators/xsdbase.py", line 249, in validation_error
    raise error
xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element 'event' at 0x71e1d337c040> with XsdElement(name='event', occurs=[0, None]):

Reason: XsdFieldSelector(path='@trigger|@attime') field selects multiple values!

Schema component:

  <xs:element xmlns:xs="http://www.w3.org/2001/XMLSchema" type="eventType" name="event" maxOccurs="unbounded" minOccurs="0">
    <xs:key name="attributeKey">
      <xs:selector xpath="." />
      <xs:field xpath="@trigger|@attime" />
    </xs:key>
  </xs:element>

Instance type: <class 'xml.etree.ElementTree.Element'>

Instance:

  <event trigger="TS01" command="powu56.py" parameters="-on -ports '1,2,3'" />

Path: /mqttservices/events/event[1]

I checked everything with xmllint and in that case everything works as expected, that is, the valid XML file validates, and the failing one fails to validate, and the correct reason is provided. But, of course, I would like to use a Python solution like xmlschema.

Please, find attached the sample XML and XSD files, as well as a very simplistic Python script.

test_keys.zip

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions