 |
| 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
|
|
|
|

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

March 15th, 2008, 09:10 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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>
|
|

March 15th, 2008, 09:48 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 :)
|
|

March 15th, 2008, 10:08 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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.
|
|

March 15th, 2008, 10:27 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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] ?
|
|

March 15th, 2008, 10:34 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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>
|
|

March 15th, 2008, 10:41 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 ?
|
|

March 15th, 2008, 10:55 AM
|
|
Friend of Wrox
|
|
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
|
|
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>
|
|

March 15th, 2008, 11:09 AM
|
|
Registered User
|
|
Join Date: Mar 2008
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
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 ?
|
|
 |