[xsd-users] Re: Help with XML Schema to map to "proper" XSD types.
Attila
atteeela at gmail.com
Wed Aug 19 22:41:49 EDT 2009
Thanks Boris, this is exactly what I am looking for.
On 8/18/09, Boris Kolpackov <boris at codesynthesis.com> wrote:
> 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
>
--
Attila
Software Developer
atteeela at gmail.com
More information about the xsd-users
mailing list