[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

variable maxOccurs

Posted on 2002-07-09
7
Medium Priority
?
745 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:
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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…

865 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