[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

Wheeler, Bill NPO bill.npo.wheeler at intel.com
Sun Nov 7 18:49:40 EST 2010


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.  I've read the CodeSyntheis doc describing "DOM Association" which indicates how I can find the underlying DOM objects so that I can infer the sequential order of the "A" and "B" elements.  However, this section goes on to say that you can't modify and then serialize the structure back out to an xml file using this technique, and that you should use the custom parsing constructs and serialization operators to do this, as demo'd in your mixed-xerces2-8 example project...but this project does not really demonstrate how to insert an element in an arbitrary position.

I'm wondering if the following idea might work instead of using the custom parsing constructs and serialization operators.

Let's say my original XML looks like:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?>

<ROOT xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="c:/xml_obj/test.xsd">
  <A>
    <some_data>1</some_data>
  </A>
  <A>
    <some_data>2</some_data>
  </A>
  <B>
    <some_other_data>0</some_other_data>
  </B>
  <A>
    <some_data>3</some_data>
  </A>
</ROOT>


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?  Do I care about the ordering of the <A> and <B> elements on their respective lists in the X_root object?  If this idea, is totally off base, what approach should I be considering to make arbitrary element insertions in mixed data lists?

Thanks,

Bill




More information about the xsd-users mailing list