3

Possible Duplicates:
Attribute Value Selection in SimpleXML
SimpleXML: Selecting Elements Which Have A Certain Attribute Value

I'm parsing an XML document and looking for a specific ID. The ID value is provided in the ArticleId element under attribute "pii". Raw XML:

<ArticleIdList>
    <ArticleId IdType="pubmed">12676398</ArticleId>
    <ArticleId IdType="pii">S0020729202004460</ArticleId>
</ArticleIdList>

Here is the entire document for reference: http://eutils.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&id=12676398&retmode=xml&rettype=abstract

Using simplexml_load_file(), I'm iterating through the document to obtain values. Here's how I'm getting to the ArticleId element:

$xml_PubmedArticle->PubmedData->ArticleIdList->ArticleId;

The problem is that attributes in ArticleId are random in order. Some ArticleId elements contain a "pii" value in the second element (as below), other records have a different attribute ("doi") in the second element.

SimpleXMLElement Object
(
    [ArticleId] => Array
        (
            [0] => 12676398
            [1] => S0020729202004460
        )
)

Variation:

SimpleXMLElement Object
(
    [ArticleId] => Array
        (
            [0] => 1234
            [1] => ABC123
            [2] => S002012345678
        )
)

I'm looking for the "S0002..." ID, which is identified in raw XML by attribute "pii".

How should I check/obtain a value based on a specific attribute?

Cœur
  • 37,241
  • 25
  • 195
  • 267
a coder
  • 7,530
  • 20
  • 84
  • 131
  • I appreciate the feedback from salathe, PeeHaa, hakra, Jurgen and J0k, but hope that they recognize the difference in how this question is asked, and how the referred question was asked. This includes examples as well as a source XML file so others can more clearly understand the problem. The referenced question provides no background - only by reading the answers do we see that it is similar to this question. The referenced question did not come up in my original StackOverflow search. – a coder Aug 21 '12 at 15:31
  • Yes, it might not have been the best choice, but there's also *another* one that comes close and which now is cross-linked as well: [SimpleXML: Selecting Elements Which Have A Certain Attribute Value (Jun 2009)](http://stackoverflow.com/q/992450/367456). Thanks for your feedback though. – hakre Jul 09 '13 at 09:46
  • My feeling is that having the first asked question doesn't necessarily make it the best overall question for that specific topic/problem. I appreciate the follow-up. – a coder Jul 09 '13 at 19:00

2 Answers2

3

A couple ways:

foreach ($xml_PubmedArticle->PubmedData->ArticleIdList->ArticleId as $id) {
   foreach ($id->attributes() as $name => $value) {
      if ($value == 'pii') {
         //FOUND!
      }
   }
}

..or much simple xpath

$xml_PubmedArticle->xpath('PubmedData/ArticleIdList/ArticleId[@IdType="pii"]');

More specific xpaths are faster.

Also note that the first option will only iterate over the first ArticleIdList where as the XPath will return an array of all the items it finds.

Explosion Pills
  • 188,624
  • 52
  • 326
  • 405
1

You can access a specific element by an attribute's value using xpath:

echo $xml_PubmedArticle->xpath('//ArticleIdList/ArticleId[@IdType="pii"]')[0];

See also Basic SimpleXML Usage in the manual.

A related question is: simplexml, returning multiple items with the same tag

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836