Search code examples
xmlxsdxsd-validationxml-validationxsd-1.0

XSD 1.0 set attribute to false if another has no value


I am trying to write a schema where I have two attributes ID and required:

  • ID can be any string or empty string
  • required is boolean true/false

I need to set required to false if ID has no text.

I searched all over this place and found this can be done using XSD 1.1, but I don't have access to this because its not supported in Visual Studio 2017. Is there an alternative to this in XSD 1.0 ?

This a part of the schema:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
       
    <xs:element name="sizeGroup">
        <xs:complexType>
            <xs:attribute name="ID" type="xs:string" use="required" />
            <xs:attribute name="required" type="xs:boolean" use="required" />
        </xs:complexType>
    </xs:element>
</xs:schema>

Solution

  • XSDs constrain or require properties of XML documents; they do not set properties.

    Assuming you mean that you wish to require, not set, an attribute to have a certain value based on another attribute's value, no, such a constraint cannot be expressed in XSD 1.0. You'll need XSD 1.1 assertions.

    Note further: Requiring that an attribute value be derived from content already present in another XML component is often a sign of design weakness.

    See also