Solved

XSLT : SELECT DISTINCT NODES

Posted on 2006-11-29
4
7,934 Views
Last Modified: 2013-11-18
I have the following XML :

<Product>
<RequirementList>
  <Requirement>
     <RequirementId>55</RequirementId>
     <RequirementValue>TEST</RequirementValue>
  </Requirement>
  <Requirement>
     <RequirementId>55</RequirementId>
     <RequirementValue>TEST</RequirementValue>
  </Requirement>
  <Requirement>
       <RequirementId>56</RequirementId>
      <RequirementValue>APPLE</RequirementValue>
  </Requirement>
    <Requirement>
       <RequirementId>56</RequirementId>
      <RequirementValue>APPLE</RequirementValue>
  </Requirement>
</RequirementList>
</Product>

I would like to return a List of RequirementId and RequirementValue with Distinct RequirementID
Expected result :

Requirements
------------------
55          TEST
56          APPLE

I'm using XSLT, however the code below only returned me a list of distinct requirementId, how do I make it to return also the associated RequirementValue?

<table border="0" width="500" id="tblDetReq" cellpadding="0">
            <tr>
              <Th colspan="2">Requirements</Th>
            </tr>
            <xsl:for-each select="Product/RequirementList/Requirement/RequirementId[not(.=preceding::RequirementId)]">
                <tr class="outputtext">
                <td align="left">
                  <xsl:value-of select="."/>
                </td>
                <td align="left">
                  <xsl:value-of select ="RequirementValue"/>
                </td>
              </tr>
            </xsl:for-each>
</table>          


0
Comment
Question by:sherly
  • 2
  • 2
4 Comments
 
LVL 12

Accepted Solution

by:
enachemc earned 500 total points
ID: 18043711
<table border="0" width="500" id="tblDetReq" cellpadding="0">
            <tr>
              <Th colspan="2">Requirements</Th>
            </tr>
            <xsl:for-each select="Product/RequirementList/Requirement/RequirementId[not(.=preceding::RequirementId)]">
                <tr class="outputtext">
                <td align="left">
                  <xsl:value-of select="."/>
                </td>
                <td align="left">
                  <xsl:value-of select="../RequirementValue"/>
                </td>
              </tr>
            </xsl:for-each>
</table>          
0
 
LVL 2

Author Comment

by:sherly
ID: 18043828
thanks for the solution. I have 1 more minor question if you don't mind, how do I apply alternating row color for this case (using css class)?
0
 
LVL 12

Expert Comment

by:enachemc
ID: 18043886
you can use the position() function of an element for this. If the position mod 2 is 1, then it's an odd row ... otherwise an even row, and you can apply different css classes.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
    xmlns:saxon="http://icl.com/saxon" extension-element-prefixes="saxon">
    <xsl:template match="/">
        <table border="0" width="500" id="tblDetReq" cellpadding="0">
            <tr>
                <Th colspan="2">Requirements</Th>
            </tr>
            <xsl:for-each
                select="Product/RequirementList/Requirement/RequirementId[not(.=preceding::RequirementId)]">
                <tr class="outputtext">
                    <xsl:if test="position() mod 2 = 1">
                        <xsl:attribute name="class">evenRow</xsl:attribute>
                    </xsl:if>
                   
                    <xsl:if test="position() mod 2 = 0">
                        <xsl:attribute name="class">oddRow</xsl:attribute>
                    </xsl:if>
                   
                    <td align="left">
                        <xsl:value-of select="."/>
                    </td>
                    <td align="left">
                        <xsl:value-of select="../RequirementValue"/>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>

</xsl:stylesheet>
0
 
LVL 2

Author Comment

by:sherly
ID: 18043909
Thank You, savior! :D
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

I will show you how to create a ASP.NET Captcha control without using any HTTP HANDELRS or what so ever. you can easily plug it into your web pages. For Example a = 2 + 3 (where 2 and 3 are 2 random numbers) Session("Answer") = 5 then we…
The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

838 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question