XML Tutorial/XSLT stylesheet/for each group

Материал из Web эксперт
Перейти к: навигация, поиск

for-each-group select="make|model" group-ending-with="*[position() mod"

   <source lang="xml">

File: Data.xml <?xml version="1.0"?>

<cars>

 <make>Alfa Romeo</make>
 <make>Bentley</make>
 <make>Chevrolet</make>
 <make>Dodge</make>
 <make>Eagle</make>
 <make>Ford</make>
 <make>GMC</make>

</cars> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="2.0"

 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html" include-content-type="no"/>
 <xsl:template match="/">
   <html>
     <head>
       <title>Car Makes and Models</title>
     </head>
     <body>

Car Makes and Models

       <paragraph>Here are the car makes and models in 
       our input document.</p>
<xsl:apply-templates select="cars"/>
     </body>
   </html>
 </xsl:template>
 <xsl:template match="cars">
   <xsl:for-each-group select="make|model" group-ending-with="*[position() mod 3 = 0]">
     <tr>
       <xsl:apply-templates select="current-group()"/>
       <xsl:if test="count(current-group()) lt 3">
         <td style="background: #CCCCCC;"
           colspan="{3 - count(current-group())}">
         </td>
       </xsl:if>
     </tr>
   </xsl:for-each-group>
 </xsl:template>
 <xsl:template match="make">
   <td>
     <xsl:apply-templates/>
   </td>
 </xsl:template>
 <xsl:template match="model">
   <td style="font-style: italic; font-weight: bold;">
     <xsl:apply-templates/>
   </td>
 </xsl:template>
 

</xsl:stylesheet> Output: <html>

  <head>
     <title>Car Makes and Models</title>
  </head>
  <body>

Car Makes and Models

     <paragraph>Here are the car makes and models in 
        our input document.
     </p>
Alfa Romeo Bentley Chevrolet
Dodge Eagle Ford
GMC
  </body>

</html></source>