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 date range in xsl 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 "date range in xsl" Watch "date range in xsl" New topic
Author

date range in xsl

Ng Jeffrey
Greenhorn

Joined: Mar 27, 2002
Posts: 12
i have an xml file =>
----------------------------------
<?xml version="1.0"?>
<data>
<customer id=1>
<Name>Tom</Name>
<DOB>
<Day>1</Day>
<Month>12</Month
<Year>1977</Year>
</DOB>
</customer>
<customer id=2>
<Name>Dick</Name>
<DOB>
<Day>1</Day>
<Month>12</Month
<Year>1977</Year>
</DOB>
</customer>
<customer id=3>
<Name>Harry</Name>
<DOB>
<Day>2</Day>
<Month>12</Month
<Year>1977</Year>
</DOB>
</customer>
</data>
-----------------------------
How do i count the total number of customer who has the same DOB - birthday? using xsl
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
total number of customer who has one particular birthday, like, say, 12/2/1977 or any birthday? Does your birthday include year?
Ng Jeffrey
Greenhorn

Joined: Mar 27, 2002
Posts: 12
count customers group by birth date using xsl
-> without hardcoding of birth date in xsl
actually, i found a <xsl:for-each-group> but it is available in xpath 2.0. it is able to handle my request... using saxon7 but the problem is i can't do multiple nest <for-each-group> working ... so i thought there might alternatives in xpath1.0
pls advise.
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
If I understood your goal right, you can use parameters. I set them in XSLT for illustrative purpose, but you can set them when you apply your stylesheet.
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:param name="Day">1</xsl:param>
<xsl:param name="Month" select="12"/>
<xsl:param name="Year" select="1977"/>
<xsl:template match="/">
<xsl:value-of select="count (//DOB[Day=$Day and Month=$Month and Year=$Year])"/>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
But if your file is long enough, this may work slow. Maybe you could use key() function instead - it is said to be much more effective.
[ April 19, 2002: Message edited by: Mapraputa Is ]
Ng Jeffrey
Greenhorn

Joined: Mar 27, 2002
Posts: 12
what you have done is set a particular day, month, year which i do not want to do...
<xsl aram name="Day">1</xsl aram>
<xsl aram name="Month" select="12"/>
<xsl aram name="Year" select="1977"/>
the xsl should not contain any hardcoded information instead it should calculate the number of customers with their DOB grouped by year,month,day respectively...
Ng Jeffrey
Greenhorn

Joined: Mar 27, 2002
Posts: 12
This is what i tried to do using xpath2.0 engine saxon7
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl utput method="text" indent="yes" />
<xsl:variable name="newline">
<xsl:text>
</xsl:text>
</xsl:variable>
<xsl:template match="/">
<xsl:for-each-group select="/"
group-by="Year">
<xsl:text>Total count (by year) = </xsl:text>
<xsl:value-of select="count(//Year)"/>
<xsl:value-of select="$newline"/>
<!-- ########LOGIC IS WRONG HERE ##### -->
<xsl:for-each-group select="/"
group-by="Month">
<xsl:text>Total count (by Month) = </xsl:text>
<xsl:value-of select="count(//Month)"/>
<xsl:value-of select="$newline"/>
<xsl:for-each-group select="/"
group-by="Day">
<xsl:text>Total count (by Day) = </xsl:text>
<xsl:value-of select="count(//Day)"/>
<xsl:value-of select="$newline"/>
</xsl:for-each-group>
</xsl:for-each-group>
<!-- TILL HERE -->
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
calculating customers GROUP BY YEAR,MONTH,DAY - that's my objective. The above result returns 3 for all groups ...
or are there alternatives to handle such situations
pls advise
Mapraputa Is
Leverager of our synergies
Sheriff

Joined: Aug 26, 2000
Posts: 10065
Then everything is much worse XSLT isn't too well suited for grouping tasks. This looks close to what you need.
Ng Jeffrey
Greenhorn

Joined: Mar 27, 2002
Posts: 12
Thanks for the useful link ... it gives me an idea how i should counter my problem ...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: date range in xsl
 
Similar Threads
passing parameters
XML searching XSL
dynamic data updation-XML ms ACCESS-ASP-IIS
How do you force a carriage return for text output?
comment tag is xsl file - is it not correct