首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >替换一个节点并复制XML中的所有现有节点

替换一个节点并复制XML中的所有现有节点
EN

Stack Overflow用户
提问于 2018-03-26 06:13:59
回答 1查看 149关注 0票数 1

我的XML看起来像这样。

代码语言:javascript
复制
<?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,则需要将所有职位/生效日期替换为输出文件中的补偿/生效日期

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

代码语言:javascript
复制
<?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>

这是我的密码。但是在运行了这个位置/生效日期之后,显示如下所示

这是我的XSLT。

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-03-26 06:35:47

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

代码语言:javascript
复制
<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>
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49485050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档