Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#253 2021-03-20 16:56:18

colak
Admin
From: Cyprus
Registered: 2004-11-20
Posts: 9,090
Website GitHub Mastodon Twitter

Re: smd_xml : extract data from XML feeds

etc wrote #329097:

Certainly too late, but for the record:

<txp:etc_query url="https://vimeo.com/api/oembed.json?url=http%3A//vimeo.com/336049258" query="." markup="json">...

Not as late as I am on this one. I have just noticed it.
Thanks so much, the snippet does work!
Here is my first effort.

<txp:etc_query url="https://vimeo.com/api/oembed.json?url=http%3A//vimeo.com/336049258" query="." markup="json">
   <img src="{thumbnail_url?}" width="{thumbnail_width?}" height="{thumbnail_height?}" class="my classes" />
    {description?}
</txp:etc_query>

The problem now is figuring out how to load high res images which do not appear to be supported with their default OEmbed.


Yiannis
——————————
NeMe | hblack.art | EMAP | A Sea change | Toolkit of Care
I do my best editing after I click on the submit button.

Offline

#254 2022-03-15 14:17:06

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 92
Website

Re: smd_xml : extract data from XML feeds

I have this xml feed and would like to extract the <content> node, but only the first one with attribute type="text".

I have tried this snippet

<txp:smd_xml data="https://itunes.apple.com/it/rss/customerreviews/page=1/id=1226931245/sortby=mostrecent/xml" record="entry" fields="title, content"><dt>{title}</dt><dd>{content}</dd></txp:smd_xml>

but as expected it concatenates both <content> nodes and concat="0" is useless for my needs.

I guess the match attribute might help, but unfortunately I found no examples. Will you please help me fix my snippet?

Thanks.

Offline

#255 2022-03-15 18:00:31

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,447
Website GitHub

Re: smd_xml : extract data from XML feeds

You’ll require a trick. I’d use ontagstart and ontagend so that you can check the attribute value and only output it if it matches text. So tweak your smd_xml tag like this:

<txp:smd_xml data="https://itunes.apple.com/it/rss/customerreviews/page=1/id=1226931245/sortby=mostrecent/xml" record="entry" fields="title, content, content|type" ontagstart="filtertext|content" ontagend="showtext|content" />

A few things to note:

  1. It’s now a self-closing tag.
  2. You pull out the content tag twice: once for the full node data, and once for just the attribute name.
  3. ontagstart then says to call a Form called filtertext every time you encounter the content tag. This will set a variable that contains the content attribute’s type.
  4. ontagend then says to call a Form called showtext every time you encounter the content tag. This will test the variable made in filtertext and only output your content if it is text.

So your filtertext form would look like this:

<txp:variable name="thisfilter">{content|type}</txp:variable>

And your showtext form would be:

<txp:if_variable name="thisfilter" value="text">
<dt>{title}</dt><dd>{content}</dd>
</txp:if_variable>

That should do the trick. Bit of a hack, but it’ll work. You can call the forms anything you like, just change their names in your smd_xml tag.

If that’s too convoluted, try the etc_query plugin, which will be a lot more compact, albeit you’ll need to have a rudimentary understanding of XSL to filter out the nodes you want.

Hope that helps.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#256 2022-03-15 20:06:57

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 92
Website

Re: smd_xml : extract data from XML feeds

Thank you, works like a charm.
Please one more thing if you can: I have tweaked smd_xml tag so that now it looks like this

fields="title, content, author->name, content|type"

and showtext form looks like this

<txp:if_variable name="thisfilter" value="text">
<dt>{title}<br />{author->name}</dt><dd>{content}</dd>
</txp:if_variable>

The smd_xml tag breaks. What am I doing wrong?

Thanks.

Offline

#257 2022-03-15 20:51:37

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,447
Website GitHub

Re: smd_xml : extract data from XML feeds

Ah, this is subtle.

You have two things going on here. Firstly, you have correctly requested author->name – i.e. name beneath author – in the field attribute, but when you come to display it, the node name is only {name}. No distinction is made of the hierarchy when it comes to output.

Secondly, since you’re now using the plugin as a single (not container) tag, ontagstart and ontagend process their content immediately as soon as the nominated tag is encountered (at their start or end, respectively) when the feed is being parsed.

Look at the structure of your data feed and notice that the <content> node you are using as a trigger comes before the author node. This means that at the moment the ontagend form is invoked – when the first <content> tag ends – the author name is unknown; it hasn’t been read yet.

So you’ll need to go back to your <txp:smd_xml> tag and tweak your ontagend so it triggers when the author tag is done:

ontagend="showtext|author"

That should fix it.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#258 2022-03-16 07:50:38

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 92
Website

Re: smd_xml : extract data from XML feeds

Thanks.
Unfortunately smd_xml tag is still broken.

I have tweaked it so that

ontagend="showtext|author"

and showtext form now looks like this

<txp:if_variable name="thisfilter" value="text">
<dt>{title}<br />{name}</dt><dd>{content}</dd>
</txp:if_variable>

but to no avail.

I have turned on debug mode, but no error message appears on the page.

Seems like extracting and outputting the author’s name is causing the issue. Any idea how to fix it?

Thanks.

Offline

#259 2022-03-16 07:55:35

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,447
Website GitHub

Re: smd_xml : extract data from XML feeds

Weird. It worked for me using your exact setup. I’ll have to look later.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#260 2022-03-16 11:48:18

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 92
Website

Re: smd_xml : extract data from XML feeds

No need for you to take a look. It was a Textile issue. Fixed it and now it’s working fine. Thanks.

Last edited by gilibaus (2022-03-16 11:48:58)

Offline

#261 2022-03-17 14:34:57

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 92
Website

Re: smd_xml : extract data from XML feeds

From a syntax point of view, how do I handle replacement tags – in this case {im:rating} – in a javascript snippet? In my showtext form I have this code:

<script type="text/javascript">
...
var val = {im:rating};
...
</script>

and it does not seem to work. I guess brackets are the culprit, but I cannot find a solution. Thanks for any help.

Offline

#262 2022-03-17 14:55:19

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,447
Website GitHub

Re: smd_xml : extract data from XML feeds

Does it work if you double or single quote the value?

var val = "{im:rating}";

The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#263 2022-03-17 17:51:48

gilibaus
Member
From: Italy
Registered: 2013-08-14
Posts: 92
Website

Re: smd_xml : extract data from XML feeds

Unfortunately no. In the xml feed, im:rating is an integer number between 1 and 5.

Offline

#264 2022-03-17 20:12:11

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,726
Website

Re: smd_xml : extract data from XML feeds

gilibaus wrote #332933:

From a syntax point of view, how do I handle replacement tags – in this case {im:rating} – in a javascript snippet? In my showtext form I have this code:

var val = {im:rating};

Long shot guess: Does {im:rating} work elsewhere outside the javascript block, i.e. will it output to the page on its own? If so, maybe try putting that in a txp:variable and then using the txp:variable in the javascript block? If not, your problem may lie elsewhere.


TXP Builders – finely-crafted code, design and txp

Offline

Board footer

Powered by FluxBB