[xsd-users] Sort of XML "reflection"

Bill Pringlemeir bpringle at sympatico.ca
Wed Nov 18 11:52:01 EST 2009


On 17 Nov 2009, PaquetP at navcanada.ca wrote:

> I have a complex type T which has a sequence of elements, each of which
> have any cardinality.  I know that each of these elements are an
> extension of a base type B.

> As an argument, a caller will be passing a string representing the name
> of one of these elements of the complex type T.  I'd like to determine
> if such an element exists, and if it does, get a reference to all
> occurences of them to a sequence of B&

I think that you could add virtual functionality to the base class.
You can do this by wrapping the XSD generated base and all of the
derived classes.  The method would be something like,

 virtual boolean amI(std::string type) = 0;

That would be elegant in that adding new types would not require the
base to know about them.  However, you can also create the same thing
with a mapping of typeid and strings.  This can be a completely static
method.

 static boolean isKind(base * ptr, std::string);

You then need a map of all derived types to the matching string (or
the string could be the typeid() name).

struct { 
  char * class_name;
  char * given_name;
} animal_map[] = {
  { typeid(elephant).name, "elephant" },
  { typeid(mole).name, "mole" },
  { typeid(monkey).name, "monkey" },
  { typeid(porpoise).name, "porpoise" },
  { typeid(zebra).name, "zebra }
};

  // get animal map index matching given name.
  return strcmp(typeid(*ptr).name,animal_map[index].class_name) == 0;

This doesn't require as many XSD wrappers, but is less fool-proof to
addition of derived classes.

In both cases, the predicate functions can be used to run through a
collection to 'filter' your desired targets.

btw, I think this is a C++ question as opposed to XSD?

fwiw,
Bill Pringlemeir.

 



More information about the xsd-users mailing list