XML/XSLT stylesheet/current group — различия между версиями

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

Текущая версия на 08:26, 26 мая 2010

Get current group

File: Data.xml
<?xml version="1.0"?>
<towns>
  <town>A</town>
  <town>B</town>
  <town>C</town>
  <town>D</town>
  <town>E</town>
  <town>F</town>
  <town>G</town>
  <town>H</town>
  <town>I</town>
  <town>J</town>
  <town>K</town>
  <town>L</town>
</towns>

File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:param name="cols" select="4" />
  <xsl:template match="towns">
    <table>
      <xsl:variable name="sorted-towns" as="element()*">
        <xsl:perform-sort select="town">
          <xsl:sort />
        </xsl:perform-sort>
      </xsl:variable>
      <xsl:for-each-group select="$sorted-towns"
        group-adjacent="(position()-1) idiv $cols">
        <tr>
          <xsl:for-each select="current-group()">
            <td>
              <xsl:value-of select="." />
            </td>
          </xsl:for-each>
        </tr>
      </xsl:for-each-group>
    </table>
  </xsl:template>

</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<table>
   <tr>
      <td>A</td>
      <td>B</td>
      <td>C</td>
      <td>D</td>
   </tr>
   <tr>
      <td>E</td>
      <td>F</td>
      <td>G</td>
      <td>H</td>
   </tr>
   <tr>
      <td>I</td>
      <td>J</td>
      <td>K</td>
      <td>L</td>
   </tr>
</table>



Use count() and current-group() to count groups

File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<employees>
  <employee>
    <FirstName>A</FirstName>
    <LastName>B</LastName>
    <Country>USA</Country>
  </employee>
  <employee>
    <FirstName>C</FirstName>
    <LastName>D</LastName>
    <Country>USA</Country>
  </employee>
  <employee>
    <FirstName>E</FirstName>
    <LastName>F</LastName>
    <Country>USA</Country>
  </employee>
</employees>

File: Transform.xslt
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:template match="/">
    <html>
      <body>
        <h3>header</h3>
        <xsl:for-each-group select="employees/employee"
          group-by="Country">
          <xsl:sort select="current-grouping-key()" />
          <p>
            Number of employees who live in:
            <b>
              <xsl:value-of
                select="current-grouping-key()" />
            </b>
            is
            <xsl:value-of select="count(current-group())" />
          </p>
          <xsl:result-document
            href="{current-grouping-key()}.xml">
            <employees>
              <xsl:copy-of select="current-group()" />
            </employees>
          </xsl:result-document>
        </xsl:for-each-group>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>



Word count

File: Data.xml 
<poem>
   <author>author 1</author>
   <date>1912</date>
   <title>Song</title>
  <stanza>
      <line>line 1</line>
      <line>line 2</line>
      <line>line 3</line>
      <line>line 4</line>
   </stanza>
   <stanza>
      <line>line 5</line>
      <line>line 6</line>
      <line>line 7</line>
      <line>line 8</line>
   </stanza>
   <stanza>
      <line>line 9</line>
      <line>line 10</line>
      <line>line 11</line>
      <line>line 12</line>
   </stanza>
</poem>
File: Transform.xslt
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="2.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:template match="/">
    <wordcount>
      <xsl:for-each-group group-by="."
        select="
          for $w in tokenize(string(.), "\W+") return lower-case($w)">
        <xsl:sort select="count(current-group())"
          order="descending" />
        <word word="{current-grouping-key()}"
          frequency="{count(current-group())}" />
      </xsl:for-each-group>
    </wordcount>
  </xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<wordcount>
   <word word="line" frequency="12"/>
   <word word="" frequency="2"/>
   <word word="1" frequency="2"/>
   <word word="author" frequency="1"/>
   <word word="1912" frequency="1"/>
   <word word="song" frequency="1"/>
   <word word="2" frequency="1"/>
   <word word="3" frequency="1"/>
   <word word="4" frequency="1"/>
   <word word="5" frequency="1"/>
   <word word="6" frequency="1"/>
   <word word="7" frequency="1"/>
   <word word="8" frequency="1"/>
   <word word="9" frequency="1"/>
   <word word="10" frequency="1"/>
   <word word="11" frequency="1"/>
   <word word="12" frequency="1"/>
</wordcount>