Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8403347
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T22:19:10+00:00 2026-06-09T22:19:10+00:00

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

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-09T22:19:11+00:00Added an answer on June 9, 2026 at 10:19 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Add an attribute with static value with xslt I have an XML
Possible Duplicate: Finding an enum value by its Description Attribute I have a generic
Possible Duplicate: Should I use Elements or Attributes in XML? I have never been
Possible Duplicate: How to change the value of a custom attribute I have input
Possible Duplicate: SimpleXML get element content based on attribute value I've looked through several
Possible Duplicate: XML attribute vs XML element I'm creating an XML document that holds
Possible Duplicate: Select xml node by attribute in php Is there any function like
Possible Duplicate: C#: Read XML Attribute using XmlDocument In C# if I were to
Possible Duplicate: Cast string to enum with enum attribute I have a enum like
Possible Duplicate: How to extract a node attribute from XML using PHP's DOM Parser

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.