469,278 Members | 2,318 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,278 developers. It's quick & easy.

problems with grouping in xslt

Hi,

someone pls help me on this.

following is my xml...

I need to take out papers for each type separately.
and then this each type should have area seperately.

which means: there are two types right now.

Journal-Piblications
Conference Publications

each of them have areas(each paper under these types has an area field).
So my desired output is.

Journal-Piblications

Area 1
papers

Area 2
papers

Conference-Publications

Area 1
papers

Area 2
papers

[html]<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<Publications>

<!--<Journal-Publications>-->
<Paper> <!--paper 6-->
<Type>Journal-Publications</Type>
<Area>A</Area>
<Author>
<Name>abc</Name>
</Author>
<Author>
<Name>abc</Name>
</Author>


<Topic>
abc
</Topic>
<Month >
</Month>
<Year>1996</Year>
<Abstract-link></Abstract-link>
<Full-paper-link></Full-paper-link>
<Abstract></Abstract>
<Conference>abc</Conference>
</Paper>

<Paper>
<Type>Journal-Publications</Type>
<Area>B</Area>
<Author>
<Name>abc</Name>
</Author>
<Author>
<Name>abc</Name>
</Author>


<Topic>
abc
</Topic>
<Month >
</Month>
<Year>1993</Year>
<Abstract-link></Abstract-link>
<Full-paper-link></Full-paper-link>
<Abstract></Abstract>
<Conference>abc</Conference>
</Paper>

<Paper> <!--paper 8 done-->
<Type>Journal-Publications</Type>
<Area>C</Area>
<Author>
<Name>abc</Name>
</Author>
<Author>
<Name>abc</Name>
</Author>
<Topic>
hbaca</Topic>
<Month >
</Month>
<Year>1993</Year>
<Abstract-link></Abstract-link>
<Full-paper-link></Full-paper-link>
<Abstract></Abstract>
<Conference>gh</Conference>
</Paper>


<!--</Journal-Publications>-->

<!--<Conference-publications>-->



<Paper> <!--paper 8 done-->
<Type>Conference-publications</Type>
<Area>C</Area>
<Author>
<Name>abc</Name>
</Author>
<Author>
<Name>abc</Name>
</Author>
<Topic>
hbaca</Topic>
<Month >
</Month>
<Year>1993</Year>
<Abstract-link></Abstract-link>
<Full-paper-link></Full-paper-link>
<Abstract></Abstract>
<Conference>gh</Conference>
</Paper>



<Paper> <!--paper 8 done-->
<Type>Conference-publications</Type>
<Area>B</Area>
<Author>
<Name>abc</Name>
</Author>
<Author>
<Name>abc</Name>
</Author>
<Topic>
hbaca</Topic>
<Month >
</Month>
<Year>1993</Year>
<Abstract-link></Abstract-link>
<Full-paper-link></Full-paper-link>
<Abstract></Abstract>
<Conference>gh</Conference>
</Paper>

</Publications>[/html]
and following is the xslt I am using.

[html]<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:url="http://www.jclark.com/xt/java/java.net.URLEncoder"
exclude-result-prefixes="url">

<xsl:key name="rows" match="Paper" use="Area" />
<xsl:template match="Publications">


<html >
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>abcd</title>
<link href="../css/stylesheet.css" rel="stylesheet" type="text/css" media="all" />
</head>
<body>

<xsl:apply-templates
select="Paper[generate-id(.) = generate-id(key('rows', Area)[1])]" >
<!-- <xsl:if test="Type='Journal-Publications'">-->
<xsl:sort select="Area"/> <!-- Select area alphabatecially. Group By-->
<!-- </xsl:if> -->
</xsl:apply-templates>

</body>
</html>

</xsl:template>

<xsl:template match="Publications/Paper">
<br/>

<b><xsl:value-of select="Area" /></b> <!-- For each area selected above select all papers-->
<br/>
<xsl:for-each select="key('rows', Area)">
<xsl:sort select="Year" data-type="number" order="descending"/>
<xsl:sort select="@monthid" data-type="number" order="descending"/>
<br/>

<xsl:choose>
<xsl:when test="string-length(Full-Paper-link)"> <!--if there is link to paper-->
<a>
<xsl:attribute name="href"><xsl:value-of select="Full-paper-link"/> </xsl:attribute >
<span style="color: #0000ff">
<xsl:value-of select="Topic"/>
</span>
</a>
<br/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Topic"/> <!--if there is no link to paper-->
<br/>
</xsl:otherwise>
</xsl:choose>

<xsl:for-each select="Author">
<xsl:if test="string-length(Name)">
<xsl:value-of select="Name"/>
<xsl:if test="not(position()=last())">
<xsl:text>, </xsl:text>
</xsl:if>
<xsl:if test="position()=last()">
<br/>
</xsl:if>
</xsl:if>
</xsl:for-each>
<xsl:value-of select="Conference"/>
<br/>
<xsl:if test="string-length(Abstract)">
<xsl:value-of select="Abstract"/>
<br/>
</xsl:if>
</xsl:for-each>

</xsl:template>


</xsl:stylesheet>[/html]


Your help is appreciated.

Thanks.

Please add always code or html tags to your code. (moderator)
Mar 14 '07 #1
2 1008
dorinbogdan
839 Expert 512MB
Nice code.

I could not get much time to find a solution.
You should use a 2 level grouping method.

See if this example with 2 keys helps (for XSLT 1.0).
Mar 15 '07 #2
dorinbogdan
839 Expert 512MB
Hi,
Did you succeed to solve the problem ?
If yes, please let me know, in order to close the thread.
Thanks,
Dorin.
Mar 21 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Kevin Brown | last post: by
3 posts views Thread by Graham | last post: by
5 posts views Thread by Mike King | last post: by
2 posts views Thread by Tristan Miller | last post: by
2 posts views Thread by Andreas Håkansson | last post: by
reply views Thread by DAnne | last post: by
reply views Thread by Hvid Hat | last post: by
1 post views Thread by Sandeep Singh | last post: by
4 posts views Thread by MRe | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.