[xsd-users] Extracting
Boris Kolpackov
boris at codesynthesis.com
Tue Dec 13 10:56:59 EST 2005
Hi,
SHAH, Sima, FM <Sima.SHAH at rbos.com> writes:
> I have an element defined as an xsi:type. To extract it, I need to do a
> dynamic cast which throws if not the correct type.
>
> Is there a way I can work out what the type is or a visitor pattern to allow
> me to call a function given the correct type.
You can use dynamic_cast on a pointer instead of a reference. In that case
it will return 0 instead of throwing std::bad_cast. This is an effective
way to test if SomeType is-a SomeOtherType.
One extension that we are thinking about implementing is to allow you to
specify native C++ base classes for XML Schema types. For example, suppose
you have the following schema:
<complexType name="person" nativeBase="person_base">
<sequence>
<element name="name" type="xsd:string"/>
</sequence>
</complexType>
<complexType name="superman">
<complexContent>
<extension base="person">
<attribute name="can-fly" type="xsd:boolean" use="required"/>
</extension>
</complexContent>
</complexType>
And the person_base C++ class:
struct person_base
{
~person_base ();
virtual
~print () = 0;
};
The generated code for the above schema fragment could look like this:
struct person: xml_schema::type, person_base
{
string
name () const;
};
struct superman: person
{
bool
can_fly () const;
};
Now, since print is pure virtual, you will need to implement those types:
struct person_impl: person
{
virtual
~print ()
{
cout << "person " << name ();
}
};
struct superman_impl: superman
{
virtual
~print ()
{
cout << (can_fly () ? "flying superman " : "superman ") << name ();
}
};
There would also be a factory mechanism that will allow one to instruct
parsing code to use person_impl and superman_impl for person and
superman. Is anybody interested in such a feature?
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/20051213/60b81d9b/attachment.pgp
More information about the xsd-users
mailing list