Solved

XSLT : SELECT DISTINCT NODES

Posted on 2006-11-29
4
7,943 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Enroll in May's Course of the Month

May’s Course of the Month is now available! Experts Exchange’s Premium Members and Team Accounts have access to a complimentary course each month as part of their membership—an extra way to increase training and boost professional development.

Question has a verified solution.

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

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

710 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