PHP5 Xpath problem

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


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 08-18-2004
Chris
 
Posts: n/a
Default PHP5 Xpath problem

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>
Reply With Quote
  #2 (permalink)  
Old 08-23-2004
Adam Harvey
 
Posts: n/a
Default Re: PHP5 Xpath problem

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!

Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 09:04 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0