Solved

XSLT : SELECT DISTINCT NODES

Posted on 2006-11-29
4
7,923 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

705 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now