[xsd-users] How do you enter mixed text when creating an element?

Boris Kolpackov boris at codesynthesis.com
Fri Oct 7 09:57:12 EDT 2011


Hi Mark,

Mark Mendell <mendell at ca.ibm.com> writes:

> If I have this xsd fragment:
> <xs:complexType name="literalType" mixed="true">
>   <xs:complexContent>
>     <xs:extension base="expnBase">
>       <xs:choice minOccurs="0">
>         <xs:element name="list" type="listType"/>
> ....
>       </xs:choice>
>     </xs:extension>
>   </xs:complexContent>
> </xs:complexType>
> 
> I know how to handle creating the list entry, and (from a different
> problem) how to read the text '5' using the DOMNode, but I can't figure
> out how to create a 'literal' with just the text 5.

Using DOM association (which is what, I assume, you are using to read
the text) works well for reading but not for writing. If you need to
modify/create elements mixed content, then the best option is to
customize the type(s) with mixed content. The 'custom/mixed' example
in the examples/cxx/tree/ directory in the XSD distribution shows how
to do exactly that. In particular, there is this sentence in the
accompanying README file:

"The use of DOM for mixed content storage is one of the options. You may
 find other data structures (e.g., a string) more suitable depending on
 your situation."

In your case, string will be exactly what you would want to use. Also
in the parsing constructor you would want to check if there are any
child elements in the content. If so, then you would want to delegate
parsing to the base (generated) implementation so that it can parse
the list element sequence. However, if there are not nested elements,
then you would assume this is a string literal and extract the text
into the string member.

Similarly, in the serialization operator, you would first check if the
string member is not empty. If so, then you create a DOMText child node.
Otherwise, you again delegate to the base implementation so that it can
write out the list element sequence.

Boris



More information about the xsd-users mailing list