Filtering With XPath and URL Parameters

This sample shows how you can use the Spry URL Utilities to filter the XPath of the data set. Straight XPath filtering can be seen in this sample.

The Spry URL Utilities allow you to grab URL params and hashes and use then in your page.

This code for this sample is:

<script src="../../includes/xpath.js" type="text/javascript"></script>
<script src="../../includes/SpryData.js" type="text/javascript"></script>
<script src="../../includes/SpryURLUtils.js" type="text/javascript"></script>
<script>
 var params = Spry.Utils.getLocationParamsAsObject();

 var xpath = "/employees/employee";

   if ((params.lastname) && (params.firstname))
   xpath = "/employees/employee[lastname = '"+params.lastname+"' and firstname= '"+params.firstname+"']";
   
   var ds1 = new Spry.Data.XMLDataSet("../../data/employees-01.xml", xpath);
</script>

We attach the Spry data scripts and include the SpryURLUtils.js file.

Then we declare a variable 'params'. This uses a function that gathers all the URL parameters into an object.

We make a var called 'xapth'. We use this variable as the default value for XPath in the Data Set constructor.

Then we check to see if the two URL params are defined (params.lastname and params.firstname). If they are, we construct the new XPath with the parameter values and assign it to the var 'xpath'.

Finally the Data Set is called.

The URLs below look like:

<a href="FilterXPath_with_params.html?firstname=Edward&lastname=Smith">"Edward Smith"</a>

"Edward Smith" | "Neil Johnson" | Show All

Lastname Firstname Phone Username @id
{lastname} {firstname} {phone} {username} {@id}