Reputation: 91
I'm newbie in XML-writer. Any idea how to generate this with PHP XMLWriter
<definitions xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport">
I've done this already
$writer->startElement('definitions');
$writer->writeAttributeNS('xmlns','tns', null,'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport');
$writer->writeAttributeNS('xmlns',null, null, 'http://schemas.xmlsoap.org/wsdl/');
$writer->writeAttributeNS('targetNamespace', null, null, 'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport');
But I get this "Warning: XMLWriter::writeAttributeNs(): Invalid Attribute Name in" 3'rd and 4'th lines
Thanks already! JM
Upvotes: 1
Views: 2942
Reputation: 19512
Here is only one "normal" attribute in your xml. An this is the targetNamespace attribute. The other two are namespace (prefix) definitions
The xmlns
attribute is the default namespace definition for the element and its child elements. It will be created automatically if you use startElementNS()
.
The targetNamespace
attribute has no namespace and can be set using writeAttribute()
.
Last xmlns:tns
is not needed in the example, it defines the namespace prefix "tns" for child elements and attibutes. Here is no element or attribute that uses it. If you add it with setAttributeNS()
, XMLWriter will add the xmlns namespace, too. This is a reserved namespace (and namespace prefix). It is builtin into XML parsers and does not need to be defined, but it can.
$writer = new XMLWriter;
$writer->openURI('php://output');
$writer->startDocument('1.0', 'UTF-8');
$writer->startElementNS(
NULL,
'definitions',
'http://schemas.xmlsoap.org/wsdl/'
);
$writer->writeAttribute(
'targetNamespace',
'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport'
);
$writer->writeAttributeNS(
'xmlns',
'tns',
'http://www.w3.org/2000/xmlns/',
'urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport'
);
$writer->endElement();
$writer->endDocument();
Output:
<definitions targetNamespace="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport" xmlns:tns="urn:microsoft-dynamics-schemas/codeunit/SalesOrderImport" xmlns:xmlns="http://www.w3.org/2000/xmlns/" xmlns="http://schemas.xmlsoap.org/wsdl/"/>
Here is an example for a namespace attribute, like with startElementNS()
the namespace definition will be added automatically:
$writer->startElement('element');
$writer->writeAttributeNS(
'prefix',
'attr',
'urn:example:namespace',
'value'
);
$writer->endElement();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<element prefix:attr="value" xmlns:prefix="urn:example:namespace"/>
Upvotes: 4