Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在php中将名称空间属性添加到xml节点_Php_Xml_Xml Parsing_Domdocument - Fatal编程技术网

在php中将名称空间属性添加到xml节点

在php中将名称空间属性添加到xml节点,php,xml,xml-parsing,domdocument,Php,Xml,Xml Parsing,Domdocument,下面是我试图在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" xmln

下面是我试图在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">
代码板链接-

完整代码-

   //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());
让我知道我在概念上做错了什么,我如何才能让它工作

我尝试将第20行[codepad link]中的
$manfiestNode
更改为
$dom
,但仍然没有成功:(

错误-

致命错误:调用未定义的方法DomeElement::createAttributeNS() 第20行


尝试使用
createAttribute
如下所示

$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());

演示代码板:

我的问题是如何生成
xmlns:xsi
这些类型的属性、版本和标识符是从上面成功创建的code@swapnesh但是您可以使用
createAttribute
创建
xmlns
?是的,我试过那种方法
$dom->createAttribute('xmlns:xsi')
,效果很好:)唯一的问题是它确实是一种干净的生成方式。如果您对此有什么想法,请告诉我+1:)我对
DOMDocument
不太了解。我只是在Google上搜索了一下php.net,想帮你。使用示例
xmlns:xsi
create属性更新了代码。如果我为xml输出添加
header('Content-type:application/xml'),则会出现这样的问题
则它不显示
xmlns:xsi
,而是以字符串形式显示
$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());