This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: Removing duplicates for unknown number of elements


Chris,

A way to ensure that the nodes are not duplicated in your output is to use a
test to see if the node() is not equal to a preceding node()

I have used the code below to test to show only values of a node which are
unique for a drop down list, and I am sure it could be adapted for your
display.

<select class="select" name="customer/city">
	<option value="" selected="selected">All Cities</option>
	<xsl:for-each select="customer/city">
		<xsl:choose>
			<xsl:when test="not(.=preceding::customer/city)">
				<option value="{.}"><xsl:value-of select="."/></option>
			</xsl:when>
		</xsl:choose>
	</xsl:for-each>
</select>

Tim Watts


-----Original Message-----
From: owner-xsl-list@lists.mulberrytech.com
[mailto:owner-xsl-list@lists.mulberrytech.com]On Behalf Of Christopher
Go
Sent: Wednesday, 14 March 2001 10:50 AM
To: XSL-List@lists.mulberrytech.com
Subject: [xsl] Removing duplicates for unknown number of elements


Hi all,

Been reading all about the grouping/removing duplicates using keys, etc. but
I'm still confused as to what to do on this situation... somebody else is
giving me the input XML but if it is not formed properly, I am also open to
suggestions as to how to properly send the XML in a *xsl-friendlier* format.

I'm using Xalan Processor (transform() method).

===================================
XML input

<WS>
  <CONTENT TYPE="DB">
    <TITLE>Laker Players</TITLE>
      <ROWS>
        <ROW>
          <NAME>Shaq O'Neal</NAME>
          <EMAIL>shaq@lakers.com</EMAIL>
        </ROW>
        <ROW>
          <NAME>Kobe Bryant</NAME>
          <EMAIL>kobe@lakers.com</EMAIL>
        </ROW>
        <ROW>
          <NAME>Rick Fox</NAME>
          <EMAIL>rick@lakers.com</EMAIL>
        </ROW>
      </ROWS>
  </CONTENT>
  <CONTENT TYPE="DB">
    <TITLE>Sports Teams in CA</TITLE>
      <ROWS>
        <ROW>
          <SPORT>Basketball</SPORT>
          <TEAM>Lakers</TEAM>
          <CITY>Los Angeles</CITY>
        </ROW>
        <ROW>
          <SPORT>Basetball</SPORT>
          <TEAM>Angels</TEAM>
          <CITY>Anaheim</CITY>
        </ROW>
        <ROW>
          <SPORT>Hockey</SPORT>
          <TEAM>Kings</TEAM>
          <CITY>Los Angeles</CITY>
        </ROW>
      </ROWS>
  </CONTENT>
</WS>

===================================

XSL

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="html" indent="yes"/>

<xsl:template name="main" match="WS">
<html>
  <body>
    <xsl:if test="CONTENT/@TYPE[.='DB']">
      <xsl:for-each select="CONTENT/@TYPE[.='DB']">
      <p/>
      <b><xsl:value-of select="../node()/../TITLE"/></b>
      <br/>
      <table border="0" cellpadding="0" cellspacing="0" width="100%">
        <tr>
          <td bgcolor="#666666">
          <table width="100%" cellspacing="1" cellpadding="2" border="0">
            <tr bgcolor="#ffffff">

                  <!------ ***** ------->
                  <xsl:for-each select="../node()/ROW/node()">
                    <th><xsl:value-of select="name()"/></th>
                  </xsl:for-each>

            </tr>
            <xsl:for-each select="../node()/ROW">
              <tr bgcolor="#ffffff">
    		    <xsl:for-each select="node()">
    		      <td><xsl:value-of select="node()"/></td>
</xsl:for-each>
    		  </tr>
            </xsl:for-each>
          </table>
          </td>
        </tr>
      </table>
      </xsl:for-each>
    </xsl:if>
  </body>
</html>
</xsl:template>
</xsl:stylesheet>

===================================

Desired output

<html>
  <body>
    <p>
    <b>Laker Players</b>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
      <td bgcolor="#666666">
	<table width="100%" cellspacing="1" cellpadding="2" border="0">
        <tr bgcolor="#ffffff">
          <th>NAME</th>
          <th>EMAIL</th>
        </tr>
        <tr bgcolor="#ffffff">
          <td>Shaq O'Neal</td>
          <td>shaq@lakers.com</td>
        </tr>
        <tr bgcolor="#ffffff">
          <td>Kobe Bryant</td>
          <td>kobe@lakers.com</td>
        </tr>
        <tr bgcolor="#ffffff">
          <td>Rick Fox</td>
          <td>rick@lakers.com</td>
        </tr>
      </table>
      </td>
      </tr>
    </table>

    <p>
    <b>Sports Teams in CA</b>
    <table border="0" cellpadding="0" cellspacing="0" width="100%">
      <tr>
      <td bgcolor="#666666">
	<table width="100%" cellspacing="1" cellpadding="2" border="0">
        <tr bgcolor="#ffffff">
          <th>SPORT</th>
          <th>TEAM</th>
          <th>CITY</th>
        </tr>
        <tr bgcolor="#ffffff">
          <td>Basketball</td>
          <td>Lakers</td>
          <td>Los Angeles</td>
        </tr>
        <tr bgcolor="#ffffff">
          <td>Basetball</td>
          <td>Angels</td>
          <td>Anaheim</td>
        </tr>
        <tr bgcolor="#ffffff">
          <td>Hockey</td>
          <td>Kings</td>
          <td>Los Angeles</td>
        </tr>
      </table>
      </td>
      </tr>
    </table>

  </body>
</html>


===================================

Questions:

1.  The first problem I have is that (from the XML input) you can see that
after WS/CONTENT/ROWS/ROW, the nodes beneath that change .. is this bad XML
design?

2.  Most of the stuff above works... it's just *bad-looking*... i got node()
calls hooked up which don't look very nice...

3.  I got most of the stuff to work on this page except for 1 thing... on
the XSL stylesheet, right after the <!---- ***** ----> comment tag, I get
duplicate <th></th> tags generated because the loop I have there has no
concept of uniqueness... so for the first chunk, I get:

<tr><th>NAME</th><th>EMAIL</th><th>NAME</th><th>EMAIL</th><th>NAME</th><th>E
MAIL</th></tr>

instead of:
<tr><th>NAME</th><th>EMAIL</th></tr> (which is my desired output).


===================================


Thanks,
Chris



 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]