【问题标题】:PHP DOM XML - Create Multiple Namespace Attributes?PHP DOM XML - 创建多个命名空间属性?
【发布时间】:2011-01-15 06:25:56
【问题描述】:

我正在开发一些 PHP 以使用 DOM 扩展从数据库创建 XML。

基本上,我需要创建一个 NameSpace 并为其添加 3 个属性:

<NameSpaceName xmlns="uri:xxx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="uri:xxx">

我写的完整代码如下:

include_once("includes/connect.php");

$sql = ("SELECT * FROM tableName");
$query = mysql_query($sql) or die("Error: " . mysql_error());


// create a new XML document
$doc = new DomDocument('1.0', 'UTF-8');

// create root node
$root = $doc->createElementNS('uri:xxx', 'PayerRecords');
$root = $doc->appendChild($root);
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
$root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xsi:schemaLocation', 'uri:xxx');

// process one row at a time
while($row = mysql_fetch_assoc($query)) {

  // add node for each row
  $occ = $doc->createElement('Content');
  $occ = $root->appendChild($occ);

  // add a child node for each field
  foreach ($row as $fieldname => $fieldvalue) {

    $child = $doc->createElement($fieldname);
    $child = $occ->appendChild($child);

    $value = $doc->createTextNode($fieldvalue);
    $value = $child->appendChild($value);

  } // foreach

} // while

// get completed xml document
$xml_string = $doc->saveXML();

echo $xml_string;

但是当我执行上述操作时,我得到了这个错误:

致命错误:未捕获的异常 带有消息“命名空间”的“DOMException” 错误 xml.php:21 堆栈跟踪:#0 xml.php(21): DOMElement->setAttributeNS('http://www.w3.o...', 'xsi:schemaLocat...', 'uri:xxx...') #1 {main} 抛出 在 xml.php 第 21 行

第 21 行是第二个 'setAttributeNS' 行。

谁能看出我哪里出错了?

【问题讨论】:

    标签: php xml dom


    【解决方案1】:

    schemaLocation 没有在命名空间http://www.w3.org/2000/xmlns/ 中声明,而是在http://www.w3.org/2001/XMLSchema-instance 中声明

    <?php
    // create a new XML document
    $doc = new DomDocument('1.0', 'UTF-8');
    // create root node
    $root = $doc->createElementNS('http://xxx', 'PayerRecords');
    $root = $doc->appendChild($root);
    $root->setAttributeNS('http://www.w3.org/2000/xmlns/' ,'xmlns:xsi', 'http://www.w3.org/2001/XMLSchema-instance');
    $root->setAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'schemaLocation', 'http://xxx');
    
    echo $doc->savexml();
    

    打印

    <?xml version="1.0" encoding="UTF-8"?>
    <PayerRecords xmlns="http://xxx" 
                  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xsi:schemaLocation="http://xxx"/>
    

    【讨论】:

      【解决方案2】:

      我第一次没有完全明白,所以我将更详细地发布我的答案。也许有人觉得这很有帮助。

      // create DOM document
      $xml = new DomDocument('1.0', 'UTF-8');
      
      // create root element
      $el = $xml->createElementNS('http://namespaceA/url/here/', 'rootelement');
      
      // to be able to add new namespaces we must first add namespace 'xsi'
      // third parameter is important (use your main namespace with .xsd)
      $root->setAttributeNS(
        'http://www.w3.org/2001/XMLSchema-instance',
        'xsi:schemaLocation',
        'http://namespaceA/url/here/ http://namespaceA/xsdfile/here.xsd');
      
      // add new namespace
      $el->setAttributeNS(
        'http://www.w3.org/2000/xmlns/',
        'xmlns:namespaceB',
        'http://namespaceB/url/here/');
      
      // add root element to DOM
      $xml->appendChild($el);
      

      此邮件存档消息非常有帮助:http://www.mail-archive.com/php-general@lists.php.net/msg135362.html

      【讨论】:

        【解决方案3】:

        将第 21 行替换为

        $root->setAttributeNS(
          'http://www.w3.org/2001/XMLSchema-instance', 
          'xsi:schemaLocation',
          'http://xxx http://xxx/xxx.xsd'
        );
        

        xsi:schemaLocation 不是在http://www.w3.org/2000/xmlns/ 或您的命名空间中定义的,而是在xsi 中定义的。所以你必须使用(完整的)xsi 命名空间 uri 作为第一个参数。

        并且:您不需要调用setAttributeNS() 两次:上面的单行生成xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://xxx http://xxx/xxx.xsd" 属性。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-03
          • 1970-01-01
          • 2021-12-06
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多