Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

XSLT : SELECT DISTINCT NODES

Posted on 2006-11-29
4
Medium Priority
?
8,000 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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

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, …
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

670 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