all groups > dotnet xml > february 2006 >
You're in the

dotnet xml

group:

How to do a distinct in XSLT as you loop through Nodes


How to do a distinct in XSLT as you loop through Nodes Patrick.O.Ige
2/23/2006 12:00:00 AM
dotnet xml:
I have a xml file and i want to format it using XSL
My XSL file and XML below
I needed to do a distinct which is ok on the first node "Code"
For the "programDescription" i did below which gets the Count of the nodes
and i get the
programDescription node but it duplicates for the selected "Code" Node.
So if the Count is 3 its shows values "Crazy Training 2" 3 times
for Code "PRG004"
Any help?



<!-- for programDescription-->
<xsl:variable name="pdescription"
select="//ProgramDetail[Code=current()]/programDescription" />


<!-- this is for the ModuleCode Node that i also doa a distinct-->
<xsl:variable name="uniqueModuleDesc"
select="//ProgramDetail[not(uniqueModuleDesc=preceding-sibling::ProgramDetai
l/uniqueModuleDesc)]/ModuleCode"/>



<tr>
<td><xsl:text>&#xa0;</xsl:text></td>
<!--
Output count of owners
//-->
<td valign="top"><xsl:value-of select="count($pdescription)" /></td>
<td valign="top">
<!--
Process owners and print them out
//-->
<xsl:for-each select="$pdescription">
<xsl:sort select="." />
<xsl:value-of select="current()" /><br/>
</xsl:for-each>
</td>
</tr>
But when it gets to the "programDescription" node it prints out values for
both Code="PRG004"
and PRG005 which is CrazyTraining 2 and Program6
I want CrazyTraining 2 for PRG004 and Program6 for PRG005



Any help?
Thx


XSL file
--------
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" indent="yes" />
<!--
Get distinct programs into variable
//-->
<xsl:variable name="uniqueCode"
select="//ProgramDetail[not(Code=preceding-sibling::ProgramDetail/Code)]/Cod
e"/>
<xsl:variable name="uniqueProgramDesc"
select="//ProgramDetail[not(programDescription=preceding-sibling::ProgramDet
ail/programDescription)]/programDescription"/>

<xsl:variable name="uniqueCoursecode"
select="//ProgramDetail[not(CourseCode=preceding-sibling::ProgramDetail/Cour
seCode)]/CourseCode"/>
<xsl:variable name="uniqueCourseDesc"
select="//ProgramDetail[not(CourseDescription=preceding-sibling::ProgramDeta
il/CourseDescription)]/CourseDescription"/>
<xsl:variable name="uniqueModuleCode"
select="//ProgramDetail[not(ModuleCode=preceding-sibling::ProgramDetail/Modu
leCode)]/ModuleCode"/>
<xsl:variable name="uniqueModuleDesc"
select="//ProgramDetail[not(uniqueModuleDesc=preceding-sibling::ProgramDetai
l/uniqueModuleDesc)]/ModuleCode"/>




<!--
Reflect to root node and form the HTML page
//-->
<xsl:template match="/">
<html>
<head>
<title>
Programs to be sent
</title>
</head>
<body>
<table border="1">
<tr>
<td><b>Code</b></td>
<td><b>Counts</b></td>
<td><b>Program Description</b></td>
<td><b>Course Code</b></td>
<td><b>Course Description</b></td>
</tr>
<!--
Process specified template
//-->
<xsl:call-template name="processprogramdetail"/>
<xsl:call-template name="processprogramdetail2"/>

<!--
<xsl:call-template name="processprogramdetail3"/>
<xsl:call-template name="processprogramdetail4"/>
<xsl:call-template name="processprogramdetail5"/>

-->


</table>


<table border="1">
<tr>
<td>
<xsl:call-template name="processprogramdetail3"/>
</td>

<td>
<xsl:call-template name="processprogramdetail4"/>
</td>

<td>
<xsl:call-template name="processprogramdetail5"/>
</td>

