views:

38

answers:

2

Hey,

im searching for the best solution of my xml schema (xsd)

I have an response:

<xs:element name="exampleCatalogResponse">
    <xs:complexType>
        <xs:sequence>
            <xs:element name="meta" type="tns:metaType" />
            <xs:element name="data" type="tns:defaultDataType" />
        </xs:sequence>
    </xs:complexType>
</xs:element>

...the default datatype:

<xs:complexType name="defaultDataType">
    <xs:sequence>
        <xs:element name="catalog">
            <xs:complexType>
                <xs:sequence maxOccurs="unbounded">
                    <xs:element name="catalogItem" type="tns:catalogItem" />
                </xs:sequence>
            </xs:complexType>
            <xs:unique name="itemIdConstraint">
                <xs:selector xpath="tns:catalogItem" />
                <xs:field xpath="tns:id" />
            </xs:unique>
        </xs:element>
    </xs:sequence>
</xs:complexType>

...and the catalogItem

<xs:complexType name="catalogItem">
    <xs:sequence>
        <xs:element name="id" type="xs:nonNegativeInteger" />
    </xs:sequence>
</xs:complexType>

...but now there is a special item which specialize catalogItem

<xs:complexType name="specialItem">
    <xs:complexContent>
        <xs:extension base="tns:catalogItem">
            <xs:sequence>
                <xs:element name="code" type="xs:string" />
            </xs:sequence>
        </xs:extension>
    </xs:complexContent>
</xs:complexType>

Now i need an specialized Response for my specialItem for answering the request which expects this "specialItem".

How can i realise this? Without writing another defaultDataType where only the type of "catalogItem" changes to "tns:specialItem" ?

Thanks in advantage

+1  A: 

I don't think you can enforce co-occurance constraints like that in XSD. In the spirit of a puzzle solution, here's an awkward way to make copies with the change you want, without manually "writing another defaultDataType":

  1. define the above complexTypes in a schema document without a namespace.

  2. <include> it in a second schema document, that does have a namespace - this gives it that namespace. We can do this in many schema documents, each time getting a copy in that distinct namespace.

  3. in each of these schema documents, extend catalogItem, so that every extension will be in its own namespace.

  4. in a final schema document, include all the above, and make them substitutable - so they can all be used as a response. (alternatively, you could make them all extend some other Response element in yet-another-schema).

  5. NOTE: You'll need a different way to enforce the itemIdConstraint constraint, that goes across namespaces. Offhand, I don't know if this is possible.

This works by making a distinct copy of defaultDataType for each one, and so won't work if you actually want them to all use the same defaultDataType. Like I said, it's awkward, but it's the only way I can see to do what you want. I offer it as a puzzle solution, rather than a practical one!

13ren
A: 

Thank you for your tipps 13ren,

I used the Subsumption Feature. So the default catalogItem is used everywhere also for the unique expression. And now if i use this in xml i can specify this via type.

  <tns:catalogItem xsi:type="tns:specialItem">
    <tns:id>0</tns:id>
    <tns:code>tns:code</tns:code>
  </tns:catalogItem>
  <tns:catalogItem xsi:type="tns:specialItem">
    <tns:id>1</tns:id>
    <tns:code>tns:code</tns:code>
  </tns:catalogItem>
  <tns:catalogItem>
    <tns:id>2</tns:id>
    <tns:code>tns:code</tns:code>
  </tns:catalogItem>

So the last item is the default, and the two above are specialized items. this works very well for me.

codedevour