1

I have the following XML:

<nodes>
    <node>
        <title>Test Org - Pleae Ignore</title>
        <logo>
            <enclosure url="https://www.mydomain.org/sites/default/files/louis%20xiv.jpg" length="71280" type="image/jpeg" />
        </logo>
    </node>
    <node>
        <title>Test Org - Pleae Ignore</title>
        <logo>
            <enclosure url="https://www.mydomain.org/sites/default/files/louis%20xiv.jpg" length="71280" type="image/jpeg" />
        </logo>
    </node>
    <node>
        <title>Test Org - Pleae Ignore</title>
        <logo>
            <enclosure url="https://www.mydomain.org/sites/default/files/louis%20xiv.jpg" length="71280" type="image/jpeg" />
        </logo>
    </node>
</nodes>

And the following template markup:

{% set params = {
    url: 'https://www.domain.org/xml-details?nid=12345',
    type: 'xml',
    element: 'node',
    cache: 60,
} %}

{% set feed = craft.feedme.feed(params) %}
{% for node in feed %}
    {{ node.title }}
    {{ node.logo.enclosure.attributes.url }}
{% endear %}

{{ node.title }} displays fine, but {{ node.logo.enclosure.attributes.url }} displays nothing.

Based on other threads I've read, I've also tried {{ node.logo.enclosure['@url'] }}

Neither returns the value of the url attribute.

Any help getting this to work is appreciated.

EDIT: Using just {{ node.logo }} in the template outputs <enclosure url="https://www.mydomain.org/sites/default/files/louis%20xiv.jpg" length="71280" type="image/jpeg" />.

EDIT: Here's my {{ dump(node) }} output.

array(30) {
  ["recordId"]=>
  string(5) "19795"
  ["title"]=>
  string(22) "Robertson Blood Center"
  ["summary"]=>
  string(101) "<p>Volunteer- Donate blood. Your donation of life is used by wounded military at home and abroad.</p>"
  ["logo"]=>
  string(156) "<enclosure url="https://www.mydomain.org/sites/default/files/1959440_10152076927213038_3915913252856478361_n%5B1%5D_2.jpg" length="35608" type="image/jpeg" />"
}
  • You could dump the {{ node.logo}} to see if it contains the property enclosure and continue with that until you reach attributes – Robin Schambach Nov 10 '17 at 5:43
  • @RobinSchambach - I should have mentioned in the OP that {{ node.logo }} does, indeed, display the full <enclosure /> element. So the content is there. I just can't get FeedMe to parse it. – Philip Jones Nov 10 '17 at 12:22
2

I believe the correct syntax you're looking for is:

{{ node.logo.enclosure['@url'] }}

As a handy hint, you can use the following to inspect any element parsed by Feed Me to see how you can access it. Using {{ dump(node.logo.enclosure) }} for example, you can see an array with keys and values for you to access.

This is also outlined in the documentation - https://verbb.io/craft-plugins/feed-me/docs/feature-tour/using-in-your-templates#example-template-code

EDIT: Refer to full code below

{% set params = {
    url: 'http://feedme.craft.dev/stack.xml',
    type: 'xml',
    element: 'node',
} %}

{% for node in craft.feedme.feed(params) %}
    <pre>
        {{ dump(node) }}
    </pre>
{% endfor %}

Produces:

array(2) {
  ["title"]=>
  string(23) "Test Org - Pleae Ignore"
  ["logo"]=>
  array(1) {
    ["enclosure"]=>
    array(3) {
      ["@url"]=>
      string(60) "https://www.mydomain.org/sites/default/files/louis%20xiv.jpg"
      ["@length"]=>
      string(5) "71280"
      ["@type"]=>
      string(10) "image/jpeg"
    }
  }
}

EDIT 2: Looking at your exact feed, I can see the issue. Your feed actually has HTML, which is interpreted as a string. For instance:

<logo>
    &lt;enclosure url="https://www.mydomain.com/1959440_10152076927213038_3915913252856478361_n%5B1%5D_2.jpg" length="35608" type="image/jpeg" /&gt;
</logo>

Notice how is your original example its assumed its all XML? In fact, the logo node contains HTML, which is just treated like a string.

  • You'll see in the OP I tried that syntax and got no output. I'll try the dump and see what happens. – Philip Jones Nov 14 '17 at 0:01
  • Added code with full implementation of the array dump on my end. – crawf Nov 14 '17 at 0:05
  • I added {{ dump(node.logo.enclosure) }} and got an unknown "dump" function error. – Philip Jones Nov 14 '17 at 0:09
  • Apparently I needed Dev Mode on. I added the dump output to my OP, which you'll see doesn't look like what you expected. – Philip Jones Nov 14 '17 at 0:21
  • Without looking at your feed, its hard to tell, but there seems to be HTML directly in your feed, which is considered a string to Feed Me (it will parse the XML, but not the inner HTML). Maybe we'll follow up further via Slack? Username is @crawf – crawf Nov 14 '17 at 5:48

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.