Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5845035
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:22:39+00:00 2026-05-22T12:22:39+00:00

How can I include an if statement in my loop conditional in XSLT? I

  • 0

How can I include an if statement in my loop conditional in XSLT?

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:

<xsl:variable name="knowledgebaseLoop">
      <xsl:choose>
        <xsl:when test="string-length($productId) &gt; 0">
          <xsl:copy-of select="$currentPage/child::* [@parentID = $parentId]"/>
        </xsl:when>
        <xsl:otherwise>
          <xsl:copy-of select="$currentPage/child::* [@parentID = $parentId]"/>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:variable>

<xsl:for-each select="knowledgebaseLoop">
      <xsl:if test="position() &gt; $recordsPerPage * number($pageNumber - 1) and position() &lt;= number($recordsPerPage * number($pageNumber - 1) + $recordsPerPage )">
        <div id="main_body_listing">
          <div class="listing_title2"><a href="{umbraco.library:NiceUrl(current()/@id)}"><xsl:value-of select="current()/@nodeName"/></a></div>
          <xsl:value-of select="current()/summary"/>
        </div>
      </xsl:if>
    </xsl:for-each>

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!

UPDATE:

Here is some sample XML:

<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=""><pageTitle>Knowledgebase</pageTitle><metaDescription>knowledgebase</metaDescription><metaKeywords>knowledgebase</metaKeywords><umbracoUrlName /><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=""><title>Change 2 Test 2</title><summary>Change 2 Test 2</summary><description>

&lt;p&gt;&lt;strong&gt;Change 2 Test 2&lt;br /&gt;
&lt;/strong&gt;&lt;/p&gt;
</description><relatedProducts>1071,1076</relatedProducts><pageTitle>asd</pageTitle><metaDescription>asd</metaDescription><metaKeywords>asd</metaKeywords><umbracoUrlName /></Article><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=""><title>test</title><summary>test 22</summary><description>
&lt;p&gt;tst&lt;/p&gt;

