SimpleXML XPath Namespace Support

php.internals

Adam Maccabee Trachtenberg

22 years ago
SimpleXML's XPath searching doesn't allow you to use namespaces. This patch: 1) Automatically registers any namespace prefixes used in the document. 2) Registers any prefixes manually added from calls registerNS(). 3) Lets you refer to Qualified Names using the standard Prefix:LocalPart syntax. It is available at: http://www.trachtenberg.com/patches/simplexml_xpath_ns_patch.txt Can someone with SimpleXML karma please review and apply. See Bug #26159 for more information. Thanks. -adam
-- adam@trachtenberg.com

Rob Richards

22 years ago
From: Adam Maccabee Trachtenberg
> http://www.trachtenberg.com/patches/simplexml_xpath_ns_patch.txt > > Can someone with SimpleXML karma please review and apply. See Bug > #26159 for more information.
The question I have is in simplexml_ce_register_ns. As nsmap has no specific scope, shouldnt all the entries be registered in the xpath context, which would then need to happen in simplexml_ce_xpath_search? In the meantime, here's an update of your patch: http://www.ctindustries.net/patches/simplexml_xpath_ns_patch.txt Rob

Adam Maccabee Trachtenberg

22 years ago
On Sat, 22 Nov 2003, Rob Richards wrote:
> The question I have is in simplexml_ce_register_ns. As nsmap has no specific > scope, shouldnt all the entries be registered in the xpath context, which > would then need to happen in simplexml_ce_xpath_search?
That's why I create a new xmlXPathNewContext inside simplexml_ce_register_ns. + if (!sxe->xpath) { + sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr); + } This seemed easier than doing an xmlHashScan inside simplexml_ce_xpath_search and this way we only register each namespace once. -adam
-- adam@trachtenberg.com

Rob Richards

22 years ago
From: Adam Maccabee Trachtenberg
> That's why I create a new xmlXPathNewContext inside
simplexml_ce_register_ns.
> > + if (!sxe->xpath) { > + sxe->xpath = xmlXPathNewContext((xmlDocPtr) sxe->document->ptr); > + }
But xpath isn't shared across the objects, so calling simplexml_ce_register_ns with different sxe objects will result in multiple XPath contexts each having their own registered namespaces (by registered namespaces I refer to ones the user adds via the register_ns call) and depending upon which object is used to search with (it may even be one which wasnt used with a register_ns call), would then determe which ones are used. I am not sure if this is the correct behavior as it seems that all the namesapces from nsmap should be used for the query. Rob

Adam Maccabee Trachtenberg

22 years ago
On Sat, 22 Nov 2003, Rob Richards wrote:
> But xpath isn't shared across the objects, so calling > simplexml_ce_register_ns with different sxe objects will result in > multiple XPath contexts each having their own registered namespaces
Ah. I see. Okay, I've prepared a modified patch that defers namespace registration until the call to simplexml_ce_xpath_search(). Inside that function, I iterate through the nsmapptr->nsmap hash and register all the namespaces. Since nsmapptr is shared across the objects this should be okay, right? Check out: http://www.trachtenberg.com/patches/simplexml_xpath_ns_patch.txt -adam PS: At a minimum, we should probably do some validity checks on the prefix, so people can't register an empty or invalid prefix. I'll look into this.
-- adam@trachtenberg.com

Rob Richards

22 years ago
From: Adam Maccabee Trachtenberg
> Ah. I see. Okay, I've prepared a modified patch that defers namespace > registration until the call to simplexml_ce_xpath_search(). Inside > that function, I iterate through the nsmapptr->nsmap hash and register > all the namespaces.
Thanks, patch applied. Rob