for-each select="listitem[position() > 1]" : axis « XSLT stylesheet « XML






for-each select="listitem[position() > 1]"


File: Data.xml

<?xml version="1.0"?>
<list xml:lang="en">
  <title>title 1</title>
  <listitem>item 1</listitem>
  <listitem>item 2</listitem>
  <listitem>item 3</listitem>
  <listitem xml:lang="sw">item 4</listitem>
  <listitem xml:lang="en-gb">item 5</listitem>
  <listitem xml:lang="zu">item 6</listitem>
  <listitem xml:lang="jz">item 7</listitem>
</list>

File: Transform.xslt

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

  <xsl:output method="html"/>

  <xsl:template match="/">
    <html>
      <head>
        <title><xsl:value-of select="list/title"/></title>
      </head>
      <body style="font-family: sans-serif;">
        <xsl:apply-templates select="list"/>
      </body>
    </html>
  </xsl:template>

  <xsl:template match="list">
    <table border="1" cellpadding="5" cellspacing="5">
      <tr>
        <td style="background: black; color: white; 
                   font-weight: bold; font-size: 125%;" 
                   width="100" align="right">
          <xsl:if test="count(listitem) > 1">
            <xsl:attribute name="rowspan">
              <xsl:value-of select="count(listitem)"/>
            </xsl:attribute>
          </xsl:if>
          <xsl:value-of select="title"/>
        </td>
        <td>
          <xsl:value-of select="listitem[1]"/>
        </td>
      </tr>
      <xsl:for-each select="listitem[position() &gt; 1]">
        <tr>
          <td>
            <xsl:value-of select="."/>
          </td>
        </tr>
      </xsl:for-each>
    </table>
  </xsl:template>

</xsl:stylesheet>

Output:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>title 1</title>
   </head>
   <body style="font-family: sans-serif;">
      <table border="1" cellpadding="5" cellspacing="5">
         <tr>
            <td style="background: black; color: white;                     font-weight: bold; font-size: 125%;" width="100" align="right" rowspan="7">title 1</td>
            <td>item 1</td>
         </tr>
         <tr>
            <td>item 2</td>
         </tr>
         <tr>
            <td>item 3</td>
         </tr>
         <tr>
            <td>item 4</td>
         </tr>
         <tr>
            <td>item 5</td>
         </tr>
         <tr>
            <td>item 6</td>
         </tr>
         <tr>
            <td>item 7</td>
         </tr>
      </table>
   </body>
</html>

 








Related examples in the same category

1.List All customers: /report/customer
2.template match="//customer"
3.template match="//customer[1]/name"
4.template match="//processing-instruction()"
5.Match all from root
6.for-each and select="."
7.tag with index
8.select="." (dot)
9.Match an element
10.match element with certain attribute value
11.match="text()|@*"
12.match="@*|node()"
13.select down to a certain level and its attribute