Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to suppress value if it's the same as the previous one

Status
Not open for further replies.

SilverStray

Programmer
Oct 25, 2001
47
AU
Hi,

How do I suppress a value of an element if it's the same as the previous one, such that it will display my data in some sort of grouped elements?

Here's my xsl code:

<xsl:for-each select="devguru_staff/programmer">
<xsl:sort data-type="text" select="code" order="ascending" />
<xsl:value-of select="code" />
<xsl:text> - </xsl:text>
<xsl:value-of select="name" />
<br />
</xsl:for-each>

And here's the XML sample data:

<devguru_staff>
<programmer>
<name>Bugs Bunny</name>
<code>BB</name>
<age>31</age>
</programmer>
<programmer>
<name>Daisy Duck</name>
<code>BB</name>
<age>51</age>
</programmer>
<programmer>
<name>Minnie Mouse</name>
<code>AA</name>
<age>24</age>
</programmer>
</devguru_staff>

With the XML data given above, I would like to achive an output that's something like:

AA - Minnie Mouse
BB - Bugs Bunny
- Daisy Duck


Hope you can help.

Thanks,
J. Echavez
 
This could do the trick.
What it does is:
- define a key for each programmer, using his/her code
- in the template, only print the code if the programmer you're parsing is not the first person with that code as key. If it IS the first person, counting "current node OR first programmer with this code as key" results in 1, in any other case it results in 2.

Instead of using keys, you could also use a variable containing a nodeset, but keys are supposed to be faster.

Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:key name="coding" match="programmer" use="code"/>
  <xsl:template match="/">
    <xsl:for-each select="devguru_staff/programmer">
      <xsl:sort data-type="text" select="code" order="ascending" />
      <xsl:if test="count(.|key('coding',code)[1])=1">
        <xsl:value-of select="code" />
      </xsl:if>
      <xsl:text> - </xsl:text>
      <xsl:value-of select="name" />
      <br />
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top