File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XSL distinct values

 
Bhupendra Mehta
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an xml of the form
<data>
<row>
<col>2003-05-04</col>
</row>
<row>
<col>2003-05-07</col>
</row>
<row>
<col>2003-05-04</col>
</row>
<row>
<col>2003-05-08</col>
</row>
<row>
<col>2004-06-08</col>
</row>
<row>
<col>2004-07-08</col>
</row>
.
.
.
</data>
The col element has dates in the form of yyyy-mm-dd. I am trying to find distinct yyyy-mm values from the above XML in a "variable". I can get distinct date values by using
<xsl:variable name="distinctdate" select="//row[not(col[1]=preceding-sibling::row/col[1])]/col[1]"></xsl:variable>
which gives me
2003-05-04
2003-05-07
2003-05-08
2004-06-08
2004-07-08

but what i'd really like is
2003-05
2004-06
2004-07
Any reponses will be greatly appreciated. Thanx
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<xsl:variable name="distinctdate" select="//row[not(col[1]=preceding-sibling::row/col[1])]/col[1]"></xsl:variable>


Fudge this rule with the substring function:
substring(col[1], 0, 7) so that you are comparing the first seven chars
and not the value of the col element in its entirety. You will have to do some trial and error.

I am leaving the homework for you.

- m
 
Bhupendra Mehta
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhav,
Thanx for the suggestion but i can't get the substring'd value. This is what I changed the rule to

<xsl:variable name="distinctdate" select="//row[not(substring(col[1],1,7)=substring(preceding-sibling::row/col[1],1,7))]/col[1]"></xsl:variable>

but I can't substring the final value that I'm retrieving //row[condition]/col[1]

Thanx again.
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will look into into and get back.....interesting problem!

- m
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok, I went ahead and modified one of my older style sheets for this -

Kinda lengthy. Maybe there's a better way of doing this but once I started
modifying my existing style sheet, I never looked back. So this probably is
one way to do it, may not be the best.



Sorry about being slack with the variable names and with the indenting.

- m
[ November 01, 2004: Message edited by: Madhav Lakkapragada ]
 
Bhupendra Mehta
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Madhav,
Thanx a lot that worked like a charm.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic