This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes XML and Related Technologies and the fly likes XSL distinct values Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "XSL distinct values" Watch "XSL distinct values" New topic
Author

XSL distinct values

Bhupendra Mehta
Greenhorn

Joined: Oct 29, 2004
Posts: 11
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

Joined: Jun 03, 2000
Posts: 5040
<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

Joined: Oct 29, 2004
Posts: 11
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

Joined: Jun 03, 2000
Posts: 5040
Will look into into and get back.....interesting problem!

- m
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
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

Joined: Oct 29, 2004
Posts: 11
Madhav,
Thanx a lot that worked like a charm.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: XSL distinct values
 
Similar Threads
Help wanted on control select/option by javascript
Calendar.roll() questions
need something similoiar to arrays in XSLT..
Question about pulling distinct using XSL
Database to multi-dimensional array problem