DTD "exclusive or" confusion
According to Beginning XML (3rd edition, p. 106), an element can (allegedly) force selection of a child element from one of a group using the "exclusive or" operator "|". Specifically, in reference to the code fragment
<!ELEMENT location (GPS | country)>
the good book doth say, "This declaration would allow our <location> element to contain one <country> or one <GPS> element. If our <location> element...contained more than one of these elements, the parser would raise an error."
Makes sense to me; that's how I've always understood an XOR to work. But while trying to decipher some production code at work, I come on an .xml that shows *both* elements as children to an element which had declared them in the .dtd to be in an XOR relationship. It parses and runs just fine.
Now I'm thinking, "Well, maybe there's something else going on in this (not-so-easily-understood) production code that's overriding the XOR. So I cobble together the following simple code
<?xml version = "1.0"?>
<!DOCTYPE Script
[
<!ELEMENT location (GPS | country)>
<!ELEMENT GPS (#PCDATA)>
<!ELEMENT country (#PCDATA)>
]>
<Root>
<GPS>Some foo</GPS>
<country>Some bar</country>
</Root>
And lo and behold! It parses and runs just fine using both the MSXML 4.0 (sp2) parser as well as (the book-recommended) Topologi "Schematron Validator". So I ask, "What up?!?"
And just to make life extra-confusing, I tried an additional little experiment. This code (adding an additional GPS element to the location parent also parses just fine despite the utter lack of any cardinality indicator.
<?xml version = "1.0"?>
<!DOCTYPE Script
[
<!ELEMENT location (GPS | country)>
<!ELEMENT GPS (#PCDATA)>
<!ELEMENT country (#PCDATA)>
]>
<Root>
<GPS>Some foo</GPS>
<country>Some bar</country>
<GPS>More foo</GPS>
</Root>
This, too, runs counter to the book's pronouncement (p. 113), "...when no cardinality indicator is used...the element must appear once and only once."
Again, I ask, "What the hey...?!?"
Thanks in advance for a clue,
Steve
|