Xslt 组合多个关键点

Xslt 组合多个关键点,xslt,Xslt,我在组合多个键以更改记录时遇到问题 我有2条XML记录。文件1和文件2 目标是创建一个包含4个部分的表。相同、更改、添加和删除 两份文件中具有相同记录号、零件号、外部数量的元素应位于相同的小节中。 文档1中的元素和文档2中的元素需要在删除小节下 文件2中的元素和文件1中的元素需要在“添加”小节下 上述3种情况下不存在的元素应在变更小节中。例如,在中存在的记录 两份文件以及数量、零件号或说明有任何变更 现在我可以添加前3个部分,没有任何问题,问题出现在第4个条件中。我无法合并前3个 获得一个结果的

我在组合多个键以更改记录时遇到问题

我有2条XML记录。文件1和文件2

目标是创建一个包含4个部分的表。相同、更改、添加和删除

两份文件中具有相同记录号、零件号、外部数量的元素应位于相同的小节中。 文档1中的元素和文档2中的元素需要在删除小节下 文件2中的元素和文件1中的元素需要在“添加”小节下 上述3种情况下不存在的元素应在变更小节中。例如,在中存在的记录 两份文件以及数量、零件号或说明有任何变更

现在我可以添加前3个部分,没有任何问题,问题出现在第4个条件中。我无法合并前3个 获得一个结果的条件,而不是那些条件

这是带有我的注释的输出表。此处记录999位于删除部分,不应归入更改部分

----------------------------------------------------
Section PartNo RecNo Desc Doc-1 Qty Doc-2 Qty Total 
----------------------------------------------------
Same    111     aaa   Desc1   1         1     200 
----------------------------------------------------
Same Total                                    100 
----------------------------------------------------
Change  222     bbb   Desc2   2         3     200 
Change  333     ccc   Desc3   3         3     200 
Change  444     ddd   Desc4   6         4     500 
Change  999     ggg   Desc9   2         0     100 
----------------------------------------------------
Change Total                                  400 
----------------------------------------------------
Add     666     ff    Desc4   0         6     400 
----------------------------------------------------
Add Total                                      0 
----------------------------------------------------
Delete  999     ggg   Desc9   2         0     100 
----------------------------------------------------
Delete Total                                  0 
----------------------------------------------------
Grand Total                                   500 
----------------------------------------------------
这是我的XML

<?xml version="1.0" encoding="utf-8"?>
<?xml-stylesheet type="text/xsl" href="sample.xslt"?>
<Logia>
  <DocHeader>
    <Document>
      <Downto>
        <part_no>111</part_no>
        <rec_no>aaa</rec_no>
        <desc>Desc1</desc>
        <ext_qty>1</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>222</part_no>
        <rec_no>bbb</rec_no>
        <desc>Desc2</desc>
        <ext_qty>2</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>333</part_no>
        <rec_no>ccc</rec_no>
        <desc>Desc3</desc>
        <ext_qty>3</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>444</part_no>
        <rec_no>ddd</rec_no>
        <desc>Desc4</desc>
        <ext_qty>6</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>999</part_no>
        <rec_no>ggg</rec_no>
        <desc>Desc9</desc>
        <ext_qty>2</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
    </Document>
    <Document>
      <Downto>
        <part_no>111</part_no>
        <rec_no>aaa</rec_no>
        <desc>Desc1</desc>
        <ext_qty>1</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>222</part_no>
        <rec_no>bbb</rec_no>
        <desc>Desc3</desc>
        <ext_qty>3</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>333</part_no>
        <rec_no>bbb</rec_no>
        <desc>Desc3</desc>
        <ext_qty>3</ext_qty>
        <mat_cost>100.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>444</part_no>
        <rec_no>ddd</rec_no>
        <desc>Desc4</desc>
        <ext_qty>4</ext_qty>
        <mat_cost>400.00</mat_cost>
      </Downto>
      <Downto>
        <part_no>666</part_no>
        <rec_no>ff</rec_no>
        <desc>Desc4</desc>
        <ext_qty>6</ext_qty>
        <mat_cost>400.00</mat_cost>
      </Downto>
    </Document>
  </DocHeader>
</Logia>

111
aaa
描述1
1.
100
222
bbb
描述2
2.
100
333
ccc
描述3
3.
100
444
ddd
描述4
6.
100
999
ggg
描述9
2.
100
111
aaa
描述1
1.
100
222
bbb
描述3
3.
100
333
bbb
描述3
3.
100
444
ddd
描述4
4.
400
666
ff
描述4
6.
400
我的XSL如下

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" omit-xml-declaration="yes" version="1.0" encoding="utf-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<xsl:key name="MAT1PARTKEY" match="Document[1]/Downto" use="part_no" />
<xsl:key name="MAT2PARTKEY" match="Document[2]/Downto" use="part_no" />

