This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Dynamic XSL Sorting - Is there a way like this?


--- Robert dot Franklin at milliman dot com wrote:

> Ok, I am fairly new to XSL transformations but up until now I have
> been
> able to get everything I wanted out of it.  Now I am stumped on the
> sort.
> 
> First, let me give you a sample XML.
>
********************************************************************************
>
********************************************************************************
> *********************************
> <?xml version="1.0"?>
> <HRISReport SponsorKey="1160" StartDate="01/01/2002"
> EndDate="07/17/2002" RptVersion="1.0">
> 	<Participants>
> 		<Participant SSN="000-00-0001">
> 			<LastName>Doe</LastName>
> 			<FirstName>John</FirstName>
> 			<Address Changed="N" International="N" AddressDateChange="">
> 				<Address1>PO Box 999</Address1>
> 				<Address2/>
> 				<City>Somewhere</City>
> 				<State>TX</State>
> 				<Zip>99999</Zip>
> 				<Country/>
> 			</Address>
> 			<Plans>
> 				<Plan PlanName="099Test" PlanID="1111" Group="C7777" ID="00001">
> 					<ContributionRateChanges>
> 						<ContributionRateChange>
> 							<HighCompCode>H</HighCompCode>
> 							<ParticipationDate>06/18/1990</ParticipationDate>
> 							<RateEffectiveDate>05/15/2002</RateEffectiveDate>
> 							<RateChanges>
> 								<RateChange Type="K" Name="401(k) Salary Deferral">
> 									<OldAmount>0</OldAmount>
> 									<OldPercent>7.5</OldPercent>
> 									<NewAmount>0</NewAmount>
> 									<NewPercent>15</NewPercent>
> 								</RateChange>
> 							</RateChanges>
> 						</ContributionRateChange>
> 					</ContributionRateChanges>
> 				</Plan>
> 			</Plans>
> 		</Participant>
> 		<Participant SSN="000-00-0002">
> 			<LastName>Smith</LastName>
> 			<FirstName>Missy</FirstName>
> 			<Address Changed="N" International="N" AddressDateChange="">
> 				<Address1>PO Box 888</Address1>
> 				<Address2/>
> 				<City>Somewhere</City>
> 				<State>TX</State>
> 				<Zip>88888</Zip>
> 				<Country/>
> 			</Address>
> 			<Plans>
> 				<Plan PlanName="099Test" PlanID="1111" Group="A7777" ID="00002">
> 					<ContributionRateChanges>
> 						<ContributionRateChange>
> 							<HighCompCode/>
> 							<ParticipationDate>10/02/2000</ParticipationDate>
> 							<RateEffectiveDate>03/22/2002</RateEffectiveDate>
> 							<RateChanges>
> 								<RateChange Type="K" Name="401(k) Salary Deferral">
> 									<OldAmount>0</OldAmount>
> 									<OldPercent>15</OldPercent>
> 									<NewAmount>0</NewAmount>
> 									<NewPercent>15</NewPercent>
> 								</RateChange>
> 								<RateChange Type="C" Name="">
> 									<OldAmount>0</OldAmount>
> 									<OldPercent>0</OldPercent>
> 									<NewAmount>40</NewAmount>
> 									<NewPercent>0</NewPercent>
> 								</RateChange>
> 							</RateChanges>
> 						</ContributionRateChange>
> 					</ContributionRateChanges>
> 				</Plan>
> 			</Plans>
> 		</Participant>
> 		<Participant SSN="000-00-0003">
> 			<LastName>Baker</LastName>
> 			<FirstName>Fred</FirstName>
> 			<Address Changed="N" International="N" AddressDateChange="">
> 				<Address1>PO Box 777</Address1>
> 				<Address2/>
> 				<City>Somewhere</City>
> 				<State>TX</State>
> 				<Zip>77777</Zip>
> 				<Country/>
> 			</Address>
> 			<Plans>
> 				<Plan PlanName="099Test" PlanID="1111" Group="A6666" ID="00003">
> 					<ContributionRateChanges>
> 						<ContributionRateChange>
> 							<HighCompCode/>
> 							<ParticipationDate>10/01/1997</ParticipationDate>
> 							<RateEffectiveDate>02/28/2002</RateEffectiveDate>
> 							<RateChanges>
> 								<RateChange Type="K" Name="401(k) Salary Deferral">
> 									<OldAmount>0</OldAmount>
> 									<OldPercent>7.5</OldPercent>
> 									<NewAmount>0</NewAmount>
> 									<NewPercent>15</NewPercent>
> 								</RateChange>
> 							</RateChanges>
> 						</ContributionRateChange>
> 					</ContributionRateChanges>
> 				</Plan>
> 			</Plans>
> 		</Participant>
> 	</Participants>
> </HRISReport>
> 
>
********************************************************************************
>
********************************************************************************
> *********************************
> 
> Ok, now the XSL code to tranform this structure into a pretty HTML
> table:
> 
>
********************************************************************************
>
********************************************************************************
> *********************************
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <xsl:stylesheet version="1.0"
> xmlns:xsl="http://www.w3.org/1999/XSL/Transform";;>
> 
> <xsl:output method="html" encoding="UTF-8"/>
> <xsl:param name="sortBy" select="'Name'"/>
> 
> <xsl:template match="/">
> 	<!-- Count the rate changes to verify have records -->
>  	<xsl:if
>
test="count(HRISReport/Participants/Participant/Plans/Plan/ContributionRateChang
> es/ContributionRateChange/RateChanges/RateChange) > 0">	
> 		<link rel='stylesheet' href='/stylesheet.css'/>
> 		<HTML>
> 		<HEAD>
> 		<TITLE>Deferral Pct Changes</TITLE>
> 		</HEAD>
> 			<BODY>
> 				<table class='datatable' cellspacing='1'>
> 					<tr>
> 						<td class='ttitle' colspan='13'>Deferral Pct Changes</td>
> 					</tr>
> 					<tr>
> 						<td class='theader' colspan='13'>
> 							From <xsl:value-of
> select="normalize-space(HRISReport/@StartDate)"/> - To <xsl:value-of
> select="normalize-space(HRISReport/@EndDate)"/>
> 						</td>
> 					</tr>
> 					<tr>
> 						<td class='theader'>SSN</td>
> 						<td class='theader'>Last Name</td>
> 						<td class='theader'>First Name</td>
> 						<td class='theader'>Participant Group</td>
> 						<td class='theader'>ID Code</td>
> 						<td class='theader'>HCE Code</td>
> 						<td class='theader'>Participation<br/>Date</td>
> 						<td class='theader'>Request Date</td>
> 						<td class='theader'>Contribution<br/>Acct</td>
> 						<td class='theader'>Old<br/>Percent</td>
> 						<td class='theader'>New<br/>Percent</td>
> 						<td class='theader'>Old<br/>Amount</td>
> 						<td class='theader'>New<br/>Amount</td>
> 					</tr>
> 					
> 						<xsl:for-each
>
select="HRISReport/Participants/Participant/Plans/Plan/ContributionRateChanges/C
> ontributionRateChange/RateChanges/RateChange">
> 							<xsl:sort select="@*[name(.)=$sortBy]"/>
> 
> 							<tr>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../../../../../@SSN)"/></td>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../../../../../LastName)"/></td>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../../../../../FirstName)"/></td>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../../../@Group)"/></td>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../../../@ID)"/></td>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../HighCompCode)"/></td>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../ParticipationDate)"/></td>
> 								<td class='tdatal'><xsl:value-of
> select="normalize-space(../../RateEffectiveDate)"/></td>
> 								<td class="tdatal"><xsl:value-of
> select="normalize-space(@Name)"/></td>
> 								<td class="tdatar"><xsl:value-of
> select="normalize-space(format-number(OldPercent,'#0.00'))"/></td>
> 								<td class="tdatar"><xsl:value-of
> select="normalize-space(format-number(NewPercent,'#0.00'))"/></td>			
> 	
> 
> 								<td class="tdatar"><xsl:value-of
> select="normalize-space(format-number(OldAmount,'###0.00'))"/></td>		
> 	
> 	
> 								<td class="tdatar"><xsl:value-of
> select="normalize-space(format-number(NewAmount,'###0.00'))"/></td>		
> 	
> 
> 							</tr>
> 						</xsl:for-each>
> 				</table>
> 			</BODY>
> 		</HTML>
> 	</xsl:if>
> </xsl:template>
> </xsl:stylesheet>
> 
> 
>
********************************************************************************
>
********************************************************************************
> *********************************
> 
> The issue is the sort.  If I use and hard code the following it
> works:
> <xsl:for-each
>
select="HRISReport/Participants/Participant/Plans/Plan/ContributionRateChanges/C
> ontributionRateChange/RateChanges/RateChange">
> 	<xsl:sort select="../../../../../../LastName"/>
> 	<xsl:sort select="../../../../../../@SSN"/>
> 	
> This sorts properly but if I try to pass in a parameter things start
> to
> fail.
> 
> The following works:
> <xsl:param name="sortBy" select="'Name'"/>
> 
> works if the sort reads:
> <xsl:sort select="@*[name(.)=$sortBy]"/>
> 
> <xsl:param name="sortBy" select="'OldPercent'"/>
>  works if the sort reads:
> <xsl:sort select="*[name(.)=$sortBy]"/>
> 
> But how do I sort off of LastName,FirstName or SSN as a parameter. 
> None of these fields appear to work unless I hardcode the sort.  I
> know
> this is a long post and I am terribly sorry but I have been fight
> this
> for a couple of days.  Thank you in advance.
> _
>     Robert A. Franklin


