Re: Can SimpleXML still return all namespaces?

From: Date: Mon, 30 Jul 2007 13:44:13 +0000
Subject: Re: Can SimpleXML still return all namespaces?
References: 1 2 3  Groups: php.xml.dev 
Request: Send a blank email to php-xml-dev+get-601@lists.php.net to get a copy of this message
Hi Ben, Ben Schmidt wrote:
Hi, Rob,
This is exactly the purpose of the children() and attributes() methods. They are used to set a namespace scope to work within.
Fantastic! They very nearly do what I want. The biggest problem is simply this: the behaviour is not documented or demonstrated in the manual! Agreeably I am a bit of a newbie, but even so, with such limited documentation it is very easy to leap to wrong conclusions/make wrong assumptions about the behaviour of the module. If you can point me to where I can get the documentation source, I would be happy to have a go improving it and sending you diffs. http://doc.php.net/php/dochowto/
So what you are proposing is just a way to be able to work with SimpleXML in a completely namespace ignorant way? Basically, ignore all namespaces/prefixes and work directly with local names only?
Yes. I think that would be useful. SimpleXML would then not only be able to work with simple XML documents, but also do simple processing of more complicated documents. I am looking into this. I tossed this idea around with a few others and they also agree it might be a good idea to allow this type of functionality (though it would *NOT* be the default behavior. You would need to enable it via some parameter when working with SimpleXML). Still thinking about the implementation for this.
Having done some further investigation in light of your exciting revelation about the way children and attributes methods work, my requests/suggestions are somewhat different. Here they are: 1. IMHO children() and attributes() without a parameter should not change the namespace scope being used. In particular as, to my knowledge, 'attributes' is the only way to enumerate attributes (unlike elements which you can do by enumerating the object itself without using 'children') there needs to be a way to enumerate the attributes in the current scope. Passing no parameter is the most logical way to do this. But passing no parameter resets the scope to 'unprefixed' from what I can gather, which is annoying. You need to keep track of the scope yourself and can't easily write generalised subroutines that work in the current scope of an object. This is not going to change. Those 2 methods are meant to determine namespace scope. SimpleXML is really meant to work with simple documents.
2. There should be a way to get the current scope of a SimpleXMLElement. (If this were added, at least there'd be a workaround for 1, too!) It would need to return something magic for 'unprefixed' and children and attributes made to understand the magic value. Well, I think that would be better than returning a list of '$ns,$is_prefix' anyway! getNamespaces() - though this will also add namespaces from attributes as well - but again if working with complex documents, SimpleXML alone might be the wrong choice - you can mix DOM and SimpleXML without the overhead of having to copy documents.
3. It would be handy to have a method to set the current scope, rather than needing to use 'children' or 'attributes'. This would be particularly useful for the root node, to set the scope for the processing of the entire document. I realise now that it can be done with a constructor, but IMHO it would be easier to understand and convenient if a separate method were provided. I think it would make code more readable, too. This is not going to change. All of this can already be done without having to add additional methods.
4. A local-name-only mode would be good, as mentioned above. answered above
Rob

« previous php.xml.dev (#601) next »