[xsd-users] Cropped data problem due to enforced upcasting in C++.

Tolga Hakan ODUNCU thakanoduncu at gmail.com
Tue Mar 24 04:42:43 EDT 2020


Dear xsd-users,
Hello, I need some help about an issue I couldn't succeed myself. I have an
enforced upcasting problem with my auto-generated C++ codes. This is a part
of the XSD schema I used to generate C++ codes to run on my Linux Ubuntu
VM.

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" version="1.6.1">

<xs:element name="X1Request" type="RequestContainer"/>
  <xs:complexType name="RequestContainer">
    <xs:sequence>
      <xs:element name="x1RequestMessage" type="X1RequestMessage"
minOccurs="1" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="X1RequestMessage">
    <xs:sequence>
      <xs:element name="admfIdentifier" type="AdmfIdentifier"/>
      // Other X1RequestMessage elements ....
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="ReportTaskIssueRequest">
    <xs:complexContent>
      <xs:extension base="X1RequestMessage">
        <xs:sequence>
          <xs:element name="xId" type="XId"/>
          // Other ReportTaskIssueRequest elements ....
        </xs:sequence>
      </xs:extension>
    </xs:complexContent>
  </xs:complexType>

  // Other referenced complexTypes and simpleTypes .....

</xs:schema>

Since this schema includes base and derived types, I generated the C++ 11
codes with -generate-polymorphic parameter as proposed on Command Line
Manuel.
My RequestContainer class is generated as follow;

class RequestContainer: public ::xml_schema::type
{
  public:
  // x1RequestMessage
  //
  typedef ::X1RequestMessage x1RequestMessage_type;
  typedef ::xsd::cxx::tree::sequence< x1RequestMessage_type >
x1RequestMessage_sequence;
  typedef x1RequestMessage_sequence::iterator x1RequestMessage_iterator;
  typedef x1RequestMessage_sequence::const_iterator
x1RequestMessage_const_iterator;
  typedef ::xsd::cxx::tree::traits< x1RequestMessage_type, char >
x1RequestMessage_traits;

  const x1RequestMessage_sequence&
  x1RequestMessage () const;

  x1RequestMessage_sequence&
  x1RequestMessage ();

  void
  x1RequestMessage (const x1RequestMessage_sequence& s);

  // Constructors.
  // ........
  ...
}

Referring manual, I see that I can only add an item to my RequestContainer
class via "x1RequestMessage_sequence", a sequence of X1RequestMessage.
What I need is a generic RequestContainer class that's capable of storing
any type of data derived from X1RequestMessage class. Because when I
serialize my RequestContainer data, for instance, it should also have
properties of ReportTaskIssueRequest class together with properties of
X1RequestMessage class and it should look like that;

<?xml version="1.0" ?>
<ns1:X1Request xmlns:ns1="http://uri.etsi.org/03221/X1/2017/10" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance">
  <ns1:x1RequestMessage xsi:type="ns1:ReportTaskIssueRequest">
    <ns1:admfIdentifier>admfID</ns1:admfIdentifier>
    //... X1RequestMessage properties
    <ns1:xId>29f28e1c-f230-486a-a860-f5a784ab9172</ns1:xId>
// .. ReportTaskIssueRequest properties
  </ns1:x1RequestMessage>
</ns1:X1Request>

However, when I push back my ReportTaskIssueRequest object to
x1RequestMessage_sequence directly or with _clone() function, my object is
upcasted to X1RequestMessage and properties belonging to
ReportTaskIssueRequest are cropped and xsi:type attribute is invisible.
Then it looks like that

<?xml version="1.0" ?>
<ns1:X1Request xmlns:ns1="http://uri.etsi.org/03221/X1/2017/10" xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance">
  <ns1:x1RequestMessage>
    <ns1:admfIdentifier>admfID</ns1:admfIdentifier>
    //... X1RequestMessage properties
  </ns1:x1RequestMessage>
</ns1:X1Request>


I tried many things, but unfortunately couldn't find a proper solution. On
user manual, section 2.11 states that in a collection including base and
derived types, items can be downcasted with dynamic_cast operation to get
derived type object, but there is not any information about upcasting.
My XSD schemes are produced by an official organization, ETSI. I don't see
what I'm missing. Could be there an issue with XSD files, or do I skip a
required command line parameter when I'm generating the codes.
Thanks already for any kind of support.

Kind Regards,
Tolga Hakan Oduncu


More information about the xsd-users mailing list