Re: DOMDocument::schemaValidate() - group nodes inside choice nodes

[email protected] ("nick eby") Thu, 11 Nov 2004 16:43:52 -0800
Newsgroups php.xml.dev
Message-ID <[email protected]>
here is a full script that much better illustrates what i'm talking about.
note the difference between the two XSD's.  i contend that the document
should validate against both, but i suspect that i've just been looking at
it for too long.

<?
$prolog = '<?xml version="1.0"?>';

$xsd = $xsd2 = $xml = $prolog;
$xsd .= <<<EOT
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customer">
<xs:complexType>
<xs:choice>
<xs:element ref="customer-id"/>
<xs:group ref="customerinfo"/>
</xs:choice>
</xs:complexType>
</xs:element>

<xs:element name="customer-id" type="xs:long" />

<xs:group name="customerinfo">
<xs:sequence>
<xs:element ref="name" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:group>

<xs:element name="name" type="xs:string"></xs:element>
</xs:schema>
EOT;

$xsd2 .= <<<EOT
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="customer">
<xs:complexType>
<xs:choice>
<xs:element ref="customer-id"/>
<xs:group name="customer-info">
<xs:sequence>
<xs:element ref="name" minOccurs="1" maxOccurs="1"/>
</xs:sequence>
</xs:group>
</xs:choice>
</xs:complexType>
</xs:element>

<xs:element name="customer-id" type="xs:long" />

<xs:element name="name" type="xs:string"></xs:element>
</xs:schema>
EOT;

$xml .= <<<EOT
<customer>
<name>Bob Eubanks</name>
</customer>
EOT;

$doc = DOMDocument::loadXML($xml);
echo ($doc->schemaValidateSource($xsd) ? " xsd1 OK " : " xsd1 NOT OK" );
echo "<BR/>";
echo ($doc->schemaValidateSource($xsd2) ? " xsd2 OK " : " xsd2 NOT OK" );
?>