简体   繁体   中英

Using substring with XQUERY

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM