[xsd-users] Re: error: cannot convert to ?int? in assignment

shubhgd at cs.vt.edu shubhgd at cs.vt.edu
Mon Feb 21 10:41:08 EST 2011


Hi Boris,

Thank you very much. It works fine with the boost::lexical_cast.

Now, have another problem with parsing a nested element type. I am  
trying to understand how a nested XML element gets mapped into C++  
classes and finding it difficult to reach to the leaf node easily.

The hierarchy is as the following. What I need to do is, to retrieve  
the def field (which is a xs:string) for a CT of an AFB. So the  
hierarchy of elements to access is: G->AFB->CT->def, however, each one  
of them have a complexType definition and I don't know how to reach  
the leaf from the following hierarchy of complexTypes and elements. It  
would be great if you can put some light on this. Thank you very much.

<element name="G">
   <complexType> <sequence> <element ref="AFB" /> </sequence> </complexType>
</element>

<xs:simpleType name="nt"><xs:restriction base="xs:string">
    <xs:enumeration value="c"/>
    <xs:enumeration value="s"/>
</xs:restriction></xs:simpleType>

<xs:complexType name="nrb">
  <xs:sequence>
     <xs:element name="ntype" type="nt" minOccurs="0" maxOccurs="1"/>
     <xs:element name="CP" type="ml" minOccurs="1" maxOccurs="1"/>
     <xs:element name="wts" type="ml" minOccurs="1" maxOccurs="1"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="ml"> <xs:complexContent>
   <xs:extension base="nd"><xs:sequence>
      <xs:element ref="v" minOccurs="0" maxOccurs="1"/>
      <xs:element ref="def" minOccurs="1" maxOccurs="1"/>
   </xs:sequence></xs:extension>
</xs:complexContent></xs:complexType>

<xs:element name="AFB"><xs:complexType>
    <xs:sequence>
       <xs:element name="CT" type="nrb" minOccurs="1" maxOccurs="1"/>
       <xs:element name="CB" type="nrb" minOccurs="1" maxOccurs="1"/>
   </xs:sequence>
</xs:complexType></xs:element>


Thanks and Regards,
Shubhangi

Quoting "Boris Kolpackov" <boris at codesynthesis.com>:

> Hi Shubhangi,
>
> shubhgd at cs.vt.edu <shubhgd at cs.vt.edu> writes:
>
>> <xs:simpleType name="scalartype">
>>       <xs:union memberTypes="xs:int xs:double xs:string xs:boolean"/>
>> </xs:simpleType>
>>
>> [...]
>>
>> However, with this piece of code, I get the following error:
>> error: cannot convert ?const scalartype? to ?int? in assignment
>
> In C++/Tree union XML Schema types are mapped to C++ classes derived
> from std::string. So what you get is a lexical representation of the
> value. If you want to convert it to one of the member types, you will
> need to do it yourself, for example:
>
> #include <sstream>
>
> using namespace std;
>
> scalartype v = ...;
> int i;
>
> istringstream is (v);
> if (!is >> i)
> {
>   // Error: value is not an int.
> }
>
> Or, if you are using Boost, you can shorten the above code with
> lexical_cast:
>
> #include <boost/lexical_cast.hpp>
>
> using boost::lexical_cast;
>
> scalartype v = ...;
> int i = lexical_cast<int> (v);
>
> Boris
>
>





More information about the xsd-users mailing list