And then filter an XML file based on the values passed from the URL.
If you’re not aware, Adobe now has it’s own AJAX/Javascript library called SPRY for you to use and better yet they include this with Dreamweaver CS3. I assume that you already probably know of or use SPRY.
So lets assume you want to pass in a value via your URL to filter your data set, baring in mind that this will be a destructive filter. So for example your url is mysite.com/index.htm?myFilter=a
And for example your XML file is something like;
<products>
<product name=”a”>description</product>
<product name=”b”>description</product>
</products>
For this to work you need to add in/ include these JavaScript files in your HTML file:
<script type=”text/javascript” src=”xpath.js”></script>
<script type=”text/javascript” src=”SpryData.js”></script>
// These are needed to interface with the XML file
<script type=”text/javascript” src=”SpryURLUtils.js”></script>
// This is whats used to get the values from the URL query string
And this is the code, if you’ve already used SPRY then you’ll understand some of this.
<script type=”text/javascript”>
var params = Spry.Utils.getLocationParamsAsObject();
// This creates an array of the values called params from the URL using the SpryURLUtils.js
var dsStuff = new Spry.Data.XMLDataSet(”<myDirectory>/<myFile>.xml”, “products/product”);
// This creates a new data set from your xml file
if (params.myFilter){
dsStuff.setXPath(”products/product[@name = '"+params.myFilter+"']“);
dsStuff.loadData();
}
// This then adds in a filter to the same data set as previously declared providing that there is a value in the URL for the variable myFilter. It now destructively filters the data set and you can probably add a case to check that the data in the XML exists.
</script>
And now when you make your call to the data set as normal, it will still function, but when a value is passed into the myFilter variable in the URL, it will then filter the data and return only the matching results.
More can be found here from Adobe
This content is published under the Attribution-Noncommercial-Share Alike 3.0 Unported license.








