java - XML 架构 : Setting fixed value for an attribute defined in a base-type

标签 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>

不应使用Parameter 类型,而应仅用作扩展它的两个复杂类型的基础:

<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 类型用作操作中的输入参数。它仅用作扩展它的两种类型的接口(interface),但是在我的 java 服务器端代码(由 Axis2 生成)中处理 Web 服务请求时,我只得到一个 Parameter 对象并且不能找到任何方法来确定请求中实际传递了两个特定子类型中的哪一个。 (除了手动解析 Parameter 对象的 xmlString,我想避免)

希望我的解释足够准确 - 如果您需要更多信息或不明白我想做什么,请告诉我。

提前致谢!

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

最佳答案

正如您所说,这可以通过限制来实现,生成的 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>

关于java - XML 架构 : Setting fixed value for an attribute defined in a base-type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7731628/

相关文章:

java - 我已经使用 JOptionPane.showOptionDialog 显示了 JDialog,有人可以告诉我如何将其设置为不可见或处置它吗?

java - 无法使用 XSLT 1.0 更新 XML 节点属性

iphone - 在 iPhone 上通过 Web 服务使用 CMS 内容

php - 扩展 prestashop webservice 资源

java - 如何在 Eclipse 中从给定的物理 wsdl 文件生成 Web 服务客户端

java - Android: fragment *.post() 中的处理程序?

java - IO程序不是从控制台运行的,而是从Eclipse(Java)运行的

java - Java中的一种负载平衡线程池

android - Textview不跳到下一行android

java - 我无法在主 Activity 之外设置ContentView()?请阅读