Hi Robert,

What seems imposible can be done, but you have to change your xml very
slightly. Just add an "ID" attribute to every "RateChange" element, and
make sure the values of this new attribute are unique.


Then do the following changes to your stylesheet:

1. Replace

<xsl:param name="sortBy" select="'Name'"/>

with:

  <xsl:key name="kName" match="@Name" 
       use="../@ID"/>

  <xsl:key name="kLastName" match="LastName" 
       use="../Plans/Plan/ContributionRateChanges
                  /ContributionRateChange/RateChanges/RateChange/@ID"/>

  <xsl:key name="kSSN" match="@SSN" 
       use="../Plans/Plan/ContributionRateChanges
                  /ContributionRateChange/RateChanges/RateChange/@ID"/>


  <xsl:param name="ksortBy" select="'kName'"/>


2. Replace:

<xsl:sort select="@*[name(.)=$sortBy]"/>

with:

              <xsl:sort select="key($ksortBy, @ID)"/>


Now you're ready to run the transformation, setting $ksortBy to
'kName',
'kLastName' and 'kSSN'.


As you can see, this works in all cases.


Hope this helped.


=====
Cheers,

Dimitre Novatchev.
http://fxsl.sourceforge.net/ -- the home of FXSL

__________________________________________________
Do You Yahoo!?
Yahoo! Health - Feel better, live better
http://health.yahoo.com

 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]