Reputation: 2048
I have an XML Feed with deals and I need to get the titles and the images.
I use xpath and this is an example: /deals/deal/deal_title
(for deal title)
/deals/deal/deal_image
(for deal image)
The problem is that some deals don't have the deal image set at all so when I link deal title with deal image I sometimes get the wrong image.
In order to track down the problem I created two separate arrays: one with titles and the other one with images.
The weird thing that causes the problem is that on the images array the empty instances are moved to the end of the array.
For example if we assume that "deal title2" has no image and "deal title3" has image the "deal title3" image is used for "deal title2".
Use this link to see the code I made: http://pastebin.com/HEuTJQjZ
The interesting part starts from: $doc = new DOMDocument();
Basically what it does is to execute many xpath queries to get titles, images, prices etc and then it adds them to the database.
The problem starts when a deal doesn't have a tag set so it just uses the next value.
I don't understand how it magically moves all the empty instances to the bottom. Xpath isn't supposed to order the results, right?
I have even tried to use the [] operators to get the specific image but doesn't help since the results are sorted the wrong way.
Example feed: http://www.clickbanner.gr/xml/?xml_type=deals&affiliate_ID=14063
EDIT:
The real problem is that xpath does not order the results by document order and modifies the expected order. Is this a bug or something or is there a way to force the results to order by document order? See also: XPath query result order
Thank you in advance.
Upvotes: 1
Views: 600
Reputation: 243449
Evaluate the following two XPath expressions for any values of $k
in the interval [1, count(/deals/deal)]
:
/deals/deal[$k]/deal_title
and
deals/deal[$k]/deal_image
In this way you know whether an image was selected, or not.
For example, if count(/deals/deal)
is 3, then you will evaluate these XPath expressions:
/deals/deal[1]/deal_title
and deals/deal[1]/deal_image
/deals/deal[2]/deal_title
and deals/deal[2]/deal_image
/deals/deal[3]/deal_title
and deals/deal[3]/deal_image
Upvotes: 1
Reputation: 168
I think you should try this way:
/deal/deal_id
tag values/deal[/deal_id="$deal_id"]/deal_title
and /deal[/deal_id="$deal_id"]/deal_image
(using real deal_id) in place of $deal_id
You will get pairs of deal_title and deal_image for each deal and they would match each over correct
Upvotes: 0