scalascalaxb

scalaxb and xsd:choice


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


Solution

  • 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