tags:

views:

518

answers:

2

Guys,

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="http://xxx"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xxx"&gt;

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('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/2000/xmlns/' ,'xsi:schemaLocation', 'http://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...', 'http://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?

+1  A: 

replace line 21 with

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

xsi:schemaLocation is not defined in the http://www.w3.org/2000/xmlns/ or your namespace, but in xsi. so you have to use the (complete) xsi namespace uri as the first parameter.

and: you don't need to call setAttributeNS() twice: the single line above generates both the xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" and xsi:schemaLocation="http://xxx http://xxx/xxx.xsd" attributes.

ax
A: 

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"/&gt;
VolkerK