Add namespace attribute to xml node in php -
following node trying create in xml -
<?xml version="1.0" standalone="no" ?> <manifest identifier="com.scorm.golfsamples.contentpackaging.multioscosinglefile.20043rd" version="1" xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xmlns:adlcp="http://www.adlnet.org/xsd/adlcp_v1p3" xmlns:adlseq="http://www.adlnet.org/xsd/adlseq_v1p3" xmlns:adlnav="http://www.adlnet.org/xsd/adlnav_v1p3" xmlns:imsss="http://www.imsglobal.org/xsd/imsss" xsi:schemalocation="http://www.imsglobal.org/xsd/imscp_v1p1 imscp_v1p1.xsd http://www.adlnet.org/xsd/adlcp_v1p3 adlcp_v1p3.xsd http://www.adlnet.org/xsd/adlseq_v1p3 adlseq_v1p3.xsd http://www.adlnet.org/xsd/adlnav_v1p3 adlnav_v1p3.xsd http://www.imsglobal.org/xsd/imsss imsss_v1p0.xsd">
code working fine identifier
, version
attribute unable generate namespace xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"
tried code here unable make :(
$doc = new domdocument( '1.0' ); $doc->loadxml( $source ); // (1) create "namespace'd" attribute without appending element. $attr_ns = $doc->createattributens( '{namespace_uri_here}', 'example:attr' ); print $doc->savexml() . "\n";
codepad link - http://codepad.org/uljc4hpp
full code -
//creating xml document $dom = new domdocument('1.0'); $dom->xmlstandalone = false; //create element manifest $manfiestnode = $dom->createelement('manifest',""); //create attribute identifier $manfiestnodeattr = $dom->createattribute('identifier'); //value manifest node identifier value $date = new datetime(); $manfiestnodeattr->value = 'course_'.date_format($date,'u'); //append attribute manifest element $manfiestnode->appendchild($manfiestnodeattr); //create attribute associated namespace $nodeattr = $manfiestnode->createattributens('{namespace_uri_here}', 'example:attr'); //append namespace manifest element $nodeattr->appendchild($manfiestnode); //append manifest element document $dom->appendchild($manfiestnode); var_dump($dom->savexml());
let me know conceptually doing wrong , how make working.
i tried changing $manfiestnode
$dom
in line 20[codepad link] still no luck :(.
error-
fatal error: call undefined method domelement::createattributens() on line 20
try createattribute
following
$dom = new domdocument('1.0','utf-8'); // root manifest $root = $dom->appendchild($dom->createelement('manifest')); // identifier $date = new datetime(); $manfiestnodeattr_value = 'course_'.date_format($date,'u'); $root->appendchild($dom->createattribute('identifier'))->appendchild($dom->createtextnode($manfiestnodeattr_value)); // version $version = 1; $root->appendchild($dom->createattribute('version'))->appendchild($dom->createtextnode($version)); // xmlns:xsi $root->appendchild($dom->createattribute('xmlns:xsi'))->appendchild($dom->createtextnode("http://www.w3.org/2001/xmlschema-instance")); print_r($dom->savexml());
demo codepad: http://codepad.org/zgug0gl3
Comments
Post a Comment