php - simplexml_load_file not showing all xml attributes

I'm trying to pull xml data from some link with simplexml_load_file.
This one xml node I am trying to access is an array with 4 elements.
Each element has a label which I am trying to read.
I try to read each label using the attributes() function, but for some reason the function only returns the first node label.

// Get data from link
$link = "https://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=12120,34323121&retmode=xml";
$xml = simplexml_load_file($link);

// Try to print attributes:
echo "<pre>";
print_r2($xml->PubmedArticle[1]->MedlineCitation->Article->Abstract->AbstractText->attributes())
echo "</pre>";

Result:

SimpleXMLElement Object
(
    [@attributes] => Array
        (
            [Label] => BACKGROUND
            [NlmCategory] => BACKGROUND
        )

)

This is the XML from the link. There is a label for each node: BACKGROUND, METHODS, RESULTS, CONCLUSION AND RELEVANCE

<AbstractText Label="BACKGROUND" NlmCategory="BACKGROUND">
Extracorporeal membrane oxygenation (ECMO) is a potential option for the management of severe acute respiratory failure secondary to COVID-19. Conflicting the use of this therapy is the known coagulopathy within COVID-19, leading to an incidence of venous thrombotic events of 25% to 49%. To date, limited guidance is available on optimal anticoagulation strategies in this population.</AbstractText><AbstractText Label="OBJECTIVE" NlmCategory="OBJECTIVE">The purpose of this study was to evaluate the utilization of a pharmacist-driven bivalirudin dosing protocol for anticoagulation in the setting of ECMO for COVID-19-associated respiratory failure.
</AbstractText>
<AbstractText Label="METHODS" NlmCategory="METHODS">
This was a single-center retrospective chart review over a 9-month period of patients receiving bivalirudin while on ECMO. All patients with acute respiratory failure requiring ECMO with a positive SARS-CoV-2 polymerase chain reaction were included. Bivalirudin was dosed via aPTT monitoring after a starting dose of 0.2 or 0.3 mg/kg/h.
</AbstractText>
<AbstractText Label="RESULTS" NlmCategory="RESULTS">
There were 33 patients included in this study, all receiving mechanical ventilation. The most common starting dose of bivalirudin was 0.2 mg/kg/h, with an average time to therapeutic range of 20 hours. Compared to previous reports, rates of bleeding were low at 15.1%, and 6.1% of patients developed a new venous thromboembolic event while on ECMO. ECMO survival was 51.5%, with an ICU mortality rate of 48.5%.
</AbstractText>
<AbstractText Label="CONCLUSION AND RELEVANCE" NlmCategory="UNASSIGNED">
In the first published report of its use within this population, bivalirudin was found to be a viable choice for anticoagulation in those patients on ECMO for severe respiratory failure secondary to COVID-19.
</AbstractText>

Could anyone please tell me why I'm not getting all of the labels from the attributes function?

Answer

Solution:

SimpleXML::attributes only appears returns the attributes from the first element in the set.

Although undocumented, this is logical, because the attributes are keyed based on the attribute name, and PHP does not allow reuse of the "Label" key in this way. Even if they weren't, I guess it might be hard to distinguish which attributes applied to which elements.

You'll need to rewrite as a foreach loop or similar.

$labels = [];
foreach ($xml->PubmedArticle[1]->MedlineCitation->Article->Abstract->AbstractText as $t) {
    $attributes = $t->attributes();
    if (isset($attributes->Label)) {
        $labels[] = (string)$attributes->Label;
    }
}

var_dump($labels);

Source