Page 1 of 1

xsd validate php

Posted: Tue Jan 12, 2010 3:54 pm
by rigas
hello

using a form disigned with html i am collecting some data and saving them to an xml temp file. then i want to validate them with an xsd file in order to finally write them to save them to the ordinary file.

using the instruction below in the xml at root element, the file opens in the browser(valid)

Code: Select all

<SONGS xmlns ="http://www.songs.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.songs.org p06166.xsd"
without the above instruction in the xml, i am trying to validate the xml in php with the code below

Code: Select all

 
$xdoc = new DomDocument;
$xmlfile = '123.xml';
$xmlschema = 'p06166.xsd';
 
//Load the xml document in the DOMDocument object
$xdoc->Load($xmlfile);
 
//Validate the XML file against the schema
if ($xdoc->schemaValidate($xmlschema)) {
print "Song inserted successfully!! <br />";
}
else {
print "$xmlfile is invalid.\n";
}


the error i get is the one below. do you have any idea why i get error, since the input is valid???

Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element 'SONGS': No matching global declaration available for the validation root. in C:\xampp\htdocs\test.php on line 48
123.xml is invalid.

Re: xsd validate php

Posted: Tue Jan 12, 2010 4:05 pm
by AbraCadaver
If the document doesn't have the SONGS name space defined in it, then how can it validate?

Re: xsd validate php

Posted: Wed Jan 13, 2010 12:44 pm
by rigas
here is the code i use to collect the data from the form. opening the xml i confirm that the element SONGS exists

Code: Select all

 
$title = $_POST['title'];
$composer = $_POST['composer'];
$lyrics = $_POST['lyrics'];
$artist = $_POST['artist'];
$album = $_POST['album'];
$produser = $_POST['produser'];
$publisher = $_POST['publisher'];
$length = $_POST['length'];
$year = $_POST['year'];
 
$dom = new DOMDocument();
$dom->load("123.xml");
 
$write_string = "<SONGS> \n";
$write_string .= "<SONG> \n";
$write_string .= "<TITLE>".$title."</TITLE> \n";
$write_string .= "<COMPOSER>".$composer."</COMPOSER> \n";
$write_string .= "<LYRICS>".$lyrics."</LYRICS> \n";
$write_string .= "<ARTIST>".$artist."</ARTIST> \n";
$write_string .= "<ALBUM>".$album."</ALBUM> \n";
$write_string .= "<PRODUCER>".$produser."</PRODUCER> \n";
$write_string .= "<PUBLISHER>".$publisher."</PUBLISHER> \n";
$write_string .= "<LENGTH>".$length."</LENGTH> \n";
$write_string .= "<YEAR>".$year."</YEAR> \n";
$write_string .= "</SONG> \n";
$write_string .= "</SONGS> \n";
 
$fp = fopen("123.xml", "w");
fwrite($fp, $write_string) or die("Error writing to file");
fclose($fp);
 
$xdoc = new DomDocument;
$xmlfile = '123.xml';
$xmlschema = 'p06166.xsd';
 
//Load the xml document in the DOMDocument object
$xdoc->Load($xmlfile);
 
//Validate the XML file against the schema
if ($xdoc->schemaValidate($xmlschema)) {
print "Song inserted successfully!! <br />";
}
else {
print "$xmlfile is invalid.\n";
}
 

Re: xsd validate php

Posted: Wed Jan 13, 2010 1:11 pm
by AbraCadaver
Put this in the file:

<SONGS xmlns ="http://www.songs.org" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.songs.org p06166.xsd"...........

Re: xsd validate php

Posted: Wed Jan 13, 2010 2:57 pm
by rigas
the connection has to be made in php, not in the xsd file