代码之家  ›  专栏  ›  技术社区  ›  Matteo NNZ

让XSD属性接受两种类型(xs:string和xs:long)的最优雅方法是什么?

  •  1
  • Matteo NNZ  · 技术社区  · 7 年前

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

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

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

    我应该选择什么类型来创建属性 价值 接受两者 xs:字符串 和 xs:长 ? 到目前为止,我可以想到两件事:

    1) 我是否应该创建两个不同的属性,例如。 stringValue 和 longValue :

    <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属性中的good值?(这在我看来很难看,但我不是一个大专家)。

    2) 我应该扩展自动生成的类吗 XmlEventContext 使用接受 价值 像 xs:字符串 然后试着把它扔给 xs:长 ?

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

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

    提前感谢!

    1 回复  |  直到 7 年前
        1
  •  0
  •   xtratic Rob    7 年前

    您需要的类型可以是 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>