473,387 Members | 1,611 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Eliminating duplicate information in selected sorted rows

Can somebody help me with this please...

I have an xml file along the lines of

<results>
<result status="Pass">
<person name="Fred"/>
<subject name="English" />
</result>
<result status="Pass">
<person name="Harry"/>
<subject name="Maths" />
</result>
<result status="Fail">
<person name="Fred"/>
<subject name="History" />
</result>
<result status="Fail">
<person name="Harry"/>
<subject name="English" />
</result>
<result status="Pass">
<person name="Fred"/>
<subject name="Maths" />
</result>
<result status="Pass">
<person name="Harry"/>
<subject name="History" />
</result>
</results>

And need to generate html for all result staus="Passes" sorted and displayed
like this:

Passes:
Name Subject
------ --------
Fred English
- Maths
Harry History
- Maths

My best attempt so far is:

....
<xsl:for-each select="results/result[@status='Pass']">
<xsl:sort select="person/@name"/>
<tr>
<td>
<xsl:value-of select="person/@name"/>
<xsl:value-of select="subject/@name"/>
</td>
</tr>
</xsl:for-each>
....

Which gives:

Passes:
Name Subject
------ --------
Fred English
Fred Maths
Harry History
Harry Maths

The problems as I see it are that the output is a sub-set af the whole data,
and it is sorted.

Any ideas on how to remove the duplicate entries in the first column would
be very welcome.

--
Matt

Oct 7 '05 #1
6 1480
Ixa
> <xsl:for-each select="results/result[@status='Pass']">
<xsl:sort select="person/@name"/>
<tr>
<td>
<xsl:value-of select="person/@name"/>
<xsl:value-of select="subject/@name"/>
</td>
</tr>
</xsl:for-each> [...] Any ideas on how to remove the duplicate entries in the first column
would be very welcome.


One suggestion:

---8<---8<---
<xsl:variable name="name" select="person/@name"/>
<xsl:if test="not(preceding::person[@name = $name])">
<xsl:value-of select="person/@name"/>
</xsl:if>
---8<---8<---

--
Ixa

Oct 8 '05 #2
"Ixa" <ix*@niksula.hut.fi> wrote in message
news:43***********************@news.fv.fi...
<xsl:for-each select="results/result[@status='Pass']">
<xsl:sort select="person/@name"/>
<tr>
<td>
<xsl:value-of select="person/@name"/>
<xsl:value-of select="subject/@name"/>
</td>
</tr>
</xsl:for-each>

[...]
Any ideas on how to remove the duplicate entries in the first column
would be very welcome.


One suggestion:

---8<---8<---
<xsl:variable name="name" select="person/@name"/>
<xsl:if test="not(preceding::person[@name = $name])">
<xsl:value-of select="person/@name"/>
</xsl:if>
---8<---8<---


Thanks for the suggestion. I tried it but got blanks for *every* line.

--
Matt
Oct 8 '05 #3
Ixa
> Thanks for the suggestion. I tried it but got blanks for *every* line.

Hmm, I get the expected result. Here's the script that I used:

---8<---8<---
<xsl:for-each select="results/result[@status='Pass']">
<xsl:sort select="person/@name"/>
<tr>
<td>
<xsl:variable name="name" select="person/@name"/>
<xsl:if test="not(preceding::person[@name = $name])">
<xsl:value-of select="person/@name"/>
</xsl:if>
</td>
<td>
<xsl:value-of select="subject/@name"/>
</td>
</tr>
</xsl:for-each>
---8<---8<---

and the result (name once in first column and all passed items in
second column):

---8<---8<---
<tr>
<td>Fred</td>
<td>English</td>
</tr>
<tr>
<td></td>
<td>Maths</td>
</tr>
<tr>
<td>Harry</td>
<td>Maths</td>
</tr>
<tr>
<td></td>
<td>History</td>
</tr>
---8<---8<---

--
Ixa

Oct 8 '05 #4
"Ixa" <ix*@niksula.hut.fi> wrote in message
news:43***********************@news.fv.fi...
Thanks for the suggestion. I tried it but got blanks for *every* line.


Hmm, I get the expected result. Here's the script that I used:

