Search code examples
phpxmlxpathsimplexml

PHP - Counting XML Nodes with SimpleXML and XPath - Error: Trying to Get Property of Non-Object


I am writing a script to count the matching nodes in my XML feed.

I'm trying to count the applicable 'INTELLCONT' nodes within the parent node ('RECORD') which has the matching username attribute.

I'm getting this error on the last few lines of the code, where I've attempted the count. It does give a count of zero, on the last line, which is incorrect.

Notice: Trying to get property of non-object

Thank you for any leads.

Here's my code: PHP:

<?php
$secondary_user_id = "jsmith";
$url_bio = "bio_short.xml";
$xml_bio_report= simplexml_load_file($url_bio);

$xml_bio_report->registerXPathNamespace('xlink','http://www.w3.org/1999/xlink');
$xml_bio_report->registerXPathNamespace('dmu','http://www.dms.com/schema/user-metadata');

$secondary_id_check = $xml_bio_report->xpath('//User[@SecondaryID="'.$secondary_id.'"]');
$user_name = $secondary_id_check[0]['username'];

$url_intellcont = "AI_Intellcont.xml";
$xml_intellcont = simplexml_load_file($url_intellcont);
$xml_intellcont_report = $xml_intellcont;

$xml_intellcont_report->registerXPathNamespace('a','http://www.dms.com/schema/data');//
$xml_intellcont_report->registerXPathNamespace('dmd','http://www.dms.com/schema/data-metadata');

$xml_intellcont_report_record = $xml_intellcont_report->xpath('//a:Record[@username="'.$user_name.'"]');

//$xml_intellcont_count = count($xml_intellcont_report->xpath('//a:Record[@username="'.$user_name.'"]')->INTELLCONT);
//$xml_intellcont_count = count($xml_intellcont_report_record->INTELLCONT[1]);


//the corrected answer - below, with the array reference.
$xml_intellcont_count = count($xml_intellcont_report_record[0]->INTELLCONT);
echo '<br>$xml_intellcont_count is...'.$xml_intellcont_count.'<br>';
?>

The first XML file: "bio_short.xml"

<?xml version="1.0" encoding="UTF-8"?>
<Users xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:dmu="http://www.dms.com/schema/user-metadata">
    <User username="john-smith" dmu:userId="14" SecondaryID="jsmith" enabled="false">
        <FirstName>John</FirstName>
        <MiddleName>L</MiddleName>
        <LastName>Smith</LastName>
        <Email>[email protected]</Email>
        <LocalAuthentication/>
        <Item xlink:type="simple" xlink:href="USERNAME:john-smith"/>
    </User>
    <User username="mary-smith" dmu:userId="19" SecondaryID="msmith">
        <FirstName>Mary</FirstName>
        <LastName>Smith</LastName>
        <Email>[email protected]</Email>
        <LocalAuthentication/>
        <Item xlink:type="simple" xlink:href="USERNAME:mary-smith"/>
    </User>   
</Users>

The second XML file: "AI_Intellcont.xml"

