Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define mutually exclusive attributes in XSD?

###First the code fragment...

<tag name="default" abc="10" def="20"> <!-- not valid, abc and def should be mutually exclusive -->

<tag name="default1" abc="10"> <!-- valid -->

<tag name="default2" def="20"> <!-- valid -->

###What I want to do...

What can I put into my XSD so that @abc and @def cannot coexist as attributes on the same element?

So that validation would fail if they coexisted on the same element?

like image 604
ycomp Avatar asked Nov 17 '15 03:11

ycomp


People also ask

How do you define an element attribute in XSD?

Attributes are defined within an XSD as follows, having name and type properties. An Attribute can appear 0 or 1 times within a given element in the XML document. Attributes are either optional or mandatory (by default the are optional).

What is complexType and simpleType in XSD?

XSD elements can be of type simpleType , complexType , or anyType . An element of type simpleType contains only text. It cannot have attributes and elements. An element of type complexType can contain text, elements, and attributes.

What is XS simpleContent in XSD?

<xsd:simpleContent> ElementContains extensions or restrictions on a complexType element with character data or a simpleType element as content and contains no elements. Copy. <simpleContent id = ID {any attributes with non-schema Namespace}...> Content: (annotation?, ( restriction | extension)) </simpleContent>

What is Nillable in XSD?

The nillable attribute can be defined on an xsd:element within an XML schema. It specifies that the xsi:nil attribute is valid for the element. If an XML schema has defined the nillable attribute as true, it is mapped as a required attribute and is included in the document, however, its values are nullified.


1 Answers

With XSD 1.0, you can use xs:keyelement.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="tag">
    <xs:complexType>
        <xs:attribute name="name" type="xs:string" use="required"/>
        <xs:attribute name="abc"  type="xs:integer"/>      
        <xs:attribute name="def"  type="xs:integer"/>
     </xs:complexType>
    <xs:key name="attributeKey">
        <xs:selector xpath="."/>
        <xs:field xpath="@abc|@def"/>
    </xs:key>
</xs:element>   

Edit: If both attributes are present (even with different values), this creates two keys, so the XML validation will fail. On the other hand, the <xs: key> requires that a key is defined for the element, and therefore one of the two attributes must be present.

the following XML doc is not valid using the above XSD. (I'am using oXygen 17.0):

<tag xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="stack3.xsd" name="" abc="12" def="13"/>

Error:

cvc-identity-constraint.3: Field "./@abc|./@def" of identity constraint "attributeKey" matches more than one value within the scope of its selector; fields must match unique values
like image 170
Kachna Avatar answered Oct 02 '22 02:10

Kachna