</tr>
</table>

</body>
</html>
</xsl:template>



<xsl:template name="processprogramdetail">

<!--
Process each distinct Code
//-->
<xsl:for-each select="$uniqueCode">
<!--
Sort by manufacturer
//-->
<xsl:sort select="." />

<!--
Output the Codes into first row
//-->
<tr>
<td><xsl:value-of select="." /></td>
<td><xsl:text>&#xa0;</xsl:text></td>
<td><xsl:text>&#xa0;</xsl:text></td>
</tr>

<!--
Gets all program Desc listed
//-->
<xsl:variable name="programDescription"
select="//ProgramDetail[Code=current()]/programDescription" />



<tr>
<td><xsl:text>&#xa0;</xsl:text></td>
<!--
Output count of ProgramDescription
//-->
<td valign="top">
<xsl:value-of select="count($programDescription)" /></td>



<td valign="top">
<!--
Process Program Description and print them out
//-->
<xsl:for-each select="$uniqueProgramDesc">
<xsl:sort select="." />
<xsl:value-of select="current()" /><br/>
</xsl:for-each>
</td>




</tr>






<tr>
<td><xsl:text>&#xa0;</xsl:text></td>
<td><xsl:text>&#xa0;</xsl:text></td>
<td><xsl:text>&#xa0;</xsl:text></td>
<td valign="top">
<!--
Process Program description print them out
//-->
<!--
<xsl:for-each select="$programDescription">
-->
<xsl:for-each select="$uniqueCoursecode">

<xsl:sort select="." />
<xsl:value-of select="current()" />

<br/>

</xsl:for-each>
</td>
</tr>


<tr>
<td><xsl:text>&#xa0;</xsl:text></td>
<td><xsl:text>&#xa0;</xsl:text></td>
<td><xsl:text>&#xa0;</xsl:text></td>
<td><xsl:text>&#xa0;</xsl:text></td>
<td valign="top">
<!--
Process Program description print them out
//-->
<!--
<xsl:for-each select="$programDescription">
-->
<xsl:for-each select="$uniqueCourseDesc">

<xsl:sort select="." />
<xsl:value-of select="current()" />

<br/>

</xsl:for-each>
</td>
</tr>





<xsl:variable name="owners"
select="//ProgramDetail[programDescription=current()]/programDescription" />









</xsl:for-each>



</xsl:template>



<xsl:template name="processprogramdetail2">

<xsl:for-each select="$uniqueCode">

<xsl:sort select="." />




<tr>
<td><xsl:value-of select="." /></td>
<td><xsl:text>&#xa0;</xsl:text></td>
<td><xsl:text>&#xa0;</xsl:text></td>
</tr>


<tr>
<td><xsl:text>&#xa0;</xsl:text></td>
<td>
<xsl:for-each select="$uniqueProgramDesc">
<xsl:sort select="." />
<xsl:value-of select="." /><br/>
</xsl:for-each>
</td>



<td><xsl:text>&#xa0;</xsl:text></td>
</tr>







</xsl:for-each>

<!--
<xsl:for-each select="$uniqueCoursecode">
<xsl:sort select="." />
<xsl:value-of select="." /><br/>
</xsl:for-each>


<xsl:for-each select="$uniqueCourseDesc">
<xsl:sort select="." />
<xsl:value-of select="." /><br/>
</xsl:for-each>





<tr>
<td><xsl:text>&#xa0;</xsl:text></td>

<td>

<xsl:for-each select="$uniqueCoursecode">
<xsl:sort select="." />
<xsl:value-of select="." /><br/>
</xsl:for-each>


</td>

Re: How to do a distinct in XSLT as you loop through Nodes Dimitre Novatchev
2/23/2006 12:00:00 AM
http://jenitennison.com/xslt/grouping/index.xml

Cheers,
Dimitre Novatchev

[quoted text, click to view]
AddThis Social Bookmark Button