質問
Using 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
how do I need to modify this code to make it work?
There is some info here, and I played with DataRecord and others, but I didn't make it work. http://scalaxb.org/narrower-choice
解決
The question was incorrectly formulated. Here the correct question and answer:
<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.
See https://github.com/eed3si9n/scalaxb/issues/138#issuecomment-3943088
他のヒント
What exactly are you expressing with <xs:choice>
? xs:choice
declares child element of the complex type that could either be ExternalAddress
or InternalAddress
. But judging from your Scala code, you might be trying to express an address type that could either be external or internal.
In that case, complex type extension is the way to go. See http://scalaxb.org/running-scalaxb for an example of USAddress
extending Address
. This generates Addressable
trait which are supertype for both Address
and USAddress
.