NYCPHP Meetup

NYPHP.org

[nycphp-talk] Schema Validation

Ben Sgro ben at projectskyline.com
Tue May 13 10:38:53 EDT 2008


Hello,

I'm having a lot of trouble getting the dom object schema validation to 
work. After looking on google, it seems there isn't a straight forward 
way to handle
complex schemas.

Take the following code:

        if (is_file($schemaFile))
        {
            $domObj->load($requestXml);
            if (!$domObj->schemaValidate($schemaFile))
            {


load() is throwing a warning:
<b>Warning</b>:  DOMDocument::load() [<a 
href='function.DOMDocument-load'>function.DOMDocument-load</a>]: I/O 
warning : failed to load external entity

And so is the call to schemaValidate()
<b>Warning</b>:  DOMDocument::schemaValidate() [<a 
href='function.DOMDocument-schemaValidate'>function.DOMDocument-schemaValidate</a>]: 
The document has no document element.

My XML struct:

<Template archived="0" id="2" label="Millenium PredictR Template" 
version="1" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Meta />
  <Phases>
    <Phase state="development" />
    <Phase state="testing" />
    <Phase state="published">
      <SubjectTypes>
        <SubjectTypeRef idref="MilleniumProperty" />
      </SubjectTypes>
      <DataPointGroups>
        <DataPointGroup dataSourceMappingRef="1" label="Millenium">
          <DataPoint name="prepredictr" />
        </DataPointGroup>
      </DataPointGroups>
      <Output>
        <Transformation enabled="true" 
filename="PredictR_result_to_pxClient" />
      </Output>
    </Phase>
  </Phases>
  <History>
    <Event dateTime="2008-04-14T10:56:12Z" type="created" user="ramap@" />
  </History>
</Template>

My schema struct:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="Template">
        <xs:complexType>
            <xs:sequence>
               
                <xs:element name="Meta" minOccurs="0">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Meta" 
type="SimpleNameValuePairType" minOccurs="0"
                                maxOccurs="unbounded"/>
                        </xs:sequence>
                    </xs:complexType>                   
                </xs:element>
 
                <xs:element name="Phases">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="Phase" minOccurs="3" 
maxOccurs="3">
                                <xs:complexType mixed="true">
                                    <xs:sequence>
                                        <xs:element name="SubjectTypes" 
minOccurs="0" maxOccurs="1">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element 
name="SubjectTypeRef" minOccurs="1" maxOccurs="unbounded">
                                                        <xs:complexType>
                                                            
<xs:attribute name="idref" type="xs:string" use="required"/>
                                                        </xs:complexType>
                                                    </xs:element>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                       
                                        <xs:element 
name="DataPointGroups" minOccurs="0" maxOccurs="1">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element 
name="DataPointGroup">
                                                        <xs:complexType>
                                                            <xs:sequence>
                                                                
<xs:element name="DataPoint">
                                                                    
<xs:complexType>
                                                                        
<xs:sequence>                                                   
                                                                            
<xs:element name="Exceptions" minOccurs="0" maxOccurs="1">
                                                                                
<xs:complexType>
                                                                                    
<xs:sequence>
                                                                                        
<xs:element name="Exception" minOccurs="0" maxOccurs="unbounded">
                                                                                            
<xs:complexType>
                                                                                                
<xs:attribute name="name" type="xs:string" use="required"/>
                                                                                                
<xs:attribute name="value" use="required"/>
                                                                                            
</xs:complexType>
                                                                                        
</xs:element>
                                                                                    
</xs:sequence>                                                           
                                                                                
</xs:complexType>
                                                                            
</xs:element>
                                                                        
</xs:sequence>                                               
                                                                        
<xs:attribute name="name" use="required"/>
                                                                        
<xs:attribute name="dataSourceMappingRef" use="required"/>
                                                                    
</xs:complexType>
                                                                
</xs:element>
                                                            
</xs:sequence>                                   
                                                            
<xs:attribute name="label"/>
                                                        </xs:complexType>
                                                    </xs:element>
                                                </xs:sequence>
                                            </xs:complexType>
                                        </xs:element>
                                       
                                        <xs:element name="Output" 
minOccurs="0" maxOccurs="1">
                                            <xs:complexType>
                                                <xs:sequence>
                                                    <xs:element 
name="Transformation"/>
                                                </xs:sequence>
                                                <xs:attribute 
name="enabled" type="xs:boolean"/>
                                                <xs:attribute 
name="filename"/>
                                            </xs:complexType>
                                        </xs:element>
                                    </xs:sequence>
                                    <xs:attribute name="state">
                                        <xs:simpleType>
                                            <xs:restriction 
base="NonEmptyString">
                                                <xs:pattern 
value="development|testing|published"></xs:pattern>
                                            </xs:restriction>
                                        </xs:simpleType>
                                    </xs:attribute>
                                    <xs:attribute name="version" 
use="optional" type="xs:integer"/>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
               
        <!--        <xs:element name="History" minOccurs="0" maxOccurs="1">
                    <xs:complexType>
                        <xs:sequence>
                           
                            <xs:element name="Event" minOccurs="0" 
maxOccurs="unbounded"/>
                           
                        </xs:sequence>
                        <xs:attribute name="type"/>
                        <xs:attribute name="user"/>
                        <xs:attribute name="dateTime"/>
                    </xs:complexType>
                </xs:element> -->
               
               <xs:element name="History">
                    <xs:complexType>
                        <xs:sequence minOccurs="0" maxOccurs="unbounded">
                            <xs:element name="Event">
                                <xs:complexType mixed="true">
                                    <xs:attribute name="type"/>
                                    <xs:attribute name="user"/>
                                    <xs:attribute name="dateTime"/>
                                    <xs:anyAttribute namespace="##any" 
processContents="lax"/>
                                </xs:complexType>
                            </xs:element>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
               
            </xs:sequence>
           
            <xs:attribute name="id" type="xs:integer"/>
            <xs:attribute name="label" type="xs:string"/>
            <xs:attribute name="version" type="xs:integer"/>
            <xs:attribute name="archived" type="xs:integer"/>
        </xs:complexType>
    </xs:element>
 

    <xs:complexType name="SimpleNameValuePairType">
        <xs:sequence>
            <xs:element name="Meta" type="SimpleNameValuePairType" 
minOccurs="0"
                maxOccurs="unbounded"/>
        </xs:sequence>
        <xs:attribute name="name" use="required">
            <xs:simpleType>
                <xs:restriction base="xs:string">
                    <xs:minLength value="1"/>
                    <xs:whiteSpace value="collapse"/>
                </xs:restriction>
            </xs:simpleType>
        </xs:attribute>
        <xs:attribute name="value" type="xs:anySimpleType"/>
    </xs:complexType>
   
    <xs:simpleType name="NonEmptyString">
        <xs:restriction base="xs:string">
            <xs:minLength value="1"/>
        </xs:restriction>
    </xs:simpleType>
   
</xs:schema>


- Ben



More information about the talk mailing list