0

I have a type named AdapterInputDataType that defines a format of some kind of input data:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://companyname.org/AdapterInputDataTypeNS"
        xmlns:tns="http://companyname.org/AdapterInputDataTypeNS"
>

    <complexType name="AdapterInputDataType">
        <sequence>
            <element name="atomicElement" type="tns:AtomicElementType" minOccurs="0" maxOccurs="unbounded"/>
        </sequence>
    </complexType>

    <complexType name="AtomicElementType">
        <simpleContent>
            <extension base="tns:AtomicElementValueType">
                <attribute name="elementName" type="tns:AtomicElementNameType" use="required"/>
            </extension>
        </simpleContent>
    </complexType>

    <simpleType name="AtomicElementValueType">
        <union memberTypes="string long decimal dateTime boolean"/>
    </simpleType>

    <simpleType name="AtomicElementNameType">
        <restriction base="string">
            <enumeration value="foo"/>
            <enumeration value="bar"/>
            <enumeration value="baz"/>
        </restriction>
    </simpleType>

</schema>

AdapterInputDataType is intended to define an element of this type inside of several namespaces:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://companyname.org/adapter1"
        xmlns:tns="http://companyname.org/adapter1"
        xmlns:inptypns="http://companyname.org/AdapterInputDataTypeNS"
>

    <import namespace="http://companyname.org/AdapterInputDataTypeNS"/>

    <element name="adapterInputData" type="inptypns:AdapterInputDataType"/>

</schema>

<schema xmlns="http://www.w3.org/2001/XMLSchema"
        targetNamespace="http://companyname.org/adapter2"
        xmlns:tns="http://companyname.org/adapter2"
        xmlns:inptypns="http://companyname.org/AdapterInputDataTypeNS"
>

    <import namespace="http://companyname.org/AdapterInputDataTypeNS"/>

    <element name="adapterInputData" type="inptypns:AdapterInputDataType"/>

</schema>

The problem is that for each namespace the type is imported to a specific set of allowed values for elementName attribute should be defined. In other words, in different namespaces AdapterInputDataType should be based on different AtomicElementNameType types.

Is there a way to use in AdapterInputDataType definition some kind of parameter instead of AtomicElementNameType and put different AtomicElementNameType types in different namespaces as value of this parameter?

4

2 回答 2

2

对我来说听起来像是替代组的情况。

使用 abstract="true" 将 atomicAlement 定义为全局元素声明,然后在各种特定于消息的模式文档中,您可以定义具体元素,使用 substitutionGroup="tns:atomicElement" 使它们可以替代抽象元素。具体元素的类型必须与抽象元素的类型相同或派生自抽象元素的类型;这通常通过给出抽象元素 type="xs:anyType" 来实现。

于 2013-05-14T07:10:52.707 回答
0

得到了解决方案(感谢 Michael Kay 关于替代组的提示)。

<schema xmlns="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://companyname.org/AdapterInputDataTypeNS"
                xmlns:tns="http://companyname.org/AdapterInputDataTypeNS"
>
        <import namespace="http://companyname.org/adapter1" schemaLocation="adapter1/ElementName.xsd"/>
        <import namespace="http://companyname.org/adapter2" schemaLocation="adapter2/ElementName.xsd"/>

        <element name="adapterInputData" type="tns:AdapterInputDataType" />

        <complexType name="AdapterInputDataType">
                <sequence>
                        <element name="atomicElement" type="tns:AtomicElementType" minOccurs="0" maxOccurs="unbounded"/>
                </sequence>
        </complexType>

        <complexType name="AtomicElementType">
                <sequence>
                        <element ref="tns:elementName" minOccurs="1" maxOccurs="1"/>
                        <element name="elementValue" type="tns:AtomicElementValueType" minOccurs="1" maxOccurs="1"/>
                </sequence>
        </complexType>

        <simpleType name="AtomicElementValueType">
                <union memberTypes="string long decimal dateTime boolean"/>
        </simpleType>

        <element name="elementName" type="string" abstract="true"/>

</schema>

<schema xmlns="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://companyname.org/adapter1"
                xmlns:tns="http://companyname.org/adapter1"
                xmlns:inptypns="http://companyname.org"
>

        <element name="elementName" type="tns:AtomicElementNameType" substitutionGroup="inptypns:elementName"/>

        <simpleType name="AtomicElementNameType">
                <restriction base="string">
                        <enumeration value="foo"/>
                        <enumeration value="bar"/>
                </restriction>
        </simpleType>

</schema>

<schema xmlns="http://www.w3.org/2001/XMLSchema"
                targetNamespace="http://companyname.org/adapter2"
                xmlns:tns="http://companyname.org/adapter2"
                xmlns:inptypns="http://companyname.org"
>

        <element name="elementName" type="tns:AtomicElementNameType" substitutionGroup="inptypns:elementName"/>

        <simpleType name="AtomicElementNameType">
                <restriction base="string">
                        <enumeration value="baz"/>
                </restriction>
        </simpleType>

</schema>

AtomicElementType对原始帖子中的一个进行了一些修改。elementName属性已替换为elementName子元素。原因是不能将属性声明为抽象的。以下是针对此架构有效的数据示例:

<ans:adapterInputData xmlns:ans="http://companyname.org">
        <atomicElement>
                <ans1:elementName xmlns:ans1="http://companyname.org/adapter1">foo</ans1:elementName>
                <elementValue>hello</elementValue>
        </atomicElement>
        <atomicElement>
                <ans2:elementName xmlns:ans2="http://companyname.org/adapter2">baz</ans2:elementName>
                <elementValue>123</elementValue>
        </atomicElement>
</ans:adapterInputData>
于 2013-05-14T17:11:35.690 回答