This is a discussion on PHP5 Xpath problem within the PHP Language forums, part of the PHP Programming Forums category; Hi all, I have recently started working with the new XML functionality in PHP5, but I am running into a ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
Hi all,
I have recently started working with the new XML functionality in PHP5, but I am running into a few problems. Specifically, I am using an Xpath query to try and pull out the data in specific elements within my XML file. The problem I am getting is that one of the XML elements contains namespace attributes and this seems to prevent the Xpath query from working (ie, if I manually strip the namespace attributes then the query works fine. After testing I will not have direct access to the XML file, so stripping the namespace attributes each time is not an option. Can anyone shed any light as to why this might be so, or offer any suggestions to overcome the problem? tia Chris ####################################### Example code: <? $dom = new DomDocument(); $dom->load("ltsnec.xml"); $path = "/OAI-PMH/ListRecords/record/metadata/lom/general/title/string"; // XPath query $xp = new DomXpath($dom); $titles = $xp->query($path); $length = $titles->length; // this echoes 0 when the namespace attributes are present echo "<p>Number = " . $length . "</p>"; foreach ($titles as $node) { echo "<p>:: " . $node->textContent . "</p>"; } ?> Example XML which works <OAI-PMH> <ListRecords> <record> <header> <identifier>http://www.english.ltsn.ac.uk/repository/lom/esc_100.xml</identifier> <datestamp>2004-7-22</datestamp> </header> <metadata> <lom> <general> <identifier>http://purl.org/poi/english.ltsn.ac.uk/100</identifier> <title> <string language="en-GB">University of Leeds - Bodington Common Sites</string> </title> <language>en-GB</language> <description> <string language="en-GB">Blah blah blah</string> </description> </general> ....etc... </lom></metadata></record></ListRecords></OAI-PMH> Example XML which doesn't work <OAI-PMH> <ListRecords> <record> <header> <identifier>http://www.english.ltsn.ac.uk/repository/lom/esc_100.xml</identifier> <datestamp>2004-7-22</datestamp> </header> <metadata> ## problem occurs when the lom tag includes the namespace attributes ## as follows <lom xmlns="http://ltsc.ieee.org/xsd/LOMv1p0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://ltsc.ieee.org/xsd/LOMv1p0 http://www.rdn.ac.uk/oai/lom/lom.xsd"> <general> <identifier>http://purl.org/poi/english.ltsn.ac.uk/100</identifier> <title> <string language="en-GB">University of Leeds - Bodington Common Sites</string> </title> <language>en-GB</language> <description> <string language="en-GB">Blah blah blah</string> </description> </general> ....etc... </lom></metadata></record></ListRecords></OAI-PMH> |
|
|||
|
On Wed, 18 Aug 2004 10:08:52 +0100, Chris wrote:
> I have recently started working with the new XML functionality in > PHP5, but I am running into a few problems. Specifically, I am using > an Xpath query to try and pull out the data in specific elements > within my XML file. The problem I am getting is that one of the XML > elements contains namespace attributes and this seems to prevent the > Xpath query from working (ie, if I manually strip the namespace > attributes then the query works fine. After testing I will not have > direct access to the XML file, so stripping the namespace attributes > each time is not an option. > > Can anyone shed any light as to why this might be so, or offer any > suggestions to overcome the problem? I can't claim to have had huge success with XPath in PHP5 when dealing with XML Namespaces myself, but you can try calling the registerNamespace method of the $xp object with the namespace details before running any queries, provided you know what the namespaces are ahead of time (or don't mind looking the information up with something like getElementsByTagNameNS. It's one of those things which at least seems like the right thing to do; hopefully we get a bit more documentation on it at some point. -- Adam Harvey Optimiser Pty Ltd To e-mail: don't make an example out of me! |