Abhijeet Kashnia
Abhijeet Kashnia

Reputation: 12890

XSD and "max occurs" based on context

<Container>
    <MyObject>
        <property1>abcd</property1>
        <property2>
            <Version>3.2</Version>
        </property2>
    </MyObject>

    <Contained>

        <MyObject>
            <property1>something</property1>
            <property2>
                <Version>1.1</Version>
                <Version>1.2</Version>
                <Version>1.6</Version>
            </property2>
        </MyObject>

        <MyObject>
            <property1>something else</property1>
            <property2>
                <Version>2.3</Version>
                <Version>2.5</Version>
                <Version>2.6</Version>
            </property2>
        </MyObject>

    </Contained>
</Container>

Given this xml structure, in the corresponding xsd file, can I put a max occurs limit on Version property, such that if MyObject is directly contained in Container, it should occur exactly one time, but if MyObject is contained in Contained, then it may occur any number of times?

Based on a similar question, I am inclined to think it it not possible, but I would like to be sure.

Upvotes: 0

Views: 101

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21638

The short answer is no, it cannot be done using XSD 1.0. If you can move up the version or add to the mix Schematron assertions, then you can. Alternatively, if a pure XSD 1.0 is the only answer, then I would try to find a way to remodel; @penartur's could give you an idea where to start with (I would instead reuse by extension, ensuring that the "differences" are modeled at the end of the content model, to allow extension to work its magic).

Upvotes: 1

penartur
penartur

Reputation: 9912

From your code it seems that logically, the Container/MyObject and Container/Contained/MyObject are different types, despite their names.

So i'd declared two different types in XSD for these. One for Container/MyObject with maxOccurs="1", and another for Contained/MyObject with maxOccurs="whateverYouWant".

Upvotes: 0

Related Questions