2

Following is the node I am trying to create in my 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 is working fine for identifier and version attribute but unable to generate it with namespace like xmlns="http://www.imsglobal.org/xsd/imscp_v1p1"

Tried this code from here but unable to make it :(

$doc = new DOMDocument( '1.0' );
$doc->loadXML( $source );

// (1) We just create a "namespace'd" attribute without appending it to any element.
$attr_ns = $doc->createAttributeNS( '{namespace_uri_here}', 'example:attr' );

print $doc->saveXML() . "\n";

Codepad Link - http://codepad.org/uLJc4hpP

Full code -

   //creating an 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 for the manifest node identifier value
    $date = new DateTime();
    $manfiestNodeAttr->value = 'course_'.date_format($date,'U');

    //append attribute to the manifest element
    $manfiestNode->appendChild($manfiestNodeAttr);

    //create attribute with an associated namespace
    $nodeAttr = $manfiestNode->createAttributeNS('{namespace_uri_here}', 'example:attr');

    //append namespace to the manifest element
    $nodeAttr->appendChild($manfiestNode);

    //append manifest element to the document
    $dom->appendChild($manfiestNode);

    var_dump($dom->saveXML());

Let me know what I am conceptually doing wrong and how could I make it working.

I tried by changing $manfiestNode to $dom in line 20[codepad link] but still no luck :(.

Error-

Fatal error: Call to undefined method DOMElement::createAttributeNS() on line 20

1 Answer 1

6

Try with createAttribute like 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

Sign up to request clarification or add additional context in comments.

7 Comments

my question is about generating xmlns:xsi these type of attributes, version and identifier is sucessfully created from above code
@swapnesh But you can create xmlns with createAttribute?
yeah i tried that way $dom->createAttribute('xmlns:xsi') and it works :) only thing is is it really a clean way of generating like this..let me know if you have some thoughts on it +1 :)
I do not know very well about DOMDocument. I just googled and looked aroud php.net and want to help you. Updated codes with sample xmlns:xsi create attribute like yours.
ohh issue with that...if I add for a xml output header('Content-type: application/xml'); then it is not showing the xmlns:xsi however it is showing in a string
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.