Reputation: 4770
I'm throwing myself into XSD as I'm trying to describe the (custom) XML protocol at work which we use to make RPC.
A simple request/response pair looks like this:
<command type="request" lineid="500477">
<request name="ping">
<node id="503456" device="meter"/>
</request>
</command>
<command type="response" lineid="500477">
<response name="ping">
<result>true</result>
</response>
</command>
The above is a trimmed down example, and furthermore, the request node can contain a list of parameter elements and the result node can contain more advanced data for other request types.
I'm trying to describe the above with XSD, but I can't seem to figure out how to describe the dynamic nature of the requests/responses.
I have tried to extend, inherit and nesting xs:complexType, but nothing seems to be "the right way".
My current attempt:
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="request">
<xs:attribute name="name" type="xs:string" use="required"/>
</xs:complexType>
<xs:complexType name="command" mixed="true">
<xs:sequence>
<xs:element name="request" type="request"></xs:element>
</xs:sequence>
<xs:attribute name="type" type="commandtype" use="required"/>
<xs:attribute name="lineid" type="xs:string"/>
</xs:complexType>
<xs:simpleType name="commandtype">
<xs:restriction base="xs:string">
<xs:enumeration value="request"/>
<xs:enumeration value="response"/>
</xs:restriction>
</xs:simpleType>
<xs:element name="command" type="command" />
</xs:schema>
Even at this stage I have no idea how to allow both request and a response element inside a generic command node.
How should I describe the above with XSD?
Upvotes: 0
Views: 210
Reputation: 12985
In this part, put both request and response but make them optional:
<xs:sequence>
<xs:element name="request" type="request" use="optional"></xs:element>
<xs:element name="response" type="request" use="optional"></xs:element>
</xs:sequence>
Warning, I'm not good at typing out XSD from memory, so this may not be exactly right but the principle is what I'm trying to show you.
By doing this, you can have one or the other. (Or you can both or neither but you can add that test to your code and the XSD won't specify it. Or just don't do both or neither.)
Upvotes: 1