Google Module Example : NXSL API

This example will discuss how to use the NXSL API within a google module.
The following code imports the NXSL API, uses _IG_FetchContent() to make a REST request of a Flickr service and processes the returned XML document.

<script src="http://www.google.com/js/nxsl.1.js" type="text/javascript"></script>
<script type="text/javascript">
  _IG_FetchContent('http://www.flickr.com/services/rest/
?api_key=YourKey&method=flickr.people.findByUsername
&username=brettf', function (responseText){
    var nxsl = xmlParse(responseText)
    var user_id = xmlParse('<div select="//user" content="@id"></div>');
    nxslProcess(nxsl, user_id);
    alert(xmlText(user_id));
   };
  );
</script>

And the returned Flickr document

<rsp stat="ok">
<user id="23687999@N00" nsid="23687999@N00">
  <username>Brettf</username>
</user>
</rsp>

Step 1
Import the NXSL API
<script src="http://www.google.com/js/nxsl.1.js" type="text/javascript"></script>

Step 2
Use _IG_FetchContent() to make a REST request to Flickr and setup the function that will process the returned XML document.
<script type="text/javascript">
    _IG_FetchContent('http://www.flickr.com/services/rest/
?api_key=YourKey&method=flickr.people.findByUsername
&username=brettf', function (responseText){ ...processing code here see step 3...};
);
</script>


Step 3
Process the responseText argument with the NXSL API. There are several steps involved in processing the document. They are explained with comments in the code.

function (responseText){
    //Convert the responseText into an DOM data structure
    var nxsl = xmlParse(responseText);
    
    //Create a NXSL template. This is where I had the most difficulty.
    //The template is a XPath expression broken into pieces.
    //This template will be processed into the id attribute of any user tag in the document.
    var user_id = xmlParse('<div select="//user" content="@id"></div>');
    
    //Process the template using the data structure created earlier
    nxslProcess(nxsl, user_id);
    
    //Convert the processed template into XML text and display
    alert(xmlText(user_id));
};

You can find the whole module at http://www.thestem.ca/flickrSearch/nxslExample.xml

6 Responses to “Google Module Example : NXSL API”

  1. cori Says:

    Hey Brett;
    Good to see someone else is having similar frustrations with Google HP modules.

    I can’t get your example to work properly with my flickr key and user name, nor in your example as written.

    I’m trying to do something very similar to what you’re doing – essentially to set a variable to be the contents of a single node – in my case the <link> node of an RSS item, but the nxsl template for the select seems not to work. Have you had luck?

  2. Brettf Says:

    Did you download the xml file? Can you send a sample of the RSS?

  3. cori Says:

    Yep, I tried it as modified with my api key and user name and with your original. Neither worked so far as I could tell, though the flickr query was successful in both cases.

    You can try any rss feed, even yours, and try to retrieve the item/link element. I can get the element’s content, but can’t set a variable. It seems to me (though error messages seem not have little support in nxsl) that the problem with setting a variable with nxsl is that there’s no xml formatted text to parse in the user_id template, and since nxsl is essentially enhanced html, without the html wrapper there’s nothing to parse. But I’m probably wrong there, just inferrence.

    It’s a bit of a moot point for me personally because I was essentially trying to figure out how to set an href attribute to the value of the item/link element. I ended up using the nxsl call to “values” to set that attribute, and it seems to work fine. That said, I’d love to figure out how to retrieve something like you’re doing and set a variable that I can later use standard javascript on (like getting a substring or something), but as I said I can’t even get your example to work without modification.

    If you ant to look at my code, it’s at http://kinrowan.net/miscellany/flickrtest.xml. I basically added an alert to return the _IG_FetchContent return text to make sure I was getting valid xml and changed to my api key. I get the xml, but the user_id content doesn;t get set. I gather it works for you, though.

  4. Brettf Says:

    Well you’re right Cori. I screwed up. In my example I have it trying to pull the value into the var user_id but that doesn’t work. The NXSL API as I am using it can only create a valid node so you have to have some tag around the value. I have updated the example and am working on how to get only the value. But the way you have gone about solving the problem seems to be the right way.

  5. cori Says:

    Actually, I had a thought about this last night.

    Since the output of the nxsl is essentially just a string that you can assign to a variable, you could use something like:

    function stripTags(inString){
    var re= /]*>/g;
    return(inString.replace(re,””));
    }

    to strip the tags out of the result. So you could use your modified version but call out to this function in the alert parameter to get just the string. Example at http://kinrowan.net/miscellany/flickrtest.xml.

    Some of this stuff might be in the NXSL library, but they’ve obfuscated it so badly that I can’t even trace it (or won’t spend the time, at least). I’m thinking of slapping together a few functions like this one into a library to include into scripts.

  6. Brettf Says:

    Yup you could do that. I thought about that as well but it seems like so much work trying to get it to do somethings it isn’t supposed to do. So I looked around a bit and found this XML for script. They have created a XML W3C parser. This is what I am going to use for my next set of experiments.