....
I tried again, and got these results - the script is after (sorry for the
length)...
With all the data in place:
Dick Geography
Dick History
Harry English
Harry French
Harry Maths
Tom English
Tom Maths
Processing person names:
Geography
History
English
French
Harry Maths
English
Tom Maths
xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="report.xsl"?>
<report>
<results>
<result status="Pass">
<subject name="Maths"/>
<person name="Tom" />
</result>
<result status="Fail">
<subject name="Maths"/>
<person name="Dick" />
</result>
<result status="Pass">
<subject name="Maths"/>
<person name="Harry" />
</result>
<result status="Fail">
<subject name="History"/>
<person name="Tom" />
</result>
<result status="Pass">
<subject name="History"/>
<person name="Dick" />
</result>
<result status="Fail">
<subject name="History"/>
<person name="Harry" />
</result>
<result status="Pass">
<subject name="English"/>
<person name="Tom" />
</result>
<result status="Fail">
<subject name="English"/>
<person name="Dick" />
</result>
<result status="Pass">
<subject name="English"/>
<person name="Harry" />
</result>
<result status="Fail">
<subject name="Geography"/>
<person name="Tom" />
</result>
<result status="Pass">
<subject name="Geography"/>
<person name="Dick" />
</result>
<result status="Fail">
<subject name="Geography"/>
<person name="Harry" />
</result>
<result status="Pass">
<subject name="French"/>
<person name="Harry" />
</result>
</results>
</report>

xsl:

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/report">
<html>
<body>
<table border="1">
<xsl:for-each select="results/result[@status='Pass']">
<xsl:sort select="person/@name"/>
<xsl:sort select="subject/@name"/>
<tr>
<td>
<xsl:value-of select="person/@name"/>
</td>
<td>
<xsl:value-of select="subject/@name"/>
</td>
</tr>
</xsl:for-each>
</table>
<table border="1">
<xsl:for-each select="results/result[@status='Pass']">
<xsl:sort select="person/@name"/>
<xsl:sort select="subject/@name"/>
<tr>
<td>
<xsl:variable name="name" select="person/@name"/>
<xsl:if test="not(preceding::person[@name = $name])">
<xsl:value-of select="person/@name"/>
</xsl:if> 
</td>
<td>
<xsl:value-of select="subject/@name"/>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

--
Matt
Oct 8 '05 #5
Ixa
> I tried again, and got these results - the script is after

So the sorting confuses the tests and another approach might be better.
Here's one (search for unique persons and their successful results, not
vice versa):

---8<---8<---
<table border="1">
<xsl:for-each select="//person/@name">
<xsl:sort/>
<xsl:variable name="name" select="."/>
<xsl:variable name="items"
select="/report/results/result[person[@name = $name] and
@status='Pass']"/>
<xsl:if test="not(preceding::person[@name = $name])">
<tr>
<td>
<xsl:value-of select="."/>
</td>
<td>
<xsl:for-each select="$items">
<xsl:sort select="subject/@name"/>
<xsl:if test="position() = 1">
<xsl:value-of select="subject/@name"/>
</xsl:if>
</xsl:for-each>
</td>
</tr>
<xsl:for-each select="$items">
<xsl:sort select="subject/@name"/>
<xsl:if test="position() &gt; 1">
<tr>
<td/>
<td>
<xsl:value-of select="subject/@name"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</table>
---8<---8<---

--
Ixa

Oct 8 '05 #6
"Ixa" <ix*@niksula.hut.fi> wrote in message
news:43***********************@news.fv.fi...
I tried again, and got these results - the script is after


So the sorting confuses the tests and another approach might be better.
Here's one (search for unique persons and their successful results, not
vice versa):


Lateral thinking - I love it! It works great!

Thanks for your trouble :-)

--
Matt
Oct 8 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Paul | last post by:
Can anyone suggest an efficient way to eliminate duplicate entries in a list? The naive approach below works fine, but is very slow with lists containing tens of thousands of entries: def...
5
by: gordy | last post by:
edit: this came out longer than I thought, any comments about anything here is greatly appreciated. thank you for reading My system stores millions of records, each with fields like firstname,...
1
by: A.T. | last post by:
I have an XML document that looks like the following, except that it has about 30,000 entries. <root> <version id="A-0000"> <!many elements within- removed for ease of reading> </version>...
7
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
9
by: Catherine Jo Morgan | last post by:
Can I set it up so that a certain combination of fields can't contain the same entries, on another record? e.g. a combination of FirstName/LastName/address? Or FirstName/LastName/phone? Or...
4
by: Henry J. | last post by:
Can somebody tell me how to delete multiple rows in a datagridview selected by the user? I tried the following two methods to no avail. I have a myDataGridView that is bound to myDataTable's...
24
by: clare at snyder.on.ca | last post by:
I have a SQL query I need to design to select name and email addresses for policies that are due and not renewed in a given time period. The problem is, the database keeps the information for every...
5
jamesd0142
by: jamesd0142 | last post by:
My manager and I where looking at some complex code to eliminate duplicate records in a database table. then it hit me how its done easily... so i thought i'd share it... In English:...
2
by: cday119 | last post by:
Hi Everyone, So I have a datagridview and it has a column called status. It has 3 values: New, Printed, and Shipped. Now a user can select multiple rows and click "set to printed". I then do...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.