【问题标题】:Most elegant way to make an XSD attribute accept two types (xs:string and xs:long)?使 XSD 属性接受两种类型(xs:string 和 xs:long)的最优雅方法?
【发布时间】:2018-12-18 12:55:47
【问题描述】:

我收到了一个 SOAP 服务的规范,我发送的请求将包含以下内容:

<eventContexts>
    <eventContext name="eventType" value="Unwind"/>
    <eventContext name="referenceId" value="26214"/>
</eventContexts>

我正在尝试在 XSD 中对此对象建模,但我无法为属性 value 选择 type。正如您在上面的示例中看到的,它可以是xs:string(案例Unwind)或xs:long(案例26214)。

我应该选择什么类型来使属性value 同时接受xs:stringxs:long? 到目前为止,我能想到两件事:

1) 我应该创建两个不同的属性,例如stringValuelongValue:

<xs:complexType name="XmlEventContext">
    <xs:attribute name="name" type="xs:string"/>Sh
    <xs:attribute name="stringValue" type="xs:string" minOccurs="0"/>
    <xs:attribute name="longValue" type="xs:long" minOccurs="0"/>   
</xs:complexType>

... 让客户在 good 属性中向我发送良好的价值? (这对我来说看起来很丑,但我不是一个大专家)。

2) 我是否应该使用将value 作为xs:string 的自定义类扩展自动生成的类XmlEventContext,然后尝试将其转换为xs:long

public class XmlEventContextComplete extends XmlEventContext {
    //code to manage a property referenceId which can either be long or string
}

3) 还有其他更优雅的建议吗?

提前致谢!

【问题讨论】:

    标签: java xsd jaxb2-maven-plugin


    【解决方案1】:

    您需要一个可以是long 或特定枚举string 的类型。

    这应该涵盖这种情况:

    <xs:complexType name="xmlEventContext">
        <xs:attribute name="name" type="xs:string" />
        <xs:attribute name="value" type="longOrUnwind" />
    </xs:complexType>
    
    <xs:simpleType name="longOrUnwind">
        <xs:union memberTypes="xs:long unwindConstant" />
    </xs:simpleType>
    
    <xs:simpleType name="unwindConstant">
        <xs:restriction base="xs:string">
            <xs:enumeration value="Unwind" />
        </xs:restriction>
    </xs:simpleType>
    

    【讨论】:

      猜你喜欢
      • 2011-10-15
      • 1970-01-01
      • 2016-09-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-02
      相关资源
      最近更新 更多