XSLT1.0 / XPath 1.0 Selecting nodes by date range. Is it possible?

Suppose I have a list of nodes that contain the datetime attribute, and I want to select only the records that occur after $ compare-datetime.

<records>
    <record @datetime="2010-01-04T16:48:15.501-05:00"/>
    <record @datetime="2010-01-03T16:48:15.501-05:00"/>
    ...etc...
</records>

In xquery to select items in a date range I would do

/records/record[xs:dateTime(@datetime) > xs:dateTime($compare-datetime)]

However, in XSLT 1.0 I tried different approaches many times and searched for answers a lot, without any success in my work.

I'm starting to think that without analyzing the actual dateTime value to an integer value, this is not an easy task in xslt.

I hope someone can give me a definite answer to this so that I can at least know what I am up against.

Greetings

Casey

+5
source share
4 answers

( ), , : , .

<xsl:variable name="datetime-punctuation" select="'-.:T'" />
<xsl:variable name="stripped-compare-datetime"
  select="number(translate($compare-datetime, $datetime-punctuation, ''))" />

/records/record[number(translate(@datetime, $datetime-punctuation, ''))
                 > $stripped-compare-datetime)]
+6

, , :

XML:

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="dates.xsl"?>
<records>
    <record datetime="2010-01-04T16:48:15.501-05:00"/>
    <record datetime="2011-01-04T16:48:15.501-05:00"/>
</records>

XSLT:

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:date="http://exslt.org/dates-and-times"
    extension-element-prefixes="date">
    <xsl:import href="date.difference.template.xsl"/>
    <!-- http://exslt.org/date/functions/difference/date.difference.template.xsl -->
    <xsl:output method="xml" indent="yes"/>        

    <xsl:template match="/*">
        <xsl:copy>
            <result1>
                <xsl:call-template name="date:difference">
                   <xsl:with-param name="start" select="record[1]/@datetime"/>
                   <xsl:with-param name="end" select="'2010-04-04T16:48:15.501-05:00'"/>
                </xsl:call-template>
            </result1>
            <result2>
                <xsl:call-template name="date:difference">
                   <xsl:with-param name="start" select="record[2]/@datetime"/>
                   <xsl:with-param name="end" select="'2010-04-04T16:48:15.501-05:00'"/>
                </xsl:call-template>
            </result2>
        </xsl:copy>
    </xsl:template>

</xsl:stylesheet>

:

<records>
    <result1>P90D</result1>
    <result2>-P275D</result2>
</records>

, .

+5

, XSLT 1.0 dateTimes. , , - - FAQ XSLT

http://www.dpawson.co.uk/xsl/rev2/dates.html#d14938e16, XSLT 2.0.

+1

, . # ( .Net XSLT):

<msxsl:script language="C#" implements-prefix="user">
  <![CDATA[
   public bool largerThan(DateTime dt0, DateTime dt1) {
     return dt0 > dt1;
   }
    ]]>
</msxsl:script>

xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:my-scripts"

( xslt)

<xsl:for-each select="../b:post[user:largerThan(@created,$created)]">
0
source

All Articles