'How to resolve the dateTime casting error in MarkLogic import

I am getting this error while casting a value from a XML element into MarkLogic column using a template for the extraction of the data.

22/05/12 10:22:17 WARN mapreduce.ContentWriter: Batch 1009629724.0: TDE-INDEX: Error applying template /powerbi/shipment-CBE123.xml to document /powerbi/shipment/CBE00038464N/C:/Users/admin-rp/Documents/Marklogic/ML_With_PowerBI/bi-tools-master/bi-tools-master/power-bi/marklogic_powerbi_tutorial/data/CBE00038464N.xml: TDE-EVALFAILED: Eval for Column POD_ETA_Act_Arr='ns10:ASN/ns10:Schedule/ns5:TransportationUnitHeader/ns5:PrimarytUnit/ns5:TransportUnit/ns5:PortOfDischarge/ns9:ActualArrivalDate' returns XDMP-CAST: (err:FORG0001) Invalid cast: xs:untypedAtomic("") cast as xs:dateTime

The element value for the field ActualArrivalDate is like this in the XML file -

<ns5:PortOfDischarge>
  <ns9:PortName />
  <ns9:PortCode />
  <ns9:ScheduledArrivalDate xsi:nil="true" />
  <ns9:ActualArrivalDate xsi:nil="true" />
  <ns9:ScheduledDepartureDate xsi:nil="true" />
  <ns9:ActualDepartureDate xsi:nil="true" />
</ns5:PortOfDischarge>

And my template form for this filed looks like this -

<column>
  <name>POD_ETA_Act_Arr</name>
  <scalar-type>dateTime</scalar-type><val>ns10:ASN/ns10:Schedule/ns5:TransportationUnitHeader/ns5:PrimarytUnit/ns5:TransportUnit/ns5:PortOfDischarge/ns9:ActualArrivalDate</val>
  <nullable>true</nullable>
</column>


Solution 1:[1]

The casting error:

XDMP-CAST: (err:FORG0001) Invalid cast: xs:untypedAtomic("") cast as xs:dateTime

is telling you that the untyped atomic value of an empty string "" cannot be cast to an xs:dateTime.

One way to avoid this might be to adjust the XPath in the template to exclude those that don't have a value. For instance, adding a predicate to ensure that the value is castable as xs:dateTime: [. castable as xs:dateTime] or just filtering out elements with empty or whitespace-only values [normalize-space()].

I'm a little surprised that it complains since your <ns9:ActualArrivalDate xsi:nil="true" /> data element has the @xsi:nil="true" and the TDE column has <nullable>true</nullable>. I would expect that it would "see" that element as having a null value, rather than an empty string. However, I think you may need to have your schemas loaded into the Schemas database indicating that the particular element can be nillable, or it won't see it as null.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1