2009-12-14 113 views
6

我有下面的XML數據:XSLT 1.0集團通過

<?xml version="1.0" encoding="iso-8859-1"?> 
<results> 
    <result> 
     <MemberFirstName>JOHN</MemberFirstName> 
     <MemberLastName>DOE</MemberLastName> 
     <MemberLogin>1</MemberLogin> 
     <Store>0180</Store> 
    </result> 
    <result> 
     <MemberFirstName>JANE</MemberFirstName> 
     <MemberLastName>DOE</MemberLastName> 
     <MemberLogin>2</MemberLogin> 
     <Store>0180</Store> 
    </result> 
    <result> 
     <MemberFirstName>ALAN</MemberFirstName> 
     <MemberLastName>SMITH</MemberLastName> 
     <MemberLogin>3</MemberLogin> 
     <Store>0181</Store> 
    </result> 
    <result> 
     <MemberFirstName>DAVID</MemberFirstName> 
     <MemberLastName>GREEN</MemberLastName> 
     <MemberLogin>4</MemberLogin> 
     <Store>0183</Store> 
    </result> 
</results> 

在XSLT 1.0,我想GROUP BY和創建一個表中的每個店的員工。所以在上面的數據中,將創建3個表格。前兩條記錄(商店180)的兩個表格等等。

如何在XSLT 1.0中執行此操作?

<table id="table"> 
    <tr class="heading"> 
    <th scope="col">Member Id</th> 
    <th scope="col">First Name</th> 
    <th scope="col">Last Name</th> 
    </tr> 
    <xsl:for-each select="results/result"> 
    <tr> 
     <td><xsl:value-of select="MemberLogin"/></td> 
     <td><xsl:value-of select="MemberFirstName"/></td> 
     <td><xsl:value-of select="MemberLastName"/></td> 
    </tr> 
    </xsl:for-each> 
</table> 
+0

[見similiar提問和回答(http://stackoverflow.com/q/18311162/287948)。 – 2015-01-27 10:22:07

回答

14

Muenchian grouping是最好的方法;是這樣的:

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"> 

    <xsl:output method="xml" indent="yes"/>  
    <xsl:key name="groups" match="/results/result" use="Store" /> 

    <xsl:template match="/results"> 
    <xsl:apply-templates select="result[generate-id() = generate-id(key('groups', Store)[1])]"/> 
    </xsl:template> 
    <xsl:template match="result"> 
    <h1><xsl:value-of select="Store"/></h1> 
    <table id="{Store}"> 
     <tr class="heading"> 
     <th scope="col">Member Id</th> 
     <th scope="col">First Name</th> 
     <th scope="col">Last Name</th> 
     </tr> 
     <xsl:for-each select="key('groups', Store)"> 
     <tr> 
      <td><xsl:value-of select="MemberLogin"/></td> 
      <td><xsl:value-of select="MemberFirstName"/></td> 
      <td><xsl:value-of select="MemberLastName"/></td> 
     </tr> 
     </xsl:for-each> 
    </table> 
    </xsl:template> 
</xsl:stylesheet> 
+1

muench是魔術字:) – annakata 2009-12-14 22:28:46

+0

謝謝Marc :-) – Neil 2009-12-15 13:49:23

+0

我需要基於子節點進行分組,這樣才能工作嗎?你能按照我的xml提供樣本嗎? – jvm 2010-08-11 12:59:45

1

也許更容易理解

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

    <xsl:output method="xml" indent="yes"/>  

    <xsl:template match="/results"> 
     <xsl:apply-templates select="result[not(preceding-sibling::result/Store = Store)]/Store" /> 
    </xsl:template> 

    <xsl:template match="result" > 
     <tr> 
     <td> 
      <xsl:value-of select="MemberLogin"/> 
     </td> 
     <td> 
      <xsl:value-of select="MemberFirstName"/> 
     </td> 
     <td> 
      <xsl:value-of select="MemberLastName"/> 
     </td> 
     </tr> 
    </xsl:template> 

    <xsl:template match="Store" > 
     <table id="{.}"> 
     <tr class="heading"> 
      <th scope="col">Member Id</th> 
      <th scope="col">First Name</th> 
      <th scope="col">Last Name</th> 
     </tr> 
     <xsl:variable name="temp" select="." /> 
     <xsl:apply-templates select="//result[Store = current()]" /> 
     </table> 
    </xsl:template> 

    </xsl:stylesheet>