<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:a="http://schemas.microsoft.com/xrm/2011/Contracts"
	xmlns:b="http://schemas.datacontract.org/2004/07/System.Collections.Generic"
	>	
<xsl:template match="/">
<!-- build a table to display output -->
<table border="1">
<tr>
	<td>Fullname</td>
	<td>State</td>
	<td>Gender</td>
	<td>Createdon</td>
</tr>
<!-- loop through each entity -->
<xsl:for-each select="//a:Entities/a:Entity">
<!-- sort entities by address1_stateorprovince -->
<xsl:sort select="a:Attributes/a:KeyValuePairOfstringanyType[b:key='address1_stateorprovince']/b:value" order="ascending"/>
<!-- create a row for each entity -->
<tr>
	<!-- select the correct attribute/formattedvalue to display in each cell using [b:key='ATTRIBUTENAME'] -->
	<td><xsl:value-of select="a:Attributes/a:KeyValuePairOfstringanyType[b:key='fullname']/b:value" /></td>
	<td><xsl:value-of select="a:Attributes/a:KeyValuePairOfstringanyType[b:key='address1_stateorprovince']/b:value" /></td>
	<td><xsl:value-of select="a:FormattedValues/a:KeyValuePairOfstringstring[b:key='gendercode']/b:value" /></td>
	<td><xsl:value-of select="a:FormattedValues/a:KeyValuePairOfstringstring[b:key='createdon']/b:value" /></td>
</tr>
<!-- close the loop -->
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet> 