Can't change XML element attributes using simpleXML. :s


 $xmlObj = simplexml_load_file('RepositorioXml/EppCreateDomain.xml');

$xmlObj->command->extension->children(self::EXT_URI_DOMAIN)->create->legitimacy->attributes('type', 'please');

//prepara
$xmlString = $xmlObj->asXML();

I was hopping that, by doing this, I was able to change the attribute type value with some value. - ‘please’.

I got nothing on my returned $xml.

What I’m I doing wrong? Any guess?

I don’t even know what can I var_dump here. :s

Now, I’m having something like this:

$xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->contact = $dominioVo->getContact();

And this works. But this places our data inside our element, not as an attribute.

I’m (almost) sure the path is correct here:
$xmlObj->command->extension->children(self::EXT_URI_DOMAIN)->create->legitimacy->attributes(‘type’, $dominioVo->getLegitimacy());

The namespace is the appropriate. And the path, if you look into the XML above, seems to be ok as well…

It should be, as normally, something really basic, maybe related with the fact that I’m missing a assignment or something?

:s


<command>
            <create>
                <domain:create xmlns:domain="urn:blabla...">
                    <domain:name></domain:name>
                    <domain:period unit="y"></domain:period>
                    <domain:registrant></domain:registrant>
                    <domain:contact type="tech"></domain:contact>
                    <domain:authInfo>
                        <domain:pw/>
                    </domain:authInfo>
                </domain:create>
            </create>
            <extension>
                <ptdomain:create xmlns:....">
                    <ptdomain:legitimacy type=""/>
                    <ptdomain:registration_basis type=""/>
                    <ptdomain:autoRenew></ptdomain:autoRenew>
                </ptdomain:create>
            </extension>
            <clTRID></clTRID>
        </command>

Please advice,
Márcio

Here’s an example of assigning a value to an attribute, if you have any trouble deciphering it and applying it to your XML document then just shout.

<?php

$xml = new SimpleXMLElement('<root><foo bar="baz"/></root>');
$xml->foo['bar'] = 'Sitepoint!';
echo $xml->saveXML();

/*
<?xml version="1.0"?>
<root><foo bar="Sitepoint!"/></root>
*/

Thank you.

Ok… it’s almost there. I’ve tested, but now I have two type attributes.
So, it seems that the code, ADDs a new attribute to our element.

But the attribute is already there. I just need to edit it. And change is value.

By your example, however, that seems to NOT be the case, so I must be doing something wrong here… :s Any clue about what could it be?

:eye:

Thanks in advance,
Márcio

It’s creating two type attributes on one element? That’s odd. Can you show us an example snippet of code (XML included) that demonstrates this?

Sure.

The xml “clean”:


<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
        <command>
            <create>
                <domain:create xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
                    <domain:name></domain:name>
                    <domain:period unit="y"></domain:period>
                    <domain:registrant></domain:registrant>
                    <domain:contact type="tech"></domain:contact>
                    <domain:authInfo>
                        <domain:pw/>
                    </domain:authInfo>
                </domain:create>
            </create>
            <extension>
                <ptdomain:create xmlns:ptdomain="http://eppdev.dns.pt/schemas/ptdomain-1.0" xsi:schemaLocation="http://eppdev.dns.pt/schemas/ptdomain-1.0 ptdomain-1.0.xsd">
                    <ptdomain:legitimacy type=""/>
                    <ptdomain:registration_basis type=""/>
                    <ptdomain:autoRenew></ptdomain:autoRenew>
                </ptdomain:create>
            </extension>
            <clTRID></clTRID>
        </command>
    </epp>

So, this is the clean XML that we are accessing to.


$xmlObj = simplexml_load_file('RepositorioXml/EppCreateDomain.xml');

$xmlObj->command->create->children(self::OBJ_URI_DOMAIN)->create->contact = $dominioVo->getContact();

//with this I was trying to access the type attribute:
$xmlObj->command->extension->children(self::EXT_URI_DOMAIN)->create->legitimacy['type'] = $dominioVo->getLegitimacy();

Then I just…

//prepara
        $xmlString = $xmlObj->asXML();

        var_dump($xmlString);

When I var_dump this, I have the following XML returned:


<epp xmlns="urn:ietf:params:xml:ns:epp-1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:ietf:params:xml:ns:epp-1.0 epp-1.0.xsd">
        <command>
            <create>
                <domain:create xmlns:domain="urn:ietf:params:xml:ns:domain-1.0" xsi:schemaLocation="urn:ietf:params:xml:ns:domain-1.0 domain-1.0.xsd">
                    <domain:name>plimplim.pt</domain:name>
                    <domain:period unit="y">1</domain:period>
                    <domain:registrant>1</domain:registrant>

                    <domain:contact type="tech">FCZA-142520-FCCN</domain:contact>
                    <domain:authInfo>
                        <domain:pw/>
                    </domain:authInfo>
                </domain:create>
            </create>
            <extension>
                <ptdomain:create xmlns:ptdomain="http://eppdev.dns.pt/schemas/ptdomain-1.0" xsi:schemaLocation="http://eppdev.dns.pt/schemas/ptdomain-1.0 ptdomain-1.0.xsd">
                    <ptdomain:legitimacy type="" type="1"/>

                    <ptdomain:registration_basis type=""/>
                    <ptdomain:autoRenew>true</ptdomain:autoRenew>
                </ptdomain:create>
            </extension>
            <clTRID>criacao-dominio-4c3749812967c</clTRID>
        </command>
    </epp>

So, as you can see, I’m getting two type attributes. :s

:shifty:

Ahh, the shorthand array-style syntax does not work for your document. Instead you will need to use a slightly more verbose approach which makes use of the SimpleXMLElement::attributes() method. You tried using it right up there in the first piece of code but you were using in incorrectly.

It looks like you will want something like:
(Split onto multiple lines since the code box on Sitepoint is really tiny)


$xmlObj
    ->command
    ->extension
    ->children(self::EXT_URI_DOMAIN)
    ->create
    ->legitimacy
    ->attributes()
    ->type = $dominioVo->getLegitimacy();

Thank you! Only monday I will be able to test. But I’m sure it will bring good results. Thank you.

:slight_smile: