malbolgia Posted April 26, 2006 Share Posted April 26, 2006 [code]<?phpclass Books extends DomDocument { function __construct() { //has to be called! parent::__construct(); } function addBook($isbn, $title, $name, $born, $dead, $charname, $charborn, $charqual){ $root = $this->createElement('library'); $root->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); $root->setAttribute("xsi:SchemaLocation", "librarysch.xsd"); $root = $this->appendChild($root); $bookelement = $this->createElement("book"); $bookelement->setAttribute("id", "b".$isbn); $bookelement->setAttribute("available", "true"); /*isbn*/ $isbnspace = $this->createElement("isbn"); $isbnspace->appendChild($this->createTextNode($isbn)); $bookelement->appendChild($isbnspace); /*title*/ $titlespace = $this->createElement("title"); $titlespace->appendChild($this->createTextNode($title)); $titlespace->setAttribute("lang", "en"); $bookelement->appendChild($titlespace); /*author*/ $authorspace = $this->createElement("author"); $temp = explode(" ", $name); $authorspace->setAttribute("id", $temp[0]); //name $namespace = $this->createElement("name"); $namespace->appendChild($this->createTextNode($name)); $authorspace->appendChild($namespace); //born $bornspace = $this->createElement("born"); $bornspace->appendChild($this->createTextNode($born)); $authorspace->appendChild($bornspace); //dead $deadspace = $this->createElement("dead"); $deadspace->appendChild($this->createTextNode($dead)); $authorspace->appendChild($deadspace); $bookelement->appendChild($authorspace); /*Character*/ $characterspace = $this->createElement("character"); $characterspace->setAttribute("id", $charname); //name $namespace = $this->createElement("name"); $namespace->appendChild($this->createTextNode($charname)); $characterspace->appendChild($namespace); //born $bornspace = $this->createElement("born"); $bornspace->appendChild($this->createTextNode($charborn)); $characterspace->appendChild($bornspace); //qualificatiom $qualspace = $this->createElement("qualification"); $qualspace->appendChild($this->createTextNode($charqual)); $characterspace->appendChild($qualspace); $bookelement->appendChild($characterspace); //$dom->appendChild($bookelement); $this->documentElement->appendChild($bookelement); }}$doc = new DOMDocument('1.0');$doc->formatOutput = true;$dom = new Books();$isbn = stripslashes($_POST['isbn']);$title = stripslashes($_POST['title']);$name = stripslashes($_POST['name']);$born = stripslashes($_POST['born']);$dead = stripslashes($_POST['dead']);$charname = stripslashes($_POST['charname']);$charborn = stripslashes($_POST['charborn']);$charqual = stripslashes($_POST['charqual']);$dom->addBook($isbn, $title, $name, $born, $dead, $charname, $charborn, $charqual);$dom->schemaValidate('librarysch.xsd');print $dom->save("newfile.xml");print $dom->saveXML();?> [/code]im making a new xml file from a form submited by a user, everything works fine until i try to add the schema location and name space.Im trying to do this with this$root->setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance"); $root->setAttribute("xsi:SchemaLocation", "librarysch.xsd"); but i get Attribute "xmlns:xsi" not allowedhow can i add schema information to the xml? and another question how do i add the stylesheet? Quote Link to comment https://forums.phpfreaks.com/topic/8483-xml-problem/ Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.