title: XSLT Conditional within For-Each
body: <p>How can I include an if statement in my loop conditional in XSLT?</p>
  
  <p>I have 2 or 3 different scenarios that define the data I want to loop through.  For example, there may or may not be a product ID.  If there is, I want to only loop through records that contain that product ID, otherwise I want all records.  I was trying to do something like this:</p>
  
  <pre><code>&lt;xsl:variable name="knowledgebaseLoop"&gt;
        &lt;xsl:choose&gt;
          &lt;xsl:when test="string-length($productId) &amp;gt; 0"&gt;
            &lt;xsl:copy-of select="$currentPage/child::* [@parentID = $parentId]"/&gt;
          &lt;/xsl:when&gt;
          &lt;xsl:otherwise&gt;
            &lt;xsl:copy-of select="$currentPage/child::* [@parentID = $parentId]"/&gt;
          &lt;/xsl:otherwise&gt;
        &lt;/xsl:choose&gt;
      &lt;/xsl:variable&gt;
  
  &lt;xsl:for-each select="knowledgebaseLoop"&gt;
        &lt;xsl:if test="position() &amp;gt; $recordsPerPage * number($pageNumber - 1) and position() &amp;lt;= number($recordsPerPage * number($pageNumber - 1) + $recordsPerPage )"&gt;
          &lt;div id="main_body_listing"&gt;
            &lt;div class="listing_title2"&gt;&lt;a href="{umbraco.library:NiceUrl(current()/@id)}"&gt;&lt;xsl:value-of select="current()/@nodeName"/&gt;&lt;/a&gt;&lt;/div&gt;
            &lt;xsl:value-of select="current()/summary"/&gt;
          &lt;/div&gt;
        &lt;/xsl:if&gt;
      &lt;/xsl:for-each&gt;
  </code></pre>
  
  <p>But it's unable to parse the XSLT.  I know the 2 conditions are the same, I was going to rewrite one once I got it working.  Thanks in advance for any help!</p>
  
  <p>UPDATE:</p>
  
  <p>Here is some sample XML:</p>
  
  <pre><code>&lt;Knowledgebase id="1073" parentID="-1" level="1" writerID="0" creatorID="0" nodeType="1062" template="1083" sortOrder="5" createDate="2011-04-20T13:43:41" updateDate="2011-05-11T14:34:23" nodeName="Knowledgebase" urlName="knowledgebase" writerName="Chris" creatorName="Chris" path="-1,1073" isDoc=""&gt;&lt;pageTitle&gt;Knowledgebase&lt;/pageTitle&gt;&lt;metaDescription&gt;knowledgebase&lt;/metaDescription&gt;&lt;metaKeywords&gt;knowledgebase&lt;/metaKeywords&gt;&lt;umbracoUrlName /&gt;&lt;Article id="1074" parentID="1073" level="2" writerID="0" creatorID="0" nodeType="1063" template="1084" sortOrder="1" createDate="2011-04-20T13:44:13" updateDate="2011-05-16T09:39:12" nodeName="What is the answer to this question?" urlName="what-is-the-answer-to-this-question" writerName="Chris" creatorName="Chris" path="-1,1073,1074" isDoc=""&gt;&lt;title&gt;Change 2 Test 2&lt;/title&gt;&lt;summary&gt;Change 2 Test 2&lt;/summary&gt;&lt;description&gt;
  
  &amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;Change 2 Test 2&amp;lt;br /&amp;gt;
  &amp;lt;/strong&amp;gt;&amp;lt;/p&amp;gt;
  &lt;/description&gt;&lt;relatedProducts&gt;1071,1076&lt;/relatedProducts&gt;&lt;pageTitle&gt;asd&lt;/pageTitle&gt;&lt;metaDescription&gt;asd&lt;/metaDescription&gt;&lt;metaKeywords&gt;asd&lt;/metaKeywords&gt;&lt;umbracoUrlName /&gt;&lt;/Article&gt;&lt;Article id="1082" parentID="1073" level="2" writerID="0" creatorID="3" nodeType="1063" template="1084" sortOrder="2" createDate="2011-05-04T09:59:31" updateDate="2011-05-11T14:34:23" nodeName="Question 2" urlName="question-2" writerName="Chris" creatorName="Content Writer" path="-1,1073,1082" isDoc=""&gt;&lt;title&gt;test&lt;/title&gt;&lt;summary&gt;test 22&lt;/summary&gt;&lt;description&gt;
  &amp;lt;p&amp;gt;tst&amp;lt;/p&amp;gt;
  
  &lt;/description&gt;&lt;relatedProducts&gt;1075&lt;/relatedProducts&gt;&lt;pageTitle /&gt;&lt;metaDescription&gt;&lt;/metaDescription&gt;&lt;metaKeywords&gt;&lt;/metaKeywords&gt;&lt;umbracoUrlName /&gt;&lt;/Article&gt;&lt;Article id="1146" parentID="1073" level="2" writerID="4" creatorID="3" nodeType="1063" template="1084" sortOrder="3" createDate="2011-05-13T15:36:14" updateDate="2011-05-13T15:37:21" nodeName="Question 3" urlName="question-3" writerName="Content Approver" creatorName="Content Writer" path="-1,1073,1146" isDoc=""&gt;&lt;title&gt;Test&lt;/title&gt;&lt;summary&gt;&lt;/summary&gt;&lt;description&gt;
  &amp;lt;p&amp;gt;Test&amp;lt;/p&amp;gt;
  &lt;/description&gt;&lt;relatedProducts /&gt;&lt;pageTitle&gt;Test&lt;/pageTitle&gt;&lt;metaDescription&gt;test&lt;/metaDescription&gt;&lt;metaKeywords&gt;test&lt;/metaKeywords&gt;&lt;umbracoUrlName /&gt;&lt;/Article&gt;&lt;/Knowledgebase&gt;
  </code></pre>
  
  <p>I always need the articles where the parent ID is 1073.  If and only if a product ID exists, I want the articles where the product ID is in the relatedProducts list.  Thanks again!</p>