Project

Profile

Help

Date conversions difers

Added by Anonymous over 17 years ago

Legacy ID: #3978425 Legacy Poster: Diego von Sohsten (dsohsten)

Hi, I have an example who returns a xs:date back to java as a Date object, but when I use the same Date object back as a parameter of xs:date type to another example it backfires a error, trying to use the string value of the Date and failing because its a description of the date and not a number. Digging the code I saw that in net.sf.saxon.value.Value the "convert(Item item)" method is testing for Type.DATE and Type.DATE_TIME, both returning a Date Object, but in the same class the method "convertToBestFit(Object object, Configuration config)" that is called upon when a parameter needed isn't testing for a Date object. I have two questions about it : 1) Won't be the test and conversion to the right type necessary ? 2) Wich type would be better when a Date Object is found ? net.sf.saxon.value.Date or net.sf.saxon.value.DateTime ? Thanks, Diego.


Replies (6)

Please register to reply

RE: Date conversions difers - Added by Anonymous over 17 years ago

Legacy ID: #3981058 Legacy Poster: Michael Kay (mhkay)

Thanks for raising this. The conversions between XPath types and Java types are never going to be perfect because there simply isn't an exact correspondence. I'll take a look at this case and see if it can be improved. However, it would help me if you could explain rather more precisely what you are trying to do, with some code samples.

RE: Date conversions difers - Added by Anonymous over 17 years ago

Legacy ID: #3982727 Legacy Poster: Diego von Sohsten (dsohsten)

Hi Michael, The samples I got was from the XQTS 1.0(?), I coded a Harness for the Suite to test Saxon-B. At extvardeclwithtype-2 test, I run the extvardeclwithtypetobind-2.xq, It returned a Date object, so I used that Object to run extvardeclwithtype-2.xq then I got the error below : [1026 14:22:58,490 DEBUG extvardeclwithtype-2:getErrorCodeLocalPart:FORG0001 ; SystemID: file:/C:/desenv/XQTS_1_0/Queries/XQuery/Expressions/PrologExpr/VariableProlog/ExternalVariablesWith/extvardeclwithtype-2.xq; Line#: 7; Column#: -1 ValidationException: Invalid date "Fri Dec 31 16:00:00 GMT-03:00 ..." (Non-numeric component) at net.sf.saxon.value.DateValue.badDate(DateValue.java:181) at net.sf.saxon.value.DateValue.setLexicalValue(DateValue.java:176) at net.sf.saxon.value.DateValue.<init>(DateValue.java:64) at net.sf.saxon.value.StringValue.convertStringToBuiltInType(StringValue.java:169) at net.sf.saxon.value.StringValue.convertPrimitive(StringValue.java:98) at net.sf.saxon.value.ObjectValue.convertPrimitive(ObjectValue.java:60) at net.sf.saxon.value.AtomicValue.convert(AtomicValue.java:147) at net.sf.saxon.instruct.Bindery.useGlobalParameter(Bindery.java:118) at net.sf.saxon.instruct.GlobalParam.evaluateVariable(GlobalParam.java:37) at net.sf.saxon.expr.VariableReference.evaluateVariable(VariableReference.java:313) at net.sf.saxon.expr.VariableReference.evaluateItem(VariableReference.java:281) at net.sf.saxon.functions.StringFn.evaluateItem(StringFn.java:30) at net.sf.saxon.expr.ComputedExpression.process(ComputedExpression.java:639) at net.sf.saxon.query.XQueryExpression.run(XQueryExpression.java:337) at testharness_xstq_for_saxon.testsuite.Main.doTest(Main.java:264) at testharness_xstq_for_saxon.testsuite.Main.process(Main.java:129) at testharness_xstq_for_saxon.testsuite.Main.process(Main.java:136) at testharness_xstq_for_saxon.testsuite.Main.process(Main.java:136) at testharness_xstq_for_saxon.testsuite.Main.process(Main.java:136) at testharness_xstq_for_saxon.testsuite.Main.process(Main.java:136) at testharness_xstq_for_saxon.testsuite.Main.main(Main.java:90) ***************************** extvardeclwithtypetobind-2.xq ***************************** (: Name: extvardeclwithtypetobind-2 :) (: Description: Binding a date value for extvardeclwithtype-2.:) xs:date("2000-01-01+05:00") ************************** extvardeclwithtype-2.xq ************************** (: Name: extvardeclwithtype-2 :) (: Description: Evaluates an external variable declaration with type:) (: Use type xs:date. :) declare variable $x as xs:date external; fn:string($x)

RE: Date conversions difers - Added by Anonymous over 17 years ago

Legacy ID: #3982862 Legacy Poster: Michael Kay (mhkay)

OK thanks. I'll see if I can find a way to make this particular example work. However, if you're getting the output of one query to pass to another query, as you are doing here, then I'd recommend using the native Saxon representations of XPath atomic values, in this case the net.sf.saxon.value.DateValue class. That way you're guaranteed no loss of information on round-tripping. This is the form that you will get if you use the XQueryExpression.iterate() method to retrieve the query results. Michael Kay

RE: Date conversions difers - Added by Anonymous over 17 years ago

Legacy ID: #3983563 Legacy Poster: Michael Kay (mhkay)

I've now added code that will recognize a java.util.Date and convert it to an xs:dateTime in timezone +00:00. There's scope in the design, at least on some paths, to take account of the required type, so when passing a parameter with a known expected type one could convert instead to an xs:date. But there are still limitations, because a java.util.Date contains no timezone information. This is why JAXB introduces its own representations of some of the XML Schema types in the javax.xml.datatype package. I'd like to move towards supporting those by default (I already do this in the XQJ interface), but unfortunately they're not present in JDK 1.4. Have you considered using the XQJ interface for your driver? Michael Kay

RE: Date conversions difers - Added by Anonymous over 17 years ago

Legacy ID: #3984113 Legacy Poster: Diego von Sohsten (dsohsten)

In fact, I confess, I didn't knew about XQJ, but I was really feeling that some api definition was lacking, that way the Suite can be customized to test any XSLT processor in java. The Suite Harness that I implemented is strongly tied to Saxon. I did it to learn XSLT/XQuery and test Saxon-B to use on a project. The code to process the Suite, at the moment, is a mess, I will clean it up and if you want I can send it to you. It parses the XML, customize the test(modules, variables, subqueries etc), run and test the results. And the last but not the least my congratulations, Saxon is an awesome product. Diego.

RE: Date conversions difers - Added by Anonymous over 17 years ago

Legacy ID: #3984156 Legacy Poster: Michael Kay (mhkay)

Until recently I've been using an XQTS test harness written in XSLT, but I've recently switched over to a new one written in Java. It's not complete yet, but close enough that I'll stick with it. But thanks for the offer anyway!

    (1-6/6)

    Please register to reply