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 3977176
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:53:32+00:00 2026-05-20T04:53:32+00:00

Quite new to xslt so forgive me if this is a basic question –

  • 0

Quite new to xslt so forgive me if this is a basic question – I can’t find the answer either on SO or by searching on Google.

What I am trying to do is return a filtered set of nodes and then have a template match on the first 1 or 2 items in that set and another template match the remainder. However I don’t seem to be able to do this without a <xsl:for-each /> loop (which is highly undesirable as I may be matching 3000 nodes and only treating 1 differently).

Using position() doesn’t work as this is unaffected by the filtering. I’ve tried sorting the result set but this doesn’t seem to take effect early enough to affect the template match. The <xsl:number /> outputs the correct numbers but I can’t use these in a match statement.

I’ve put some example code below. I’m using the unsuitable position() method below to illustrate the problem.

Thanks in advance!

XML:

<?xml version="1.0" encoding="utf-8"?>
<news>
    <newsItem id="1">
        <title>Title 1</title>
    </newsItem>
    <newsItem id="2">
        <title>Title 2</title>
    </newsItem>
    <newsItem id="3">
        <title></title>
    </newsItem>
    <newsItem id="4">
        <title></title>
    </newsItem>
    <newsItem id="5">
        <title>Title 5</title>
    </newsItem>
</news>

XSL:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
    <xsl:output method="xml" indent="yes"/>

    <xsl:template match="/">
        <ol>
            <xsl:apply-templates select="/news/newsItem [string(title)]" />
        </ol>
    </xsl:template>

    <xsl:template match="newsItem [position() &lt; 4]">
        <li>
            <xsl:value-of select="title"/>
        </li>
    </xsl:template>

    <xsl:template match="*" />
</xsl:stylesheet>

Desired Result:

  1. Title 1
  2. Title 2
  3. Title 5
  • 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-20T04:53:33+00:00Added an answer on May 20, 2026 at 4:53 am

    This one’s actually simpler than you might think. Do:

     <xsl:template match="newsItem[string(title)][position() &lt; 4]">
    

    And drop the [string(title)] predicate from your <xsl:apply-templates select.

    Like this:

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
        <xsl:output method="xml" indent="yes"/>
    
        <xsl:template match="/">
            <ol>
                <xsl:apply-templates select="/news/newsItem" />
            </ol>
        </xsl:template>
    
        <xsl:template match="newsItem[string(title)][position() &lt; 4]">
            <li><xsl:value-of select="position()" />
                <xsl:value-of select="title"/>
            </li>
        </xsl:template>
    
        <xsl:template match="*" />
    </xsl:stylesheet>
    

    What you’re effectively doing here is applying a second filter ([position() &lt; 4]) after your [string(title)] filter, which results in the position() being applied to the filtered list.

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

Sidebar

Related Questions

Iam quite new to Google chart API's and I am trying to integrate it
Being quite new at JavaScript, I'm assuming the answer to my question is extremely
im quite new to android, so i apologise if this is a noob-ish question
Im quite new to stackoverflow so I dont know if this question has been
Still quite new to Python. I have a problem similar to this question .
Quite new to maven here so let me explain first what I am trying
I'm quite new to this (understanding the WP Guts), and I wanted to understand
Am quite new to namespacing etc so hopefully this will be straightforward. I have
I am quite new to vim and I am trying to edit coffeescript. Whenever
I am quite new to application development on iPhone, so please forgive me if

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.