Jump to content


Photo

xml problem


  • Please log in to reply
No replies to this topic

#1 malbolgia

malbolgia
  • Members
  • Pip
  • Newbie
  • 5 posts

Posted 26 April 2006 - 06:11 PM

<?php

class 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();
?>

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/20...chema-instance");
$root->setAttribute("xsi:SchemaLocation", "librarysch.xsd");

but i get Attribute "xmlns:xsi" not allowed

how can i add schema information to the xml?

and another question how do i add the stylesheet?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users