仅当存在其他子节点属性时才更改子节点的 XSLT

XSLT to change the child node only when other child node attribute is present

我需要更改 XML 文件,其中 peci:Organization 是重复的子节点。 我需要进行更改,如果 <peci:Organization_Type = 'Alternate_Cost_Center' 存在于员工的文件中时,我才需要执行此操作。

我一直在尝试以下代码:

-----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>

预期输出:

-<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>

存在 <peci:Organization_Type>Alternate_Cost_Center</peci:Organization_Type> 时,我的 XSLT 代码工作正常。 但是即使它不存在,<peci:Organization_Type>Cost_Center</peci:Organization_Type> 也会更改为 Dummy_Cost_Center。 我不想要这个,我只想在员工 peci:Organization_Type>Alternate_Cost_Center</peci:Organization_Type> 存在时进行更改。

您只需使用两个“替换”模板即可完成此操作:

<?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>

输出应该符合预期。
当且仅当 peci:Organization_Type 与所需值匹配时,这两个模板才能将所需元素与谓词匹配。否则只会执行 Identity 模板

编辑:
现在,只有在整个文件 中 存在 Organization_Type='Alternate_Cost_Center' 节点时,才会执行第二个模板。如果您只需要在同一级别 上检查此节点,请改用以下模板匹配规则:

<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>

我已经设法编写了下面的 xslt 代码,它可以满足我的要求:

<?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>