[xsd-users] Help for User Defined Types

Boris Kolpackov boris at codesynthesis.com
Wed Nov 25 10:58:43 EST 2009


Hi Monte,

Jones, Monte J CTR USAF AFSPC SLG/WML <Monte.Jones.ctr at peterson.af.mil> writes:

> I am attempting to parse an XML with user defined data types.  I am
> using these because some of these data types appear in multiple places
> in the XML document.  For example one of the types I have is a location
> which is defined as indicated below.  I use this for many assets, each
> with different other parameters.
> 
> <xsd:complextType name="Location">
>     <xsd:sequence>
>         <xsd:element name="Latitude">
>             <xsd:restriction base="xsd:float">
>                 <xsd:minInclusive value="-90.0" />
>                 <xsd:maxInclusive value="90.0" />
>             </xsd:restriction>
>         </xsd:element>
>         <xsd:element name="Longitude">
>             <xsd:restriction base="xsd:float">
>                 <xsd:minInclusive value="-180.0" />
>                 <xsd:maxInclusive value="180.0" />
>             </xsd:restriction>
>         </xsd:element>
>         <xsd:element name="Altitude">
>             <xsd:restriction base="xsd:float">
>                 <xsd:minInclusive value="-300.0" />
>                 <xsd:maxInclusive value="43000.0" />
>             </xsd:restriction>
>         </xsd:element>
>     </xsd:sequence>
> </xsd:complexType>
> 
> 
> Here is the example of how I am using my type in the xsd.
> 
>     <xsd:element name="Site_Location" type ="lib:Location" />
> 
> 
> The schema validates, and the parser code compiles but does not link.

What kind of link errors do you get?


> I think the problem comes in the Location contains no actual data, only
> the definitions of data.  The auto generated -pskel and -pimpl files
> have shells for parsing Location data, but then I am having trouble with
> the use for different assets because I do not have a defined
> implementation for the shell.

My understanding is that you have generated sample parser implementations 
(either using the --generate-noop-impl or --generate-print-impl) and the
problem is how to "return" the parsed location information to the various
parsers that have elements of the Location type so that they can do 
something useful with it. This is similar to how, say, the built-in
xml_schema::float_pimpl parser implementation returns a float value.

The cleanest way would be to define a data structure that represents the
location information, for example:

// location.h
//
struct Location
{
  float lat;
  float lon;
  float alt;
};

Then use the C++/Parser type map mechanism to tell the XSD compiler that 
the Location XML Schema type is mapped to this struct and also map the 
anonymous types for the Latitude, Longitude, and Altitude elements to 
float:

// type-map.map
//
include "location.h";

Location ::Location;
Latitude float;
Longitude float;
Altitude float;

This will result in the Location parser skeleton having the following 
interface:

struct Location_pskel
{
  virtual void
  Latitude (float);

  virtual void
  Longitude (float);

  virtual void
  Altitude (float);

  virtual ::Location
  post_Location ();
};

Then we can provide the following implementation:

struct Location_pimpl: Location_pskel
{
  virtual void
  Latitude (float v)
  {
    loc_.lat = v;
  }

  virtual void
  Longitude (float v)
  {
    loc_.lon = v;
  }

  virtual void
  Altitude (float v)
  {
    loc_.alt = v;
  }

  virtual ::Location
  post_Location ()
  { 
    return loc_;
  }

  ::Location loc_;
};

We will also need to provide the following implementations for the
Latitude, Longitude, and Altitude parsers (only Latitude is shown):

struct Latitude_pimpl: Latitude_pskel, xml_schema::float_pimpl
{
  virtual float
  post_Latitude ()
  {
    return post_float ();
  }
};

For a more detailed discussion of type maps and the parser return mechanism, 
see Chapter 4, "Type Maps", in the C++/Parser Mapping Getting Started Guide:

http://www.codesynthesis.com/projects/xsd/documentation/cxx/parser/guide/#4

Boris



More information about the xsd-users mailing list