<xsl:key name="MATERIALBYPARTNO" match="Document/Downto" use="part_no" />

<xsl:key name="MATERIAL2BYKEY" match="Document[2]/Downto" use="concat(rec_no, '||',  part_no, '||', ext_qty)" />
  <xsl:key name="MATERIALKEY" match="Document[1]/Downto" use="concat(rec_no, '||',  part_no, '||', ext_qty)" />

<xsl:template match="/Logia/DocHeader">
    <table border="1">
        <!-- header -->
        <tr>
            <th>Section</th>
            <th>PartNo</th>
            <th>RecNo</th>
            <th>Desc</th>
            <th>Doc-1 Qty</th>
            <th>Doc-2 Qty</th>   
            <th>Total</th>
        </tr>
        <!-- same -->
        <xsl:variable name="same" select="Document[1]/Downto[key('MATERIAL2BYKEY', concat(rec_no, '||',  part_no, '||', ext_qty))]" />  
        <xsl:apply-templates select="$same">
            <xsl:with-param name="section">Same</xsl:with-param>
        </xsl:apply-templates>
        <xsl:variable name="same-total" select="sum($same/mat_cost)" />    
        <tr>
            <td colspan="6">Same Total</td>
            <th><xsl:value-of select="$same-total"/></th>
        </tr>       
        <!-- change -->
       <xsl:variable name="change" select="Document[1]/Downto[not(key('MATERIAL2BYKEY', concat(rec_no, '||',  part_no, '||', ext_qty)))]" />
        <xsl:apply-templates select="$change">
            <xsl:with-param name="section">Change</xsl:with-param>
        </xsl:apply-templates>
        <xsl:variable name="change-total" select="sum($change/mat_cost)" />    
        <tr>
            <td colspan="6">Change Total</td>
            <th><xsl:value-of select="$change-total"/></th>
        </tr>                  
       <!-- Add --> 
       <xsl:variable name="add" select="Document[2]/Downto[not(key('MAT1PARTKEY', part_no ))]" />  
        <xsl:apply-templates select="$add">
            <xsl:with-param name="section">Add</xsl:with-param>
        </xsl:apply-templates>
        <xsl:variable name="add-total" select="sum($add/Value)" />  
        <tr>
            <td colspan="6">Add Total</td>
            <th><xsl:value-of select="$add-total"/></th>
        </tr>
        <!-- delete -->
        <xsl:variable name="delete" select="Document[1]/Downto[not(key('MAT2PARTKEY', part_no ))]" />   
        <xsl:apply-templates select="$delete">
            <xsl:with-param name="section">Delete</xsl:with-param>
        </xsl:apply-templates>

        <xsl:variable name="delete-total" select="sum($delete/Value)" />    
        <tr>
            <td colspan="6">Delete Total</td>
            <th><xsl:value-of select="$delete-total"/></th>
        </tr>
        <!-- grand total -->
        <tr>
            <th colspan="6">Grand Total</th>
            <th><xsl:value-of select="$same-total + $change-total"/></th>
        </tr>
    </table>
</xsl:template>

<xsl:template match="Downto">
    <xsl:param name="section"/>
    <xsl:if test="generate-id() = generate-id(key('MATERIALBYPARTNO', part_no)[1])">
      <tr>
          <td><xsl:value-of select="$section"/></td>
          <td><xsl:value-of select="part_no"/></td>  
          <td><xsl:value-of select="rec_no"/></td>            
          <td><xsl:value-of select="desc"/></td>
          <td><xsl:value-of select="sum(key('MAT1PARTKEY', part_no)/ext_qty)"/></td>
          <td><xsl:value-of select="sum(key('MAT2PARTKEY', part_no)/ext_qty)"/></td>
          <td><xsl:value-of select="sum(key('MATERIALBYPARTNO', part_no)/mat_cost)"/></td>
      </tr>   

    </xsl:if>
</xsl:template>

</xsl:stylesheet>

部分
零件号
记录号
描述
单据-1数量
Doc-2数量
全部的
同样的
相同总数
改变
变动总额
添加
加总
删除
删除总数
总计

Wow。。我得到了我想要的。非常感谢你,马克
<xsl:variable name="change" select="Document[1]/Downto[
    key('MAT2PARTKEY', part_no) and not(
        key('MATERIAL2BYKEY', concat(rec_no, '||',  part_no, '||', ext_qty))
    )
]" />