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 8469207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:09:32+00:00 2026-06-10T16:09:32+00:00

I am attempting to parse an xml file with attributes, and keep getting an

  • 0

I am attempting to parse an xml file with attributes, and keep getting an empty array. Here is a sample of the xml, which is parsed as a simplexml_load_string:

     <NumberOfOfferListings>
    <OfferListingCount condition="Any">61</OfferListingCount>
    <OfferListingCount condition="Used">45</OfferListingCount>
    <OfferListingCount condition="New">16</OfferListingCount>
  </NumberOfOfferListings>

Here is the php code that I am using

$priceComp_xml = amazonCompPrice_xml($asin);
$compPricing = $priceComp_xml->xpath('OfferListingCount[@condition="Any"]'); 

amazonCompPrice($asin) is the parsed xml file based on the ASIN value.

I need to extract just:

<OfferListingCount condition="Any">61</OfferListingCount>

I have looked at many examples on here to get to this point, and it looks like what I have is correct, just returns an empty array when I use either print_r($compPricing) or var_dump.
How do I fix this to get the information that I need??
I can upload any more snippets of code that will help resolve this issue.

  • 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-10T16:09:34+00:00Added an answer on June 10, 2026 at 4:09 pm

    There are (at least) two separate issues at play here.

    The XPath

    Your XPath, OfferListingCount[@condition="Any"], will only return matching <OfferListingCount> elements which are children of the element held in $priceComp_xml. It will not match descendant elements which are grand-children, or further down the tree.

    So, it needs to be amended to match the <OfferListingCount> element(s). A quick fix usually to employ the shorthand // (short for /descendant-or-self::node()/), like //OfferListingCount[@condition="Any"].

    Namespaces

    Your question did not mention this, but some digging uncovered that the XML document probably has a default namespace applied to it. This can be recognised by looking at the document element for xmlns="…". When using XPath, this namespace needs to be registered and used when querying.

    $priceComp_xml->registerXPathNamespace('products', 'http://mws.amazonservices.com/schema/Products/2011-10-01');
    $compPricing = $priceComp_xml->xpath('//products:OfferListingCount[@condition="Any"]');
    

    Finally, remember that SimpleXMLElement::xpath() returns an array, so your matching <offerListingCount> element will be available as $compPricing[0].

    $count = (int) $compPricing[0];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am attempting to parse a XML file using Javascript and I'm running into
I am attempting to parse an XML file using python expat. I have the
I'm attempting to parse an XML file, and generate & insert the element data
I am attempting to parse an xml file in Javascript while ignoring the namespace
I am attempting to parse some JSON from a URL via Flash/AS3. Here's my
I'm attempting to parse XML in the following format (from the European Central Bank
I'm attempting to parse load a rather complicated XML schema into a Schema object
My task is to parse some JSON which was created by running an XML
I'm attempting to parse xml containing foreign letters (æøå specifically), however I'm having problems
I am attempting to parse an XML and store the URL from one element

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.