Jump to content

How to sort on the same tag elements


terrypink

Recommended Posts

Hi everyone,New to XSLT and was hoping someone would be nice enough to offer some concrete help.I have the following XML ->--------------------------------------------------------------------------------------------------------

<fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs3740878</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.30</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>intron</value>  </slot></fact><fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs11037909</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.30</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>intron</value>  </slot></fact><fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs13266634</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.60</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>nonsynonymous</value>  </slot></fact><fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs7480010</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.30</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>intron</value>  </slot></fact><fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs7903146</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.30</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>intron</value>  </slot></fact><fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs1113132</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.30</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>intron</value>  </slot></fact>

---------------------------------------------------------------------------------------------------I'm using the following XSLT code ->

<?xml version="1.0" encoding="UTF-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">	<xsl:template match="/">		<xsl:for-each select="fact">			<xsl:if test="name = 'MAIN::SNPRank'">				<tr width="80%" align="center">					<xsl:for-each select="slot">						<xsl:sort select="value" order="descending"/>						<td align="center">							<xsl:value-of select="value"/>						</td>					</xsl:for-each>				</tr>			</xsl:if>		</xsl:for-each>	</xsl:template></xsl:stylesheet>

------------------------------------------------------------------------------------------------------------------------------------This is the result I'm getting ->rs3740878 intron 0.30rs11037909 intron 0.30rs13266634 nonsynonymous 0.60rs7480010 intron 0.30rs7903146 intron 0.30rs1113132 intron 0.30But what I'm trying to do is sort the rows by the number score, so rs1326634 with the score of 0.60 should be at the top of the list.If I should use another strategy like grouping, could someone tell me how?I'm stuck with the XML I have...Could somebody please help me?Thanks SO much for any responses,~Terry

Link to comment
Share on other sites

Oops. Spoke too soon. I found the answer on another forum. In case anyone's interested, here's the code that worked ->

<xsl:for-each select="fact">				<xsl:if test="name = 'MAIN::SNPRank'">				  <tr width="80%" align="center">					<xsl:for-each select="slot">					  <xsl:sort select="slot[name='rank']/@value" data-type="number"/>					  <td align="center">						<xsl:value-of select="value"/>					  </td>					</xsl:for-each>				  </tr>				</xsl:if>			  </xsl:for-each>

Link to comment
Share on other sites

Yikes! I spoke too soon, it didn't work. If I use the code ->

<xsl:for-each select="fact">				<xsl:if test="name = 'MAIN::SNPRank'">				  <tr width="80%" align="center">					<xsl:for-each select="slot">					  <xsl:sort select="slot[name='rank']/@value" data-type="number"/>					  <td align="center">						<xsl:value-of select="value"/>					  </td>					</xsl:for-each>				  </tr>				</xsl:if>			  </xsl:for-each>

With this code, I at first thought it was working, but then I realized that it was just returning the rows in the order that you entered the data. So if I use this XML ->

<fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs4525</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.60</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>nonsynonymous</value>  </slot></fact><fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs405509</value>	</slot>  <slot>	<name>rank</name>	<value type='STRING'>0.20</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>locus-region</value>  </slot></fact><fact>  <name>MAIN::SNPRank</name>  <slot>	<name>id</name>	<value type='STRING'>rs13266634</value>  </slot>  <slot>	<name>rank</name>	<value type='STRING'>0.60</value>  </slot>  <slot>	<name>category</name>	<value type='STRING'>nonsynonymous</value>  </slot></fact>

I get back this result ->rs4525 0.60 nonsynonymousrs405509 0.20 locus-regionrs13266634 0.60 nonsynonymousI also tried adding on order="ascending" as well as order="descending", still it's not looking like it's sorting.Does anyone have any other possible thoughts? I'd really be very appreciative if someone could help me figure this out.

Link to comment
Share on other sites

try this

<xsl:output method="html"/>	<xsl:template match="*">		<xsl:for-each select="//fact">			<xsl:sort select="slot[name='rank']/value" data-type="number" order="descending"/>			<xsl:value-of select="slot"/><br/>		</xsl:for-each>	</xsl:template></xsl:stylesheet>

look closely at this vs yours and you'll see the differences

Link to comment
Share on other sites

Thank you so much for your suggestion! I had posted the question to the p2p.wrox.com forum as well, and an expert there also offered a solution. Here's the one that worked in case anyone else is interested.

<xsl:template match="rules">		  <tr bgcolor="#aaccdd">			<th><h2>SNP rs number</h2></th>			<th><h2>Rank Score</h2></th>			<th><h2>Ranking Determination</h2></th>		  </tr>		  <xsl:for-each select="fact[name='MAIN::SNPRank']">				 <xsl:sort select="slot[name='rank']/value" data-type="number" order="descending"/>				 <tr>					<td align="center">					   <xsl:value-of select="slot[name='id']/value"/>					</td>					<td align="center">					   <xsl:value-of select="slot[name='rank']/value"/>					</td>					<td align="center">					   <xsl:value-of select="slot[name='category']/value"/>					</td>									 </tr>   		  </xsl:for-each>		</xsl:template>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...