The assertions described in this section are assertions on complex types. Such an assertion is defined in an xs:assert element, which was introduced in XML Schema version 1.1, and serves as a validity constraint on the complex type. (The other kind of assertion is an assertion on a simple type, which is defined in an xs:assertion element and is created and edited in the Facets entry helper of a simple type. That kind of assertion is not covered by the feature described in this topic.)
|Note:||Assertions are an XSD 1.1 feature. So the Assertions editing features will be available only in XSD 1.1 mode.|
Where to edit assertions
In Schema View, complex type assertions can be created and edited via the following GUI access points:
•In Schema Overview: In the Assertions tab of the Attributes/Assertions/Identity-Constraints (AAIDC) pane (screenshot below). In order for the Assertions tab to be enabled, change the XSD mode to 1.1 (for instance, via the XSD 1.1 toolbar icon).
•In Content Model View: Assertions can be edited in the Assertions tab (screenshot above) or in the diagram (screenshot below). Only one of these two editing options (tab or diagram) is enabled at a given time. You can switch between them by toggling off/on the Display Assertions in Diagram icon in the Schema Design toolbar (see below). (To set one of these options as the default, go to the Schema Display Configuration dialog (Schema Design | Configure View | Element tab).) In the diagram, select the Assertion box of the complex type or complex-content element. Then enter or edit the Assertion's definition directly or in the Details entry helper.
Display Assertions in Diagram: Enabled in Content Model View. Toggles the display of assertions between the diagram (toggled on) and the Assertions tab.
Scope of the assertion
The XPath expression used to define the assertion's constraint must be within the scope of the complex type on which it is defined. So if the XPath expression is required to access a particular node, then the assertion must be defined on an ancestor of that node.
Adding and deleting assertions
A complex type can have multiple assertions. The XPath expression of each assertion must evaluate to boolean true for the element in the instance document to be valid. To add an assertion to a complex type, do the following:
•In Schema Overview: Select the complex type. Then, in the Assertions tab of the AAIDC pane (see screenshot above), click the Add or Insert icon at the top left of the tab. You can add multiple assertions. To delete an assertion, select it and click the Delete icon at the top right of the tab.
•In Content Model View (see screenshot above): Right-click the complex type and select Add Child | Assertion. Alternatively, right-click an existing assertion in the diagram of the complex type and select Append | Assertion or Insert | Assertion. You can add multiple assertions to a complex type. To delete an assertion, select it and press the Delete key.
Defining the assertion's XPath expression
The XPath expression of a complex type assertion defines the validation constraint to be applied on the complex type element in the instance document. For example, in the screenshots above, the assertion is on the complex-type element team and the assertion's XPath expression is: @region="US". In the XML Schema document, the assertion appears as:
The assertion specifies that, in the instance document, the team element must have a region attribute with a value of US. If it does not, the document will be invalid.
Note the following points:
•XPath expressions must be written in the XPath 2.0 language
•Nodes tested in the XPath expression must be within the scope of the assertion (see above)
•If an expression does not evaluate to boolean true/false, the returned value is converted to a boolean value. A non-empty sequence is converted to true, while an empty sequence is converted to false.
•Syntax errors in the expression are flagged by displaying the expression in red. Context errors are not flagged. For example, if the XPath expression tests an attribute and that attribute is not defined in the schema, no error is flagged.
The assertion's message
It is very useful if an explanation of the assertion is supplied together with its definition, so that in case the assertion is not fulfilled when the XML instance document is validated, an appropriate message can be displayed. Since the XML Schema specification does not make provision for such a message, XMLSpy allows a message in the Altova xml-schema-extensions namespace https://www.altova.com/xml-schema-extensions (or any other namespace) to be provided with the definition of the assertion and to be used in the validation of the XML instance document. For example:
<xs:assert test="count(//MyNode) ge 1" altova:message="There must be at least one MyNode element"/> or
<xs:assertion test="count(//MyNode) ge 1" altova:message="There must be at least one MyNode element"/>
If the restriction specified in the assertion is not fulfilled, XMLSpy's validation engine will display, along with the validation-error message, the message associated with the assertion as a hint. The validator will report the value of an assert/@message attribute or of an assertion/@message attribute regardless of the namespace in which the message attribute is. However, in Schema View, you can edit only message attributes that are in the Altova xml-schema-extension namespace. To edit message attributes in other namespaces, use Text View.
See Assertion Messages for details.
A default namespace declared in the XML Schema document is the default namespace of the XML Schema document. It applies to unprefixed element names in the schema document—but not to unprefixed element names in XPath expressions in the schema document.
The xpathDefaultNamespace attribute (a new feature in XSD 1.1) is the mechanism used to specify the namespace to which unprefixed element names in XPath expressions belong. XPath default namespaces are scoped on the XML Schema elements on which they are declared. The xpathDefaultNamespace attribute can occur on the following XML Schema 1.1 elements:
•xs:assert and xs:assertion
•xs:selector and xs:field (in identity constraints)
The xpathDefaultNamespace on xs:schema is set, in XSD 1.1 mode, in the Schema Settings dialog (Schema Design | Schema Settings). For the other elements listed above, the xpathDefaultNamespace attribute is set in their respective Details entry helpers (see screenshot below for example).
Declaring the XPath default namespace on xs:schema, declares the XPath default namespace for the scope of the entire schema. You can override this declaration on elements where the xpathDefaultNamespace attribute is allowed (see list above).
Instead of containing an actual namespace, the xpathDefaultNamespace attribute can contain one of three keywords:
•##targetNamespace: The XPath default namespace will be the same as the target namespace of the schema
•##defaultNamespace: The XPath default namespace will be the same as the default namespace of the schema
•##local: There is no XPath default namespace
If no XPath default namespace is declared in the document, unprefixed elements in XPath expressions will be in no namespace.
|Note:||The XPath default namespace declaration does not apply to attributes.|
For XPath expressions in assertions, you can also specify the XPath default namespace on the definition of the assertion. In the Assertions tab of the Attributes/Assertions/Identity-Constraints (AAIDC) pane (screenshot below), select the required keyword from the dropdown list of the XPathDefaultNS field.
The selected namespace will be in scope on the assertion.