[xsd-users] how to make arbitrary element insertions in a
"mixed" sequence of elements where the CodeSynthesis data model
does not adequately describe the ordering of elements
Boris Kolpackov
boris at codesynthesis.com
Mon Nov 8 10:00:11 EST 2010
Wheeler, Bill NPO <bill.npo.wheeler at intel.com> writes:
> I have a schema (which I cannot modify) that has the property where 2 types of elements can be inter-mingled arbitrarily. To be more specific, the following stripped down schema describes the situation I have:
>
> <?xml version="1.0" ?>
>
> <xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
>
> <xsd:complexType name="A_elem">
> <xsd:sequence>
> <xsd:element name="some_data" type="xsd:int"></xsd:element>
> </xsd:sequence>
> </xsd:complexType>
>
> <xsd:complexType name="B_elem">
> <xsd:sequence>
> <xsd:element name="some_other_data" type="xsd:boolean"></xsd:element>
> </xsd:sequence>
> </xsd:complexType>
>
> <xsd:group name="X_group_elems">
> <xsd:choice>
> <xsd:element name="A" type="A_elem" />
> <xsd:element name="B" type="B_elem" />
> </xsd:choice>
> </xsd:group>
>
> <xsd:complexType name="X_root">
> <xsd:sequence>
> <xsd:group ref="X_group_elems" minOccurs="0" maxOccurs="unbounded" />
> </xsd:sequence>
> </xsd:complexType>
>
> <xsd:element name="ROOT" type="X_root"></xsd:element>
> </xsd:schema>
>
>
> I think this falls under the category of a "mixed content" model whereby
> the CodeSynthesis data model does not reflect the actual ordering of
> elements. That is, the X_root object will have an A list and B list
> of elements, but not a list that reflects the inter-mingled list of
> A/B elements.
This is not really a mixed content model. The issue here is with the
way C++/Tree represents the content model. To keep the API simple,
C++/Tree "flattens" the content which, in some cases, can lead to
loss of the order information. The following thread discusses this
problem in detail and lists all the available solutions:
http://www.codesynthesis.com/pipermail/xsd-users/2010-March/002741.html
> Say I want to insert a new <B> elem just before the <B> elem (and just
> after the 2nd <A> elem). Could I simply create a new B elem and do a
> "push_back" onto the B elem list of the X_root object. Then find the
> underlying DOM node for that new B elem, remove it from its parent node
> and then re-insert it as a child node of the same parent node in the
> correct position. Would this work?
Unfortunately, this won't work. When you programmatically create a node
in the object model, the underlying DOM nodes are not created. For this
to work you would need to manually create the DOM node (which can be
done using the serialization operator), then associate it with the
object model node, and, finally, insert it in the right position in
the DOM document. This can be done. It is just a lot of work. Type
customization will be a simpler solution.
Boris
More information about the xsd-users
mailing list