jwnl
jwnl

Reputation: 3

from string to number xslt

I want to make a comparison of two different date’s. To realize that, I want to transform to number. Input example:

<file>
                <date>2015-11-06 09:00/>
</file>
<history>
                <date>2016-01-12 10:00/>
</history>

First I extract the time, from date. And put the result in a var.

   <xsl:for-each select="//item/metadata/document/file/date">          
            <xsl:variable name="d_log" 
                select="substring-before(., ' ')" as="xs:string"/>

So the value of the variable would be, for example 2015-11-06. The next step is, that I want to transform 2015-11-06 in to 20151106.

Questions is how I can transform from a string to a number? Or is there a easier way?

Upvotes: 0

Views: 240

Answers (2)

jwnl
jwnl

Reputation: 3

Here is my result.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="xs"
    version="2.0">
    <xsl:output method="xml" indent="yes"></xsl:output>
    <xsl:template match="/">

        <aaa>
            <xsl:for-each select="//item/metadata/document/file/date">          
            <xsl:variable name="d_log" 
                select="translate(substring-before(., ' '), '-', '')"
                 as="xs:string"/>

                <xsl:for-each select="../../history/log/date">
                    <xsl:variable name="d_doc" select="translate(substring-before(., ' '), '-', '')" as="xs:string"/>               
                <xsl:if test="$d_doc > $d_log">
                    <bb>
                    <xsl:value-of select="node()"/>
                    </bb>
                </xsl:if>         
            </xsl:for-each>
            </xsl:for-each>         
        </aaa>
    </xsl:template> 
</xsl:stylesheet>

Upvotes: 0

michael.hor257k
michael.hor257k

Reputation: 117165

You can do:

translate(substring-before(., ' '), '-', '')

to get the expected number.

However, your syntax suggests you are using XSLT 2.0. If so, why don't you convert the given strings to xs:date or xs:dateTime and compare them as such?

Upvotes: 1

Related Questions