Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP DOM XML - Create Multiple Namespace Attributes?

Tags:

dom

php

xml

I'm working on some PHP to create XML from a database using the DOM extension.

Basically, I need to create a NameSpace and add 3 attributes to it:

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

The full code i've written is below:

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;

But when I execute the above I get this error:

Fatal error: Uncaught exception 'DOMException' with message 'Namespace Error' in xml.php:21 Stack trace: #0 xml.php(21): DOMElement->setAttributeNS('http://www.w3.o...', 'xsi:schemaLocat...', 'uri:xxx...') #1 {main} thrown in xml.php on line 21

Line 21 is the second 'setAttributeNS' line.

Can anyone see where i'm going wrong?

like image 343
Sjwdavies Avatar asked Feb 10 '10 13:02

Sjwdavies


People also ask

How to declare Multiple namespaces in XML?

Declaring namespaceswhere <name> is the namespace prefix and <"uri"> is the URI that identifies the namespace. After you declare the prefix, you can use it to qualify elements and attributes in an XML document and associate them with the namespace URI.

How to declare a namespace in XML?

When using prefixes in XML, a namespace for the prefix must be defined. The namespace can be defined by an xmlns attribute in the start tag of an element. The namespace declaration has the following syntax. xmlns:prefix="URI".

What is the purpose of defining a default namespace in an XML file?

One of the primary motivations for defining an XML namespace is to avoid naming conflicts when using and re-using multiple vocabularies. XML Schema is used to create a vocabulary for an XML instance, and uses namespaces heavily.

What is a namespace URI?

A namespace name is a uniform resource identifier (URI). Typically, the URI chosen for the namespace of a given XML vocabulary describes a resource under the control of the author or organization defining the vocabulary, such as a URL for the author's Web server.


2 Answers

schemaLocation is not declared in the namespace http://www.w3.org/2000/xmlns/ but in 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();

prints

<?xml version="1.0" encoding="UTF-8"?>
<PayerRecords xmlns="http://xxx" 
              xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
              xsi:schemaLocation="http://xxx"/>
like image 141
VolkerK Avatar answered Nov 07 '22 20:11

VolkerK


I didn't quite get it the first time so I'm posting my answer in more detail. Maybe someone finds this helpful.

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

This mail archive message was very helpful: http://www.mail-archive.com/[email protected]/msg135362.html.

like image 41
hrvoj3e Avatar answered Nov 07 '22 21:11

hrvoj3e