• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

XSL distinct values

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will look into into and get back.....interesting problem!

- m
 
Madhav Lakkapragada
Ranch Hand
Posts: 5040
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Madhav,
Thanx a lot that worked like a charm.
 
You are HERE! The other map is obviously wrong. Better confirm with this tiny ad:
New web page for Paul's Rocket Mass Heaters movies
https://coderanch.com/t/785239/web-page-Paul-Rocket-Mass
reply
    Bookmark Topic Watch Topic
  • New Topic