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

  • SEARCH
  • Home
  • 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 6621799
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:19:02+00:00 2026-05-25T21:19:02+00:00

I have an XSLT file which renders articles. I’m passing a variable into the

  • 0

I have an XSLT file which renders articles. I’m passing a variable into the XSLT file which is supposed to limit the number of records output. The trouble is, this ‘limit’ variable is optional – if it’s not there the XSLT file needs to output all values; if the ‘limit’ variable is a number, then it needs to only output that number of items (presumably using a position() test).

Here’s my code:

<xsl:variable name="limit" select="/macro/limit"/>

<xsl:template match="/">
  <xsl:variable name="allNodes" select="$localSiteRoot/descendant-or-self::*[articles != '']"/>
  <xsl:for-each select="$allNodes">

    <div class="articleItem">
      <h3><xsl:value-of select="./articleHeader"/></h3>
      <xsl:if test="./articleSubheader != ''">
        <h4><xsl:value-of select="./articleSubheader"/></h4>
      </xsl:if>
    </div>

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

Clearly I could just do an XSLT choose around this and say

<xsl:choose>
  <xsl:when select="$limit!= ''">
    <xsl:if test="position() &lt; $limit">

But then I’d need to repeat the code twice. There must be a better way, perhaps using templates, but I just can’t figure out how it would work and my XSLT isn’t great.

Could anyone point me in the right direction with the best/neatest way to approach this?

Thanks!

  • 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-25T21:19:03+00:00Added an answer on May 25, 2026 at 9:19 pm

    Just use:

    <xsl:for-each select="$allNodes[not(position() > $limit)]"> 
    

    Explanation:

    1. If the value of $limit is castable to a number, then the expression in the select attribute above selects not more than $limit nodes.

    2. If the value of $limits isn’t castable to a number (e.g. empty or ‘abc’) then it is converted to NaN. By definition comparisons involving NaN are always false(), therefore the predicate not(position() > $limit is true() and in this case all nodes in $allNodes are selected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a UserControl which rather than putting tags into its ascx markup file,
I have an xslt file which I initially added to the Resources section of
I have made a small xslt file to create an html output called weather.xsl
All, I have an XML file which I transform it using an XSLT document
I have an XSLT file generating plain HTML. I need to wrap some elements
How to access App_Data folder in WCF service? I have placed a xslt file
we have XSLT to transform XML into HTML in XHTML 1.0 Strict in XSLT
Does anyone have an XSLT that will take the app.config and render it into
I have a maven project that loads an xslt file and executes the transformation
Next question: I have InputDoc xml document and xslt file in order to transform

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.