Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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 DOM XML-创建多个名称空间属性?_Php_Xml_Dom - Fatal编程技术网

PHP DOM XML-创建多个名称空间属性?

PHP DOM XML-创建多个名称空间属性?,php,xml,dom,Php,Xml,Dom,我正在使用一些PHP来使用DOM扩展从数据库创建XML 基本上,我需要创建一个名称空间并向其添加3个属性: <NameSpaceName xmlns="uri:xxx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="uri:xxx"> 但当我执行上述操作时,我会得到以下错误: 致命错误:未捕获异常 带有消息“命名空间”的“DomeException” 中的“错误”

我正在使用一些PHP来使用DOM扩展从数据库创建XML

基本上,我需要创建一个名称空间并向其添加3个属性:

<NameSpaceName xmlns="uri:xxx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="uri:xxx">
但当我执行上述操作时,我会得到以下错误:

致命错误:未捕获异常 带有消息“命名空间”的“DomeException” 中的“错误” php:21 堆栈跟踪:#0 php(21): DomeElement->setAttributeNS(“…”,“xsi:schemaLocat…”, 'uri:xxx…')#1{main} 在里面 xml.php 在线21

第21行是第二行“setAttributeNS”

有人知道我哪里出错了吗?

将第21行替换为

$root->setAttributeNS(
  'http://www.w3.org/2001/XMLSchema-instance', 
  'xsi:schemaLocation',
  'http://xxx http://xxx/xxx.xsd'
);
xsi:schemaLocation
不是在或您的命名空间中定义的,而是在
xsi
中定义的。因此,必须使用(complete)
xsi
名称空间uri作为第一个参数


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

架构位置未在命名空间中声明
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();
createElements('http://xxx","付款人纪录";;
$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“,”模式定位“,”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"/>

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

// 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);
此邮件存档消息非常有用:

// 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);