xsd - Build XML schema that requires element or attribute, but not both -


i create xml schema following constraints:

  • the root element shall list.
  • list contains set of node elements.
  • a node element can have value attribute.
  • a node element can contain value element.
  • a node element can have (and exactly) 1 value attribute or value element.

here example of valid xml verify above constraints:

<?xml version="1.0" ?> <list>     <node id="1" value="a" />     <node id="2">         <value>b</value>     </node> </list> 

i tried following xsd schema:

<xs:schema attributeformdefault="unqualified" elementformdefault="qualified" xmlns:xs="http://www.w3.org/2001/xmlschema">   <xs:element name="list">     <xs:complextype>       <xs:sequence>         <xs:element name="node" maxoccurs="unbounded" minoccurs="0">           <xs:complextype mixed="true">             <xs:sequence>               <xs:element type="xs:string" name="value" minoccurs="0"/>             </xs:sequence>             <xs:attribute type="xs:byte" name="id" use="optional"/>             <xs:attribute type="xs:string" name="value" use="optional"/>           </xs:complextype>         </xs:element>       </xs:sequence>     </xs:complextype>   </xs:element> </xs:schema> 

this xsd schema verify first , second constraint, not third one.

the following xml example not valid according constraints, however, valid against previous xsd schema.

<?xml version="1.0" ?> <list>     <node id="2" value="a" /> <!-- valid -->     <node id="4">         <value>d</value>     </node><!-- valid -->     <node id="1" /><!-- not valid, missing value -->     <node id="3" value="b">         <value>c</value>     </node><!-- not valid, both attribute , element declared --> </list> 

how can change schema verify constraints?

thanks in advance.

this not possible using xsd 1.0.

it should possible xsd 1.1 using assertions:

<xs:assert test="not(@value , value)"/> 

that fails if @value , value both present. check there 1 attribute or sub-element use:

<xs:assert test="count((value, @value))=1"/> 

as suggested michael kay in comments.

it might possible using other xml validation technology schematron.


Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -