|
[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message] count all the distinct timezones in an XMLdocumentusing XPathMatthew Rawlings matthew at stickledown.comFri Dec 12 13:05:05 PST 2008
Thanks to David Carlisle and Mike Kay I found my mistakes and solved the problem. The final form of the XPath I used is below: count(distinct-values(//((element(*, xs:date)|attribute(*, xs:date))/timezone-from-date(.),(element(*, xs:dateTime)|attribute(*, xs:dateTime))/timezone-from-dateTime(.)))) eq 1 I am glad I can express it in pure XPath rather than XQuery as then I can use it in a xs:assert in a schema written in XML Schema 1.1. I agree with Mike's point about polymorphism - why can't timezone-from-date and timezone-from-dateTime be the one function with either a xs:date or xs:dateTime parameter. Both functions return a xs:duration and do the same thing. It would make the XPath much simpler. _____ From: http://x-query.com/mailman/listinfo/talk [mailto:http://x-query.com/mailman/listinfo/talk] On Behalf Of Michael Kay Sent: 12 December 2008 11:48 To: 'Matthew Rawlings'; http://x-query.com/mailman/listinfo/talk Subject: RE: count all the distinct timezones in an XMLdocumentusing XPath The nearest to a map() function is the "for" expression count(distinct-values(for $x in //element(*, xs:date) return timezone-from-date($x), for $x in //element(*, xs:dateTime) return timezone-from-dateTime($x), for $x in //element(*, xs:time) return timezone-from-time($x))) eq 1 But there is also the "simple mapping operator" "/" - //element(*,xs:date)/timezone-from-date(.) It's rather tedious that there's no polymorphism (or support of union types) so that the different date/time types have to be enumerated like this. Note that if a dateTime has no timezone, timezone-from-dateTime() will return (), which is ignored in counting the distinct values. Michael Kay http://www.saxonica.com/ _____ From: http://x-query.com/mailman/listinfo/talk [mailto:http://x-query.com/mailman/listinfo/talk] On Behalf Of Matthew Rawlings Sent: 12 December 2008 11:27 To: http://x-query.com/mailman/listinfo/talk Subject: count all the distinct timezones in an XML documentusing XPath How can I count all the distinct timezones in a single XML document using XPath? I have an XML document with many elements and attributes with the type xs:date or xs:datetime. One of the rules of the system receiving my document is that all timezones must be the same in the document. Ideally I would like to express this constraint purely in XPath 2 rather than XQuery so I can put the constraint in a xs:assert statement. If there was a map function in XPath I would write something like this: count(distinct-values(map(fn:date-from-timezone(), //(element(*, xs:date)|attribute(*, xs:date)|element(*, xs:datetime)|attribute(*, xs:datetime))))) eq 1 This XPath would return true if all timezones were the same, and false otherwise. However there is no map function in XPath, so how can I achieve the same thing without it? What is the nearest I could get. - Matthew -------------- next part -------------- An HTML attachment was scrubbed... URL: http://x-query.com/pipermail/talk/attachments/20081212/8b4ef258/attachment-0001.htm
|
PURCHASE STYLUS STUDIO ONLINE TODAY!Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced! Download The World's Best XML IDE!Accelerate XML development with our award-winning XML IDE - Download a free trial today! Subscribe in XML format
|






