[xsd-users] How to clone/copy an XML message and add it to a another message using anyType

Laura E Fowler Laura_E_Fowler at raytheon.com
Thu Mar 5 17:32:27 EST 2009


Boris,

I have been looking at this, but I think I'm not understanding the 
following statement you made:
"This way you could just serialize one of the commands into a DOM document
fragment corresponding to the wildcard and then, when the Status message 
is serialized, everything is handled automatically."

I'm a little confused by "DOM document fragment corresponding to the 
wildcard". I've been looking at the wildcard example, but I'm just not 
getting it.

My schema is similar to:

<complexType name="Status">
  <sequence>
    ...
    <xsd:element name="commandEcho" type="xxx:commandEcho" maxOccurs="1" 
minOccurs="1" />
    ...
  </sequence>
</complexType>

 <xsd:complexType name="commandEcho">
      <xsd:sequence>
          <xsd:any namespace="##targetNamespace" processContents="strict" 
/>
      </xsd:sequence>
 </xsd:complexType>

I serialized my command to a DOMDocument (not a fragment), but I'm unclear 
as to how put it into commandEcho.

I apologize for being slow, but I just haven't worked with XML and XSD 
enough to be fluent.

Thanks,
Laura Fowler




From:
Boris Kolpackov <boris at codesynthesis.com>
To:
Laura E Fowler <Laura_E_Fowler at raytheon.com>
Cc:
xsd-users at codesynthesis.com
Date:
03/05/2009 10:51 AM
Subject:
Re: [xsd-users] How to clone/copy an XML message and add it to a another 
message using anyType



Hi Laura,

Laura E Fowler <Laura_E_Fowler at raytheon.com> writes:

> We are using XML messages to command our system. I need to grab a copy 
of 
> the received command  and return a copy of it in a status message. The 
> command copy is defined as anyType in the schema. 
> 
> I patterned my command parsing after the multiroot example using 
> xml_schema::type as the base.
> 
> I need to save the copy of the command until the status message is 
> formatted at a later time.
> 
> What is the best way to save a copy of the received command?

Ok, let me see if I understood you correctly. You have various
command messages (probably defined as types and/or elements in
the schema) plus you have a special status message which embeds
one of the commands as anyType content, something along these
lines:

<complexType name="CommandA">
  ...
</complexType>

<element name="command-a" type="CommandA"/>


<complexType name="CommandB">
  ...
</complexType>

<element name="command-b" type="CommandB"/>

<complexType name="Status">
  <sequence>
    ...
    <element name="command" type="anyType"/>
    ...
  </sequence>
</complexType>

Here, the content of command-a or command-b should be stored
inside the command element in Status.

As you probably know, anyType is special in that it can contain
arbitrary content (elements, attributes, text, in any order).
It would actually be more straightforward (and more flexible)
to handle this case if you were using the any wildcard instead
of anyType, for example:

<complexType name="Status">
  <sequence>
    ...
    <element name="command">
      <complexType>
        <sequence>
          <any namespace="##targetNamespace" processContents="strict"/>
        </sequence>
      </complexType>
    </element>
    ...
  </sequence>
</complexType>

With the wildcard approach you get a convenient DOM-based mapping, as 
discussed in Section 2.12, "Mapping for any and anyAttribute" in the 
C++/Tree Mapping User Manual:

http://codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#2.12

This way you could just serialize one of the commands into a DOM document
fragment corresponding to the wildcard and then, when the Status message 
is serialized, everything is handled automatically. See the 'wildcard'
example in the examples/cxx/tree/ directory for more information.

If I had to handle anyType, I would do it like this: I would customize
the Status class by (1) adding an additional member to store the command
as an object model node or as a DOM document fragment, (2) overriding
the parsing constructor to populate this member based on the data
supplied for the command element (you will somehow need to know which
command it is), and (3) overriding the serialization operator to serialize
the command member to the command element.

For more information on type customization see the C++/Tree Mapping 
Customization Guide:

http://wiki.codesynthesis.com/Tree/Customization_guide

As well as the examples in the examples/cxx/tree/custom/ directory.
The 'wildcard' example in this directory is probably the closest to
what you would want to achieve.

Boris





More information about the xsd-users mailing list