if test="(position() mod 2) = 1" : if « XSLT stylesheet « XML Tutorial






File: Data.xml

<?xml version="1.0"?>
<list>
  <title>Java</title>
  <listitem>Item 1</listitem>
  <listitem>Item 2</listitem>
  <listitem>Item 3</listitem>
  <listitem>Item 4</listitem>
  <listitem>Item 5</listitem>
  <listitem>Item 6</listitem>
  <listitem>Item 7</listitem>
  <listitem>Item 8</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="text"/>

  <xsl:variable name="newline">
  <xsl:text></xsl:text>
  </xsl:variable>

  <xsl:template match="/">
    <xsl:value-of select="$newline"/>
    <xsl:text>Here are the odd-numbered items from the list:</xsl:text>
    <xsl:value-of select="$newline"/>
    <xsl:for-each select="list/listitem">
      <xsl:if test="(position() mod 2) = 1">
        <xsl:number format="1. "/>
        <xsl:value-of select="."/>
        <xsl:value-of select="$newline"/>
      </xsl:if>
    </xsl:for-each>
  </xsl:template>
  
</xsl:stylesheet>

Output:

Here are the odd-numbered items from the list:1. Item 13. Item 35. Item 57. Item 7








5.44.if
5.44.1.The Element: Conditional Processing
5.44.2.Use if statement to test if an element as an attribute
5.44.3.Check two attributes
5.44.4.Nested if statement
5.44.5.if test="not(preceding-sibling::address[zip=$lastZip])"
5.44.6.if test="(position() mod 2) = 1"
5.44.7.select with if them else statement
5.44.8.xsl:if instruction enables conditional processing
5.44.9.Use if statement to check whether it is the last