在多结构XML -comparing值和更新XML字段

问题描述:

我有下面的XML在多结构XML -comparing值和更新XML字段

<Messages> 
<Message1> 
    <EmpLDU> 
     <Row> 
      <Emp_id>325132 </Emp_id> 
      <Pay_Group>AUS_NI102</Pay_Group> 
      <Date_from_ec>20170814</Date_from_ec> 
      <Date_to_ec>20170816</Date_to_ec>   
      <Counter>1</Counter> 
     </Row> 
     <Row> 
      <Emp_id>1 </Emp_id> 
      <Pay_Group>AUS_NI102</Pay_Group> 
      <Date_from_ec>20170720</Date_from_ec> 
      <Date_to_ec>20170720</Date_to_ec> 
      <Counter>1</Counter> 
     </Row> 
    </EmpLDU> 
</Message1> 
<Message2> 
    <PayCalendar> 
     <PayCalendar>  
      <toPayPeriod> 
       <PayPeriod> 
        <PayCalendar_payGroup>AUS_NI102</PayCalendar_payGroup> 
        <payPeriodBeginDate>2016-08-01T00:00:00.000</payPeriodBeginDate> 
        <payPeriodEndDate>2016-08-31T00:00:00.000</payPeriodEndDate> 
        <payCheckIssueDate>2016-08-15T00:00:00.000</payCheckIssueDate> 
        <externalCode>1297</externalCode> 
       </PayPeriod> 
       <PayPeriod> 
        <PayCalendar_payGroup>AUS_NI102</PayCalendar_payGroup> 
        <payPeriodBeginDate>2016-07-01T00:00:00.000</payPeriodBeginDate> 
        <payPeriodEndDate>2016-07-31T00:00:00.000</payPeriodEndDate> 
        <payCheckIssueDate>2016-07-15T00:00:00.000</payCheckIssueDate> 
        <externalCode>1296</externalCode> 
       </PayPeriod> 
      </toPayPeriod> 
     </PayCalendar> 
     <PayCalendar> 
      <toPayPeriod> 
       <PayPeriod> 
        <PayCalendar_payGroup>ARE_M01</PayCalendar_payGroup> 
        <payPeriodBeginDate>2017-12-01T00:00:00.000</payPeriodBeginDate> 
        <payPeriodEndDate>2017-12-31T00:00:00.000</payPeriodEndDate> 
        <payCheckIssueDate>2017-12-25T00:00:00.000</payCheckIssueDate> 
        <externalCode>1237</externalCode> 
       </PayPeriod> 
       <PayPeriod> 
        <PayCalendar_payGroup>ARE_M01</PayCalendar_payGroup> 
        <payPeriodBeginDate>2017-11-01T00:00:00.000</payPeriodBeginDate> 
        <payPeriodEndDate>2017-11-30T00:00:00.000</payPeriodEndDate> 
        <payCheckIssueDate>2017-11-25T00:00:00.000</payCheckIssueDate> 
        <externalCode>1236</externalCode> 
       </PayPeriod> 
      </toPayPeriod> 
     </PayCalendar> 
    </PayCalendar> 
    <Message2><Messages> 

我需要在输出以下消息

<EmpLDU> 
     <Row> 
      <Emp_id>325132 </Emp_id> 
      <Date_from_ec>20170814</Date_from_ec> 
      <Date_to_ec>20170816</Date_to_ec> 
     </Row> 
     <Row> 
      <Emp_id>1 </Emp_id> 
      <Date_from_ec>20170720</Date_from_ec> 
      <Date_to_ec>20170720</Date_to_ec> 
     </Row></EmpLDU> 

其中场Date_from_ec在参考被更新消息2> PayPeriod节点(其中一个在当月存在payCheckIssueDate) 那么如果payPeriodBeginDate大于Date_from_ec那么

Date_from_ec的值需要使用payPeriodBeginDate更新,否则保持现有值。

同样 领域Date_from_ec在参考与消息2> PayPeriod节点(一个已payCheckIssueDate当月existsing) 那么如果payPeriodBeginDate小于Date_to_ec然后

更新Date_to_ec的值需要用payPeriodEndDate更新,否则保持现有的值。

我已经开发出了下面的代码,但没有喜悦:

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xs="http://www.w3.org/2001/XMLSchema" 
xmlns:fn="http://www.w3.org/2005/02/xpath-functions" xmlns:xdt="http://www.w3.org/2005/02/xpath-datatypes" 
exclude-result-prefixes="xsl xs fn xdt"> 
<xsl:output method="xml" version="1.0" encoding="utf-8" 
    indent="yes" omit-xml-declaration="yes" /> 

<xsl:variable name="Current_Date"> 
    <xsl:value-of select="format-date(current-date(), '[Y0001][M01][D01]')" /> 
</xsl:variable> 
<xsl:variable name="Current_MonthYear"> 
    <xsl:value-of select="substring($Current_Date, 1, 6)" /> 
