Re: [PHP-XML-DEV] Can SimpleXML still return all namespaces?

[email protected] (Ben Schmidt) Mon, 23 Jul 2007 14:22:43 +1000
Newsgroups php.xml.dev
Message-ID <[email protected]>
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.

> 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.

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.

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!

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.

4. A local-name-only mode would be good, as mentioned above.

What do you think?

Cheers,

Ben.




Send instant messages to your online friends http://au.messenger.yahoo.com