IBM developerWorks
           XML Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming Languages - MoreXML Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old August 20th, 2003, 04:57 AM
Dan Timis Dan Timis is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 2 Dan Timis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Help with XSLT

Hi,

I understand that XSLT has no side effects. I'm trying to do something that would be very simple in a procedural language and I just can't figure out how to do it.

I have an XML file, something like this:

<root>
<data tag="a">1</data>
<data tag="b">2</data>
<data tag="a">3</data>
<data tag="b">4</data>
</root>

I would like to sort the "data" elements by tag, but print the tag only once. Something like this:

a:
- 1
- 3
b:
- 2
- 4

I can of course do this:

a:
- 1
a:
- 3
b:
- 2
b:
- 4

I can also print just the first tag using <xsl:if test="position() = 1"> I tried using [position() - 1] to compare it with the previous one when position() != 1, but it didn't work.

How can I do this?

Thanks,

Dan Timis

Reply With Quote
  #2  
Old August 21st, 2003, 08:50 AM
raman_27 raman_27 is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Location: Chennai(Madras)
Posts: 13 raman_27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
answer to help with XSLT

hi,
The solution for the above said problem
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:key name="dist" match="@tag" use="."/>



<xsl:template match="/">
<html>
<head>
<title>Distinct</title>
</head>

<body>
<p>

</p>

<xsl:apply-templates/>

</body>

</html>


</xsl:template>

<!--=============================Table================================================-->

<xsl:template match="root">


<table border="0" cellPadding="1" cellSpacing="1" width="50%" align="left">


<xsl:for-each select="data[count(@tag | key('dist', @tag)[1]) = 1]">

<tr>
<td><xsl:value-of select="@tag"/></td>
</tr>



</xsl:for-each>

</table>


<table border="0" cellPadding="1" cellSpacing="1" width="100%" align="center">
<xsl:for-each select="data">


<tr><td><xsl:value-of select="."/></td></tr>

</xsl:for-each>




</table>


</xsl:template>


</xsl:stylesheet>

Reply With Quote
  #3  
Old August 21st, 2003, 04:17 PM
Dan Timis Dan Timis is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Aug 2003
Posts: 2 Dan Timis User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Thanks raman_27,

The key part was:

select="data[count(@tag | key('dist', @tag)[1]) = 1]"

I understand that key('dist', @tag) will have a node set with all the @tag's matching @tag of <data> and that key('dist', @tag)[1] will have just the first @tag.

I suppose that this:

@tag | key('dist', @tag)[1]

means a union of two sets and that the nodes in each set are references not values. Correct?

So for the first <data> @tag and key('dist', @tag)[1] are the same and the count is 1, while for the third <data> @tag and key('dist', @tag)[1] are different (even though their value is the same) and therefore count is 2.

Correct?

Thanks again,

Dan

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming Languages - MoreXML Programming > Help with XSLT


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway