XML Schema:为基类型中定义的属性设置固定值

时间:2011-10-11 19:45:40

标签: java xml web-services wsdl xsd

覆盖基类型时是否可以将xml属性的值设置为固定值?

例如,我的基本类型如下所示:

<xs:complexType name="Parameter" abstract="true">
  ... stuff that all parameters have in common ... 
  <xs:attribute name="parameterType" type="ax21:parameterType"/>
</xs:complexType> 

类型 parameterType 是一个包含两个可能值的枚举:

<xs:simpleType name="parameterType">
   <xs:restriction base="xs:string">
      <xs:enumeration value="singleParameter" />
      <xs:enumeration value="arrayParameter" />
   </xs:restriction>
</xs:simpleType>

不应使用参数类型,但仅作为扩展它的两种复杂类型的基础:

<xs:complexType name="ParameterImpl1">
        <xs:complexContent>
            <xs:extension base="ax21:Parameter">
                ...stuff specific for this implementation of parameter...
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="ParameterImpl2">
        <xs:complexContent>
            <xs:extension base="ax21:WS_Parameter">
                ...stuff specific for this implementation of parameter...
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>

在这些子类型中,我想将 parameterType 属性设置为固定值。 有可能这样做吗?

另外,我想在我的案例中解释背景 - 因为我认为对我的整个问题可能有一个更简单的解决方案:
我正在编写WSDL文件, Parameter 类型用作操作中的输入参数。它仅用作扩展它的两种类型的接口,但在我的java服务器端代码(由Axis2生成)处理web服务请求时,我只得到参数对象而不能找到任何方法来确定请求中实际传递了哪两个特定子类型。 (除了手动解析 Parameter 对象的xmlString,我想避免)

希望我的解释足够精确 - 只要告诉我你是否需要其他信息或者不理解我想要做什么。

提前致谢!

更新
在对此主题进行额外研究之后,我认为执行此类操作的唯一方法是使用继承限制多态,如本文所述:
http://www.ibm.com/developerworks/library/x-flexschema/
所以在这种情况下,base-type包含属性,继承类'覆盖'它,设置一个固定值。

1 个答案:

答案 0 :(得分:1)

正如你所说,这可以通过限制来实现,结果XSD看起来像这样

enter image description here

<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML 2015 Developer Bundle Edition 12.1.2.5004 ([http://www.liquid-technologies.com][2])-->
<xs:schema elementFormDefault="qualified"
           xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:complexType abstract="true"
                    name="Parameter">
        <xs:attribute name="paramterType"
                      type="parameterType" />
    </xs:complexType>
    <xs:simpleType name="parameterType">
        <xs:restriction base="xs:string">
            <xs:enumeration value="singleParameter" />
            <xs:enumeration value="arrayParameter" />
        </xs:restriction>
    </xs:simpleType>
    <xs:complexType name="ParameterImpl1">
        <xs:complexContent>
            <xs:restriction base="Parameter">
                <xs:attribute name="paramterType"
                              fixed="singleParameter"
                              type="parameterType" />
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
    <xs:complexType name="ParameterImpl2">
        <xs:complexContent>
            <xs:restriction base="Parameter">
                <xs:attribute name="paramterType"
                              fixed="arrayParameter"
                              type="parameterType" />
            </xs:restriction>
        </xs:complexContent>
    </xs:complexType>
</xs:schema>
相关问题