Friday, December 28, 2007
Using Amazon Web Services with REST Part 2: Binding Data To A Web Page With ASP.net
In Part 1, I showed how to construct a URL which would pull out data from Amazon's web services on the best selling Power Ranger toys. With that part done, it's now time to think about how we make use of that data by using it in a web page.
For this example, I am going to show you how I do this using VB in ASP.net. I realise that ASP.net is a bit of a minority interest and it seems like most people out there are using PHP. I will add tutorials for other options later on, but today I will just do ASP.net and for that you will need web hosting space which supports the .net framework.
Overview
ASP.net comes with some nice controls which you can easily bind to an XML data source. The only challenge here is that to use them in their simplest form, they like the XML source to be rather flat in structure. Unfortunately the XML from Amazon is quite complex and has many layers to it which makes it trickier than I like to bind to a data control. So the way I do it is in two steps:
1) Apply an XSLT transformation to the XML source to "flatten it" and just give me the data I want.
2) Bind the transformed XML to my data control.
There will surely be other ways to achieve the same goal, but this is the way I to do it to achieve the result I want with the skillset I have.
Use XSLT to Transform and Flatten the XML from Amazon
Amazon's Web Services have an XSLT processor which means you are able to apply an set of XSL rules to the XML it generates. I have written the following XSL tranformation which extracts key information from the AWS Large Response I mentioned in the previous post and outputs it as more simplified, flatter XML.
See XSL Transformation file (save with .xslt extension to use)
If you want to try following this example, you can save that text file as something like transform.xslt and upload it to your own webspace. I will also zip it up with and make it available for download with the web page source code which I will come to shortly.
Once it has been uploaded to some webspace that you have accessible to you, you then add a Style parameter to the querystring that we built in Part 1.
http://xml-uk.amznxslt.com/onca/xml?Service=AWSECommerceService&
Operation=ItemSearch&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXXX&
AssociateTag=toysandvideogames-21&&SearchIndex=Toys&
Title=Power%20Rangers&Sort=salesrank&ResponseGroup=Large&Style=http://mydomainname/transform.xslt
Bind the Data to a Web Page Control
Now that the source XML is a simplified form, it's very easy to bind to a data control in a webpage in asp.net. I won't explain too much but just give you the source code. It's really hard to paste code into Blogger without it kicking up a fuss so here's a link to the source code in a text file.
ASP.net Example Source Code
Replace the XXXXX in the code with the URL from Part 1 and then save that with a .aspx extension, upload it and then access it in the browser and it should look something like this.

There we have it - live product information from Amazon with the best selling Power Rangers toys. That's one way you can do it in asp.net using VB - I'm sure there are even better ways. I'll soon post up instruction on achieving the same using different development environments.
Use at your own risk - remember your web server needs to support asp.net
Any questions?
For this example, I am going to show you how I do this using VB in ASP.net. I realise that ASP.net is a bit of a minority interest and it seems like most people out there are using PHP. I will add tutorials for other options later on, but today I will just do ASP.net and for that you will need web hosting space which supports the .net framework.
Overview
ASP.net comes with some nice controls which you can easily bind to an XML data source. The only challenge here is that to use them in their simplest form, they like the XML source to be rather flat in structure. Unfortunately the XML from Amazon is quite complex and has many layers to it which makes it trickier than I like to bind to a data control. So the way I do it is in two steps:
1) Apply an XSLT transformation to the XML source to "flatten it" and just give me the data I want.
2) Bind the transformed XML to my data control.
There will surely be other ways to achieve the same goal, but this is the way I to do it to achieve the result I want with the skillset I have.
Use XSLT to Transform and Flatten the XML from Amazon
Amazon's Web Services have an XSLT processor which means you are able to apply an set of XSL rules to the XML it generates. I have written the following XSL tranformation which extracts key information from the AWS Large Response I mentioned in the previous post and outputs it as more simplified, flatter XML.
See XSL Transformation file (save with .xslt extension to use)
If you want to try following this example, you can save that text file as something like transform.xslt and upload it to your own webspace. I will also zip it up with and make it available for download with the web page source code which I will come to shortly.
Once it has been uploaded to some webspace that you have accessible to you, you then add a Style parameter to the querystring that we built in Part 1.
http://xml-uk.amznxslt.com/onca/xml?Service=AWSECommerceService&
Operation=ItemSearch&AWSAccessKeyId=XXXXXXXXXXXXXXXXXXXXX&
AssociateTag=toysandvideogames-21&&SearchIndex=Toys&
Title=Power%20Rangers&Sort=salesrank&ResponseGroup=Large&Style=http://mydomainname/transform.xslt
Bind the Data to a Web Page Control
Now that the source XML is a simplified form, it's very easy to bind to a data control in a webpage in asp.net. I won't explain too much but just give you the source code. It's really hard to paste code into Blogger without it kicking up a fuss so here's a link to the source code in a text file.
ASP.net Example Source Code
Replace the XXXXX in the code with the URL from Part 1 and then save that with a .aspx extension, upload it and then access it in the browser and it should look something like this.

There we have it - live product information from Amazon with the best selling Power Rangers toys. That's one way you can do it in asp.net using VB - I'm sure there are even better ways. I'll soon post up instruction on achieving the same using different development environments.
Use at your own risk - remember your web server needs to support asp.net
Any questions?
Labels: amazon, asp.net, aws, REST, web services, xml
Subscribe to Posts [Atom]












