2

http://scalaxb.org/を使用

  <xsd:complexType name="Address">
    <xsd:choice>
      <xsd:element ref="ExternalAddress" />
      <xsd:element ref="InternalAddress" />
    </xsd:choice>
  </xsd:complexType>


val internalAddrress = InternalAddress(...);  // this works.
val address : Address = internalAddrress;     // error: type mismatch

このコードを機能させるには、どのように変更する必要がありますか?

ここにいくつかの情報があり、DataRecord などで遊んでみましたが、うまくいきませんでした。 http://scalaxb.org/narrower-choice

4

2 に答える 2

3

質問の定式化が間違っていました。ここに正しい質問と答えがあります:

 <xsd:complexType name="Address">
    <xsd:choice>
      <xsd:element name="externalAddress" type="ExternalAddress" />
      <xsd:element name="internalAddress" type="InternalAddress" />
    </xsd:choice>
  </xsd:complexType>


val internalAddress = InternalAddress(...);  // this works.
val address = Address(scalaxb.DataRecord(None, Some("internalAddress"), internalAddress));  // now this works.

https://github.com/eed3si9n/scalaxb/issues/138#issuecomment-3943088を参照してください。

于 2012-02-13T14:48:27.310 に答える
1

正確には何で表現してい<xs:choice>ますか?またはxs:choiceのいずれかである可能性のある複合型の子要素を宣言します。しかし、Scalaコードから判断すると、外部または内部のアドレスタイプを表現しようとしている可能性があります。ExternalAddressInternalAddress

その場合は、複合型拡張が最適です。拡張の例については、http://scalaxb.org/running-scalaxbを参照してください。これにより、との両方のスーパータイプである特性が生成されます。USAddressAddressAddressableAddressUSAddress

于 2012-02-10T18:36:54.030 に答える