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 March 15th, 2008, 08:54 AM
Registered User
 
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default XSLT Time transform

Hello,
I'm new to XSLT, but I noticed it's not so hard tu understand. I run a little project, and I run into little problem. It's time transformation.
In input files I have time in format like:
hours:minutes:seconds.miliseconds (eg. 11:22:33.34)
In output file I want time in the same format as in the input, but I also want to add hours, minutes, seconds or miliseconds to the time stored in output.
Is it possible to do ?
Best regards

 
Old March 15th, 2008, 09:10 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

XSLT and XPath 2.0 have support for the xs:time datatype so with version 2.0 of XSLT you have support for dealing with times or dates and times if you need that.
Arithmetic operations on times and dateTimes are described here: http://www.w3.org/TR/xpath-functions...ime-arithmetic

You can for instance add a duration to a time:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:xsd="http://www.w3.org/2001/XMLSchema"
  version="2.0">

  <xsl:template match="/">
    <xsl:value-of select="current-time() + xsd:dayTimeDuration('P0DT1H')"/>
  </xsl:template>

</xsl:stylesheet>
 
Old March 15th, 2008, 09:48 AM
Registered User
 
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Maybe a little egzample:
I have input xsd file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="col1" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="czas" type="xs:time"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
output xsd file:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
    <xs:element name="test">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="new_time" maxOccurs="unbounded"/>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
and XML file, which contains input data:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\DOCUME~1\MasterAdmin\Pulpit\czas_input.xsd">
    <col1>
        <czas>11:34:45.34</czas>
    </col1>
    <col1>
        <czas>11:34:44:45</czas>
    </col1>
</test>
"czas" is polish word for time,
I use Altova MapForce to generate XSLT files, and XSLT generated is like below:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:fn="http://www.w3.org/2005/xpath-functions" exclude-result-prefixes="fn xs">
    <xsl:output method="xml" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/test">
        <test>
            <xsl:attribute name="xsi:noNamespaceSchemaLocation">
                <xsl:sequence select="'C:/DOCUME~1/MasterAdmin/Pulpit/czas_output.xsd'"/>
            </xsl:attribute>
            <xsl:for-each select="col1">
                <xsl:for-each select="czas">
                    <new_time>
                        <xsl:sequence select="xs:data(.)"/>
                    </new_time>
                </xsl:for-each>
            </xsl:for-each>
        </test>
    </xsl:template>
</xsl:stylesheet>
and output XML should be like below:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:/DOCUME~1/MasterAdmin/Pulpit/czas_output.xsd">
    <new_time>11:34:45.34</new_time>
    <new_time>11:34:44.45</new_time>
</test>
Type xs:data works well, even it could be string, but I must be time.
When I set type to xs:time I get error message like below:
Code:
XSLT2 Transformation failed: Error in XPath2 expression (Invalid lexical value - '11:34:44.45' - xs:time)
What I'm looking for is time transformation code, that could transform my time (11:34:44.45) to be a valid time in output file. And having this valid time would allow me to perform math on it.
Hope it got much more clearer now.
Regards :)
 
Old March 15th, 2008, 10:08 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Your input has <czas>11:34:44:45</czas> which is not a valid time value, you need to correct the last colon ':' to '.'.
Other than that I don't see a problem.

 
Old March 15th, 2008, 10:27 AM
Registered User
 
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Small mistake, corrected.
Now, Can you explain why there in no more problem ?
I have time in format like 11:34:44.45,
all I need is time transformation, that could transform input data (like 11:34:44.45) to output (like 11:34:44.45), but... this time in output will be valid time type, and I'll be able to add 60 seconds and get NOT 11:34:104.45, but 11:35:44.45.

I have a book about XSLT, there's time transformation, ane transforming time to 11:34:44 requires usage of
Code:
format-time ($t, "[H]:[m]:[s]")
Is there something like [ms] ?


 
Old March 15th, 2008, 10:34 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

As shown in my initial example, if you want to add then add a dayTimeDuration e.g. to add 60 seconds use
Code:
  <xsl:template match="czas">
    <new_time>
      <xsl:value-of select="xs:time(.) + xs:dayTimeDuration('P0DT0H0M60S')"/>
    </new_time>
  </xsl:template>
 
Old March 15th, 2008, 10:41 AM
Registered User
 
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

but still, 11:34:44.45 is not valid xs:time type


in book I found that fractions of second are marked as 'f'
I believe that the code below should be the one I'm looking for
Code:
format-time ($t, "[H]:[m]:[s].[f]")
And another question, how can I call a template that would transform input time with transformation above ?
 
Old March 15th, 2008, 10:55 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

I think it is a valid xs:time value and I do not have any problems to process it.
Here is the XML input:
Code:
<test>
    <col1>
        <czas>11:34:45.34</czas>
    </col1>
    <col1>
        <czas>11:34:44.45</czas>
    </col1>
</test>
Here is the stylesheet:
Code:
<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema"
  exclude-result-prefixes="xs">

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="test">
    <xsl:copy>
      <xsl:attribute name="vendor" select="system-property('xsl:vendor')"/>
      <xsl:attribute name="processor" select="concat(system-property('xsl:product-name'), ' ', system-property('xsl:product-version'))"/>
      <xsl:apply-templates select="col1/czas"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="czas">
    <new_time>
      <xsl:value-of select="xs:time(.) + xs:dayTimeDuration('P0DT0H0M60S')"/>
    </new_time>
  </xsl:template>

</xsl:stylesheet>
And here are the results, with Saxon 9:
Code:
<test vendor="SAXON 9.0.0.2 from Saxonica" processor="SAXON 9.0.0.2">
   <new_time>11:35:45.34</new_time>
   <new_time>11:35:44.45</new_time>
</test>
and with Altova:
Code:
<test vendor="Altova GmbH" processor="Altova XSLT Engine 2007">
        <new_time>11:35:45.34</new_time>
        <new_time>11:35:44.45</new_time>
</test>
 
Old March 15th, 2008, 11:09 AM
Registered User
 
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Great, this is working, Thank You Very Much :)
There was a my mistake in my XML file, and that was a reason why MapForce generated an error message, you pointed that out, and I corrected it much more later.
Now the 11:23:33.22 is valid time in xs:type.
But still I have questions:
What does P0DT mean in xs:dayTimeDuration('P0DT0H0M60S') ?
0H is hours, 0M is minutes, 60S is 60 seconds

And off topic question:
How do you call Saxon to perform a transformation ?

 
Old March 15th, 2008, 11:14 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

It is a dayTime duration so 0D means 0 days. 'P' is just the letter to start such a duration.

As for Saxon, the documentation is online here: http://www.saxonica.com/documentation/index.html
How to run an XSLT transformation from the command line is described here: http://www.saxonica.com/documentatio...mmandline.html






Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT: ONE template to transform all the elements Behl_Neha XSLT 8 December 15th, 2007 07:31 PM
Problem to transform xslt with xsltproc command. nadavvin XSLT 1 October 17th, 2006 03:34 PM
XSLT Transform - need assistance emartin24 XSLT 2 September 7th, 2006 12:38 PM
transform in xslt kgoldvas XSLT 1 January 24th, 2006 07:14 AM
XSLT Transform scubaduba C# 0 November 30th, 2004 06:31 AM





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