XPath query using dates

2

I have the following xml file

<clientes>
    <cliente>
        <nombre>
            Juan Martinez
        </nombre>
        <fecha_inicio>
            2016-10-2
        </fecha_inicio>
    </cliente>
    <cliente>
        <nombre>
            Jose Gonzalez
        </nombre>
        <fecha_inicio>
            2014-8-2
        </fecha_inicio>
    </cliente>
</clientes>

fecha_inicio is the date the customer registered with the company.

And I want an XPath query that returns the names of clients that are more than one year old in the company.

It is something like this:

/clientes/cliente[fecha_inicio > 1 año]/nombre
    
asked by Fredo Martinez 22.11.2016 в 15:03
source

1 answer

3

If you can change the XML to format the dates with two digits both the month and the day, and leave it like this:

<clientes>
    <cliente>
        <nombre>
            Juan Martinez
        </nombre>
        <fecha_inicio>
            2016-10-02
        </fecha_inicio>
    </cliente>
    <cliente>
        <nombre>
            Jose Gonzalez
        </nombre>
        <fecha_inicio>
            2014-08-02
        </fecha_inicio>
    </cliente>
</clientes>

Then you can do this:

/clientes/cliente[(current-date() - xs:date(fecha_inicio)) > xs:dayTimeDuration('P365D') ]

Which in this case returns this:

<cliente>
        <nombre>
            Jose Gonzalez
        </nombre>
        <fecha_inicio>
            2014-08-02
        </fecha_inicio>
    </cliente>
    
answered by 22.11.2016 / 16:56
source