Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Simple XML Element with PHP that has a Colon

Tags:

php

xml

I am attempting to add an XML attribute with PHP that contains a colon.

I want it to look like this:

<record xsi:schemaLocation="http://abc.com/abcItem file:///somename.xsd">

I attempted to add the attribute like this:

$record_xml->addAttribute('xsi:schemaLocation','http://abc.com file:///somepath/somename.xsd');

but the resulting XML is:

<record schemaLocation="http://abc.com/abcItem file:///somepath/somename.xsd">
like image 279
user2540499 Avatar asked Oct 15 '25 07:10

user2540499


1 Answers

In addition to the namespace prefix (the part before the colon), you must also include the corresponding namespace URI (as the third argument):

$record_xml->addAttribute(
    'xsi:schemaLocation',
    'http://abc.com file:///somepath/somename.xsd',
    'http://www.w3.org/2001/XMLSchema-instance'
);
like image 172
PleaseStand Avatar answered Oct 16 '25 23:10

PleaseStand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!