Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


advertisement
 

Taking XML Validation to the Next Level: Explore CAM's Expressive Power : Page 5

The generic-sounding Content Assembly Mechanism, or CAM, is an exciting step beyond XML Schema, but it's new and not well documented. This article series represents CAM: The Missing Manual. This last installment is a deep exploration of CAM's ability to express exactly what you need for data-centric documents.


advertisement

Cardinality and the setChoice Predicate

Earlier you learned about the predicates available for specifying cardinality (see Table 3). This section reveals the next level of complexity by layering setChoice on top of cardinality. The downloadable code includes a sample CAM template (Compositors/compositors_setChoice_sandbox.cam) and XML file (Compositors/compositors_setChoice_sandbox.xml) that you can modify to duplicate the examples in this section.

Example 1

Rule Set Cardinality
setChoice(//waterfowl-category/*) Exactly 1

This rule set permits exactly one of any of the child nodes of waterfowl-category because when there are no cardinality rules the cardinality defaults to 1. The particular child node does not matter since the rule specifies the * XPath selector. In the following examples, only the middle one—containing exactly one child—validates successfully.

XML Sample Child Elements Validates?

<myRoot> <waterfowl-category> <dabbling>string</dabbling> <diving>string</diving> </waterfowl-category> </myRoot>

2 Fail

<myRoot> <waterfowl-category> <dabbling>string</dabbling> </waterfowl-category> </myRoot>

1 Pass


<myRoot> <waterfowl-category> </waterfowl-category> </myRoot>

0 Fail

Example 2

Rule Set Cardinality
setChoice(//waterfowl-category/*)
makeOptional(//waterfowl-category/*)
0 or 1

This rule set adds an explicit cardinality rule to the setChoice rule. By referencing the same set of nodes that states that you want 0 or 1 child nodes to appear. That means a total of 0 or 1 nodes, not 0 or 1 of each choice. The first sample shown below fails to validate because the total child node count is 2. As long as there is only 1 of either possible choice, or none, the tree will validate.

XML Sample Child Elements Validates?

<myRoot> <waterfowl-category> <dabbling>string</dabbling> <diving>string</diving> </waterfowl-category> </myRoot>

2 Fail

<myRoot> <waterfowl-category> <dabbling>string</dabbling> </waterfowl-category> </myRoot>

1 Pass

<myRoot> <waterfowl-category> <diving>string</diving> </waterfowl-category> </myRoot>

1 Pass

<myRoot> <waterfowl-category> </waterfowl-category> </myRoot>

0 Pass

Example 3

Rule Set Cardinality
setChoice(//waterfowl-category/*)
makeRepeatable(//waterfowl-category/*)
1 or more

This cardinality rule permits any non-zero combination of child nodes. The setChoice rule allows them in any combination. Thus the XML may contain all dabbling nodes, just one diving node, or some of both, as shown below. Only the final row containing none violates the cardinality constraints.

XML Sample Child Elements Validates?

<myRoot> <waterfowl-category> <dabbling>string</dabbling> <diving>string</diving> <dabbling>string</dabbling> </waterfowl-category> </myRoot>

3 Pass

<myRoot> <waterfowl-category> <dabbling>string</dabbling> <dabbling>string</dabbling> </waterfowl-category> </myRoot>

2 Pass

<myRoot> <waterfowl-category> <dabbling>string</dabbling> <diving>string</diving> </waterfowl-category> </myRoot>

2 Pass

<myRoot> <waterfowl-category> <diving>string</diving> </waterfowl-category> </myRoot>

1 Pass

<myRoot> <waterfowl-category> </waterfowl-category> </myRoot>

0 Fail

Example 4

Rule Set Cardinality
setChoice(//waterfowl-category/*)
makeRepeatable(//waterfowl-category/*)
makeOptional(//waterfowl-category/*)
0 or more

By combining makeOptional (0 or 1) with makeRepeatable (1 or more), the result of 0 or more allows any combination of the set of choices, including none.

XML Sample Child Elements Validates?

<myRoot> <waterfowl-category> <dabbling>string</dabbling> <diving>string</diving> <dabbling>string</dabbling> </waterfowl-category> </myRoot>

3 Pass

<myRoot> <waterfowl-category> <dabbling>string</dabbling> <diving>string</diving> </waterfowl-category> </myRoot>

2 Pass

<myRoot> <waterfowl-category> <diving>string</diving> </waterfowl-category> </myRoot>

1 Pass

<myRoot> <waterfowl-category> </waterfowl-category> </myRoot>

0 Pass

Example 5

Rule Set Cardinality
setRequired(//waterfowl-category/*,2)
setLimit(//waterfowl-category/*,5)
setChoice(//waterfowl-category/*)
Between2 and 5

This rule set shows the remaining available cardinality predicates from Table 3: setRequired specifies a lower bound while setLimit specifies an upper bound. Any occurrences outside those bounds will fail validation, as shown.

XML Sample Child Elements Validates?

<myRoot>
<waterfowl-category>
<dabbling>string</dabbling>
<dabbling>string</dabbling>
<dabbling>string</dabbling>
<diving>string</diving>
<dabbling>string</dabbling>
<diving>string</diving>
</waterfowl-category>
</myRoot>

6 Fail

<myRoot>
<waterfowl-category>
<dabbling>string</dabbling>
<dabbling>string</dabbling>
<diving>string</diving>
<dabbling>string</dabbling>
</waterfowl-category>
</myRoot>

4 Pass

<myRoot>
<waterfowl-category>
<dabbling>string</dabbling>
<diving>string</diving>
</waterfowl-category>
</myRoot>

2 Pass

<myRoot>
<waterfowl-category>
<diving>string</diving>
</waterfowl-category>
</myRoot>

1 Fail



Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap