?
Solved

variable maxOccurs

Posted on 2002-07-09
7
Medium Priority
?
736 Views
Last Modified: 2012-06-27
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?
0
Comment
Question by:jdaues
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 1

Expert Comment

by:jetball
ID: 7155968
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
 

Author Comment

by:jdaues
ID: 7156097
>><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
 
LVL 6

Accepted Solution

by:
Wayne Bradney earned 200 total points
ID: 7158345
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
CHALLENGE LAB: Troubleshooting Connectivity Issues

Goal: Fix the connectivity issue in the lab's AWS environment so that you can SSH into the provided EC2 instance.  

 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 7158356
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
 

Author Comment

by:jdaues
ID: 7160026
jetball:
you have said nothing incorrect, but wbradney has gone into much greater depth
0
 

Author Comment

by:jdaues
ID: 7160036
>>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
 
LVL 6

Expert Comment

by:Wayne Bradney
ID: 7162325
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

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Client Need Led Us to RSS I recently had an investment company ask me how they might notify their constituents about their newsworthy publications.  Probably you would think "Facebook" or "Twitter" but this is an interesting client.  Their cons…
Create a Windows 10 custom Image with custom task bar and custom start menu using XML for deployment.
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

800 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question