复制之前的 xml 内容,如果缺少则添加新标签

Copy previous xml content and add a new tag if it's missing

如果您能提供您对 xslt 的最佳想法以及实现以下输出的最简单方法,将会非常有帮助。

情况 1:-在 account/acc4300 下如果缺少 empid 字段 解决方案:- 从 account/Fintrans[1]/acc4300 读取 empid 的值并将其放入 account/acc4300

情况 2:-如果 empid 字段已经存在,则在 account/acc4300 下 解决方案:-什么都不做

情况 3:- 如果缺少 account/acc4300 解决方案:-什么都不做

输入XML:

<Root>
<corp att="abc">
    <account number="111">
        <acc4300>
            <name>stack</name>
            //empid tag is not present here as in first occurance, so I want to read it from below node of Fintrans and the value should be 5678 in output xml
        </acc4300>
        <Fintrans>
            <Amount>12.00</Amount>
            <Tax>1.00</Tax>
            <empid>5678</empid>
        </fintrans>
        <Fintrans>
            <Amount>112.00</Amount>
            <Tax>13.00</Tax>
            <empid>5678</empid>
        </fintrans>
    </account>
<account number="321">
        <acc4300>
            <name>stack1</name>
            //empid tag is not present here as in first occurance, so I want to read it from below node of Fintrans and the value should be 5678 in output xml
        </acc4300>
        <Fintrans>
            <Amount>152.00</Amount>
            <Tax>19.00</Tax>
            <empid>9734</empid>
        </fintrans>
        <Fintrans>
            <Amount>142.00</Amount>
            <Tax>17.00</Tax>
            <empid>9734</empid>
        </fintrans>
    </account>
    <account number="222">
        <acc4300>
            <name>overflow</name>
            <empid>1234</empid>
            // Do nothing to employee id as already in input file empid is present under node acc4300
        </acc4300>
        <Fintrans>
            <Amount>121.00</Amount>
            <Tax>15.00</Tax>
            <empid>1234</empid>
        </fintrans>
        <Fintrans>
            <Amount>122.00</Amount>
            <Tax>15.00</Tax>
            <empid>1234</empid>
        </fintrans>
    </account>
    <account number="333">
        //Since acc400 tag is missing in this block do nothing.
        <Fintrans>
            <Amount>12.00</Amount>
            <Tax>1.00</Tax>
            <empid>4285</empid>
        </fintrans>
        <Fintrans>
            <Amount>112.00</Amount>
            <Tax>13.00</Tax>
            <empid>4285</empid>
        </fintrans>
    </account>
</corp>
<corp att="asfd">
...........
...........
...........
</corp>
</Root>

结果应该如下所示

<Root>
<corp att="abc">
    <account number="111">
        <acc4300>
            <name>stack</name>
            <empid>5678</empid>
        </acc4300>
        <Fintrans>
            <Amount>12.00</Amount>
            <Tax>1.00</Tax>
            <empid>5678</empid>
        </fintrans>
        <Fintrans>
            <Amount>112.00</Amount>
            <Tax>13.00</Tax>
            <empid>5678</empid>
        </fintrans>
    </account>
    <account number="321">
        <acc4300>
            <name>stack1</name>
            <empid>9734</empid>
        </acc4300>
        <Fintrans>
            <Amount>152.00</Amount>
            <Tax>19.00</Tax>
            <empid>9734</empid>
        </fintrans>
        <Fintrans>
            <Amount>142.00</Amount>
            <Tax>17.00</Tax>
            <empid>9734</empid>
        </fintrans>
    </account>
    <account number="222">
        <acc4300>
            <name>overflow</name>
            <empid>1234</empid>
        </acc4300>
        <Fintrans>
            <Amount>121.00</Amount>
            <Tax>15.00</Tax>
            <empid>1234</empid>
        </fintrans>
        <Fintrans>
            <Amount>122.00</Amount>
            <Tax>15.00</Tax>
            <empid>1234</empid>
        </fintrans>
    </account>
    <account number="333">
        <Fintrans>
            <Amount>12.00</Amount>
            <Tax>1.00</Tax>
            <empid>4285</empid>
        </fintrans>
        <Fintrans>
            <Amount>112.00</Amount>
            <Tax>13.00</Tax>
            <empid>4285</empid>
        </fintrans>
    </account>
</corp>
<corp att="asfd">
...........
...........
...........
</corp>
</Root>

注意:- 这是一个示例 XML,我在原始 XML 中有 500 多个其他字段,因此为每个节点编写 XSLT 是不可行的。请建议使用 XSLT 1.0 或 2.0 版的任何最佳方法。

这里有一个方法可以做到这一点。

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

  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="acc4300">
      <xsl:copy>
          <xsl:apply-templates/>
          <xsl:if test="not(empid)">
              <empid><xsl:value-of select="../fintrans[1]/empid"/></empid>
          </xsl:if>
      </xsl:copy>
  </xsl:template>

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

</xsl:stylesheet>

看到它在这里工作:https://xsltfiddle.liberty-development.net/pNmC4J2/1