Creating a xslt transformation based based on effective dates and sequence

孤人 提交于 2021-02-11 12:17:17

问题


I have the following xml file. I need to be able to generate a csv file based on below. However the catch is if its the same effective moment and there are multiple sequence attributes it should generate just one line of output, however if the effective moment (just date part) is different then it should generate a line each based on the effective moment. I have trying to play with xslt but have been struggling so far. Any help provided will be appreciated.

XML file

    <?xml version="1.0" encoding="UTF-8"?>
<peci:Workers_Effective_Stack xmlns:peci="urn:com.w/peci">
<peci:Worker>
    <peci:Summary>
        <peci:Integration_Event>14d964101fdb0156e164341b45548d01</peci:Integration_Event>
        <peci:Integration_Sent_On>2018-05-10T08:41:39.375-07:00</peci:Integration_Sent_On>
        <peci:Payroll_Company_Name>West Brooke - Biweekly: B</peci:Payroll_Company_Name>
        <peci:Pay_Group_Country>US</peci:Pay_Group_Country>
        <peci:Pay_Group_Code>j</peci:Pay_Group_Code>
        <peci:Pay_Period_Start>2018-04-30-07:00</peci:Pay_Period_Start>
        <peci:Pay_Period_End>2018-05-13-07:00</peci:Pay_Period_End>
        <peci:Updated_From>2018-05-07T19:50:30.963-07:00</peci:Updated_From>
        <peci:Updated_To>2018-05-10T08:41:39.375-07:00</peci:Updated_To>
        <peci:Effective_From>2018-04-30-07:00</peci:Effective_From>
        <peci:Effective_To>2018-05-13-07:00</peci:Effective_To>
        <peci:Change_Detection>Include only changes entered since the last successful run.</peci:Change_Detection>
        <peci:First_Primary_Run_of_Pay_Period>0</peci:First_Primary_Run_of_Pay_Period>
        <peci:Ad_Hoc_Run>0</peci:Ad_Hoc_Run>
        <peci:Full_Snapshot>0</peci:Full_Snapshot>
        <peci:Worker_Count>2</peci:Worker_Count>
        <peci:Version>1</peci:Version>
    </peci:Summary>
    <peci:Worker>
        <peci:Worker_Summary>
            <peci:WID>rd</peci:WID>
            <peci:Employee_ID>33333</peci:Employee_ID>
            <peci:Name>Test 1</peci:Name>
        </peci:Worker_Summary>
        <peci:Effective_Change peci:Sequence="0">
            <peci:Derived_Event_Code>DTA</peci:Derived_Event_Code>
            <peci:Effective_Moment>2018-05-10T08:29:21.235-07:00</peci:Effective_Moment>
            <peci:Entry_Moment>2018-05-10T08:29:21.235-07:00</peci:Entry_Moment>
            <peci:Worker_Status>
                <peci:Status>Active</peci:Status>
                <peci:Active>1</peci:Active>
                <peci:Active_Status_Date>2018-01-08-08:00</peci:Active_Status_Date>
                <peci:Terminated>0</peci:Terminated>
                <peci:Hire_Date>2018-01-08-08:00</peci:Hire_Date>
                <peci:Original_Hire_Date>2018-01-08-08:00</peci:Original_Hire_Date>
                <peci:Is_Rehire>0</peci:Is_Rehire>
                <peci:Continuous_Service_Date>2018-01-08-08:00</peci:Continuous_Service_Date>
                <peci:First_Day_of_Work>2018-01-08-08:00</peci:First_Day_of_Work>
                <peci:Seniority_Date>2018-01-08-08:00</peci:Seniority_Date>
            </peci:Worker_Status>
        </peci:Effective_Change>
        <peci:Effective_Change peci:Sequence="1">
            <peci:Derived_Event_Code>DTA</peci:Derived_Event_Code>
            <peci:Effective_Moment>2018-05-10T08:31:27.880-07:00</peci:Effective_Moment>
            <peci:Entry_Moment>2018-05-10T08:31:27.880-07:00</peci:Entry_Moment>
            <peci:Worker_Status>
                <peci:Status>Active</peci:Status>
                <peci:Active>1</peci:Active>
                <peci:Active_Status_Date>2018-01-08-08:00</peci:Active_Status_Date>
                <peci:Terminated>0</peci:Terminated>
                <peci:Hire_Date>2018-01-08-08:00</peci:Hire_Date>
                <peci:Original_Hire_Date>2018-01-08-08:00</peci:Original_Hire_Date>
                <peci:Is_Rehire>0</peci:Is_Rehire>
                <peci:Continuous_Service_Date>2018-01-08-08:00</peci:Continuous_Service_Date>
                <peci:First_Day_of_Work>2018-01-08-08:00</peci:First_Day_of_Work>
                <peci:Seniority_Date>2018-01-08-08:00</peci:Seniority_Date>
            </peci:Worker_Status>
        </peci:Effective_Change>
                <peci:Effective_Change peci:Sequence="2">
            <peci:Derived_Event_Code>DTA</peci:Derived_Event_Code>
            <peci:Effective_Moment>2018-05-12T08:29:21.235-07:00</peci:Effective_Moment>
            <peci:Entry_Moment>2018-05-12T08:29:21.235-07:00</peci:Entry_Moment>
            <peci:Worker_Status>
                <peci:Status>Active</peci:Status>
                <peci:Active>1</peci:Active>
                <peci:Active_Status_Date>2018-01-08-08:00</peci:Active_Status_Date>
                <peci:Terminated>0</peci:Terminated>
                <peci:Hire_Date>2018-01-08-08:00</peci:Hire_Date>
                <peci:Original_Hire_Date>2018-01-08-08:00</peci:Original_Hire_Date>
                <peci:Is_Rehire>0</peci:Is_Rehire>
                <peci:Continuous_Service_Date>2018-01-08-08:00</peci:Continuous_Service_Date>
                <peci:First_Day_of_Work>2018-01-08-08:00</peci:First_Day_of_Work>
                <peci:Seniority_Date>2018-01-08-08:00</peci:Seniority_Date>
            </peci:Worker_Status>
        </peci:Effective_Change>
    </peci:Worker>
</peci:Workers_Effective_Stack>

Desired Output: 3333,2018-10-05,Active 3333,2018-10-10,Active


回答1:


Assuming that:

  • you can use XSLT 2.0,
  • the output should contain separate row for each Effective_Moment (date part), for each employee,

you can do the task the following way:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:peci="urn:com.w/peci" xpath-default-namespace="urn:com.w/peci">
  <xsl:output method="text" encoding="UTF-8"/>

  <xsl:template match="Workers_Effective_Stack">
    <xsl:for-each-group select="Worker/Effective_Change" group-by=
      "concat(../Worker_Summary/Employee_ID, substring(Effective_Moment,1,10))">
      <xsl:value-of select="../Worker_Summary/Employee_ID"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="substring(Effective_Moment, 1, 10)"/>
      <xsl:text>,</xsl:text>
      <xsl:value-of select="Worker_Status/Status"/>
      <xsl:text>&#xA;</xsl:text>
    </xsl:for-each-group>
  </xsl:template>
</xsl:transform>

Caution: Your source XML has one unbalanced peci:Worker opening tag, I deleted them.

Note: It is possible to perform grouping solely by the effective date, but I think is not what you want. If your input contained a number of effective changes for different employees, but on the same effective date, then the output would have contained only one record from this group. This is why I performed grouping using a composite key, including also the employee ID.



来源:https://stackoverflow.com/questions/50284159/creating-a-xslt-transformation-based-based-on-effective-dates-and-sequence

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!