替换一个节点并复制XML

时间:2018-03-26 06:14:00

标签: xml xslt

我的XML看起来像这样。

<?xml version="1.0" encoding="UTF-8"?>
<Extract_Employees>
   <PayGroup>
      <Header>
         <Version>24</Version>
      </Header>
      <Employee>
         <Summary>
            <ID>12345</ID>
         </Summary>
         <Position>
            <Effective_Date>2017-05-01</Effective_Date>
            <Pay PriorValue="111.11">1111.11</Pay>
         </Position>
        <Compensation>
            <Effective_Date>2018-04-01</Effective_Date>
         </Compensation>
         <Additional_Information>
            <Field1>Field1Value</Field1>
         </Additional_Information>
      </Employee>
      <Employee>
         <Summary>
            <ID>54321</ID>
         </Summary>
         <Position>
            <Effective_Date>2017-09-01</Effective_Date>
            <Pay PriorValue="222.22">2222.22</Pay>
         </Position>
         <Compensation>
            <Effective_Date>2018-12-31</Effective_Date>
         </Compensation>
         <Additional_Information>
            <Field1>Field1Value</Field1>
         </Additional_Information>
      </Employee>
   </PayGroup>
   <PayGroup>
      <Header>
         <Version>27</Version>
      </Header>
   </PayGroup>
</Extract_Employees>

如果Position / Pay的属性为PriorValue,我需要将所有Position / Effective_Date替换为输出文件中的补偿/生效日期

因此输出文件应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Extract_Employees>
       <PayGroup>
          <Header>
             <Version>24</Version>
          </Header>
          <Employee>
             <Summary>
            <ID>12345</ID>
         </Summary>
         <Position>
            <Effective_Date>2018-04-01</Effective_Date>
            <Pay PriorValue="111.11">1111.11</Pay>
         </Position>
        <Compensation>
            <Effective_Date>2018-04-01</Effective_Date>
         </Compensation>
         <Additional_Information>
            <Field1>Field1Value</Field1>
         </Additional_Information>
     </Employee>
      <Employee>
         <Summary>
            <ID>54321</ID>
         </Summary>
         <Position>
            <Effective_Date>2018-12-31</Effective_Date>
            <Pay PriorValue="222.22">2222.22</Pay>
         </Position>
         <Compensation>
            <Effective_Date>2018-12-31</Effective_Date>
         </Compensation>
         <Additional_Information>
            <Field1>Field1Value</Field1>
         </Additional_Information>
      </Employee>
   </PayGroup>
   <PayGroup>
      <Header>
         <Version>27</Version>
      </Header>
   </PayGroup>
</Extract_Employees>

这是我的代码。但运行Position / Effective_Date之后会显示如下

enter image description here

这是我的XSLT。

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:template name="copyall" match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="Position/Effective_Date">
        <xsl:for-each select="/Extract_Employees/PayGroup">
            <xsl:for-each select="Employee">
                <xsl:choose>
                    <xsl:when test="Position/Pay/@PriorValue">
                        <Effective_Date>
                            <xsl:value-of select="Compensation/Effective_Date"/>
                        </Effective_Date>
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:copy-of select="Position/Effective_Date"/>
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:for-each>
    </xsl:template>

</xsl:stylesheet>

1 个答案:

答案 0 :(得分:1)

可以优化现有模板以匹配<Position>元素,然后检查Pay[@PriorValue]以将Position/Effective_Date的值替换为Compensation/Effective_Date

<xsl:template match="Position">
    <xsl:copy>
        <xsl:if test="Pay[@PriorValue]">
            <Effective_Date>
                <xsl:value-of select="../Compensation/Effective_Date" />
            </Effective_Date>
            <xsl:apply-templates select="*[not(self::Effective_Date)]" />
        </xsl:if>
    </xsl:copy>
</xsl:template>