I've never used xpath before and it's driving me batty. I have the following XML:
<entry>
<updated>2018-02-20</updated>
<api:related>
<api:object>
<api:ever-approved>true</api:ever-approved>
<api:reporting-date-1>2017-12-31</api:reporting-date-1>
<api:repository-items>
<api:repository-item repository-id="1">
<api:public-url>http://url.com</api:public-url>
<api:content-file-count>1</api:content-file-count>
<api:licence-file-count>1</api:licence-file-count>
<api:status>accepted</api:status>
</api:repository-item>
</api:repository-items>
<api:all-labels type="keyword-list">
<api:keywords>
<api:keyword>Africa</api:keyword>
<api:keyword>Economy</api:keyword>
</api:keywords>
</api:all-labels>
</api:object>
</api:related>
...which repeats in that structure. I just need to write some PHP to check whether "api:repository-items" - or any child node within it - exists for each entry. I can do this successfully for:
- api:ever-approved
- api:reporting-date-1
- first api:keyword
by doing variations of:
foreach ( $this->xml->xpath( 'api:related/api:object/api:all-labels/api:keywords/api:keyword[1]' ) as $field ) {
$this->properties['xxx'] = $field;
}
if ( isset( $this->properties['xxx'] ) ){
return $this->properties['xxx'];
} else {
return 'nada';
}
And that works fine. However, I cannot traverse to anything in the repository-items - I only get 'nada'.
I have tried:
- 'api:related/api:object/api:repository-items/api:repository-item/api:status'
- 'api:related/api:object/api:repository-items/api:repository-item/api:content-file-count'
- 'api:related/api:object/api:repository-items/api:repository-item'
- 'api:related/api:object/api:repository-items'
- 'api:related/api:object//api:status'
What am I doing wrong - and any other suggestions?! Thanks!