PHP - 使用SimpleXML和XPath计算XML节点 - 错误:尝试获取非对象的属性

时间:2018-03-08 21:55:02

标签: php xml xpath simplexml

我正在编写一个脚本来计算XML Feed中的匹配节点。

我试图计算适用的' INTELLCONT'父节点(' RECORD')中具有匹配用户名属性的节点。

我在代码的最后几行收到此错误,我已尝试计算。它确实在最后一行给出了一个零,这是不正确的。

Notice: Trying to get property of non-object

感谢您的任何线索。

这是我的代码: 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>';
?>

第一个XML文件: &#34; bio_short.xml&#34;

<?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>john-smith@email.com</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>mary-smith@email.com</Email>
        <LocalAuthentication/>
        <Item xlink:type="simple" xlink:href="USERNAME:mary-smith"/>
    </User>   
</Users>

第二个XML文件: &#34; AI_Intellcont.xml&#34;

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

1 个答案:

答案 0 :(得分:0)

对SimpleXMLelement :: xpath()答案的数组引用将是:

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