How can I make my XML Schema require foo or bar, or both?
August 18, 2006 8:37 AM   Subscribe

XMLSchemaFilter: I have a spot in my schema where I need to require one of two elements, or both, but not neither, and not two of each.

Originally the schema had an xs:choice with maxOccurs of 2, containing elements foo and bar of FooType and BarType. My understanding of this is that it would allow two foo elements or two bar elements in a row, which I can't have.

I changed the choice so that it can (and must) only occur once and contains: 1) a choice between foo and bar, and 2) a sequence of foo and bar. I figured that would satisfy my requirement of having one or the other, or both, but not neither, and no doubles.

The problem is that VisualStudio (2005) is giving me a warning about redefining foo within the sequence, and I don't want to confuse the validator when it comes time to check XML files that are supposed to match this schema. Oh, and to make matters a little weirder, if I change the name of the foo element, it doesn't seem to care that bar is being redefined in exactly the same way...

I get the feeling that there is a better/more elegant way to achieve what I want here, but I've spent too much trying to figure it out already and really need to move on to other things. Nobody here at work seems to know, and my google-fu has failed me.

Help me hive-mind, you're my only hope!
posted by benign to Computers & Internet (3 answers total)
 
Can you define it as either:
  • foo followed by an optional bar; or
  • a singleton bar
That seems much simpler to me than what you're describing.
posted by cerebus19 at 9:43 AM on August 18, 2006


I haven't played around very much with schemas, but it seems like this could work:

<xs:choice minOccurs="1" maxOccurs="2">
<xs:choice maxOccurs="1">
<xs:element name="foo">
</xs:choice>
<xs:choice maxOccurs="1">
<xs:element name="bar">
</xs:choice>
</xs:choice>

Seems then the outer choice element has to appear at least once (thus eliminating "neither") and no more than twice, and must pick from the two inncer choice elements, which can each only appear once (thus eliminating the possibility of duplicates).

Not road-tested, not sure if that'll work, but that's my best guess.
posted by chrominance at 10:02 AM on August 18, 2006


cerebus19: I don't believe I can define it as foo followed by an optional bar. I can't require foo, because as long as bar is present, it's legitimate for foo to be absent, and vice versa.

chrominance: I just tested that code out and unfortunately it seems to allow duplicates as well.

both: Thanks for taking the time to respond. It's heartening to know that anyone would even read such a question.

Also, the code I thought would work looks like this:

<xsd:choice>
  <xsd:choice>
    <xsd:element name="foo" type="xsd:string" />
    <xsd:element name="bar" type="xsd:string" />
  </xsd:choice>
  <xsd:sequence>
    <xsd:element name="foo" type="xsd:string" />
    <xsd:element name="bar" type="xsd:string" />
  </xsd:sequence>
</xsd:choice>

and I get this warning in VS: Multiple definition of element 'foo' causes the content model to become ambiguous. A content model must be formed such that during validation of an element information item sequence, the particle contained directly, indirectly or implicitly therein with which to attempt to validate each item in the sequence in turn can be uniquely determined without examining the content or attributes of that item, and without any information about the items in the remainder of the sequence.
posted by benign at 11:24 AM on August 18, 2006


« Older hindi music help!   |   Stories about clever young moneymakers? Newer »
This thread is closed to new comments.