[xsd-users] Help with XML Schema to map to "proper" XSD types.

Boris Kolpackov boris at codesynthesis.com
Tue Aug 18 09:42:42 EDT 2009


Hi Attila,

Attila <atteeela at gmail.com> writes:

> I would like to get this following XML instance documents to be 
> validated by the schemas above:
> 
> 
> SampleCfgAddRequest.xml:
> -snip-
> 
> <Envelope>
>     <Request>
>         <RequestMsg type="cfg:CfgAddMessageRequest">
>             <Property>SOMETHING</Property>
>         </RequestMsg>
>     </Request>
> </Envelope>
> 
> -snip-
> 
> and also:
> 
> 
> SampleCfgAddResponse.xml:
> -snip-
> 
> <Envelope>
>     <Response>
>         <Header>
>             <RequestId>1</RequestId>
>             <ReturnId>1</ReturnId>
>         </Header>
>         <ResponseMsg type="cfg:CfgAddMessageResponse">
>             <Success>asdf</Success>
>         </ResponseMsg>
>     </Response>
> </Envelope>
> -snip-

There is no way to achieve this exact form for your XML documents.
That is, without any namespace-prefix declarations and using type
to specify the dynamic type. If you want to keep XML representation
as minimal as possible, your best bet is to not use namespaces and
to use substitution groups. Then you will be able to do something
like this:

<Envelope>
    <Request>
        <CfgAddMessageRequest>
            <Property>SOMETHING</Property>
        </CfgAddMessageRequest>
    </Request>
</Envelope>

and

<Envelope>
    <Response>
        <Header>
            <RequestId>1</RequestId>
            <ReturnId>1</ReturnId>
        </Header>
        <CfgAddMessageResponse>
            <Success>asdf</Success>
        </CfgAddMessageResponse>
    </Response>
</Envelope>

Note that we use the element names (CfgAddMessageRequest, 
CfgAddMessageResponse) to carry the type information.

In your schemas you will need to make the following changes:

1. Get rid of all targetNamespace/elementFormDefault attributes.

2. Use xs:include instead of xs:import.

3. Add substitution groups:

Envelope.xsd:

    <xs:complexType name="RequestMsg_t" abstract="true"/>
    <xs:element name="RequestMsg" type="RequestMsg_t" abstract="true"/>

    <xs:complexType name="Request_t">
        <xs:sequence>
            <xs:element ref="RequestMsg"/>
            <xs:any minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>

    <xs:complexType name="Response_t">
        <xs:sequence>
            <xs:element name="Header" type="Header_t"/>
            <xs:element ref="RequestMsg"/>                        
            <xs:any minOccurs="0"/>
        </xs:sequence>
    </xs:complexType>


ConfigObject.xsd:

<xs:element name="CfgAddMessageRequest" 
            type="CfgAddMessageRequest_t" 
            substitutionGroup="RequestMsg"/>

<xs:element name="CfgAddMessageResponse" 
            type="CfgAddMessageResponse_t" 
            substitutionGroup="RequestMsg"/>

You can then pre-load the schemas before parsing your documents as
shown in the 'caching' example.

Boris




More information about the xsd-users mailing list