<?xml version="1.0" encoding="UTF-8"?>
<Data xmlns="http://www.dms.com/schema/data" xmlns:dmd="http://www.dms.com/schema/data-metadata" dmd:date="2018-01-03">
    <Record userId="148" username="john-smith" termId="4" dmd:surveyId="12">
        <dmd:IndexEntry indexKey="D" entryKey="Dylan" text="Dylan"/>
        <INTELLCONT id="14" dmd:originalSource="54TEX" dmd:lastModified="2017-04-18T10:54:29" dmd:startDate="2011-01-01" dmd:endDate="2011-12-31">
            <CONTYPE>Sales Tools</CONTYPE>
            <CONTYPEOTHER>Sales History</CONTYPEOTHER>
            <NEWREV>New</NEWREV>
            <STATUS>Published</STATUS>
            <TITLE>Comparison of Sales Tools</TITLE>
            <TITLE_SECONDARY/>
            <INTELLCONT_AUTH id="929">
                <FACULTY_NAME/>
                <FNAME>John</FNAME>
                <MNAME/>
                <LNAME>Smith</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>
            <INTELLCONT_AUTH id="930">
                <FACULTY_NAME/>
                <FNAME>S.</FNAME>
                <MNAME>K.</MNAME>
                <LNAME>Smith</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>
            <INTELLCONT_AUTH id="931">
                <FACULTY_NAME/>
                <FNAME>K.</FNAME>
                <MNAME/>
                <LNAME>Smith</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>
            <INTELLCONT_AUTH id="932">
                <FACULTY_NAME/>
                <FNAME>B.</FNAME>
                <MNAME/>
                <LNAME>Stanwyck</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>      
            <PUBLISHER>Crafts Publishing</PUBLISHER>
            <PUBCTYST/>
            <PUBCNTRY/>
            <VOLUME/>
            <ISSUE/>
            <PAGENUM/>
            <WEB_ADDRESS>https://www.myweb.com/3442</WEB_ADDRESS>
            <EDITORS/>
            <ISBNISSN>17-3 (Electronic) 159-63 (Linking)</ISBNISSN>
            <PMCID/>
            <AUDIENCE/>
            <REFEREED/>
            <PUBLICAVAIL/>
            <IMPACT/>
            <CITATIONS/>
            <ABSTRACT/>
            <FULL_TEXT/>
            <DTM_EXPSUB/>
            <DTD_EXPSUB/>
            <DTY_EXPSUB/>
            <EXPSUB_START></EXPSUB_START>
            <EXPSUB_END></EXPSUB_END>
            <DTM_SUB/>
            <DTD_SUB/>
            <DTY_SUB/>
            <SUB_START></SUB_START>
            <SUB_END></SUB_END>
            <DTM_ACC/>
            <DTY_ACC/>
            <ACC_START></ACC_START>
            <ACC_END></ACC_END>
            <DTM_PUB/>
            <DTD_PUB/>
            <DTY_PUB>2011</DTY_PUB>
            <PUB_START>2011-01-01</PUB_START>
            <PUB_END>2011-12-31</PUB_END>
            <USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
        </INTELLCONT>
        <INTELLCONT id="15" dmd:originalSource="58TEX" dmd:lastModified="2017-04-18T10:54:29" dmd:startDate="2011-01-01" dmd:endDate="2011-12-31">
            <CONTYPE>Sales Tools 2</CONTYPE>
            <CONTYPEOTHER>Sales History</CONTYPEOTHER>
            <NEWREV>New</NEWREV>
            <STATUS>Published</STATUS>
            <TITLE>Review of Sales Tools</TITLE>
            <TITLE_SECONDARY/>
            <INTELLCONT_AUTH id="976">
                <FACULTY_NAME/>
                <FNAME>John</FNAME>
                <MNAME/>
                <LNAME>Smith</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>
            <INTELLCONT_AUTH id="977">
                <FACULTY_NAME/>
                <FNAME>S.</FNAME>
                <MNAME/>
                <LNAME>McQueen</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>
            <INTELLCONT_AUTH id="978">
                <FACULTY_NAME/>
                <FNAME>G.</FNAME>
                <MNAME/>
                <LNAME>Cooper</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>
            <PUBLISHER>Tool Review</PUBLISHER>
            <PUBCTYST/>
            <PUBCNTRY/>
            <VOLUME>12</VOLUME>
            <ISSUE>8</ISSUE>
            <PAGENUM>1346-59</PAGENUM>
            <WEB_ADDRESS>https://www.mywebsite.com/479</WEB_ADDRESS>
            <EDITORS/>
            <ISBNISSN>146-80 (Electronic) 10-072 (Linking)</ISBNISSN>
            <PMCID/>
            <AUDIENCE/>
            <REFEREED/>
            <PUBLICAVAIL/>
            <IMPACT/>
            <CITATIONS/>
            <ABSTRACT/>
            <FULL_TEXT/>
            <DTM_EXPSUB/>
            <DTD_EXPSUB/>
            <DTY_EXPSUB/>
            <EXPSUB_START></EXPSUB_START>
            <EXPSUB_END></EXPSUB_END>
            <DTM_SUB/>
            <DTD_SUB/>
            <DTY_SUB/>
            <SUB_START></SUB_START>
            <SUB_END></SUB_END>
            <DTM_ACC/>
            <DTY_ACC/>
            <ACC_START></ACC_START>
            <ACC_END></ACC_END>
            <DTM_PUB/>
            <DTD_PUB/>
            <DTY_PUB>2013</DTY_PUB>
            <PUB_START>2013-01-01</PUB_START>
            <PUB_END>2013-12-31</PUB_END>
            <USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
        </INTELLCONT>

    </Record>




    <Record userId="149" username="mary-smith" termId="33" dmd:surveyId="232">
        <dmd:IndexEntry indexKey="D" entryKey="Dylan" text="Dylan"/>
        <INTELLCONT id="18" dmd:originalSource="54TEX" dmd:lastModified="2017-04-18T10:54:29" dmd:startDate="2011-01-01" dmd:endDate="2011-12-31">
            <CONTYPE>Sales Tools 34</CONTYPE>
            <CONTYPEOTHER>Sales History</CONTYPEOTHER>
            <NEWREV>New</NEWREV>
            <STATUS>Published</STATUS>
            <TITLE>Comparison of Sales Tools</TITLE>
            <TITLE_SECONDARY/>
            <INTELLCONT_AUTH id="959">
                <FACULTY_NAME/>
                <FNAME>John</FNAME>
                <MNAME/>
                <LNAME>Smith</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>
            <INTELLCONT_AUTH id="960">
                <FACULTY_NAME/>
                <FNAME>Mary</FNAME>
                <MNAME/>
                <LNAME>Smith</LNAME>
                <STUDENT_LEVEL/>
            </INTELLCONT_AUTH>   
            <PUBLISHER>Crafts Publishing 2</PUBLISHER>
            <PUBCTYST/>
            <PUBCNTRY/>
            <VOLUME/>
            <ISSUE/>
            <PAGENUM/>
            <WEB_ADDRESS>https://www.myweb.com/3442</WEB_ADDRESS>
            <EDITORS/>
            <ISBNISSN>17-3 (Electronic) 159-63 (Linking)</ISBNISSN>
            <PMCID/>
            <AUDIENCE/>
            <REFEREED/>
            <PUBLICAVAIL/>
            <IMPACT/>
            <CITATIONS/>
            <ABSTRACT/>
            <FULL_TEXT/>
            <DTM_EXPSUB/>
            <DTD_EXPSUB/>
            <DTY_EXPSUB/>
            <EXPSUB_START></EXPSUB_START>
            <EXPSUB_END></EXPSUB_END>
            <DTM_SUB/>
            <DTD_SUB/>
            <DTY_SUB/>
            <SUB_START></SUB_START>
            <SUB_END></SUB_END>
            <DTM_ACC/>
            <DTY_ACC/>
            <ACC_START></ACC_START>
            <ACC_END></ACC_END>
            <DTM_PUB/>
            <DTD_PUB/>
            <DTY_PUB>2011</DTY_PUB>
            <PUB_START>2011-01-01</PUB_START>
            <PUB_END>2011-12-31</PUB_END>
            <USER_REFERENCE_CREATOR>Yes</USER_REFERENCE_CREATOR>
        </INTELLCONT>

    </Record>    
</Data>

Solution

  • The array reference to the SimpleXMLelement::xpath() answer would be:

    $xml_intellcont_count = count($xml_intellcont_report_record[0]->INTELLCONT);