开发者

A problem with the usage of recursive XML schema types

开发者 https://www.devze.com 2023-01-13 06:39 出处:网络
It is easy to define two elements with the same name like <any> in one XML schema and different permitted subelements. One should just declare two XSD types AnyType1 and AnyType2 as complexTypes

It is easy to define two elements with the same name like <any> in one XML schema and different permitted subelements. One should just declare two XSD types AnyType1 and AnyType2 as complexTypes which describes permitted subelements. Then one can declare element <any> in one context as <xs:element name="any" type="AnyType1" /> and as <xs:element name="any" type="AnyType2" /> in another context. My problem is that both AnyType1 and AnyType2 are recursive. To define a recursive type in XSD I use the following syntax

<xs:element name="any" type="AnyType1" />
<xs:complexType name="AnyType1">
    <xs:choice minOccurs="1" maxOccurs="unbounded">
        <!-- … some permitted subelements -->
        <xs:element name="any" type="AnyType1" />
        <!-- … some other permitted subelements -->
    </xs:choice>
</xs:complexType>

Di开发者_如何学运维sadvantage of this syntax is that we have to declare the element "any" on the top of schema and we can’t use <xs:element name="any" type="AnyType2" /> later to define recursive type AnyType2 somewhere later.

My question is: how to declare two recursive element types in the same XML schema in the same namespace.

The background of my question is following. I need to define XML schema which describe some tests. The tests should be combined (grouped) with respect of <all> and <any> elements ("or" and "and" operations) to describes results of some tests. In one context (in one place of XML file) I need to allow one subset of tests which can be grouped by <all> and <any> and in another place other subelements should be permitted. So I don’t want to define <anyTest1> and <anyTest2> elements or <n1:any> and <n2:any>. I want just use <any>, but if <any> is subelement of <TestBeforeProgramStart> I want allow one subset of subelements and if <any> is subelement of <TestAfterProgramEnd> another subset.


I found the answer on my question myself. It seems very simple.

It is not need to declare <xs:element name="any" type="AnyType1" /> before <xs:complexType name="AnyType1">, so the declaration of the type AnyType1 can be just following

<xs:complexType name="AnyType1">
    <xs:choice minOccurs="1" maxOccurs="unbounded">
        <!-- … some permitted subelements -->
        <xs:element name="any" type="AnyType1" />
        <!-- … some other permitted subelements -->
    </xs:choice>
</xs:complexType>

So the problem which I described seems not really exist.

0

精彩评论

暂无评论...
验证码 换一张
取 消