Results 1 to 10 of 10

Thread: [RESOLVED] XML remove duplicates

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Resolved [RESOLVED] XML remove duplicates

    Hi all,

    I've only recently started using XML with XSL and i've got what hopefully should be an easy question.

    I have a dataset in my asp.net application which contains a table of data.

    The data consists of Owners, a number of possible job status' and the number of jobs at the given status.

    The output in SQL looks like so:

    Owner, Status, Total
    Bob, Unknown, 2
    Bob, Known, 4
    Bob, Verified, 1
    Fred, Known, 3
    Fred, Verified, 3
    .....

    and so on. The grouping is on the employee and then the status and a count is calculated.

    I have an XSL template that i want to use to display this data. the part i'm interested in looks:
    Code:
            <TABLE>
              <COLGROUP WIDTH="130" ALIGN="LEFT"></COLGROUP>
              <COLGROUP WIDTH="100" ALIGN="LEFT"></COLGROUP>
              <COLGROUP WIDTH="100" ALIGN="LEFT"></COLGROUP>
              <TR>
    			<TD CLASS="HDR">Owner</TD>
    			<TD CLASS="HDR">Status</TD>
    			<TD CLASS="HDR">Total</TD>
              </TR>
              <xsl:for-each select="IssuesByStatus/Table1">
                <TR>
    			<TD><xsl:value-of select="Owner"/></TD>
    				<TD><xsl:value-of select="Status"/></TD>
                  <TD><xsl:value-of select="Total"/></TD>
                </TR>
              </xsl:for-each>
            </TABLE>
    The problem I'm having is that the output repeats the Owner for each status. as shown in the example above. I'd like to be able (using XSL if possible) to only show the Owner name once for each group of status'.

    Is this possible and if so how would i go about doing that.

    Many thanks in advance

    Grant

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: XML remove duplicates

    XSL transforms XML, not comma-separated data like your SQL output. You need to generate XML if you want to use XSL to transform it.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Re: XML remove duplicates

    Sorry, perhaps I should have explained better. In my code I have a section that converts my dataset to an XML data document. Its this that I want to perform the transformation on.

    I can get the above code working and it outputs to a file, however it has the Owner name repeated on every row of data.

    Thanks

    Grant

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: XML remove duplicates

    So what you effectively want is grouping?
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  5. #5

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Re: XML remove duplicates

    That sounds about right. Having only just touched the subject of XSL, i'm not sure of the process around grouping. Have you any examples of how to group in XSL or links to pages that might.

    Thanks for your help.

  6. #6
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: XML remove duplicates

    Try this (requires the entries to be sorted by Owner):
    Code:
            <TABLE>
              <COLGROUP WIDTH="130" ALIGN="LEFT"></COLGROUP>
              <COLGROUP WIDTH="100" ALIGN="LEFT"></COLGROUP>
              <COLGROUP WIDTH="100" ALIGN="LEFT"></COLGROUP>
              <TR>
    			<TD CLASS="HDR">Owner</TD>
    			<TD CLASS="HDR">Status</TD>
    			<TD CLASS="HDR">Total</TD>
              </TR>
              <xsl:for-each select="IssuesByStatus/Table1">
                <TR>
                            <xsl:variable name="pos" select="position()"/>
    			<TD><xsl:if test="string(Owner) != string(../Table1[$pos-1]/Owner)"><xsl:value-of select="Owner"/></xsl:if></TD>
    				<TD><xsl:value-of select="Status"/></TD>
                  <TD><xsl:value-of select="Total"/></TD>
                </TR>
              </xsl:for-each>
            </TABLE>
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  7. #7

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Re: XML remove duplicates

    Hi again,

    I think i see what you're trying to do here but i'm getting an error when running it.

    "The variable or param 'pos-1' is either not defined or it is out of scope."

    Not sure why this is the case, any ideas?

    Thanks for your help.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: XML remove duplicates

    Because - is a valid character for variable names in XSLT. Put a space on each side of the -.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Re: XML remove duplicates

    Sorry,

    Found out what was causing the problem. I've changed to the code to as below

    Code:
    <xsl:for-each select="IssuesByStatus/Table1">
              <TR>
    		  <xsl:variable name="pos" select="position()"/>		  
    		  <td>
    		  <xsl:if test="$pos = 1"><xsl:value-of select="Owner"/></xsl:if>
    		  <xsl:if test="$pos > 1"><xsl:if test="string(Owner) != string(../Table1[$pos - 1]/Owner)"><xsl:value-of select="Owner"/></xsl:if></xsl:if>
    		  </td>
    		  <TD><xsl:value-of select="Status"/></TD>
              <TD><xsl:value-of select="Total"/></TD>
              </TR>
              </xsl:for-each>
    and that does the job. Thanks for all your help.

    Grant

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Sep 2003
    Location
    Bonny Scotland
    Posts
    141

    Re: [RESOLVED] XML remove duplicates

    Just noted your reply as i posted what i found to work. I've taken out the first <xsl:if> clause as it doesn't do anything.

    I appreciate your time on this matter. It makes much more sense now.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width