[xsd-users] Serializing just the contents of an "any" element

Boris Kolpackov boris at codesynthesis.com
Mon Jun 19 13:19:30 EDT 2017


Iain Sharp <isharp at atis.org> writes:

> <xs:complexType name="primitiveContent">
>   <xs:choice minOccurs="0" maxOccurs="unbounded">
>     <xs:any namespace="http://www.onem2m.org/xml/protocols" processContents="lax" />
>     <xs:any namespace="##other" processContents="lax"  />
>   </xs:choice>
> </xs:complexType>
>
> [...]
>
> <m2m: primitiveContent xmlns:m2m="http://www.onem2m.org/xml/protocols">
>   <m2m:ae rn="myAE" xmlns:m2m="http://www.onem2m.org/xml/protocols">
>     <api>myAE</api>
>     <aei>myAE</aei>
>     <rr>true</rr>
>   </m2m:ae>
> </m2m: primitiveContent >
> 
> What I want is:
>
>   <m2m:ae rn="myAE" xmlns:m2m="http://www.onem2m.org/xml/protocols">
>     <api>myAE</api>
>     <aei>myAE</aei>
>     <rr>true</rr>
>   </m2m:ae>

Your schema allows for multiple elements in the wildcard content. That
is, your could have something like this:

<m2m: primitiveContent xmlns:m2m="http://www.onem2m.org/xml/protocols">
  <m2m:ae xmlns:m2m="http://www.onem2m.org/xml/protocols">
    ...
  </m2m:ae>
  <m2m:ae xmlns:m2m="http://www.onem2m.org/xml/protocols">
    ...
  </m2m:ae>
  <m2m:ae xmlns:m2m="http://www.onem2m.org/xml/protocols">
    ...
  </m2m:ae>
</m2m:primitiveContent >

I am not sure what you want to do with that. Serializing them all will
create a "multi-root" document which is not a valid XML.

In any case, what you need to do is serialize primitiveContent to DOM.
Then you can iterate over its child elements and serialize them one
at a time. Or, alternatively, you could iterate over the wildcard
content and just serialize each element -- will probably be more
efficient. Examples and the FAQ on the Wiki have some sample code
for doing this.

Boris



More information about the xsd-users mailing list