【问题标题】:Add namespace attribute to xml node in php在php中将命名空间属性添加到xml节点
【发布时间】:2013-09-04 06:49:00
【问题描述】:

以下是我试图在我的 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">

代码对于identifierversion 属性运行良好,但无法使用xmlns="http://www.imsglobal.org/xsd/imscp_v1p1" 等命名空间生成它

here 尝试了这段代码,但无法成功:(

$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";

键盘链接 - http://codepad.org/uLJc4hpP

完整代码-

   //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,但仍然没有运气:(。

错误-

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

【问题讨论】:

    标签: php xml xml-parsing domdocument


    【解决方案1】:

    尝试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());
    

    演示键盘: http://codepad.org/zgug0Gl3

    【讨论】:

    • 我的问题是关于生成xmlns:xsi这些类型的属性,版本和标识符是从上面的代码成功创建的
    • @swapnesh 但是你可以用createAttribute创建xmlns
    • 是的,我尝试过这种方式$dom-&gt;createAttribute('xmlns:xsi') 并且它有效:) 唯一的问题是它真的是一种干净的生成方式......如果你对此有一些想法,请告诉我 +1 :)
    • 我不太了解DOMDocument。我只是用谷歌搜索并查看了 php.net 并想帮助你。使用示例 xmlns:xsi 更新代码,创建与您类似的属性。
    • 哦,这个问题...如果我为 xml 输出添加 header('Content-type: application/xml'); 那么它不会显示 xmlns:xsi 但是它显示在一个字符串中
    猜你喜欢
    • 2013-09-21
    • 2023-03-22
    • 2016-07-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-23
    • 1970-01-01
    相关资源
    最近更新 更多