2013-10-01 14 views
11

im wciąż się uczę for-each-group jaki jest najlepszy sposób grupowania czegoś takiego przy użyciu XSL? (Według kraju) Próbuję użyć XSL do konwersji tego XML na inny XML.Jak używać dla każdej grupy w XSL

<?xml version="1.0" encoding="UTF-8"?> 
<Person> 
    <Student> 
     <Info Country="England" Name="Dan" Age="20" Class="C" /> 
    </Student> 
    <Student> 
     <Info Country="England" Name="Dan" Age="20" Class="B" /> 

    </Student> 
    <Student> 
     <Info Country="England" Name="Sam" Age="20" Class="A" /> 
    </Student> 

    <Student> 
     <Info Country="Australia" Name="David" Age="22" Class="D" /> 
    </Student> 
    <Student> 
     <Info Country="Australia" Name="David" Age="22" Class="A" /> 
    </Student> 

</Person> 
+0

Zależy gdzie chcesz na grupy. Jak chcesz, aby twój wynik wyglądał? Pogrupowane w "Kraj" lub zgrupowane w "Nazwa" lub "Klasa" lub kombinacja? –

Odpowiedz

27

Jeśli grupujesz według kraju, zacznij od np.

<xsl:template match="Person"> 
    <xsl:for-each-group select="Student/Info" group-by="@Country"> 
    <country name="{current-grouping-key()}"> 

    </country> 
    </xsl:for-each-group> 
</xsl:template> 

Następnie musisz zdecydować, czy chcesz dalej grupować Info elementów w każdej grupie krajów, na przykład według nazwy:

<xsl:template match="Person"> 
    <xsl:for-each-group select="Student/Info" group-by="@Country"> 
    <country name="{current-grouping-key()}"> 
     <xsl:for-each-group select="current-group()" group-by="@Name"> 
     <student name="{current-grouping-key()}"> 
      <classes> 
      <xsl:for-each select="current-group()"> 
       <class><xsl:value-of select="@Class"/></class> 
      </xsl:for-each> 
      </classes> 
     </student> 
     </xsl:for-each-group> 
    </country> 
    </xsl:for-each-group> 
</xsl:template> 
Powiązane problemy