[xsd-users] Obtaining a referenced element via IDREF

Boris Kolpackov boris at codesynthesis.com
Mon Feb 20 06:19:45 EST 2006


Marco,

Marco Fischer <marco.fischer at chipvision.com> writes:

> I have problems to obtain a referenced element from an IDREF attribute.
> My schema looks like the following:
>
> <complexType name="A">
>   <attribute name="name" type="ID"/>
> </complexType>
>
> <complexType name="B">
>   <attribute name="ref" type="IDREF"/>
> </complexType>
>
> I work on an in-memory representation with the following code:
>
> A *a;
> B *b;
> ...
> b->ref(xml_schema::idref(a->name()));
> ...
> xsd::cxx::tree::type *t = b->ref().get().get();
> // t is null here

Do a and b point to objects that are in the same tree? It is important
that they do when you try to resolve the idref. Otherwise the ID/IDREF
mechanism don't have a way of finding them. For example, the following
won't work:

A a;
B b;

a.name ("foo");
b.ref (a.name ());

xml_schema::type* p (b.ref ().get ().get ()); // p is 0

While, provided we have something like this:

<complexType name="C">
  <sequence>
    <element name="a" type="A"/>
    <element name="b" type="B"/>
  </sequence>
</complexType>


the following will work as expected:


C c (a, b);
p = c.b ().ref ().get ().get (); // p is &(c.a ())


Also note that you should used xml_schema::type instead of
xsd::cxx::tree::type.


> As I understood, ref is a container, ref() returns the optional
> representing the attribute, ref().get() returns the idref instance and
> ref().get().get() should resolve to the referenced element.

That's correct.


> For some reason, I can't get back a pointer to 'a'.
> Surprisingly, using the same code to get a pointer to a referenced
> element on the in-memory representation of a just de-serialized XML file
> works.

That's probably because all the individual objects belong to a tree
right after parsing and therefore "connected".


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/20060220/9d87e428/attachment.pgp


More information about the xsd-users mailing list