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.x ml");
$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>htt p://www.english.lts n.ac.uk/repository/lom/esc_100.xml</identifier>
<datestamp>20 04-7-22</datestamp>
</header>
<metadata>
<lom>
<general>
<identifier>htt p://purl.org/poi/english.ltsn.ac .uk/100</identifier>
<title>
<string language="en-GB">Universit y 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>htt p://www.english.lts n.ac.uk/repository/lom/esc_100.xml</identifier>
<datestamp>20 04-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:schemaLocat ion="http://ltsc.ieee.org/xsd/LOMv1p0
http://www.rdn.ac.uk/oai/lom/lom.xsd">
<general>
<identifier>htt p://purl.org/poi/english.ltsn.ac .uk/100</identifier>
<title>
<string language="en-GB">Universit y 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>