1

I'm trying to extract an attribute from a top level XML node but having some difficulty.

Given some XML:

<?xml version="1.0" encoding="UTF-8"?>
<slots>
  <slot start="Monday" finish="Tuesday">
    <title id="1234">John</title>
  </slot>
  <slot start="Thursday" finish="Friday">
    <title id="5678">Fred</title>
  </slot>
</slots> 

and some Twig:

{% set params = {
        url: 'http://above.xml',
        type: 'xml',
} %}
{% set feed = craft.feedme.feed(params) %}

I can extract values and attributes from the inner nodes

{{feed.slots.slot.0.title.attributes.id}}
{{feed.slots.slot.0.title}}

which yields 1234 John as I would expect. But now, how do I get the attributes from the slot node e.g. I want the 'Monday'. I've tried something like

{{feed.slots.slot.0.attributes.start}}

but that just throws an error.

EDIT: If I change the XML (remove the inner title nodes) to

<?xml version="1.0" encoding="UTF-8"?>
<slots>
  <slot start="Monday" finish="Tuesday">
  </slot>
  <slot start="Thursday" finish="Friday">
  </slot>
</slots> 

then

{{feed.slots.slot.0.attributes.start}}

works. So maybe the question now is how do I extract attributes from a node that has sub-nodes?

  • By using the dump command it seems to me that if a node has sub nodes, its attributes are not loaded into the array (craft.feedme.feed(params)). – Jack McKenzie May 18 '16 at 5:12
1

It appears now that this is an issue with the plugin being unable to read attributes of a node which has sub-nodes. The developer has noted this, and will hopefully post a fix. In the meantime as work around I used a short php script on the server to call the XML file first and rework the data into a format that the plugin can read.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.