Another interesting YQL feature: XML with callback (JSON-P-X)

Yesterday’s announcement of YQL now supporting insert, update and delete somehow overshadowed another interesting new feature: XML with callback!

XML with callback?

On first glance having a callback parameter on an XML output doesn’t make any sense. The normal use case of callbacks is to turn JSON data into JSON-P so that you can use the data immediately in JavaScript. The reason for XML with callback is to make it easier to use the same data when getting data from the web. Say I want to use YQL to get the images and links of the people I follow from twitter. The YQL statement is:

select * from html where url="http://twitter.com/codepo8" 
and xpath = "//div[@id='following_list']"

The XML output of this call is a pretty hefty XML document with all the HTML as an XML node structure.

The JSON (and JSON-P output) is even worse as it gives you a structure of all the elements and their attributes as properties of nested objects:

div: {
  id: "following_list"
  span: [
    {
      a: {
        href: "/jemimakiss"
        hreflang: "en"
        rel: "contact"
        title: "Jemima Kiss"
        img: {
          alt: "Jemima Kiss"
          height: "24"
          src: "http://s3.amazonaws.c...glasto_mini.jpg"
          width: "24"
        }
      }
    }
  //... and so on ...

Converting this back into HTML could be quite an annoying job - not to say slow. This is why YQL now offers the callback parameter for XML. The JSONP-X output as it is called in the YQL changelog makes this task a lot easier by returning a JSON object with the XML as a string:

foo({
  "query":{
    "count":"1","created":"2009-07-09T02:44:07Z","lang":"en-US",
    "updated":"2009-07-09T02:44:07Z","uri":"http://...g_list%27%5D%22",
    "diagnostics":{
      "publiclyCallable":"true",
      "url":{
      "execution-time":"1220","proxy":"DEFAULT",
      "content":"http://twitter.com/codepo8"
      },
      "user-time":"1227","service-time":"1220","build-version":"2174"
    }
  },
  "results":[
    "<div id=\"following_list\"><span class=\"vcard\"><a class=\"url\" href=\"/jemimakiss\" hreflang=\"en\" rel=\"contact\" title=\"Jemima Kiss\"><img alt=\"Jemima Kiss\" class=\"photo fn\" height=\"24\" src=\"http://s3.amazonaws.com/twitter_production/profile_images/80996825/glasto_mini.jpg\" width=\"24\"/><\/a><\/span> [...] <\/div>"
    ]
});

This makes it dead easy to render the results back as HTML:

<script type="text/javascript" charset="utf-8">
function foo(o){
  var out = document.getElementById('container');
  var content = o.results[0]
  out.innerHTML = content.replace(/href="\//g,'href="http://twitter.com/');
}
</script>    
<script type="text/javascript" src="http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20html%20where%20url%3D%22http%3A%2F%2Ftwitter.com%2Fcodepo8%22%20and%20xpath%20%3D%20%22%2F%2Fdiv[%40id%3D%27following_list%27]%22&format=xml&callback=foo">
</script>

Nice? I think so!

Tags: , , , , , ,

13 Responses to “Another interesting YQL feature: XML with callback (JSON-P-X)”

  1. codepo8 (Christian Heilmann) Says:

    Twitter Comment


    JSONP-X is here (well in YQL): [link to post]

    Posted using Chat Catcher

  2. Frank Manno Says:

    That is nice! I should definitely start looking into YQL.

    This looks pretty sweet, Christian!

  3. fintler (Jon Bringhurst) Says:

    Twitter Comment


    JSONPX looks sorta-useful… It just seems like too much of a hack for me… [link to post]

    Posted using Chat Catcher

  4. vladnem (Vlad Nemes) Says:

    Twitter Comment


    Another interesting YQL feature: XML with callback (JSON-P-X): Yesterday’s announcement of YQL now supporting in.. [link to post]

    Posted using Chat Catcher

  5. rckenned (Ryan Kennedy) Says:

    Twitter Comment


    @codepo8 please tell me we’re going to make YIV an option in the HTML table now…I see this ending badly for someone

    Posted using Chat Catcher

  6. Jonathan Trevor Says:

    We already provide an HTML safe function, see http://developer.yahoo.com/yql/guide/sorting.html

    sanitize() is your friend.

  7. anupjodish (anupjodish) Says:

    Twitter Comment


    YQL feature: XML with callback [link to post]

    Posted using Chat Catcher

  8. codepo8 (Christian Heilmann) Says:

    Twitter Comment


    @rckenned what do you mean?

    Posted using Chat Catcher

  9. tomayac (Thomas Steiner) Says:

    Twitter Comment


    Nice XPath-based screen-scraping technique over at Yahoo’s: #YQL now offers #JSON-P-X. And it’s dead-simple! [link to post]

    Posted using Chat Catcher

  10. sullrich (Scott Ullrich) Says:

    Twitter Comment


    Another interesting YQL feature: XML with callback (JSON-P-X) [link to post]

    Posted using Chat Catcher

  11. dantecl (Dante Lanznaster) Says:

    Twitter Comment


    @sullrich YQL is awesome… the YQL connector for Pipes makes it also very interesting and newbie-friendly

    Posted using Chat Catcher

  12. sullrich (Scott Ullrich) Says:

    Twitter Comment


    @dantecl It does seem really unique and powerful. I have only done simple queries with it.

    Posted using Chat Catcher

  13. luiscameroon (Luis Cameroon) Says:

    Twitter Comment


    Another interesting YQL feature: JSON-P-X [link to post]

    Posted using Chat Catcher

Leave a Reply

Wait till I come! is the blog of Christian Heilmann , a developer evangelist living and working in London, England. Download vcard.

Feed me, Seymour: Entries (RSS) and Comments (RSS).