开发者

XSLT 1.0 Group By

开发者 https://www.devze.com 2022-12-14 04:12 出处:网络
I have the following XML data: <?xml version=\"1.0\" encoding=\"iso-8859-1\"?> <results> <result>

I have the following XML data:

<?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>
        <开发者_StackOverflow社区MemberLogin>4</MemberLogin>
        <Store>0183</Store>
    </result>
</results>

In XSLT 1.0, I want to group by and create a table for employees in each store. so in the above data, 3 tables would be created. A table with two rows for the first 2 records (store 180), etc..

How can I do this in 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>


Muenchian grouping is the best approach; something like:

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


maybe easier to understand

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

精彩评论

暂无评论...
验证码 换一张
取 消