Jump to content

Schema validation


mpsn

Recommended Posts

Hi, here is my validateXML function, it checks first if an XML file is well-formed and result $result is true, then if well-formed and schema provided, it does further check to see if schema validated and result $result returns true too (using php core function: schemaValidate(string $schemaFileName), so my let's say I use stuff.xml and houses.xsd, so even if stuff.xml is well formed, and the function goes to evaluate houses.xsd(assume also validated), then the $result of true doesn't really indicate that the well formed stuff.xml is also schema validated since schemaValidate only checks if a schema itself is validated, BUT how does it link the validated schema to the well-formed stuff.xml.

 

In short, I am saying I can pass a wellformed xml file that with a validated schema file BUT the two may be totally different.

 

Here is the function:

</php
public function validateXML($xmlFilename, $xmlSchema=null)
    {
        $result = false; //which line you don't understand
        $dom = new DOMDocument();
        if ( $dom->load($xmlFilename) || $dom->loadXML($xmlFilename))//so this tests if it is well formed?yes
        { //and if false, $result is false
            //the xml is well-form, now test schema
            $result = true;
            if ( $xmlSchema ) // if we don't pass schema , mean we don't need to test the shcme, the validate will return true still
            {                
                $result = $dom->schemaValidate($xmlSchema);//returns true on success
			}            
        }      
        //error occurrs, if there is not erro, this code will not run, because errors is empty array
        $errors = libxml_get_errors(); //stores each line as array elem
foreach ($errors as $error) { 
	print $this->showLibXMLErrors($error); 
} 
libxml_clear_errors(); 
        return $result;//1 is TRUE, 0 is FALSE   
    }
?>

 

I'd appreciate any help!

Link to comment
https://forums.phpfreaks.com/topic/251704-schema-validation/
Share on other sites

It's ok, I  think I got it. The XML file needs to have an attribute that references the schema file as such:

 

(direct from w3schools)

<?xml version="1.0"?>

========================

<note

xmlns="http://www.w3schools.com"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://www.w3schools.com note.xsd">

  <to>Tove</to>

  <from>Jani</from>

  <heading>Reminder</heading>

  <body>Don't forget me this weekend!</body>

</note>

=========================

So it means I can change the xsi:schemaLocation to the directory where the schema lies as in:

xsi:schemaLocation="C:\dir\dir2\ note.xsd" or is that just a namespace so and the XML file will assume the schema is in same directory?

 

Please any help!

Link to comment
https://forums.phpfreaks.com/topic/251704-schema-validation/#findComment-1290872
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.