Conditional Type Assignment

XML Schema 1.1 : Conditional Type Assignment revisited

One of the other features of XML Schema 1.1, that I like very much is "conditional type assignment", or CTA. The only requirement is, that there must be an attribute on an XML element to use this feature.

Here is a very simple example.

I'm directly writing an XML Schema 1.1 document below using CTA:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

   <xs:element name="X">
        <xs:alternative type="Type1" test="@xa = 1"/>
        <xs:alternative type="Type2" test="@xa = 2"/>
        <xs:alternative type="xs:error"/>
   </xs:element>
  
   <xs:complexType name="Type1">
        <xs:sequence>
            <xs:element name="a" type="xs:int"/>
            <xs:element name="b" type="xs:int"/>
        </xs:sequence>
        <xs:attribute name="xa" type="xs:int"/>
   </xs:complexType>
  
   <xs:complexType name="Type2">
        <xs:sequence>
            <xs:element name="p" type="xs:int"/>
            <xs:element name="q" type="xs:int"/>
        </xs:sequence>
        <xs:attribute name="xa" type="xs:int"/>
   </xs:complexType>
 
</xs:schema>

The requirement of XML Schema 1.1 validation in this case is: If the attribute "xa" on element "X" has value 1, then element "X" has a certain type. If the value of attribute "xa" is 2, then element "X" has another type.

The two valid XML documents for the given XML Schema document are following:

<?xml version="1.0" encoding="UTF-8"?>
<X xa="1">
    <a>1</a>
    <b>2</b> 
</X>

and,

<?xml version="1.0" encoding="UTF-8"?>
<X xa="2">
  <p>1</p>
  <q>2</q> 
</X>

For anything else as value of attribute "xa", or infact any other kind of content the element "X" will be assigned the type xs:error (which makes the element "X" invalid").

Assign conditional-based values and rules

  • 6 minutes to read

VSTS (Hosted XML) | TFS 2018 | TFS 2017 | TFS 2015 | TFS 2013

You can define rules that are run conditionally by using the WHEN, WHENNOT, WHENCHANGED, and WHENNOTCHANGED elements. You use these rules to define which elements are run when the defined clause is . You can define conditions that are based on what value is assigned to a specific field or whether a user modifies a specific field. For example, you can create a dependent pick list to provide detailed security or custom behavior.

Field conditions are additional elements that you list inside a (Definition) element or the (Workflow) element. For more information about these elements, see FIELD (Definition) element reference and FIELD (Workflow).

The following code is a simple example of the WHEN clause:

This clause means that anything within this FIELD element is applicable as long as the field has the value "yyy". The field must be a valid field reference name. For more information, see Naming conventions for work item tracking objects.

Note

The value attribute is case-insensitive. Therefore, if the field reference name holds "YYY", matches include the values "yyy" and "YYY".

Syntax structure for conditional elements

The following table describes conditional rules that you can specify as child elements of the (Definition) element or (Workflow) element. These elements accept one or more of the following attributes:

  • : A string that describes the field. Must contain 1 to 255 characters.
  • : When the specified field has this value, the rules in the and elements are applied to the current field.

Element

Syntax

Description

WHEN

Specifies one or more rules to apply to the current field when another field has a specific value. The parent element defines the current field.

When the specified field has the specified value, the rules in this element are applied to the current field.

WHENNOT

Specifies a condition under which to apply one or more rules to the current field. The rules apply to the current field when the value of another field changes. The parent element defines the current field.

When the specified field does not contain the specified value, the rules in this element are applied to the current field.

WHENCHANGED

Specifies a condition under which to apply one or more rules to the current field. The rules apply to the current field when the value of another field is changed in a revision to a work item. The parent element defines the current field.

WHENNOTCHANGED

Specifies a condition under which to apply one or more rules to the current field. The rules apply to the current field when the value of another field is not changed in a revision to a work item. The parent element defines the current field.

The following table describes how each optional, conditional-based rule is applied to the parent field when the conditional clause that you specify by using a WHEN, WHENNOT, WHENCHANGED, or WHENNOTCHANGED element is true. For more information, see Apply a field rule.

ElementDescription
ALLOWEDVALUESThe parent field must have a value that comes from the specified list of values.
ALLOWEXISTINGVALUEThe value of the parent field that already exists will be allowed, even if it violates other rules. This element is not applicable if the value of the parent field is changed.
CANNOTLOSEVALUEUsers can change the value of the parent field to NULL, but they cannot change it to any other value.
COPYThe value of a third field is automatically copied into the parent field. You specify the third field in the COPY element.
DEFAULTThis element specifies the default value of the parent field.
EMPTYThe parent field must not contain a value.
FROZENThe parent field is frozen. When a field is frozen, you can change its value to NULL, but you cannot change it to any other value.
MATCHThe value of the parent field must match the pattern that you specify.
NOTSAMEASThe value of the parent field cannot match the value of a third field. You specify the third field in the NOTSAMEAS element.
PROHIBITEDVALUESThe parent field cannot contain any values in the enumerated list.
READONLYThe parent field is read-only.
REQUIREDThe parent field must contain a value that is not NULL.
SERVERDEFAULTThe parent field takes its value from the specified server component. The valid server components are clock, which is the time when the work item is updated, and currentuser, which is the identity of the user who updated the work item.
SUGGESTEDVALUESThe enumerated list contains suggested values for the parent field.
VALIDUSEROnly the users whom you specify can modify the parent field.

Define a dependent required field

You can specify that a field is required only when another field contains a specific value. In the following example, when a customer reports a bug, a customer severity must be specified. If the bug was not reported by a customer, a customer severity is not required.

Define a conditional pick list

The following example demonstrates a conditional pick list in which the allowed values for the Problem Type field are limited, based on whether the value of the ProblemCharacteristic field is set to Documentation.

Define a field when the user changes another field (WHENCHANGED)

In the following example, when a user changes the value of the MyCorp.State field, the MyCorp.StateDate field is set to the current date and time, as the server clock shows.

In the following example, when a user changes the value of the MyCorp.State field, the value of the MyCorp.Status field is cleared.

Define a field value based on a user not modifying a field (WHENNOTCHANGED)

In the following example, when a user does not change the value of the MyCorp.State field, the MyCorp.StateDate field becomes read-only.

Related notes

0 Replies to “Conditional Type Assignment”

Lascia un Commento

L'indirizzo email non verrĂ  pubblicato. I campi obbligatori sono contrassegnati *