Consider the following (poorly designed?) XML document:
<?xml version="1.0" ?>
<command_result>
<param name="protocol_version" value="3"/>
<param name="player_state" value="navigator"/>
</command_result>
What I want to do is create an XML schema (XSD format) that specifies the datatype of the value attribute based on the actual value of the name attribute.
Example (pseudocode):
if (param name = "protocol_version") then (param value type="xs:integer")
if (param name = "player_state") then (param value type="xs:string")
Is there a way, without modifying the source XML, to introduce such a conditional statement in the schema file? I’ve looked at various solutions, but all of them required changes to the source somehow (e.g. setting xsi:type).
EDIT
My current schema:
<?xml version="1.0" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="command_result">
<xs:complexType>
<xs:sequence>
<xs:element name="param" minOccurs="2" maxOccurs="unbounded">
<xs:complexType>
<xs:attribute name="name" type="xs:string" use="required"/>
<!-- the following line needs to be modified somehow -->
<xs:attribute name="value" type="xs:string" use="optional"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I don’t think this is possible. The only way I can think of is something like this:
But apparently this is not allowed. When I validate this schema in Eclipse, it gives me the following errors:
As you can see, you can’t have two elements with the same name but with a different type. But this is what your XML requires.
Edit: You can’t even define a simpler schema like this:
In this version, no
<choice>is given, meaning your parameters would have to appear in the given order. But while this removes thenonambigerror, theelement-consistenterror remains.