|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
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
|
|||
|
|||
|
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 |
|
#2
|
|||
|
|||
|
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> |
|
#3
|
|||
|
|||
|
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 |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Help with XSLT |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|