-1

How do I compare the year of a given date to int?

<if test="year(@varDate)>=1900">
true
</if>

Where the format of the date in the input file is:

<date varDate="31/12/1999" />
Abel
  • 56,041
  • 24
  • 146
  • 247
Rod
  • 14,529
  • 31
  • 118
  • 230
  • Please show us the XML input where the date is. – michael.hor257k Sep 21 '15 at 15:05
  • You tagged your question [tag:xslt-1.0], but your code shows `xs:gYear`, which is a constructor function of XSLT 2.0, did you mean to tag as [tag:xslt-2.0]? – Abel Sep 21 '15 at 15:06
  • Please show us the XML input where the date is and indicate whether using XSLT 1.0 or 2.0 (makes a big difference in this case). – michael.hor257k Sep 21 '15 at 15:14
  • ok, I'm trying to figure out how to determine what version I'm using and will report back. – Rod Sep 21 '15 at 15:17
  • 1
    "*how to determine what version I'm using*" http://stackoverflow.com/questions/25244370/how-can-i-check-which-xslt-processor-is-being-used-in-solr/25245033#25245033 – michael.hor257k Sep 21 '15 at 15:19
  • 1
    You can find out what version your processor support by using `system-property('xsl:version')`, or by simply creating an XSLT 2.0 construct, i.e., set the version attribute to 2.0 and try to add `use-when="true()"` to any of your existing instructions. If you receive an error, you are on XSLT 1.0 (some XSLT 1.0 processors ignore the version attribute). – Abel Sep 21 '15 at 15:20
  • thank you, got the version and updated the tags. I'm using Microsoft. Ver. 1.0 – Rod Sep 21 '15 at 15:25
  • Good. Well, all you need to do is to extract the year-part, then you can compare it. – Abel Sep 21 '15 at 15:26

2 Answers2

1

If you are stuck with XSLT 1.0, the most common way is to extract the year-part, like so:

substring-before('1994-12-31', '-')

Or, if your format is, let's say dd/MM/yyyy, you can do1:

substring-after('31/12/1999', '/')

In your case that would be something along those lines:

<xsl:if test="substring-after('31/12/1999', '/') > 1900">
    true
</xsl:if>

Since you didn't provide input data, I assume you can work out the details yourself. Your original question was tagged , but this has since changed. If that means that you can use XSLT 2.0 or higher, and you have an actual typed xs:date value, you can also use fn:year-from-date:

<xsl:if test="year-from-date(xs:date('1994-12-31')) > 1900">
    true
</xsl:if>

1 This is not enough as there are two slashes, use substring-after(substring-after('31/12/1999', '/'), '/'), as seen in Michael's post (credit to him).

Community
  • 1
  • 1
Abel
  • 56,041
  • 24
  • 146
  • 247
1

it turns out my date is in a different format that in solution above (dd/mm/yyyy).

If that's really your date format - i.e.both days and month are always padded to two digits - you can use:

<if test="substring(@varDate, 7) >= 1900">

as your test.

If not, you'll need:

<if test="substring-after(substring-after(@varDate, '/'), '/') >= 1900">
michael.hor257k
  • 113,275
  • 6
  • 33
  • 51