0

I have a multiple nodes with a date of the style: 2019-11-16

I want to get the month of theses dates, so far I tried this and some variations on this:

let $DATE:=data(//activity/DATE)
let $month:=substring($DATE, 6, 2)
return $month

But I have an error saying that the cardinality doesn't match the expected parameter,I know its a string related issue, if I insert manually a date like '1999-12-24' it returns the expected value but I need it to read the dates and then return them as indicated in the substring, any ideas? I thought that data("...") returned a string but it seems like it doesnt

1 Answer 1

1

The most likely reason is that the path //activity/DATE selects more than one element, you will need to check that and if so, decide whether you want to select only the first with (//activity/DATE)[1] or head(//activity/DATE), then you can certainly call substring on that one selected element, even without using the data function.

If you want to extract a sequence of month numbers from a sequence of DATE elements you can use //activity/DATE/substring(., 6, 2).

Note however, that your format fits the xs:date data type so using //activity/DATE/xs:date(.) seems more appropriate to deal with dates and then you can exploit the functions on xs:date, //activity/DATE/month-from-date(xs:date(.)): https://www.w3.org/TR/xpath-functions/#func-month-from-date.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, I wanted to iterate through a list of dates, your solution worked, much appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.