Solved

variable maxOccurs

Posted on 2002-07-09
7
676 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
  • 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:
wbradney earned 50 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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 6

Expert Comment

by:wbradney
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:wbradney
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

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…
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

758 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now