JDK-6937964 : XML Duration do not conform to W3C specifications
  • Type: Bug
  • Component: xml
  • Sub-Component: jaxp
  • Affected Version: 6u18
  • Priority: P2
  • Status: Closed
  • Resolution: Fixed
  • OS: solaris_10
  • CPU: sparc
  • Submitted: 2010-03-24
  • Updated: 2012-06-08
  • Resolved: 2010-07-22
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other JDK 6 JDK 7
1.4.0Fixed 6u20-rev b09Fixed 7Fixed
Description
XML DURATIONS DO NOT CONFORM TO W3C SPECIFICATIONS (Oracle Bug 9477446 )
Expected output:
Answer should be in canonical form, i.e. 21 years, 3 months.
Actual output: 
20 years, 15 months.

This appears to be a bug in javax.xml.datatype.Duration or DatatypeFactory.

JDK: 6u18-b09 (latest)

/* test case */

import javax.xml.datatype.*;

public class Bug {
 public static void main(String[] args) throws DatatypeConfigurationException {
  DatatypeFactory dtf = DatatypeFactory.newInstance();
  Duration d = dtf.newDuration("P20Y15M");
  System.out.println(d.getYears()==21 ? "pass" : "fail");
 }
}

Comments
EVALUATION As discussed previouly, durations created using newDurationYearMonth and newDurationDayTime methods should comply with the W3C specs as defined in the Java API document. The impl for the base type (Duration) should be left as it is since there's risk of regression changing it (e.g. examine the input to determine if subtypes may be used).
09-04-2010

EVALUATION I agree that the two subtypes have defined Canonical representation, and total order. However, the rules for subtypes can not be applied back to their base type. As in the W3C spec, Duration [1] does not define canonical representation. That said, DatatypeFactory does have newDurationYearMonth and newDurationDayTime methods that are defined as the XQuery/XPath specs. I expected that a duration created with these methods would return normalized. Unfortunately, it did not in my test (Duration d0 = dtf.newDurationYearMonth("P20Y15M");). That gave me a second thought, and I think I might understand what you're expecting, that is, even with a general representation, the implementation could have been made smart enough to detect that the input conforms to a defined subtype and therefore behaves accordingly. Would this change cause regression? It's possible. But the newDurationYearMonth methods are wrong. [1] http://www.w3.org/TR/2001/REC-xmlschema-2-20010502/#duration
09-04-2010

EVALUATION The spec for Duration does not define a canonical lexical representation. Such operations would require presence of a calendar since some values, for example 60 days, are indeterminate.
08-04-2010