2017-10-18 13 views
1

Mon entrée xml est:Fusion noeuds XML avec des valeurs de nœud enfant identiques en utilisant XSLT

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
    <Row> 
     <EmployeeID>21001</EmployeeID> 
     <FMLAStartDate>2017-10-10</FMLAStartDate> 
     <FMLAEndDate></FMLAEndDate> 
     <FMLACorrectDate></FMLACorrectDate> 
     <LTDStartDate></LTDStartDate> 
     <LTDEndDate></LTDEndDate>  
     <LTDCorrectdate></LTDCorrectdate> 
    </Row> 

    <Row> 
     <EmployeeID>21002</EmployeeID> 
     <FMLAStartDate>2017-10-10</FMLAStartDate> 
     <FMLAEndDate></FMLAEndDate> 
     <FMLACorrectDate></FMLACorrectDate> 
     <LTDStartDate></LTDStartDate> 
     <LTDEndDate></LTDEndDate>  
     <LTDCorrectdate></LTDCorrectdate> 
    </Row> 

    <Row> 
     <EmployeeID>21002</EmployeeID> 
     <FMLAStartDate></FMLAStartDate> 
     <FMLAEndDate></FMLAEndDate> 
     <FMLACorrectDate></FMLACorrectDate> 
     <LTDStartDate></LTDStartDate> 
     <LTDEndDate>2017-10-08</LTDEndDate>  
     <LTDCorrectdate></LTDCorrectdate> 
    </Row> 

    <Row> 
     <EmployeeID>21003</EmployeeID> 
     <FMLAStartDate></FMLAStartDate> 
     <FMLAEndDate></FMLAEndDate> 
     <FMLACorrectDate></FMLACorrectDate> 
     <LTDStartDate>2017-05-01</LTDStartDate> 
     <LTDEndDate></LTDEndDate>  
     <LTDCorrectdate></LTDCorrectdate> 
    </Row> 

    <Row> 
     <EmployeeID>21004</EmployeeID> 
     <FMLAStartDate></FMLAStartDate> 
     <FMLAEndDate></FMLAEndDate> 
     <FMLACorrectDate>2017-01-10</FMLACorrectDate> 
     <LTDStartDate></LTDStartDate> 
     <LTDEndDate></LTDEndDate>  
     <LTDCorrectdate></LTDCorrectdate> 
    </Row> 
</Root> 

Notez que 21002 des employés a deux noeuds, un tenant la date FMLA début et autre tenant la date de fin LTD. L'attente de transformation Post XSLT consiste à avoir un nœud de ligne par ID d'employé et ce nœud de ligne doit contenir l'intégralité de l'information pour cet ID d'employé. Par conséquent la production attendue est:

<?xml version="1.0" encoding="UTF-8"?> 
<Root> 
    <Row> 
    <EmployeeID>21001</EmployeeID> 
    <FMLAStartDate>2017-10-10</FMLAStartDate> 
    <FMLAEndDate></FMLAEndDate> 
    <FMLACorrectDate></FMLACorrectDate> 
    <LTDStartDate></LTDStartDate> 
    <LTDEndDate></LTDEndDate>  
    <LTDCorrectdate></LTDCorrectdate> 
    </Row> 

    <Row> 
    <EmployeeID>21002</EmployeeID> 
    <FMLAStartDate>2017-10-10</FMLAStartDate> 
    <FMLAEndDate></FMLAEndDate> 
    <FMLACorrectDate></FMLACorrectDate> 
    <LTDStartDate></LTDStartDate> 
    <LTDEndDate>2017-10-08</LTDEndDate>  
    <LTDCorrectdate></LTDCorrectdate> 
    </Row> 

    <Row> 
    <EmployeeID>21003</EmployeeID> 
    <FMLAStartDate></FMLAStartDate> 
    <FMLAEndDate></FMLAEndDate> 
    <FMLACorrectDate></FMLACorrectDate> 
    <LTDStartDate>2017-05-01</LTDStartDate> 
    <LTDEndDate></LTDEndDate>  
    <LTDCorrectdate></LTDCorrectdate> 
    </Row> 

    <Row> 
    <EmployeeID>21004</EmployeeID> 
    <FMLAStartDate></FMLAStartDate> 
    <FMLAEndDate></FMLAEndDate> 
    <FMLACorrectDate>2017-01-10</FMLACorrectDate> 
    <LTDStartDate></LTDStartDate> 
    <LTDEndDate></LTDEndDate>  
    <LTDCorrectdate></LTDCorrectdate> 
    </Row> 
</Root> 

J'ai essayé de-chaque groupe pour le regroupement en fonction d'identification de l'employé, mais il ne donne pas une sortie correcte. Quelqu'un peut-il m'aider s'il vous plaît avec ceci? J'ai des connaissances de base sur XSLT mais je n'ai pas travaillé avec des fonctionnalités avancées de XSLT.

Merci!

+0

Nous ne pouvons pas trouver les bugs dans votre code, sauf si vous nous montrez le code. –

Répondre

0

Vous pouvez regrouper les lignes et concat toutes les valeurs pour chaque champ:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:template match="/Root"> 
    <xsl:for-each-group select="Row" group-by="EmployeeID"> 
     <Row> 
     <EmployeeID> 
      <xsl:value-of select="current-grouping-key()"/> 
     </EmployeeID> 
     <FMLAStartDate> 
      <xsl:value-of select="current-group()/FMLAStartDate"/> 
     </FMLAStartDate> 
     <FMLAEndDate> 
      <xsl:value-of select="current-group()/FMLAEndDate"/> 
     </FMLAEndDate> 
     <FMLACorrectDate> 
      <xsl:value-of select="current-group()/FMLACorrectDate"/> 
     </FMLACorrectDate> 
     <LTDStartDate> 
      <xsl:value-of select="current-group()/LTDStartDate"/> 
     </LTDStartDate> 
     <LTDEndDate> 
      <xsl:value-of select="current-group()/LTDEndDate"/> 
     </LTDEndDate> 
     <LTDCorrectdate> 
      <xsl:value-of select="current-group()/LTDCorrectdate"/> 
     </LTDCorrectdate> 
     </Row> 
    </xsl:for-each-group> 
    </xsl:template> 
</xsl:stylesheet> 
+0

Merci beaucoup! –

0

je ferais

<xsl:template match="/Root"> 
    <xsl:for-each-group select="Row" group-by="EmployeeID"> 
     <Row> 
     <xsl:for-each-group select="current-group()/*" 
          group-by="node-name()"> 
      <xsl:copy-of select="(current-group()[normalize-space()], 
           current-group()[not(normalize-space()])[1]"/> 
     </Row> 
    </xsl:for-each-group> 
    </xsl:template> 

C'est: pour chaque groupe de dossiers des employés pour le même ID employé, sortie une ligne, et pour chaque élément présent dans tout ou partie des éléments employés dans le groupe, affiche une copie de l'un d'entre eux, en donnant la préférence à ceux qui ont un contenu non vide.