sissaschool / xmlschema

XML Schema validator and data conversion library for Python
MIT License
404 stars 75 forks source link

Validation of key erroneously fails with 'duplicated value' #364

Closed pandaxtc closed 11 months ago

pandaxtc commented 1 year ago

Validating this document with this schema fails with the following:

xmlschema.validators.exceptions.XMLSchemaValidationError: failed validating <Element '{http://www.omg.org/spec/XTCE/20180204}BooleanArgumentType' at 0x7f2d03c6b680> with XsdElement(name='xtce:BooleanArgumentType', occurs=[1, 1]):

Reason: duplicated value ('bool1',) for XsdKey(name='xtce:argumentTypeNameKey')

Schema:

  <element xmlns="http://www.w3.org/2001/XMLSchema" name="BooleanArgumentType" type="xtce:BooleanArgumentType">
      <annotation>
          <documentation xml:lang="en">Describe an argument type that has an engineering/calibrated value in the form of a boolean enumeration.</documentation>
      </annotation>
  </element>

Instance:

  <BooleanArgumentType xmlns="http://www.omg.org/spec/XTCE/20180204" name="bool1">
      <IntegerDataEncoding sizeInBits="8" />
  </BooleanArgumentType>

Path: /SpaceSystem/CommandMetaData/ArgumentTypeSet/BooleanArgumentType[1]

It seems like this portion of the schema is responsible for this:

        <key name="argumentTypeNameKey">
            <annotation>
                <documentation xml:lang="en">This key ensures a unique argument type name at the system level.</documentation>
            </annotation>
            <selector xpath="xtce:CommandMetaData/xtce:ArgumentTypeSet/*|xtce:CommandMetaData/xtce:ParameterTypeSet/*"/>
            <field xpath="@name"/>
        </key>

however, no elements covered by the selector have the same name. After changing the name attribute a few times, it seems like the conflict is occurring with elements in TelemetryMetaData/ParameterTypeSet which should not be covered by this key. Other XSD validators also seem to not take any issue with this document. However, I'm not entirely familiar with XML Schema, so I may be overlooking something.

Thank you!

brunato commented 11 months ago

Hi, this is a challenging issue that forces a partial rewrite of identity keys collecting process.

In this case there are two XSD keys that target to the same XSD element instance through different paths, so XsdKey(name='xtce:argumentTypeNameKey') is used also with a non matching path. For these ambiguous cases I will add an additional check on XML data (that slow down the processing, so it's better to use this only if necessary ...).

Thank you

brunato commented 11 months ago

Hi, v2.5.0 has a fix for XSD keys collecting. I found a way for tracking base XML elements and to apply XPath selectors on XML data without having a big impact on performances, so ambiguities shouldn't happen anymore.

Please confirm if your failing case is resolved.

Thank you

pandaxtc commented 11 months ago

Hi, v2.5.0 has a fix for XSD keys collecting. I found a way for tracking base XML elements and to apply XPath selectors on XML data without having a big impact on performances, so ambiguities shouldn't happen anymore.

Please confirm if your failing case is resolved.

Thank you

Hello, thank you so much for fixing this issue! I can confirm the document validates correctly now. I really appreciate it.