Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect amount of XMLSchemaChildrenValidationError being raised #424

Open
toan-t-le opened this issue Oct 22, 2024 · 1 comment
Open
Labels
enhancement New feature or request

Comments

@toan-t-le
Copy link

toan-t-le commented Oct 22, 2024

Here is my setup to produce the bug

schema.xsd containing the schema:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           elementFormDefault="qualified">
    <xs:element name="parentTag">
        <xs:complexType>
            <xs:sequence>
                <xs:element ref="optionalTagA" minOccurs="0" maxOccurs="1"/>
                <xs:element ref="optionalTagB" minOccurs="0" maxOccurs="1"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
    <xs:element name="optionalTagA">
    </xs:element>
    <xs:element name="optionalTagB">
    </xs:element>
</xs:schema>

test.xml containing the example XML code to be validated:

<parentTag>
    <optionalTagA>
    </optionalTagA>
    <optionalTagA>
    </optionalTagA>
    <optionalTagB>
    </optionalTagB>
    <optionalTagB>
    </optionalTagB>
</parentTag>

test.py containing the python script to load the schema and run the validation

import xmlschema

schema = xmlschema.XMLSchema('./schema.xml')
xml_file = './test.xml'

error_count = sum(1 for _ in schema.iter_errors(xml_file))

print(f"'{xml_file}' contains {error_count} error(s).")

Only one error will be reported in the setup above, despite two elements exceeding their occurrence limit. This behavior relates to issue #372, where a fix was implemented. The fix back then didn't go with reporting multiple errors but added multiple attributes instead. However, since the issue reappears with a different combination here, I believe the best approach is to report multiple errors whenever a parent tag contains multiple violations, both for #372 and in this case.

@brunato
Copy link
Member

brunato commented Oct 24, 2024

Hi,
it's not so simple do that because, when the model is broken by an error, often one can't be sure if the right choice is to continue with a tag or another.

Other validators that I've tested show only the first error, as is for your case validated with Xerces j:

Attempting well-formed, namespace-aware parse
Error:file:///home/brunato/Development/xmlschema/tests/test_cases/issues/issue_424/issue_424.xml:4:19:cvc-complex-type.2.4.a: Invalid content was found starting with element 'optionalTagA'. One of '{optionalTagB}' is expected.
Parse succeeded (0.3) with 1 error and no warnings.

Recently the ModelVisitor has been improved in order to be usable for generating content, so one can try to use it for checking more validation hypothesis on remaining children after an invalid child. We will see if it works after the next major release.

@brunato brunato added the enhancement New feature or request label Oct 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants