user1955411
user1955411

Reputation: 13

XSLT counting elements containing a date that is less than another date

I am trying to generate a .csv from an xml file which includes a count of "OtherEmployees" with a date of birth prior to a date (e.g. 01/01/1970) and I am unsure of the syntax.

Note that the dates are currently in dd/mm/yyyy format.

This is an example of the xml -

<Companies>
<Company>
    <CompanyReference>00000060</CompanyReference>
    <Contact>
        <PersonID>63</PersonID>
        <Title>Mrs</Title>
        <Forename>EXAMPLE</Forename>
        <Middlename/>
        <Surname>NAME</Surname>
        <DOB>27/05/1928</DOB>
    </Contact>
    <OtherEmployees>
        <OtherEmployee>
            <PersonID>28870</PersonID>
            <Title>Miss</Title>
            <Forename>EXAMPLE</Forename>
            <Middlename/>
            <Surname>NAME2</Surname>
            <DOB>03/05/1953</DOB>
        </OtherEmployee>
        <OtherEmployee>
            <PersonID>28871</PersonID>
            <Title>Miss</Title>
            <Forename>EXAMPLE</Forename>
            <Middlename/>
            <Surname>NAME3</Surname>
            <DOB>11/07/1961</DOB>
        </OtherEmployee>
    </OtherEmployees>
</Company>

I was able to get as far as a count of OtherEmployees with a date of birth with the following -

<xsl:value-of select="count(OtherEmployees/OtherEmployee[DOB])"/>

However I now need to compare the DOB with another date - say 01/01/1970 and only return the OtherEmployee in the count if they have a date of birth before 01/01/1970.

Upvotes: 1

Views: 653

Answers (1)

Martin Honnen
Martin Honnen

Reputation: 167696

With XSLT 2.0 you can make use of the xs:date data type e.g.

<xsl:value-of select="count(OtherEmployees/OtherEmployee[DOB and xs:date(concat(substring(DOB, 7), '-', substring(DOB, 4, 2), '-', substring(DOB, 1, 2))) lt xs:date('1970-01-01')])"/>

With XSLT 1.0 you can easily convert your data format dd/mm/yyyy to a number yyyymmdd and compare on that:

<xsl:value-of select="count(OtherEmployees/OtherEmployee[DOB and number(concat(substring(DOB, 7), substring(DOB, 4, 2), substring(DOB, 1, 2))) &lt; 19700101])"/>

Upvotes: 2

Related Questions