Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

XSLT : SELECT DISTINCT NODES

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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

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, …
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 …
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Suggested Courses

927 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