</description><relatedProducts>1075</relatedProducts><pageTitle /><metaDescription></metaDescription><metaKeywords></metaKeywords><umbracoUrlName /></Article><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=""><title>Test</title><summary></summary><description>
&lt;p&gt;Test&lt;/p&gt;
</description><relatedProducts /><pageTitle>Test</pageTitle><metaDescription>test</metaDescription><metaKeywords>test</metaKeywords><umbracoUrlName /></Article></Knowledgebase>

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:22:40+00:00Added an answer on May 22, 2026 at 12:22 pm

    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 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.

    Use:

      <xsl:variable name="vDesiredArticles" select=
       "/*/Article[@parentID='1073'
                  or
                   contains(concat(',',relatedProducts,','),
                            concat(',', $pProductId, ',')
                            )
                   ]"/>
    

    Here is a complete and short example:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:param name="pProductId" select="1077"/>
    
     <xsl:template match="/">
      <xsl:variable name="vDesiredArticles" select=
       "/*/Article[@parentID='1073'
                  or
                   contains(concat(',',relatedProducts,','),
                            concat(',', $pProductId, ',')
                            )
                   ]"/>
      <xsl:copy-of select="$vDesiredArticles"/>
     </xsl:template>
    </xsl:stylesheet>
    

    when this transformation is applied on the following XML document (based on the provided one in the Update, but made more interesting):

    <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="">
        <pageTitle>Knowledgebase</pageTitle>
        <metaDescription>knowledgebase</metaDescription>
        <metaKeywords>knowledgebase</metaKeywords>
        <umbracoUrlName />
        <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="">
            <title>Change 2 Test 2</title>
            <summary>Change 2 Test 2</summary>
            <description>  &lt;p&gt;&lt;strong&gt;Change 2 Test 2&lt;br /&gt; &lt;/strong&gt;&lt;/p&gt; </description>
            <relatedProducts>1071,1076</relatedProducts>
            <pageTitle>asd</pageTitle>
            <metaDescription>asd</metaDescription>
            <metaKeywords>asd</metaKeywords>
            <umbracoUrlName />
        </Article>
        <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="">
            <title>test</title>
            <summary>test 22</summary>
            <description> &lt;p&gt;tst&lt;/p&gt;  </description>
            <relatedProducts>1075</relatedProducts>
            <pageTitle />
            <metaDescription></metaDescription>
            <metaKeywords></metaKeywords>
            <umbracoUrlName />
        </Article>
        <Article id="1088" parentID="1077" 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="">
            <title>test</title>
            <summary>test 22</summary>
            <description> &lt;p&gt;tst&lt;/p&gt;  </description>
            <relatedProducts>1075,1039,1077</relatedProducts>
            <pageTitle />
            <metaDescription></metaDescription>
            <metaKeywords></metaKeywords>
            <umbracoUrlName />
        </Article>
        <Article id="1089" parentID="1077" 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="">
            <title>test</title>
            <summary>test 22</summary>
            <description> &lt;p&gt;tst&lt;/p&gt;  </description>
            <relatedProducts>1075,1039,1078</relatedProducts>
            <pageTitle />
            <metaDescription></metaDescription>
            <metaKeywords></metaKeywords>
            <umbracoUrlName />
        </Article>
        <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="">
            <title>Test</title>
            <summary></summary>
            <description> &lt;p&gt;Test&lt;/p&gt; </description>
            <relatedProducts />
            <pageTitle>Test</pageTitle>
            <metaDescription>test</metaDescription>
            <metaKeywords>test</metaKeywords>
            <umbracoUrlName />
        </Article>
    </Knowledgebase>
    

    the wanted, correct result is produced (only Articles with parentID="1073" or whose relatedProducts contains the$pProductId (1077)):

    <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="">
       <title>Change 2 Test 2</title>
       <summary>Change 2 Test 2</summary>
       <description>  &lt;p&gt;&lt;strong&gt;Change 2 Test 2&lt;br /&gt; &lt;/strong&gt;&lt;/p&gt; </description>
       <relatedProducts>1071,1076</relatedProducts>
       <pageTitle>asd</pageTitle>
       <metaDescription>asd</metaDescription>
       <metaKeywords>asd</metaKeywords>
       <umbracoUrlName/>
    </Article>
    <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="">
       <title>test</title>
       <summary>test 22</summary>
       <description> &lt;p&gt;tst&lt;/p&gt;  </description>
       <relatedProducts>1075</relatedProducts>
       <pageTitle/>
       <metaDescription/>
       <metaKeywords/>
       <umbracoUrlName/>
    </Article>
    <Article id="1088" parentID="1077" 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="">
       <title>test</title>
       <summary>test 22</summary>
       <description> &lt;p&gt;tst&lt;/p&gt;  </description>
       <relatedProducts>1075,1039,1077</relatedProducts>
       <pageTitle/>
       <metaDescription/>
       <metaKeywords/>
       <umbracoUrlName/>
    </Article>
    <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="">
       <title>Test</title>
       <summary/>
       <description> &lt;p&gt;Test&lt;/p&gt; </description>
       <relatedProducts/>
       <pageTitle>Test</pageTitle>
       <metaDescription>test</metaDescription>
       <metaKeywords>test</metaKeywords>
       <umbracoUrlName/>
    </Article>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I write an insert statement which includes the & character? For example,
How can include .asp file inside of html file and have it proccessed besides
I'm looking for a key/value pair object that I can include in a web
When data binding two elements together, how can I include the binding information AND
If I generate an exception on my own, I can include any info into
In a .NET Datatable, the columns are Object types, which can include a datatable
How can I include a bookmarklet in a Markdown parsed document? Is there any
I can successfully include main jquery files in /Scripts, but if my custom test.js
So I know I can use #include in BIB and REG files to pull
I can set the PHP include path in the php.ini : include_path = /path/to/site/includes/

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.