I need to change the XML file where peci:Organization is a repeated child node.
I need to make change that if <peci:Organization_Type = 'Alternate_Cost_Center' is present in file, then change this to <peci:Organization_Type = 'Cost_Center' and change <peci:Organization_Type = 'Cost_Center' to <peci:Organization_Type = 'Dummy_Cost_Center'.
I need to do this only when <peci:Organization_Type = 'Alternate_Cost_Center'
is present in file for an employee.
I have been trying below code:
-----enter code here---
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wd="urn:com.workday/bsvc"
xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions"
xmlns:peci="urn:com.workday/peci" xmlns:this="urn:this-stylesheet"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xdiff="urn:com.workday/esb/xdiff"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:op="http://www.w3.org/2005/xpath-functions" version="2.0"
exclude-result-prefixes="xsl wd this xsd op xdiff peci xsi is">
<xsl:output indent="yes" method="xml" />
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="peci:Organization_Type">
<xsl:copy>
<xsl:choose>
<xsl:when test=". = 'Alternate_Cost_Center'">
<xsl:value-of select="'Cost_Center'" />
</xsl:when>
<xsl:when test=". = 'Cost_Center'">
<xsl:value-of select="'Dummy_Cost_Center'" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="." />
</xsl:otherwise>
</xsl:choose>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
**Example XML:**
<?xml version="1.0" encoding="UTF-8"?>
-<peci:Worker_Effective_Stack_Aggregate xmlns:peci="urn:com.workday/peci">
-<peci:Workers_Effective_Stack>
-<peci:Summary> <peci:Integration_Event>da1c439dcf86011a442a9562d9960000</peci:Integration_Event>
<peci:Integration_Sent_On>20211121T07:52:23</peci:Integration_Sent_On> <peci:Payroll_Company_ID>Payroll_Company_Jabil_Switzerland</peci:Payroll_Company_ID>
<peci:Payroll_Company_Name>Jabil - Switzerland</peci:Payroll_Company_Name>
<peci:Payroll_Company_Code>5200</peci:Payroll_Company_Code>
<peci:Pay_Group_ID>C1</peci:Pay_Group_ID>
<peci:Pay_Group_Name>CHE - Monthly - 5200</peci:Pay_Group_Name>
<peci:Pay_Group_Country>CH</peci:Pay_Group_Country>
<peci:Pay_Group_Code>C1</peci:Pay_Group_Code>
<peci:Pay_Period_Start>20211101</peci:Pay_Period_Start>
<peci:Pay_Period_End>20211130</peci:Pay_Period_End>
<peci:Updated_From>20210601T10:25:54</peci:Updated_From>
<peci:Updated_To>20211121T07:52:23</peci:Updated_To>
<peci:Effective_From>20211101</peci:Effective_From>
<peci:Effective_To>20211130</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>1</peci:Ad_Hoc_Run>
<peci:Full_Snapshot>0</peci:Full_Snapshot>
<peci:Worker_Count>1</peci:Worker_Count>
<peci:Version>1</peci:Version>
</peci:Summary>
-<peci:Worker>
-<peci:Worker_Summary>
<peci:WID>4d73f467a8440137745f162288014b40</peci:WID>
<peci:Employee_ID>3062905</peci:Employee_ID>
<peci:Name>Anna Conti</peci:Name>
</peci:Worker_Summary>
-<peci:Effective_Change peci:Sequence="0">
<peci:Derived_Event_Code>DTA</peci:Derived_Event_Code>
<peci:Effective_Moment>19000101T00:00:00</peci:Effective_Moment>
<peci:Entry_Moment>20211109T10:17:13</peci:Entry_Moment>
-<peci:Position peci:isUpdated="1">
<peci:Position_ID>P3341022</peci:Position_ID>
<peci:Position_Time_Type>Full_time</peci:Position_Time_Type>
<peci:Job_Profile>51016106</peci:Job_Profile>
-<peci:Supervisor>
<peci:ID>2115831</peci:ID>
<peci:Name>Damian Gruber</peci:Name>
</peci:Supervisor>
-<peci:Organization>
<peci:Organization_Type>Cost_Center</peci:Organization_Type>
<peci:Organization_Code>30900498</peci:Organization_Code>
<peci:Organization_Name>General Above Site</peci:Organization_Name>
</peci:Organization>
-<peci:Organization>
<peci:Organization_Type>Alternate_Cost_Center</peci:Organization_Type>
<peci:Organization_Code>31020420</peci:Organization_Code>
<peci:Organization_Name>JJMD - Finance Europe</peci:Organization_Name>
</peci:Organization>
-<peci:Organization peci:isUpdated="1">
<peci:Organization_Type>Pay_Group</peci:Organization_Type>
<peci:Organization_Code peci:isAdded="1">C1</peci:Organization_Code>
<peci:Organization_Name>CHE - Monthly -5200</peci:Organization_Name>
</peci:Organization>
-<peci:Organization>
<peci:Organization_Type>Supervisory</peci:Organization_Type>
<peci:Organization_Code>2202783</peci:Organization_Code>
<peci:Organization_Name>Financial Planning Analysis</peci:Organization_Name>
</peci:Organization>
-<peci:Organization>
<peci:Organization_Type>Company</peci:Organization_Type>
<peci:Organization_Code>5200</peci:Organization_Code> <peci:Organization_Name>Jabil Switzerland Manufacturing GmbH</peci:Organization_Name>
</peci:Organization>
<ptdf:HR_Admin xmlns:ptdf="urn:com.workday/peci/tdf">N</ptdf:HR_Admin>
<ptdf:Position_Name xmlns:ptdf="urn:com.workday/peci/tdf">Sr. Financial Analyst</ptdf:Position_Name>
<ptdf:Job_Profile_Name xmlns:ptdf="urn:com.workday/peci/tdf">Sr. Financial Analyst</ptdf:Job_Profile_Name>
<ptdf:Employee_Group xmlns:ptdf="urn:com.workday/peci/tdf">1</ptdf:Employee_Group>
<ptdf:Employee_Subgroup xmlns:ptdf="urn:com.workday/peci/tdf">C1</ptdf:Employee_Subgroup>
<ptdf:Personnel_Area xmlns:ptdf="urn:com.workday/peci/tdf">C210</ptdf:Personnel_Area>
<ptdf:Personnel_SubArea xmlns:ptdf="urn:com.workday/peci/tdf">5204</ptdf:Personnel_SubArea>
<ptdf:Business_Area xmlns:ptdf="urn:com.workday/peci/tdf">5204</ptdf:Business_Area>
<ptdf:Work_Schedule xmlns:ptdf="urn:com.workday/peci/tdf">S540000</ptdf:Work_Schedule>
</peci:Position>
-<peci:Additional_Information>
<peci:WorkerID>4d73f467a8440137745f162288014b40</peci:WorkerID>
</peci:Additional_Information>
</peci:Effective_Change>
-<peci:Effective_Change peci:Sequence="1">
<peci:Derived_Event_Code>HIR-R</peci:Derived_Event_Code>
<peci:Effective_Moment>20210801T00:00:00</peci:Effective_Moment>
<peci:Entry_Moment>20210818T02:26:42</peci:Entry_Moment>
-<peci:Worker_Status>
<peci:Status>Active</peci:Status>
<peci:Active>0</peci:Active>
<peci:Active_Status_Date>20210801</peci:Active_Status_Date>
<peci:Terminated>0</peci:Terminated>
<peci:Hire_Date>20210801</peci:Hire_Date>
<peci:Original_Hire_Date>20210816</peci:Original_Hire_Date> <peci:Hire_Reason>General_Event_Subcategory_New_Hire_Replacement</peci:Hire_Reason>
<peci:Continuous_Service_Date>20210801</peci:Continuous_Service_Date>
<peci:First_Day_of_Work>20210801</peci:First_Day_of_Work>
<peci:Seniority_Date>20210816</peci:Seniority_Date>
</peci:Worker_Status>
-<peci:Additional_Information>
<peci:WorkerID>4d73f467a8440137745f162288014b40</peci:WorkerID>
</peci:Additional_Information>
</peci:Effective_Change>
<peci:Effective_Change peci:Sequence="2">
<peci:Derived_Event_Code>HIR</peci:Derived_Event_Code>
<peci:Effective_Moment>20210816T00:00:00</peci:Effective_Moment>
<peci:Entry_Moment>20210818T02:26:42</peci:Entry_Moment>
-<peci:Worker_Status peci:isUpdated="1">
<peci:Status>Active</peci:Status>
<peci:Active peci:priorValue="0">1</peci:Active>
<peci:Active_Status_Date peci:priorValue="20210801">20210816</peci:Active_Status_Date>
<peci:Terminated>0</peci:Terminated>
<peci:Hire_Date peci:priorValue="20210801">20210816</peci:Hire_Date>
<peci:Original_Hire_Date peci:priorValue="20210801">20210816</peci:Original_Hire_Date> <peci:Hire_Reason>General_Event_Subcategory_New_Hire_Replacement</peci:Hire_Reason>
<peci:Continuous_Service_Date peci:priorValue="20210801">20210816</peci:Continuous_Service_Date>
<peci:First_Day_of_Work peci:priorValue="20210801">20210816</peci:First_Day_of_Work>
<peci:Seniority_Date peci:priorValue="20210801">20210816</peci:Seniority_Date>
</peci:Worker_Status>
</peci:Effective_Change>
</peci:Worker>
</peci:Workers_Effective_Stack>
</peci:Worker_Effective_Stack_Aggregate>
Expected Output:
-<peci:Organization>
<peci:Organization_Type>Dummy_Cost_Center</peci:Organization_Type>
<peci:Organization_Code>30900498</peci:Organization_Code>
<peci:Organization_Name>General Above Site</peci:Organization_Name>
</peci:Organization>
-<peci:Organization>
<peci:Organization_Type>Cost_Center</peci:Organization_Type>
<peci:Organization_Code>31020420</peci:Organization_Code>
<peci:Organization_Name>JJMD - Finance Europe</peci:Organization_Name>
</peci:Organization>
My XSLT code is working fine when <peci:Organization_Type>Alternate_Cost_Center</peci:Organization_Type>
is present.
But when it is not present even then <peci:Organization_Type>Cost_Center</peci:Organization_Type>
getting changed to Dummy_Cost_Center.
I do not want this, I want to make change only when peci:Organization_Type>Alternate_Cost_Center</peci:Organization_Type>
is present for employee.
CodePudding user response:
You can do this by simply using two "replace" templates:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:wd="urn:com.workday/bsvc"
xmlns:is="java:com.workday.esb.intsys.xpath.ParsedIntegrationSystemFunctions"
xmlns:peci="urn:com.workday/peci" xmlns:this="urn:this-stylesheet"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xdiff="urn:com.workday/esb/xdiff"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:op="http://www.w3.org/2005/xpath-functions" version="2.0"
exclude-result-prefixes="xsl wd this xsd op xdiff peci xsi is">
<xsl:output indent="yes" method="xml" />
<!-- Identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()" />
</xsl:copy>
</xsl:template>
<xsl:template match="peci:Organization[peci:Organization_Type='Alternate_Cost_Center']">
<xsl:copy>
<peci:Organization_Type>Cost_Center</peci:Organization_Type>
<xsl:copy-of select="peci:*[name()!='peci:Organization_Type']" />
</xsl:copy>
</xsl:template>
<xsl:template match="peci:Organization[peci:Organization_Type='Cost_Center' and //peci:Organization_Type='Alternate_Cost_Center']">
<xsl:copy>
<peci:Organization_Type>Dummy_Cost_Center</peci:Organization_Type>
<xsl:copy-of select="peci:*[name()!='peci:Organization_Type']" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
The output should be as expected.
These two templates match the desired elements with a predicate if, and only if, the peci:Organization_Type
matches the desired value. Otherwise just the Identity template is executed.
EDIT:
Now the second template is only executed if there is an Organization_Type='Alternate_Cost_Center'
node present in the whole file. If you need to check for this node only on the same level, use the following template-match-rule instead:
<xsl:template match="peci:Organization[peci:Organization_Type='Cost_Center' and ../peci:Organization/peci:Organization_Type='Alternate_Cost_Center']">
<xsl:copy>
<peci:Organization_Type>Dummy_Cost_Center</peci:Organization_Type>
<xsl:copy-of select="peci:*[name()!='peci:Organization_Type']" />
</xsl:copy>
</xsl:template>
CodePudding user response:
I have managed to write the below xslt
code, which is working for my requirements:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:etv="urn:com.workday/etv" xmlns:peci="urn:com.workday/peci" xmlns:this="urn:this-stylesheet" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xtt="urn:com.workday/xtt" exclude-result-prefixes="xs this" version="2.0">
<xsl:template match="node()|@*">
<xsl:copy>
<xsl:apply-templates select="node()|@*" />
</xsl:copy>
</xsl:template>
<xsl:template match="peci:Worker/peci:Effective_Change/peci:Position/peci:Organization[peci:Organization_Type = 'Cost_Center']/peci:Organization_Type">
<xsl:choose>
<xsl:when test="exists(current()/../../peci:Organization[peci:Organization_Type = 'Alternate_Cost_Center'])">
<xsl:copy>
<xsl:value-of select="'Dummy_Cost_Center'" />
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:value-of select="'Cost_Center'" />
</xsl:copy>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template match="peci:Worker/peci:Effective_Change/peci:Position/peci:Organization[peci:Organization_Type = 'Alternate_Cost_Center']/peci:Organization_Type">
<xsl:copy>
<xsl:value-of select="'Cost_Center'" />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>