0

I'm trying to make this xml using SimpleXMLElement:

<ds:Signature xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:etsi="http://uri.etsi.org/01903/v1.3.2#" Id="Signature620397">

 $signature = $invoice->addChild('ds:Signature', null, 'http://www.w3.org/2000/09/xmldsig#');
 $signature->addAttribute('xmlns:etsi', 'http://uri.etsi.org/01903/v1.3.2#');
 $signature->addAttribute('Id', 'Signature620397');

Later in the xml i need to reference the etsi namespace, but i could not find a way

<etsi:QualifyingProperties Target="#Signature620397">

Is there anyway to do this? Or that would be the limitations of SimpleXMLElement

  • If you aren't required to use `SimpleXMLElement`, I suggest you give [`DOMDocument`](http://us1.php.net/manual/en/class.domdocument.php) a whirl, instead. The API is much more consistent and well understood. – Sean Bright Jul 17 '18 at 21:30

1 Answers1

0

You might find this related answer useful.

A namespace is ultimately identified only by its full URI; the etsi prefix might change meaning halfway down the document. The way SimpleXML has been designed, you have to mention this full URI every time you add a namespaced element or attribute.

On the other hand, you don't need to manually add the xmlns:etsi attribute; SimpleXML will add that automatically when you first use the namespace.

In other words, you have to write:

 $xml->addChild('etsi:QualifyingProperties', null, 'http://uri.etsi.org/01903/v1.3.2#');

And SimpleXML will generate this if it's the first time you've mentioned that namespace:

<etsi:QualifyingProperties xmlns:etsi="http://uri.etsi.org/01903/v1.3.2#" />

Or just this if the prefix etsi is already defined with the right value at that point in the document:

<etsi:QualifyingProperties />

Obviously, it's tedious to write out the full URI over and over, but that can be resolved simply by having a variable or constant in your code with a name that means something to you, like this:

define('XMLNS_ETSI_132', 'http://uri.etsi.org/01903/v1.3.2#');
$xml->addChild('etsi:QualifyingProperties', null, XMLNS_ETSI_132);
IMSoP
  • 89,526
  • 13
  • 117
  • 169