Re: XPath and Multiple Namespaces

[email protected] ("Daniel Pupius") Tue, 2 Dec 2003 09:41:05 -0000
Newsgroups php.xml.dev
Organization act e-learning
Message-ID <[email protected]>
In case anyone else has this problem, someone contacted me directly and
prompted me to upgrade the version of libxml2.

It now works a treat.


"Daniel Pupius" <[email protected]> wrote in message
news:[email protected]...
> I'm having nightmares with xpath queries on xml documents with multiple
> namespaces.  For example, take the following test file:
>
> <test xmlns="http://main.com" xmlns:sub="http://sub.com">
>   <item>
>     <subitem>
>       <sub:testnode>Testing 1 2 3</sub:testnode>
>     </subitem>
>   </item>
> </test>
>
> You'd expect that I could access the testnode using:
> /i:test/i:item/i:subitem/sub:testnode
>
> Where i is a reference to the default namespace specified using
> xpath_register_ns() as was suggested in a post on one of the news groups.
>
> But it seems that xpath_register_ns() overwrites previously specified
> namespaces, for example:
>
> 1  $xpath = $dom->xpath_new_context();
> 2  xpath_register_ns($xpath,"sub", "http://sub.com");
> 3  xpath_register_ns($xpath,"i", "http://main.com");
> 4  $xnode = xpath_eval($xpath,"/i:test");
> 5  print_r($xnode);
>
> Will find the root node, however swap lines 2 and 3 around and it will
fail.
> So my problem seems to stem from registering multiple name spaces.
>
> Hope this makes sense and someone can help.
>
> Regards
>
> Dan
>
> ---------------------------------------------
>        act : making e-learning work
> ---------------------------------------------
>
>   Daniel Pupius BSc(hons) MA
>   Senior Lead Developer
>   email: [email protected]
>
>   New Oxford House
>   30 Barker's Pool
>   Sheffield S1 2HB
>   Tel: 0114 278 0798
>   Fax: 0114 249 1773
>
> ---------------------------------------------
>        http://www.actelearning.com/
> ---------------------------------------------