2013-10-01 13 views
11

im vẫn đang học for-each-group cách tốt nhất để nhóm một thứ gì đó như thế này bằng XSL? (Theo quốc gia) tôi đang cố gắng sử dụng XSL để chuyển đổi XML này sang một XML khác.Cách sử dụng cho mỗi nhóm trong 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

Phụ thuộc nơi bạn muốn nhóm trên. Bạn muốn kết quả của mình trông như thế nào? Được nhóm trên 'Quốc gia' hoặc được nhóm trên' Tên' hoặc 'Lớp 'hoặc kết hợp? –

Trả lời

27

Nếu bạn nhóm theo quốc gia, bạn sẽ bắt đầu bằng ví dụ:

<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> 

Sau đó, bạn phải quyết định xem bạn có muốn tiếp tục nhóm Info yếu tố trong mỗi nhóm quốc gia, ví dụ theo tên:

<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> 
Các vấn đề liên quan