Mapping Empty or Missing attributes and elements with different combinations of parameters: Required/Optional, Min/MaxOccurs, Default, Fixed


When I create maps it is annoying to guess which values I’ll get if I have to mapping Empty or Missing attributes/elements. I’ve


made an investigation on the base of a Standard “XML Schema Part 0: Primer. W3C Recommendation, 2 May 2001”.
http://www.w3.org/TR/2001/REC-xmlschema-0-20010502/#OccurrenceConstraints (below is this article).
I added the last column with my comments to the original table (in BizTalk mapping are some differences from the standard!).


I used the BTS2004 Mapper and “Test Map” command. I’ve changed the properties of attribute and element in a target schema.
I waited that the behaviour of mapping should be the same as in standard.
Results are different in several points:
1) “Fixed” and “Default” properties don’t change mapping. They only change validation and they have a different effect on validation.
2) “Required” property of attributes: If source attribute is missing the mapping creates the attribute with Empty value.
3) Something strange with “min/maxOccures” properties of elements: If they are (1,1), (-,1), (1,-), or (-,-) and source element is missing the mapping creates an element with Empty value! Only these combinations generate an element with Empty value, other combinations do not create an element.




2.2.1 Occurrence Constraints
The comment element is optional within PurchaseOrderType because the value of the minOccurs attribute in its declaration is 0. In general, an element is required to appear when the value of minOccurs is 1 or more. The maximum number of times an element may appear is determined by the value of a maxOccurs attribute in its declaration. This value may be a positive integer such as 41, or the term unbounded to indicate there is no maximum number of occurrences. The default value for both the minOccurs and the maxOccurs attributes is 1. Thus, when an element such as comment is declared without a maxOccurs attribute, the element may not occur more than once. Be sure that if you specify a value for only the minOccurs attribute, it is less than or equal to the default value of maxOccurs, i.e. it is 0 or 1. Similarly, if you specify a value for only the maxOccurs attribute, it must be greater than or equal to the default value of minOccurs, i.e. 1 or more. If both attributes are omitted, the element must appear exactly once.


Attributes may appear once or not at all, but no other number of times, and so the syntax for specifying occurrences of attributes is different than the syntax for elements. In particular, attributes can be declared with a use attribute to indicate whether the attribute is required (see for example, the partNum attribute declaration in po.xsd), optional, or even prohibited.


Default values of both attributes and elements are declared using the default attribute, although this attribute has a slightly different consequence in each case. When an attribute is declared with a default value, the value of the attribute is whatever value appears as the attribute’s value in an instance document; if the attribute does not appear in the instance document, the schema processor provides the attribute with a value equal to that of the default attribute. Note that default values for attributes only make sense if the attributes themselves are optional, and so it is an error to specify both a default value and anything other than a value of optional for use.


The schema processor treats defaulted elements slightly differently. When an element is declared with a default value, the value of the element is whatever value appears as the element’s content in the instance document; if the element appears without any content, the schema processor provides the element with a value equal to that of the default attribute. However, if the element does not appear in the instance document, the schema processor does not provide the element at all. In summary, the differences between element and attribute defaults can be stated as: Default attribute values apply when attributes are missing, and default element values apply when elements are empty.


The fixed attribute is used in both attribute and element declarations to ensure that the attributes and elements are set to particular values. For example, po.xsd contains a declaration for the country attribute, which is declared with a fixed value US. This declaration means that the appearance of a country attribute in an instance document is optional (the default value of use is optional), although if the attribute does appear, its value must be US, and if the attribute does not appear, the schema processor will provide a country attribute with the value US. Note that the concepts of a fixed value and a default value are mutually exclusive, and so it is an error for a declaration to contain both fixed and default attributes.


The values of the attributes used in element and attribute declarations to constrain their occurrences are summarized in Table 1.










































































Table 1. Occurrence Constraints for Elements and Attributes
ElementsAttributesNotes Notes [Leonid Ganeline]
(minOccurs, maxOccurs), fixed, defaultuse, fixed, default
(1, 1) -, –required, -, –element/attribute must appear once, it may have any valueIf source attrbute/element is missing the mapping creates the attribute/element with Empty value!
(1, 1) 37, –required, 37, –element/attribute must appear once, its value must be 37If source attrbute/element is missing the mapping creates the attribute/element with Empty value, not 37! (Of course, Target XML doc doesn’t pass the output validation.)
(2, unbounded) 37, –n/aelement must appear twice or more, its value must be 37; in general, minOccurs and maxOccurs values may be positive integers, and maxOccurs value may also be “unbounded”If source attrbute/element is missing the mapping creates the attribute with Empty value and non an element! I’ve tried different combinations of min/maxOccurs. Only (1,1),(-,1),(1,-), and (-,-) created an Empty element!
(0, 1) -, –optional, -, –element/attribute may appear once, it may have any valueexactly
(0, 1) 37, –optional, 37, –element/attribute may appear once, if it does appear its value must be 37, if it does not appear its value is 37If source attrbute/element is missing Non attrbute/element is created! If source attrbute/element is != Fixed_value Non attrbute/element is changed to Fixed_value, it mapped from a source value, And we’ve got the output validation errors.
(0, 1) -, 37optional, -, 37element/attribute may appear once; if it does not appear its value is 37, otherwise its value is that givenIf source attrbute/element is missing/empty Non attrbute/element is created! If source attrbute/element is != Default_value Non attrbute/element is changed to Default_value, it mapped from a source value, And we have NOT got the output validation errors.
(0, 2) -, 37n/aelement may appear once, twice, or not at all; if the element does not appear it is not provided; if it does appear and it is empty, its value is 37; otherwise its value is that given; in general, minOccurs and maxOccurs values may be positive integers, and maxOccurs value may also be “unbounded”If source element is missing/empty Non element is created! If source element is != Default_value non element is changed to Default_value, it mapped from a source value, And we have NOT got the output validation error.
(0, 0) -, –prohibited, -, –element/attribute must not appearexactly. BTW If I delete an element but save a whitespace on its place I’ve got an error”error btm1046: Output validation error: The element cannot contain text or white space. Content model is empty.” (?)
Note that neither minOccurs, maxOccurs, nor use may appear in the declarations of global elements and attributes.