xsd validate php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
rigas
Forum Newbie
Posts: 6
Joined: Sun Jan 10, 2010 12:14 pm

xsd validate php

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: xsd validate php

Post by AbraCadaver »

If the document doesn't have the SONGS name space defined in it, then how can it validate?
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rigas
Forum Newbie
Posts: 6
Joined: Sun Jan 10, 2010 12:14 pm

Re: xsd validate php

Post 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";
}
 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: xsd validate php

Post 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"...........
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
rigas
Forum Newbie
Posts: 6
Joined: Sun Jan 10, 2010 12:14 pm

Re: xsd validate php

Post by rigas »

the connection has to be made in php, not in the xsd file
Post Reply