</xsl:variable> 

<!-- template that matches the root node --> 
<xsl:template match="/"> 
    <EmpLDU> 
     <xsl:for-each select="EmpLDU/Row[Pay_Group !='']"> 

      <xsl:variable name="v_Pay_Group" select="Pay_Group" /> 
      <xsl:variable name="v_Date_from_ec" select="Date_from_ec" /> 
      <xsl:variable name="v_Date_to_ec" select="Date_to_ec" /> 

      <Row> 
       <Emp_id> 
        <xsl:value-of select="Emp_id" /> 
       </Emp_id> 
       <xsl:variable name="v_payPeriodBeginDate"> 
        <xsl:for-each select="Messages/Message2/PayCalendar/PayCalendar[payGroup = $v_Pay_Group]"> 
         <xsl:for-each select="toPayPeriod/PayPeriod"> 
          <xsl:variable name="v_payCheckIssueDate"> 
           <xsl:value-of select="format-dateTime(payCheckIssueDate, '[Y0001][M01][D01]')" /> 
          </xsl:variable> 
          <xsl:variable name="v_payCheckIssue_MonthYear"> 
           <xsl:value-of select="substring($v_payCheckIssueDate, 1, 6)" /> 
          </xsl:variable> 

          <xsl:if test="$Current_MonthYear = $v_payCheckIssue_MonthYear" > 
           <xsl:value-of select="format-dateTime(payPeriodBeginDate, '[Y0001][M01][D01]')" />       
          </xsl:if> 
         </xsl:for-each> 
        </xsl:for-each> 
       </xsl:variable> 
       <xsl:variable name="v_payPeriodEndDate"> 
        <xsl:for-each select="Messages/Message2/PayCalendar/PayCalendar[payGroup = $v_Pay_Group]"> 
         <xsl:for-each select="toPayPeriod/PayPeriod"> 
          <xsl:variable name="v_payCheckIssueDate"> 
           <xsl:value-of select="format-dateTime(payCheckIssueDate, '[Y0001][M01][D01]')" /> 
          </xsl:variable> 
          <xsl:variable name="v_payCheckIssue_MonthYear"> 
           <xsl:value-of select="substring($v_payCheckIssueDate, 1, 6)" /> 
          </xsl:variable> 

          <xsl:if test="$Current_MonthYear = $v_payCheckIssue_MonthYear" > 
           <xsl:value-of select="format-dateTime(payPeriodEndDate, '[Y0001][M01][D01]')" />          
          </xsl:if> 
         </xsl:for-each> 
        </xsl:for-each> 
       </xsl:variable> 
       <Date_from_ec> 
        <xsl:choose> 
         <xsl:when test="$v_Date_from_ec &lt; $v_payPeriodBeginDate"> 
          <xsl:value-of select="$v_payPeriodBeginDate"/> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of select="$v_Date_from_ec" /> 
         </xsl:otherwise> 
        </xsl:choose> 
       </Date_from_ec> 
       <Date_to_ec> 
        <xsl:choose> 
         <xsl:when test="$v_Date_to_ec &gt; $v_payPeriodEndDate"> 
          <xsl:value-of select="$v_payPeriodEndDate"/> 
         </xsl:when> 
         <xsl:otherwise> 
          <xsl:value-of select="$v_Date_to_ec" /> 
         </xsl:otherwise> 
        </xsl:choose> 
       </Date_to_ec>    
      </Row> 
     </xsl:for-each> 
    </EmpLDU> 
</xsl:template> 
<!-- --> 

需要你输入。 谢谢

+0

您可能想要将示例数据减少到相关数据来回答问题。输入代码段中的日期字段和所显示的所需输出片段中是否有任何更改?我不确定我注意到这些领域的变化。 –

+0

你好马丁,我已经把问题修剪成最大的相关性。是的,这是必需的功能 - 其中 - Date_from_ec和Date_to_ec等字段需要根据xml -message2中的字段进行计算。用我的代码,我得到了Date_from_ec的值,但奇怪的是Date_to_ec是空的。 – Vicky

+0

如果参考消息2> PayPeriod节点(其中一个在当月存在payCheckIssueDate)更新字段Date_from_ec ,那么如果payPeriodBeginDate大于Date_from_ec然后是 >则需要使用payPeriodBeginDate更新Date_from_ec的值否则保持现有值。 – Vicky

上面提到的XSLT代码工作正常。有一个数据问题。 欢呼声, Vicky

+0

我不认为''xsl:for-each select =“/ * [name()='Messages/Message2/PayCalendar/PayCalendar [payGroup = $ v_Pay_Group]”>'可以正常工作。 –

+0

以上xpath是最初的一个,我已经完善了该问题的查询。我更新了上面的代码部分。感谢提及 – Vicky

+0

你现在可以更新投票 – Vicky