Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8962645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:10:32+00:00 2026-06-15T16:10:32+00:00

Given a list of elements with a date attribute, for example, <foo> <bar date=2001-04-15/>

  • 0

Given a list of elements with a date attribute, for example,

<foo>
 <bar date="2001-04-15"/>
 <bar date="2002-01-01"/>
 <bar date="2005-07-04"/>
 <bar date="2010-11-10"/>
</foo>

I would like to retrieve the element closest to but not after a given date using XSLT.

Calling this function with parameter “2008-01-01” should print <bar date="2005-07-04">. Assume the context node is already <foo>.

I’m not sure what would be easier, but I could also set up three attributes: day, month, year, instead of having one date attribute.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T16:10:33+00:00Added an answer on June 15, 2026 at 4:10 pm

    Here’s an XSLT 2.0 option…

    XML Input

    <foo>
        <bar date="2001-04-15"/>
        <bar date="2005-07-04"/>
        <bar date="2002-01-01"/>
        <bar date="2010-11-10"/>
    </foo>
    

    XSLT 2.0

    <xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema">
        <xsl:output indent="yes"/>
        <xsl:strip-space elements="*"/>
    
        <xsl:param name="threshold" select="xs:date('2008-01-01')"/>
    
        <xsl:template match="@*|node()">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:template>
    
        <xsl:template match="foo">
            <xsl:variable name="closestDate" as="node()*">
                <xsl:apply-templates select="bar[$threshold >= xs:date(@date)]">
                    <xsl:sort select="@date" data-type="text"/>
                </xsl:apply-templates>                  
            </xsl:variable>
            <xsl:copy-of select="$closestDate[last()]"/>
        </xsl:template>
    
    </xsl:stylesheet>
    

    XML Output

    <bar date="2005-07-04"/>
    

    Explanation of “foo” template…

    <xsl:template match="foo">
        <!--First a variable named 'closestDate' is created by doing an 
            'xsl:apply-templates' to all 'bar' elements that have a '@date' 
            attribute that is less than or equal to the 'threshold' parameter 
            (which is '2008-01-01' in the example). Notice that both '@date' 
            and '$threshold' are cast as 'xs:date' so that the date comparison 
            will work correctly. Also, we use the 'as="node()*"' attribute to 
            cast the variable as zero or more nodes() so that each individual 
            'bar' can be accessed individually.-->
        <xsl:variable name="closestDate" as="node()*">
            <xsl:apply-templates select="bar[$threshold >= xs:date(@date)]">
                <!--This 'xsl:sort' is used to put all the 'bar' elements in order 
                    based on the '@date' attribute.-->
                <xsl:sort select="@date" data-type="text"/>
            </xsl:apply-templates>
        </xsl:variable>
        <!--What we end up with for the 'closestDate' variable is this:
                <bar date="2001-04-15"/>
                <bar date="2002-01-01"/>
                <bar date="2005-07-04"/>
            In the following 'xsl:copy-of', we choose the last node 
            in 'closestDate'.-->
        <xsl:copy-of select="$closestDate[last()]"/>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a list of elements like so: int[] ia = new int[] { -4,
Given a list of elements like this: <ul> <li class=favourite></li> <li class=favourite></li> <li class=favourite></li>
Design an algorithm that, given a list of n elements in an array, finds
Given a list like this: num = [1, 2, 3, 4, 5] There are
Given a list of elements, does a shuffling algorithm exist that will guarantee that
Given a list of n comparable elements (say numbers or string), the optimal algorithm
Given a list of elements, how to process all elements if every element requires
Suppose I am given a sorted list of elements and I want to generate
Given a list of elements containing text: <root> <element>text text text ...</element> <element>text text
I have two lists based on the same class. I would like one list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.