Today I was again totally amazed by the power of xpath when working on a SAP HR driver. The SAP system publishes data as IDocs, which are basically textfiles where every line contains one record, with the value fields defined by their position in the line.
Funny enough every line can be different in structure, which only works because the first part in every line gives a hint on how to decode the rest. I think I've read that even SAP has started using XML for data exchange nowadays, nevertheless, the SAP HR driver still has to deal with traditional IDocs.
I was looking for a way to obtain an employee's exit date, which is not directly available as an IDoc field but needs to be calculated as the day before the begin of the validity period of the action record of type "10" with the highest validity end date. Know what I mean?
Let's look at the first lines of an example IDoc for workforceID 1234:
EDI_DC40 1000000000000123456700 3012 HRMD_A07...
E2PLOGI001 10000000000001234560716140000000201P 00001234 U
E2PITYP001 10000000000001234560716150716140301P 000012340000 1800010199991231
E2P0000001 100000000000012345607161607161504000012340000 200112312000011700020000125SOMEONE 01 031
E2P0000001 100000000000012345607161707161504000012340000 200211302002010100020020121SOMEONE 02 031
E2P0000001 100000000000012345607161807161504000012340000 200312312002120100020021122SOMEONE 02 031
E2P0000001 100000000000012345607161707161504000012340000 200411302004010100020020121SOMEONE 10 000
E2P0000001 100000000000012345607161807161504000012340000 200412312004120100020021122SOMEONE 12 031
E2P0000001 100000000000012345607161907161504000012340000 200712312005010100020040719SOMEONE 02 031
E2P0000001 100000000000012345607162007161504000012340000 999912312008010100020080123SOMEONE 1017031
The lines starting with "E2P0000001" are all action records and the last line here is the record showing the relevant exit action. The first two char blocks in that line are mostly header info, the third one is a concatenation of validity end date, validity begin date, sequence number, modification date and modifiers name, and the first two chars of last block give the action type "10" (take a look at Geoffrey's excellent article about IDoc structures if you want your brain twisted a bit: Decoding iDOCs with the IDM SAP Driver).
When I map the driver's action type attribute (MASSN at position 74 with field length 2 in InfoType 0000 for the initiated ;-) against our custom string attribute auxEmployeeExitDate and put it into the driver publisher filter, I end up seeing it in a trace coming in like this:
<modify-attr attr-name="P0000:MASSN:none:74:2">
<remove-all-values/>
<add-value>
<value seqnr="000" timestamp="19990117-20001231" type="string">01</value>
<value seqnr="000" timestamp="20020101-20021130" type="string">02</value>
<value seqnr="000" timestamp="20021201-20031231" type="string">02</value>
<value seqnr="000" timestamp="20040101-20041130" type="string">10</value>
<value seqnr="000" timestamp="20041201-20041231" type="string">12</value>
<value seqnr="000" timestamp="20050101-20071231" type="string">02</value>
<value seqnr="000" timestamp="20080101-99991231" type="string">10</value>
</add-value>
</modify-attr>
Again: the exit date I am looking for is the day before the begin of the validity period (the part before the hyphen in @timestamp) of the value that is "10". If there are more than one of these values (in case someone got hired