2016-07-30 237 views
0

我需要帮助来汇总和汇总XML文件。 我使用Talend来聚合这个文件,但是使用的语言是xslt 1.0。Xslt 1.0汇总和总计

这是我原来的XML:

<ROOT> 
    <Id>1000021</Id> 
    <Commandes> 
    <Commande> 
     <Id>12363806</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 
       <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>4</Qty> 
        <QtyOrder>4</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
       <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>1</Qty> 
        <QtyOrder>1</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
</ROOT> 

我想这(数量是根据CUG,intitule或EAN加)

<ROOT> 
    <Id>1000021</Id> 
    <Commandes> 
    <Commande> 
     <Id>12363806</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 
       <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>5</Qty> 
        <QtyOrder>5</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
</ROOT> 

这是我的XSLT(映射):

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output indent="yes" /> 
    <xsl:template match="/"> 
      <ROOT> 
       <Id><xsl:value-of select="ROOT/Id" /></Id> 
       <Commandes> 
       <xsl:for-each select="ROOT/Commandes"> 
        <Commande>   
        <xsl:for-each select="Commande">   
         <Id> 
          <xsl:value-of select="Id" /> 
         </Id> 
         <CdeAteliers> 
         <xsl:for-each select="CdeAteliers"> 
          <CdeAtelier> 
          <xsl:for-each select="CdeAtelier"> 
           <AId><xsl:value-of select="AId" /></AId> 
           <Cartons> 
           <xsl:for-each select="Cartons"> 
            <Carton> 
            <xsl:for-each select="Carton"> 
             <Numero><xsl:value-of select="Numero" /></Numero> 
             <Produits> 
             <xsl:for-each select="Produits" > 
              <Produit> 
              <xsl:for-each select="Produit"> 
               <CUG><xsl:value-of select="CUG" /></CUG> 
               <Intitule><xsl:value-of select="Intitule" /></Intitule> 
               <Qty><xsl:value-of select="Qty" /></Qty> 
               <QtyOrder><xsl:value-of select="QtyOrder" /></QtyOrder> 
               <EANs> 
               <xsl:for-each select="EANs"> 
                <EAN><xsl:value-of select="EAN" /></EAN> 
               </xsl:for-each> 
               </EANs> 
              </xsl:for-each> 
              </Produit> 
             </xsl:for-each> 
             </Produits> 
            </xsl:for-each> 
            </Carton> 
           </xsl:for-each> 
           </Cartons> 
          </xsl:for-each> 
          </CdeAtelier> 
         </xsl:for-each> 
         </CdeAteliers> 
        </xsl:for-each> 
        </Commande> 
       </xsl:for-each> 
       </Commandes>   
      </ROOT> 
    </xsl:template> 
</xsl:stylesheet> 

我试图总结金额,但没有增加金额。 我看到了XSLT/Muenchian分组的方式(https://en.wikipedia.org/wiki/XSLT/Muenchian_grouping),但我不明白系统是如何工作的。

我迷路了这个问题 预先感谢您的帮助:)

编辑:

谢谢马丁为你解答。 我很感谢!

但如果它是我的文件是这样的:

<ROOT> 
    <Id>1000021</Id> 
    <Commandes> 
    <Commande> 
     <Id>12363806</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 
       <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>5</Qty> 
        <QtyOrder>5</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
        <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>5</Qty> 
        <QtyOrder>5</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
    <Commandes> 
    <Commande> 
     <Id>12363807</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 
       <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>5</Qty> 
        <QtyOrder>5</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
        <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>5</Qty> 
        <QtyOrder>5</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
</ROOT> 

我有这个文件输出文件:

<?xml version="1.0" encoding="UTF-8"?><ROOT> 
    <Id>1000021</Id> 
    <Commandes> 
    <Commande> 
     <Id>12363806</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 
       <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>20</Qty> 
        <QtyOrder>20</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
    <Commandes> 
    <Commande> 
     <Id>12363807</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 


       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
</ROOT> 

但我想这一点: 所有量的第一控制汇总。

<?xml version="1.0" encoding="UTF-8"?><ROOT> 
    <Id>1000021</Id> 
    <Commandes> 
    <Commande> 
     <Id>12363806</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 
       <Produit> 
        <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>10</Qty> 
        <QtyOrder>10</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produit> 
       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
    <Commandes> 
    <Commande> 
     <Id>12363807</Id> 
     <CdeAteliers> 
     <CdeAtelier> 
      <AId>2</AId> 
      <Cartons> 
      <Carton> 
       <Numero>0</Numero> 
       <Produits> 
       <CUG>48384</CUG> 
        <Intitule>MENES ALBERT Confiture extra 370 g</Intitule> 
        <Qty>10</Qty> 
        <QtyOrder>10</QtyOrder> 
        <EANs> 
        <EAN>316233</EAN> 
        </EANs> 
       </Produits> 
      </Carton> 
      </Cartons> 
     </CdeAtelier> 
     </CdeAteliers> 
    </Commande> 
    </Commandes> 
</ROOT> 

我该怎么办?

+0

邮政目前的样式表(在一个地方,你已经实现了Muenchian分组的)所以我们可以找到你的错误并修复它。 –

+0

我使用Martin Honnen的样式表(请参阅评论和我的编辑)。 – Natacha

回答

3

到组Produit S代表各Commande,那么你必须定义键:

<xsl:key name="group" match="Produit" use="concat(ancestor::Commande/Id, '|', CUG)"/> 

,并相应地调整到key()函数的调用 - 如:

<xsl:template match="Produit[generate-id() = generate-id(key('group', concat(ancestor::Commande/Id, '|', CUG))[1])]/Qty"> 

下面是一个完整的样式表,这也是(恕我直言)简单一点:

XSLT 1.0

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

<xsl:key name="group" match="Produit" use="concat(ancestor::Commande/Id, '|', CUG)"/> 

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

<xsl:template match="Produits"> 
    <xsl:copy> 
     <xsl:apply-templates select="Produit[count(. | key('group', concat(ancestor::Commande/Id, '|', CUG))[1]) = 1]"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="Qty"> 
    <xsl:copy> 
     <xsl:value-of select="sum(key('group', concat(ancestor::Commande/Id, '|', ../CUG))/Qty)"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="QtyOrder"> 
    <xsl:copy> 
     <xsl:value-of select="sum(key('group', concat(ancestor::Commande/Id, '|', ../CUG))/QtyOrder)"/> 
    </xsl:copy> 
</xsl:template> 

</xsl:stylesheet> 
+0

谢谢!有用 ! Mickael和马丁,你是神! – Natacha

3

在你的文章中描述使用一键,你可以的,如果你想通过CUG分别减少到

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

    <xsl:key name="group" match="Produits/Produit" use="CUG"/> 

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

    <xsl:template match="Produit[generate-id() = generate-id(key('group', CUG)[1])]/Qty"> 
     <xsl:copy> 
      <xsl:value-of select="sum(key('group', ../CUG)/Qty)"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Produit[generate-id() = generate-id(key('group', CUG)[1])]/QtyOrder"> 
     <xsl:copy> 
      <xsl:value-of select="sum(key('group', ../CUG)/QtyOrder)"/> 
     </xsl:copy> 
    </xsl:template> 

    <xsl:template match="Produit[not(generate-id() = generate-id(key('group', CUG)[1]))]"/> 

</xsl:stylesheet> 
+0

谢谢Martin! 太棒了! 你可以看看编辑我的文章,我有一个小问题(它是完美的!)? – Natacha