Altova XMLSpy 2025 Enterprise Edition

Conditional Type Assignment

Home Prev Top Next

Conditional type assignment is an XSD 1.1 feature that allows the type of an element to be determined by content in the XML document, specifically by the value of the element's attributes or by the presence or absence of attributes. For example, say the XML document has the following element:

 

<publication kind="magazine">

...

</publication>

 

In the schema, the type of the publication element can be specified to vary according to the value of the instance element's @kind attribute value. In the schema, this is done using the alternative element, which is new in XSD 1.1. Multiple types are specified, each in an alternative element.

 

In the screenshot below, the Publication element is declared with three alternative child elements, two of which have test attributes (@kind eq 'magazine' and @kind eq 'book'). The third alternative element has no test attribute and a simple type assignment of xs:error (assigned in the Details entry helper, not shown in the diagram), which, if triggered, returns an XML validation error.

CTAAlternatives01

The listing for the above declarations is given below:

 

<xs:complexType name="PublicationType">

  <xs:sequence>

 <xs:element name="Title" type="xs:string"/>

 <xs:element name="Author" type="xs:string" minOccurs="0" maxOccurs="unbounded"/>

 <xs:element name="Date" type="xs:gYear"/>

  </xs:sequence>

  <xs:attribute name="kind" type="xs:string"/>

</xs:complexType>

 

<xs:complexType name="MagazineType">

  <xs:complexContent>

 <xs:restriction base="PublicationType">

            <xs:sequence>

         <xs:element name="Title" type="xs:string"/>

         <xs:element name="Date" type="xs:gYear"/>

    </xs:sequence>

 </xs:restriction>

  </xs:complexContent>

</xs:complexType>

 

<xs:element name="Publication" type="PublicationType">

  <xs:alternative test="@kind eq 'magazine'" type="MagazineType"/>

  <xs:alternative test="@kind eq 'book'">

 <xs:complexType>

            <xs:complexContent>

         <xs:extension base="PublicationType">

              <xs:sequence>

                 <xs:element name="ISBN" type="xs:string"/>

                 <xs:element name="Publisher" type="xs:string"/>

            </xs:sequence>

         </xs:extension>

    </xs:complexContent>

 </xs:complexType>

  </xs:alternative>

  <xs:alternative type="xs:error"/>

</xs:element>

 

Note the following points:

 

The first alternative element from among the alternative siblings to evaluate to true is selected. So the order of alternative elements is important. In Content Model View, the order can be changed by dragging the alternative element boxes into the desired order.

Notice that the Publication element has a type (PublicationType). This type serves as the default type if none of the alternative elements are used. In our example above, however, the alternative element of type xs:error will be used if both the previous alternative element conditions return false.

If no alternative element condition is met and if the element has no default type, then the element is assigned a type of anyType. In this event, the element may have any well-formed XML content.

The alternative element and the simple type xs:error are new in XSD 1.1.

 

Content Model View editing

You can add an alternative type to an element declaration as a child via the element's context menu (see the content model in screenshot above).

 

The type of the alternative element is specified in the Details entry helper. If the type is a complex type, it is shown in the alternative element's expanded rectangle and can be further edited there (see screenshot below). Simple types are not shown in the diagram, but can be defined in the Simple Type tab of the Details entry helper.

CTABoxExpandedCTADetailsEH
Note:You can specify a namespace for the XPath expression via the xpathDefaultNamespace attribute in the Details entry helper. For more information about XPath default namespaces, see the section below.

 

Using xpathDefaultNamespace

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:schema

xs:assert and xs:assertion

xs:alternative

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).

AssertEHDetails02XPDNS

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.

 

 

© 2018-2024 Altova GmbH