Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

XSD multiple elements with same name but different types

Tags:

xml

xsd

I'm trying to write an XSD against some XML that looks (structurally wise) something like this:

<responses>

    <response>
        <id>foo</id>
        <values>
            <value>bar</value>
        </values>
    </response>

    <response>
        <id>asdf</id>
        <values>
            <value>
                <a>a</a>
                <b>b</b>
                <c>c</c>
            </value>
        </values>
    </response>

</responses>

When I try to validate (using javax APIs) it, I'm getting the following error message:

Multiple elements with name 'response', with different types, appear in the model group

Is it possible to write an XSD that validates multiple nodes with the same names like this? The order in which the individual response nodes is consistent, so that part wouldn't be an issue at least.

like image 394
chinabuffet Avatar asked Jun 27 '26 04:06

chinabuffet


1 Answers

At the design level, the question to ask is why you wish to specify two different elements with different validity conditions, and then use their element type names to say that they are the same thing. Do you believe that lying to the processor in this way is good design?

At the schema level: the type to be assigned to an element is completely determined by the path from the validation root. (If the validation starts at the document root, then in your example the type of a response element must be completely determined by the path /responses/response. It is perfectly legal to have two elements with the same name which appear in different contexts (where 'context' is defined solely by ancestry and not by siblings); it is not allowed in XSD to have different default types for two siblings of the same name.

like image 190
C. M. Sperberg-McQueen Avatar answered Jun 30 '26 08:06

C. M. Sperberg-McQueen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!