Search

I’ve been trying to work out a way to write an if statement that says “If there are entries 1-4 present, do this; if there are entries 5-8 present, then do such-and-such.

Thoughts? Thanks!

@lbradford - Try this…

<xsl:if test="position() &lt; 5">
  <!-- Entries 1-4 -->
</xsl:if>

<xsl:if test="position() &gt; 4 and position() &lt; 9">
  <!-- Entries 5-8 -->
</xsl:if>

Would you mind posting your XML and your XSLT on pastie.org and then posting the links to that code here?

Is this sort of what you wanted to accomplish? Say entries 1-4 have the class “first-group” on list item and entries 5-8 have a class of second-group…

Here’s an example XML…

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <example-list>
        <entry id="1">
            <title>One</title>
        </entry>
        <entry id="2">
            <title>Two</title>
        </entry>
        <entry id="3">
            <title>Three</title>
        </entry>
        <entry id="4">
            <title>Four</title>
        </entry>
        <entry id="5">
            <title>Five</title>
        </entry>
        <entry id="6">
            <title>Six</title>
        </entry>
        <entry id="7">
            <title>Seven</title>
        </entry>
        <entry id="8">
            <title>Eight</title>
        </entry>
        <entry id="9">
            <title>Nine</title>
        </entry>
        <entry id="10">
            <title>Ten</title>
        </entry>
    </example-list>
</data>

Here’s the example XSLT, entries 1-4 have the class “first-group” on list item and entries 5-8 have a class of second-group…

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

    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <ul>
            <xsl:apply-templates select="data/example-list/entry"/>
        </ul>
    </xsl:template>

    <xsl:template match="entry">
        <xsl:if test="position() &lt; 5">
            <li class="first-group"><xsl:value-of select="title"/></li>
        </xsl:if>
        <xsl:if test="position() &gt; 4 and position() &lt; 9">
            <li class="second-group"><xsl:value-of select="title"/></li>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

Which would output…

<ul>
  <li class="first-group">One</li>
  <li class="first-group">Two</li>
  <li class="first-group">Three</li>
  <li class="first-group">Four</li>
  <li class="second-group">Five</li>
  <li class="second-group">Six</li>
  <li class="second-group">Seven</li>
  <li class="second-group">Eight</li>
</ul>

OR if you wanted put the class on the ul, then here’s the XSLT…

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

    <xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="yes"/>

    <xsl:template match="/">

        <xsl:if test="count(data/example-list/entry) &gt; 0">
            <ul class="first-group">
                <xsl:apply-templates select="data/example-list/entry[position() &lt; 5]"/>
            </ul>
        </xsl:if>
        <xsl:if test="count(data/example-list/entry) &gt; 4">
            <ul class="second-group">
                <xsl:apply-templates select="data/example-list/entry[position() &gt; 4 and position() &lt; 9]"/>
            </ul>
        </xsl:if>

    </xsl:template>

    <xsl:template match="entry">
        <li><xsl:value-of select="title"/></li>
    </xsl:template>

</xsl:stylesheet>

Here’s the output…

<ul class="first-group">
  <li>One</li>
  <li>Two</li>
  <li>Three</li>
  <li>Four</li>
</ul>
<ul class="second-group">
  <li>Five</li>
  <li>Six</li>
  <li>Seven</li>
  <li>Eight</li>
</ul>

Let me know if that helps any.

Wow, this great! Thanks!

Awesome. Glad it helped.

Create an account or sign in to comment.

Symphony • Open Source XSLT CMS

Server Requirements

  • PHP 5.3-5.6 or 7.0-7.3
  • PHP's LibXML module, with the XSLT extension enabled (--with-xsl)
  • MySQL 5.5 or above
  • An Apache or Litespeed webserver
  • Apache's mod_rewrite module or equivalent

Compatible Hosts

Sign in

Login details