variable maxOccurs

i want to have a variable number of maxOccurs.
Something like this:

  <xsd:element name="numberOfRows" >
     <xsd:simpleType base="xsd:unsignedShort"/>
     <xsd:minInclusive Value="0"/>
     <xsd:maxInclusive Value="10000"/>
  </xsd:element>

  <xsd:element name="row" maxOccurs="numberOfRows"/>

Obviously, this is not valid, since maxOccurs is a union of the value space for nonNegativeInteger with the value space of a simpleType which contains only one enumeration value - "unbounded".  

Is there anyway to do what I want?
jdauesAsked:
Who is Participating?
 
Wayne BradneyConnect With a Mentor Independent ConsultantCommented:
A schema is a static definition of how conforming instance documents should look. There's no way to have dynamic checks on the minimum and maximum numbers of elements occuring in the document. What is it exactly that you are trying to do here, in plain English?

With XML Schema, you can say, for example:

"Parent must have at least 1 but no more than 5 Children":
<xsd:element name="Child" maxOccurs="5"/>
OR <xsd:element name="Child" minOccurs="1" maxOccurs="5"/>

"Parent must have exactly 1 Child":
<xsd:element name="Child"/>
OR <xsd:element name="Child" minOccurs="1"/>
OR <xsd:element name="Child" maxOccurs="1"/>
OR <xsd:element name="Child" minOccurs="1" maxOccurs="1"/>

"Parent may have any number of Children, including none at all":
<xsd:element name="Child" minOccurs="0" maxOccurs="unbounded"/>


There's no way to "compute" the minOccurs and maxOccurs values because the validating parser will have no way to do the computation as it's parsing and validating the document. If you *really* need run-time variability of occurrence checking, you'll have to do it in the DOM after the document has been checked for validity.

Again, what is it *exactly* you are trying to achieve here -- maybe there's a different way to do what you want.

See the spec for more info at:
http://www.w3.org/TR/xmlschema-0/#OccurrenceConstraints

Regards,
WMB
0
 
jetballCommented:
is this what you wanted to do?

<xsd:simpleType name="numberOfRows">
  <xsd:restriction base="xsd:unsignedShort">
    <xsd:minInclusive value="0"/>
    <xsd:maxInclusive value="10000"/>
  </xsd:restriction>
</xsd:simpleType>
<xsd:element name="row" type="numberOfRows"/>

this restrict the text value of element row to 0..10000
<row>0</row> to <row>10000</row>

or do you wanted
<xsd:element name="row" minOccurs="0" maxOccurs="10000"/>
where the 0 and 10000 is reference by some name? I don't think you can do this...
0
 
jdauesAuthor Commented:
>><xsd:element name="row" minOccurs="0" maxOccurs="10000"/>
>>where the 0 and 10000 is reference by some name? I don't >>think you can do this...
this is what i want
0
[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

 
Wayne BradneyIndependent ConsultantCommented:
On reading this topic again it seems to me that what you're looking for is something like this:

VALID DOCUMENT:
---------------
<Parent>
  <NumberOfChildren>3</NumberOfChildren>
  <Child>Bill</Child>
  <Child>Bob</Child>
  <Child>Betty</Child>
</Parent>

INVALID DOCUMENT:
-----------------
<Parent>
  <NumberOfChildren>4</NumberOfChildren>
  <Child>Bill</Child>
  <Child>Bob</Child>
  <Child>Betty</Child>
</Parent>

There's no way to put together an XML Schema that will distinguish between these cases. A validating parser has no way to know (and there's no way in XML Schema to specify), that there's any link between <NumberOfChildren> and the count of how many <Child> elements there are in the document. That is application-specific knowledge that would have to be checked after parsing.

The question is: Why would you need the <NumberOfChildren> element at all in your document? The number of elements is the number of elements, why would you need redundant information?

Regards,
WMB
0
 
jdauesAuthor Commented:
jetball:
you have said nothing incorrect, but wbradney has gone into much greater depth
0
 
jdauesAuthor Commented:
>>There's no way to "compute" the minOccurs and maxOccurs >>values because the validating parser will have no way to do >>the computation as it's parsing and validating the document. >>If you *really* need run-time variability of occurrence >>checking, you'll have to do it in the DOM after the document >>has been checked for validity.
This is what I had suspected, but wasn't certain

>>Why would you need the <NumberOfChildren> element at all in >>your document? The number of elements is the number of >>elements, why would you need redundant information?
Hmm...this is a very good point.  The reason I have there is because the data in the java object that the XML data will be bound to has both the <NumberOfChildren> and then that many <Child>'s.  I suppose I could dispose of <NumberOfChildren> and instead count the <Child>'s as the XML document is parsed.

0
 
Wayne BradneyIndependent ConsultantCommented:
Yes, indeed. Counting the number of elements that are actually in the document would be the way to go. It's off topic, but I guess the question could be extended to the Java class in question: Why store a separate "count" variable when you can derive that information by asking the Vector or HashMap or whatever Collection object stores the items?

It almost seems like this is a hang-over from an old C/C++ program where you have a dynamically allocated array for which you need to keep a separate count of how many items are allocated/used. There's no room (nor use) for this kind of thing in Java or XML Schema.

Aside from anything else, introducing redundant information increases the chance of error in the instance documents, since the two redundant items must be kept in sync through code (and in turn, more code = more chance for bugs).

As far as the schema is concerned, by having the redunant counter, you're essentially asking the validating parser to make sure that you haven't made a mistake in keeping these two redundant pieces of information in sync, which is not really the design goal of XML Schema. I believe that there are other schema languages which do offer more sophistication with respect to this type of "cross-element" checking, but I don't have any direct experience with those languages.

Regards,
WMB
0
All Courses

From novice to tech pro — start learning today.