Ammu1991
Ammu1991

Reputation: 5

Remove seconds from dateTime in XQUERY

So i have a dateTime like "2022-06-25T03:00:00+02:00" and i have to just remove the seconds and retain everything else.

eg: "2022-06-25T03:00+02:00"

Below is the input xml, i have to tranform the data to match the eg: that i ahve mentioned above.

Could someone please help? Note: I am new to XQuery, so please tell me what is that your xquery doing too.

Input XML

<?xml version="1.0" encoding="UTF-8"?>
<Message xsi:schemaLocation="http://abcd.ch http://abcd.ch/ns/Message.xsd" xmlns="http://abcd.ch/ns/Message" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
 <messageId>4894954fyfuguyutfu_98798_98798</messageId>
 <messageType>Message</messageType>
 <messageDate>2022-06-23T12:49:11+02:00</messageDate>
</Message>

Expected output XML

<?xml version="1.0" encoding="UTF-8"?>
<Nomination xmlns="http://www.example.com" Release="1" Version="EGAS40">
    <Identification v="ABC123"/>
    <Type v="55G"/>
    <messageDate v="2022-06-23T12:49+02:00"/>
</Nomination>

Upvotes: 0

Views: 252

Answers (2)

zx485
zx485

Reputation: 29022

The easiest approach is using fn:replace with RegEx:

replace(xs:string($file/dateTimeValue),'(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}):\d{2}(\+\d{2}:\d{2})','$1$2')

Result is

2022-06-25T03:00+02:00

You could not convert the resulting string back to an xs:dateTime because, well, it would be wrong format (obviously).

Upvotes: 0

Joe Wicentowski
Joe Wicentowski

Reputation: 5294

Assuming your XQuery processor supports XQuery 3.0, you could use the format-dateTime() function with a picture string that excludes the seconds:

let $date := "2022-06-23T12:49:11+02:00"
return
    $date
    => xs:dateTime()
    => format-dateTime("[Y0001]-[M01]-[D01]T[h01]:[m01][Z]")

This returns "2022-06-23T12:49+02:00".

If you're not familiar with =>, see Arrow operator.

All of these links point to the current version of the official XQuery specification, XQuery 3.1.

Upvotes: 1

Related Questions