[xsd-users] help about XSD

Boris Kolpackov boris at codesynthesis.com
Mon Jun 11 10:30:10 EDT 2007


Hi Jérôme,

jerome.martinet at mpsa.com <jerome.martinet at mpsa.com> writes:

> I'm working on a project named Autosar. I have the XSD file (which you can
> find here :http://autosar.org/download/AUTOSAR_xsd.zip) and I want to
> generate C++ code using your tools XSD with Visual studio 2005 express.

Uh, quite a big schema.

>
> I generated the code but I can't access to elements I need
> (ATOMiC-SOFTWARE-COMPONENT for example)
>
> I have a constructor on the class AUTOSAR but nothing on ELEMENTS , how can
> I use a link between this two elements ?

The root element of your vocabulary is element AUTOSAR and its type is
AUTOSAR. So you get an instance of this type AUTOSAR when you parse
you XML document, e.g.,

using namespace NS_AUTOSAR;

std::auto_ptr<AUTOSAR> autosar (AUTOSAR_ ("autosar.xml"));

Looking at the definition of type AUTOSAR in the schema we see that
its content is defined as a reference to a group (xsd:group) named
AUTOSAR. When a group is referenced somewhere in the schema it is as
if the contents of the group were copied to the place of reference.
So we look at the AUTOSAR group and see that it contains a single
optional (minOccurs="0") element TOP-LEVEL-PACKAGES. Since this
element is optional, the generated code will follow the rules for
the optional cardinality class as described in the manual. So we can
test for presence of this element and get its content like so:

if (autosar->TOP_LEVEL_PACKAGES ().present ())
{
  TOP_LEVEL_PACKAGES& tlp = autosar->TOP_LEVEL_PACKAGES ().get ();


Looking at the type for TOP-LEVEL-PACKAGES we see that it is a
sequence of AR-PACKAGE elements of type AR-PACKAGE. To iterate
over them we can do this:

  TOP_LEVEL_PACKAGES::AR_PACKAGE::container& c = tlp.AR_PACKAGE ();

  for (TOP_LEVEL_PACKAGES::AR_PACKAGE::iterator i (c.begin ());
       i != c.end ();
       ++i)
  {
    AR_PACKAGE& arp = *i;
  }
}

Next you can figure out the contents of AR_PACKAGE, etc. You can also
look into the generated header file to figure out to which cardinality
class a particular element belongs if it is not immediately obvious from
studying the schema.


hth,
-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20070611/ed12da22/attachment.pgp


More information about the xsd-users mailing list