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

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

When I am creating maps it is annoying to me to guess which values I'm getting if I map 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 (the BizTalk Mapper has several 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 was waiting that the behavior 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  
   
Elements Attributes Notes Notes [Leonid Ganeline]
(minOccurs, maxOccurs), fixed, default use, fixed, default    
(1, 1) -, - required, -, - element/attribute must appear once, it may have any value If 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 37 If source attrbute/element is missing the mapping creates the attribute/element with Empty value, not 37! (and of course, Target XML document doesn't pass the output validation.)
(2, unbounded) 37, - n/a element 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 attribute/element is missing the mapping creates the attribute with Empty value and any 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 value no changes
(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 37 If source attribute/element is missing any attribute/element is created! If source attribute/element is != Fixed_value any attribute/element is changed to Fixed_value, it mapped from a source value, And we have getting the output validation errors.
(0, 1) -, 37 optional, -, 37 element/attribute may appear once; if it does not appear its value is 37, otherwise its value is that given If source attribute/element is missing, any attribute/element is created! If source attribute/element is != Default_value any attribute/element is changed to Default_value, it mapped from a source value, And we have NOT got the output validation errors.
(0, 2) -, 37 n/a element 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, any element is created! If source element is != Default_value any element is changed to Default_value, it mapped from a source value, And we do NOT have the output validation error.
(0, 0) -, - prohibited, -, - element/attribute must not appear no changes. BTW If I delete an element but save a white space 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.
Print | posted on Friday, December 23, 2005 6:51 PM

Feedback

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

left by Lorin at 12/23/2005 7:34 PM Gravatar
Very good info on schemas in XML.

One interesting tidbit to note: your chart was from Excel, and when it got dumped out it included a style to set all TDs on the page to have white-space:nowrap, which goofed with the display on the main gwb page. I had posted a little earlier, and modified my post with another style tag to compensate. So in the future you'll want to clean out all white-space style references before posting!
Post A Comment
Title:
Name:
Email:
Comment:
Verification: