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 February 15th, 2006, 09:09 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default Time Comparisons of Nodes

Hi,

I hope someone can help me with the following code examples?:

[u]XML</u>
<ShippingInstructions ShippingType="Collection">
<ShippingDate DateTimeType="Earliest Delivery">20060216 07:00</ShippingDate>
<ShippingDate DateTimeType="Dispatch Date">20060216 08:30</ShippingDate>
<ShippingDate DateTimeType="Earliest Available">20060216 07:00</ShippingDate>
<ShippingDate DateTimeType="Latest Available">20060216 18:00</ShippingDate>
<ShippingDate DateTimeType="Latest Delivery">20060216 17:00</ShippingDate>
<Description>test example</Description>
</ShippingInstructions>

From the above code extraction, I need to compare each 'ShippingDate' element
which I'm running through with the following xsl routine:

[u]XSL</u>
<xsl:template name="ShippingCollection">
<xsl:attribute name="ShippingType"><xsl:text>Collection</xsl:text></xsl:attribute>
<xsl:for-each select="ShippingDate">
<xsl:element name="ShippingDate">
<xsl:variable name="ShippingDateNodes" select="../ShippingDate"/>

<xsl:variable name="dteColl" select="."/>

<xsl:variable name="lenColl" select="string-length(.)"/>

<xsl:variable name="strColl" select="$dteColl/@*"/>
<xsl:choose>
<xsl:when test="position()=1">

<xsl:attribute name="DateTimeType"><xsl:value-of select="$strColl"/></xsl:attribute>
<xsl:value-of select="."/>
....
</xsl:when>
....
</xsl:choose>
....
</xsl:element>
....
</xsl:for-each>
....
</xsl:template name>

[u]Q1</u>
How do I compare the times between two nodes?
I've seen two examples on the web (op:datetime-equal & fn:hours-from-dateTime),
but I don't know how this is coded in an xsl statement. Using the XML example, can
you show me how this is implimented?

[u]Q2</u>
From the XSL, is this the correct way to retrieve the node value?

<xsl:attribute name="DateTimeType"><xsl:value-of select="$strColl"/></xsl:attribute>
<xsl:value-of select="."/>

It works, but is there a more robust method?

Thanks,


Neal

A Northern Soul
__________________
Neal

A Northern Soul
 
Old February 15th, 2006, 07:36 PM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

Q1. In XSLT 1.0 the best way to compare the dates/times is probably to reduce them to numbers. Strip the space and colon and convert to a number by using

number(translate(., ' :', ''))

and then just compare using "&lt;" or "&gt;".

In XSLT 2.0/XPath 2.0 there are more specific ways of comparing dates and times, but only if you use the standard ISO representation which is yyyy-mm-ddThh:mm:ss.

Q2. A more robust method to copy the element is to include the identity template in your stylesheet and then use xsl:apply-templates. The identity template rule (one variant) looks like this

<xsl:template match="*">
<xsl:copy>
  <xsl:copy-of select="@*"/>
  <xsl:apply-templates/>
</xsl:copy>
</xsl:template>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 16th, 2006, 08:30 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

Thanks, that's great.

One thing though.... I'm having trouble reading the values from each variable when the 'for each' routine has completed. How do I declare them and retrive the values once the routine has finished? If I declare them globally as:

<xsl:variable name="str1"></xsl:variable>

and perform the following code, a message box informs me that the 'str1' variable has been declared previously.

<xsl:variable name="str1"></xsl:variable>

<xsl:when test="position()=1">

<xsl:attribute name="DateTimeType"><xsl:value-of select="$strColl"/></xsl:attribute>
<xsl:value-of select="."/>
<xsl:variable name="str1" select="number(translate(., ' :', ''))"/>

Sorry!!!, but I'm not used to xsl syntax.

Thanks,

Neal

A Northern Soul
 
Old February 16th, 2006, 09:58 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

If you need to capture the result of for-each processing in a variable, you do this:

<xsl:variable name="x">
  <xsl:for-each ...

  </xsl:for-each>
</xsl:variable>

If you need to return more than one result, construct a tree-fragment containing new elements, e.g.

<xsl:variable name="x">
  <xsl:for-each ...
    <part1><xsl:value-of select="...."/></part1>
    <part2><xsl:value-of select="...."/></part2>

  </xsl:for-each>
</xsl:variable>



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old February 16th, 2006, 11:38 AM
Friend of Wrox
 
Join Date: Jan 2006
Posts: 131
Thanks: 10
Thanked 0 Times in 0 Posts
Default

I understand the logic behind the first part, but I don't understand the logic to:

<part1><xsl:value-of select="...."/></part1>
<part2><xsl:value-of select="...."/></part2>

is it something like?
<part1><xsl:value-of select="number(translate(., ' :', ''))"/></part1>


Are Part1; Part2; etc elements of 'x'?

How do you extract each part separately?

Regards,


Neal

A Northern Soul





Similar Threads
Thread Thread Starter Forum Replies Last Post
Time Shift time in minus time out lechalas Beginning VB 6 1 August 11th, 2008 01:56 PM
special date/time comparisons hamffjs Access VBA 3 July 12th, 2006 11:41 AM
Performance comparisons Jim Dean BOOK: Beginning Visual Basic 2005 Databases ISBN: 978-0-7645-8894-5 4 July 8th, 2006 01:22 PM
Using xs:time to generate time in desired format krayan001 XSLT 0 June 27th, 2005 04:28 PM
Normalization and comparisons mega Excel VBA 3 March 7th, 2005 02:05 PM





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