1

I am learning XML and XSLT, I have two parts codes, one is XML and another one is XSLT,

this is my XML file

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="xslt.xslt"?>
<groups>
    <elements>
        <name>//1</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//2</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//3</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-07-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//4</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//5</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-02-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//6</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2008-06-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//7</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//8</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-14</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//9</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-28</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//10</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-02-25</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//11</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-12-20</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//12</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-11-07</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//13</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-07-09</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//14</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2005-10-19</PostDate>
        <trackno></trackno>
    </elements>
</groups>

and this is my XSLT code

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

  <html>
  <body>
    <h2>Try to display only 9 or 12</h2>
      <xsl:for-each select="/groups/elements">
       <xsl:sort select="PostDate" order="descending"/>
        <b><xsl:value-of select="PostDate" /></b> - 
        <font color='red'><xsl:value-of select="name" /></font>

        <p></p>
      </xsl:for-each>

  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 

My question is how can I limit item numbers to only display 12 and make them show on the page 3 items or 4 items per row?

I found someone uses position, just no idea to achieve which position to limit numbers and make rows ?

you can copy and paste my codes here to see the output http://markbucayan.appspot.com/xslt/index.html

Thanks a lot for any advices and solutions

1
  • Can you please show your expected output HTML? Is the transform client-side or server-side? If server-side, are you stuck on XSLT 1.0? Or can you use 2.0? Sep 6, 2012 at 11:31

4 Answers 4

1

This transformation:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
 xmlns:ext="http://exslt.org/common" exclude-result-prefixes="ext">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

  <xsl:variable name="vrtfPass1">
      <xsl:for-each select="/*/elements">
        <xsl:sort select="PostDate" order="descending"/>

        <xsl:if test="not(position() > 10)">
          <item>
            <xsl:copy-of select="PostDate|name"/>
          </item>
        </xsl:if>
      </xsl:for-each>
  </xsl:variable>

   <xsl:template match="node()|@*">
         <xsl:copy>
           <xsl:apply-templates select="node()|@*"/>
         </xsl:copy>
   </xsl:template>

    <xsl:template match="/*">
      <table border="1">
        <xsl:apply-templates mode="pass2" select="ext:node-set($vrtfPass1)/*"/>
      </table>
    </xsl:template>

    <xsl:template mode="pass2" match="item[position() mod 3 =1]">
      <tr>
        <xsl:apply-templates mode="inRow" select=
        ".|following-sibling::*[not(position() >2)]"/>
      </tr>
    </xsl:template>

    <xsl:template match="item" mode="inRow">
      <td>
       <b><xsl:value-of select="PostDate" /></b> -
       <font color='red'><xsl:value-of select="name" /></font>
      </td>
    </xsl:template>

    <xsl:template match="item" mode="pass2"/>
</xsl:stylesheet>

when applied on the provided XML document:

<groups>
    <elements>
        <name>//1</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//2</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-03-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//3</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-07-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//4</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//5</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2009-02-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//6</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2008-06-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//7</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-01-04</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//8</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-14</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//9</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2012-05-28</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//10</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-02-25</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//11</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-12-20</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//12</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-11-07</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//13</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2011-07-09</PostDate>
        <trackno></trackno>
    </elements>
    <elements>
        <name>//14</name>
        <BOD></BOD>
        <description/>
        <URL></URL>
        <Hobbbies>
            <Sport></Sport>
            <Music></Music>
        </Hobbbies>
        <Height></Height>
        <PostDate>2005-10-19</PostDate>
        <trackno></trackno>
    </elements>
</groups>

produces the wanted (10 latest items, 3 items per row), correct result:

<table border="1">
   <tr>
      <td>
         <b>2012-07-04</b> -
       <font color="red">//3</font>
      </td>
      <td>
         <b>2012-05-28</b> -
       <font color="red">//9</font>
      </td>
      <td>
         <b>2012-05-14</b> -
       <font color="red">//8</font>
      </td>
   </tr>
   <tr>
      <td>
         <b>2012-03-04</b> -
       <font color="red">//1</font>
      </td>
      <td>
         <b>2012-01-04</b> -
       <font color="red">//7</font>
      </td>
      <td>
         <b>2011-12-20</b> -
       <font color="red">//11</font>
      </td>
   </tr>
   <tr>
      <td>
         <b>2011-11-07</b> -
       <font color="red">//12</font>
      </td>
      <td>
         <b>2011-07-09</b> -
       <font color="red">//13</font>
      </td>
      <td>
         <b>2011-03-04</b> -
       <font color="red">//2</font>
      </td>
   </tr>
   <tr>
      <td>
         <b>2011-02-25</b> -
       <font color="red">//10</font>
      </td>
   </tr>
</table>
3
  • Thank you Dimitre, Do you know how to add a class to every 3rd item ?
    – olo
    Sep 6, 2012 at 23:44
  • @olo, This is very easy to do. Please, ask a new question and let me know via a comment you did so. Sep 7, 2012 at 1:34
  • Thanks a lot here it is: stackoverflow.com/questions/12310097/…. Could you please just change my code :-) been working on XSLT a bit long, brain doesnt work properly. thanks again
    – olo
    Sep 7, 2012 at 1:35
1

Have solved the number limit issue, added greater than or less than mark, still no idea how to make the rows like only display 3 items or 4 times a line

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



    <xsl:template match="/">

      <html>
      <body>
        <h2>Try to display only 9 or 12</h2>
          <xsl:for-each select="/groups/elements">
            <xsl:sort select="PostDate" order="descending"/>
            <xsl:if test="not(position() > 10)">
            <b><xsl:value-of select="PostDate" /></b> - 
            <font color='red'><xsl:value-of select="name" /></font>
           <p></p>
           </xsl:if>
          </xsl:for-each>

      </body>
      </html>
    </xsl:template>

    </xsl:stylesheet> 
0

Please check this , i hope t will be use full for you

<table>
<tr>
<td>
<xsl:if test="(position() mod $pagesize) = 0 and position() != 1">
</td><td>
</xsl:if>
</td>
<tr>
</table>
1
  • Many thanks for this , but could I please have a bit more explanation for this?
    – olo
    Sep 6, 2012 at 11:07
0

try this

<?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="html" version="1.0" encoding="utf-8" indent="yes"  omit-xml-declaration="no"/>

  <xsl:param name="pagesize"/>

  <xsl:template match="/">
    <table>
      <tr>
        <xsl:apply-templates select="//groups/elements"/>
      </tr>
    </table>
  </xsl:template>

  <xsl:template match="//groups/elements">

    <td>
      <xsl:value-of select="name"/>
      <xsl:if test="(position() mod 3) = 0 and position() != 1">
        <xsl:text>&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;</xsl:text>
      </xsl:if>
    </td>

  </xsl:template>
</xsl:stylesheet>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.