1

I am new with XML namespaces, and in working with this PHP simplexml code, I've found that the code does provide results without namespaces in the XML and the references to them in the PHP code.

The two examples cited as duplicates, however, do not provide a method using registerXPathNamespace method, which is in my code.

Would the solution lie in somehow applying the XML namespaces to the simplexml queries? Thanks for any leads.

PHP code:

$url_congrant_file = "test.xml";

$xml_congrant_feed = simplexml_load_file($url_congrant_file);
$xml_congrant_feed->registerXPathNamespace('a','http://www.digitalmeasures.com/schema/data');//
$xml_congrant_feed->registerXPathNamespace('dmd','http://www.digitalmeasures.com/schema/data-metadata');

$username="bill-smith";
$username=strtolower($username); 
$xml_record=$xml_congrant_feed->xpath('//a:Record[contains(translate(@username,"ABCDEFGHIJKLMNOPQRSTUVWXYZ","abcdefghijklmnopqrstuvwxyz"),"'.$username.'")]');

echo '<br>$xml_record[0] is...'.$xml_record[0]['username'];

$xml_record_congrant_funded = $xml_record[0]->xpath('CONGRANT[STATUS="Funded"]');
//this needs to store all 'congrant' nodes with the status of "funded" 

echo '<br>$xml_record_congrant_funded[0] is...'.$xml_record_congrant_funded[0]->status;
//this needs to display the value of the 'status' node


//I've put in the actual constants for the $offset (0), $per_page (2)   
for ($i = 0; $i < (0 + 2); $i++) {
        echo '<br>$i is...'.$i;
        $strCongrant_id = $xml_record_congrant_funded[$i]->ident;
        echo '<br>$strCongrant_id...'.$strCongrant_id;
        $strCongrant_status = $xml_record_congrant_funded[$i]->status;
        echo '<br>$strCongrant_id...'.$strCongrant_status;
        //this yields no results 
}   
var_dump($xml_record_congrant_funded);
print_r($xml_record_congrant_funded);

XML ("test.xml"):

<?xml version="1.0" encoding="utf-8"?>
<Data xmlns="http://www.digitalmeasures.com/schema/data" xmlns:dmd="http://www.digitalmeasures.com/schema/data-metadata" dmd:date="2019-09-09">
    <Record userId="9a" username="bill-smith" termId="2" dmd:surveyId="1">
        <dmd:IndexEntry indexKey="DE" entryKey="S7" text="S7"/>
        <CONGRANT id="12" dmd:originalSource="MA" dmd:created="2018-03-30T14:22:43" dmd:lastModifiedSource="MA" dmd:lastModified="2019-02-19T10:49:07" dmd:startDate="2017-08-08" dmd:endDate="2022-07-31">
            <TYPE>Book Grant</TYPE>
            <TITLE>Modulation Theory</TITLE>
            <STATUS>Funded</STATUS>
        </CONGRANT>
        <CONGRANT id="11" dmd:originalSource="MA" dmd:created="2019-07-05T10:14:55" dmd:lastModifiedSource="MA" dmd:lastModified="2019-07-05T10:17:44" dmd:startDate="2019-06-15" dmd:endDate="2019-06-15">
            <TYPE>Digital Grant</TYPE>
            <TITLE>Frequency Modulation</TITLE>
            <STATUS/>
        </CONGRANT>
        <CONGRANT id="16" dmd:originalSource="MA" dmd:created="2018-03-26T11:39:42" dmd:lastModifiedSource="MA" dmd:lastModified="2018-03-26T11:40:15" dmd:startDate="2017-10-04" dmd:endDate="2017-10-04">
            <TYPE>Book Grant</TYPE>
            <TITLE>Ferrite Curves</TITLE>
            <STATUS>Currently Under Review</STATUS>
        </CONGRANT>
    </Record>
</Data>
buck1112
  • 504
  • 8
  • 24
  • The two examples cited as duplicates, however, do not provide a method using registerXPathNamespace method, which is in my code. – buck1112 Sep 11 '19 at 15:21

0 Answers0