Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 16th, 2013, 11:58 AM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default counting type of element

Hi All,
I want to count the number of passengers individually, which are characterized by their age. For example, age <= 2 years is infant, age <= 12 years is child, else = adult.

I have been given with the date of birth of every passenger. From DOB, I need to calculate the age and then I have to count the passengers in 3 above categories.

Input..

Code:
                 <ns3:PassengerCounts>
                    <ns3:PassengerCount BirthDate="1988-04-16"/>
                    <ns3:PassengerCount BirthDate="1988-04-16"/>
                </ns3:PassengerCounts>
In the output, I need to create 3 different variable, which i will then place at the appropriate position.

Code:
<xsl:variable name="adultCount"> ??? </xsl:variable>
<xsl:variable name="childCount"> ??? </xsl:variable>
<xsl:variable name="infantCount"> ??? </xsl:variable>
I have created a following variable, which uses a template to return the passenger age, but I am a bit confused as to how I can count the total number of adults, children and infants from it?

Any help in xslt 1, would be appreciated. Thanks.

Code:
<xsl:variable name="age">
					<xsl:call-template name="getAge_template">
						<xsl:with-param name="birhtdate" select="@BirthDate"/>
						<xsl:with-param name="traveldate" select="//ota:OTA_TT_GroundAvailRQ/ota:Locations[1]/ota:Pickup/@DateTime"/>
					</xsl:call-template>
				</xsl:variable>		


<xsl:template name="getAge_template">
		<xsl:param name="birhtdate"/>
		<xsl:param name="traveldate"/>
		<xsl:param name="pattern">yyyy-MM-dd</xsl:param>
		<xsl:param name="age" select="java:text.ParsePosition.new(0)"/>
		<xsl:variable name="sdformater" select="java:text.SimpleDateFormat.new($pattern)"/>
		<xsl:variable name="bd" select="java:util.Calendar.getInstance()"/>
		<xsl:variable name="td" select="java:util.Calendar.getInstance()"/>
		<xsl:if test="java:setTimeInMillis($bd, java:getTime(java:parse($sdformater, $birhtdate)))"/>
		<xsl:if test="java:setTimeInMillis($td, java:getTime(java:parse($sdformater, $traveldate)))"/>
		<xsl:if test="java:setIndex($age, java:get($td, 1) - java:get($bd, 1) )"/>
		<xsl:choose>
			<xsl:when test="java:get($td, 2) &lt; java:get($bd, 2)">
				<xsl:value-of select="java:getIndex($age) -1"/>
			</xsl:when>
			<xsl:when test=" java:get($td, 2) = java:get($bd, 2)  and java:get($td, 5) &lt; java:get($bd, 5)">
				<xsl:value-of select="java:getIndex($age) -1"/>
			</xsl:when>
			<xsl:otherwise>
				<xsl:value-of select="java:getIndex($age)"/>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>
 
Old July 16th, 2013, 12:02 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

All those uses of Java APIs suggest your XSLT processor is implemented in Java. That means you should be able to use the Java version of Saxon 9 and XSLT 2.0 and that way you could implement it in pure XSLT 2.0, no?
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 16th, 2013, 12:08 PM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

it's a huge xsl file, which are using xslt 1.0 features and by using xslt 2 for this problem, the other things would get disturbed, unfortunately.
 
Old July 16th, 2013, 12:19 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

>it's a huge xsl file, which are using xslt 1.0 features and by using xslt 2 for this problem, the other things would get disturbed, unfortunately.

What makes you think that? It's entirely possible to introduce XSLT 2.0 features incrementally into a 1.0 stylesheet.
__________________
Michael Kay
http://www.saxonica.com/
Author, XSLT 2.0 and XPath 2.0 Programmer\'s Reference
 
Old July 16th, 2013, 12:25 PM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I can try if u could help?
 
Old July 16th, 2013, 02:07 PM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

If you want to use XSLT 2.0 then http://www.stylusstudio.com/xsllist/...post60440.html has some snippet to try to compute the age.
__________________
Martin Honnen
Microsoft MVP (XML, Data Platform Development) 2005/04 - 2013/03
My blog
 
Old July 16th, 2013, 02:59 PM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

thanks for the info, Martin. My main problem is to compute the count per passenger type (adult, children, infant). Age calculation i could do myself but I couldnt able to handle the count factor. May be some pointer in that direction might help?

Thanks.
 
Old July 17th, 2013, 07:38 AM
Authorized User
 
Join Date: May 2009
Posts: 32
Thanks: 0
Thanked 0 Times in 0 Posts
Default

this is how i finally managed to do it:

Code:
	<xsl:variable name="checkAgeInfants">
			<xsl:value-of select="xs:date(xs:dateTime(ota:Locations[1]/ota:Pickup/@DateTime)) - xs:yearMonthDuration('P2Y')"/>
		</xsl:variable>
		<xsl:variable name="checkAgeChildren">
			<xsl:value-of select="xs:date(xs:dateTime(ota:Locations[1]/ota:Pickup/@DateTime)) - xs:yearMonthDuration('P12Y')"/>
		</xsl:variable>


		<xsl:variable name="adultCount">
			<xsl:value-of select="count(ota:PassengerPreferences/ota:PassengerCounts/ota:PassengerCount[xs:date(@BirthDate) &lt; xs:date($checkAgeChildren)])"/>
		</xsl:variable>
		<xsl:message>adultCount <xsl:value-of select="$adultCount"/>
		</xsl:message>
		<xsl:variable name="childCount">
			<xsl:value-of select="count(ota:PassengerPreferences/ota:PassengerCounts/ota:PassengerCount[(xs:date(@BirthDate) &gt; xs:date($checkAgeChildren)) and (xs:date(@BirthDate) &lt; xs:date($checkAgeInfants))])"/>
		</xsl:variable>
		<xsl:message>childCount <xsl:value-of select="$childCount"/>
		</xsl:message>
		<xsl:variable name="infantCount">
			<xsl:value-of select="count(ota:PassengerPreferences/ota:PassengerCounts/ota:PassengerCount[xs:date(@BirthDate) &gt; xs:date($checkAgeInfants)])"/>
		</xsl:variable>





Similar Threads
Thread Thread Starter Forum Replies Last Post
Identify element based on the element data/content ROCXY XSLT 7 September 8th, 2011 02:48 AM
Chapter 9: Element[user control] is not a known element Arya BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 December 20th, 2009 07:31 AM
Problem adding element to the previous element dani1 XSLT 5 September 10th, 2008 01:38 AM
Element value type in XSLT 1.0 NEO1976 XSLT 9 February 13th, 2007 11:11 AM
counting the preceding cousins of an element muki XSLT 1 June 15th, 2006 02:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.